diff --git a/azurerm/resource_arm_app_service.go b/azurerm/resource_arm_app_service.go index adcbc368f22d..ecbea344681f 100644 --- a/azurerm/resource_arm_app_service.go +++ b/azurerm/resource_arm_app_service.go @@ -3,6 +3,7 @@ package azurerm import ( "fmt" "log" + "regexp" "github.com/Azure/azure-sdk-for-go/arm/web" "github.com/hashicorp/terraform/helper/schema" @@ -22,16 +23,13 @@ func resourceArmAppService() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateAppServiceName, }, - "resource_group_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, + "resource_group_name": resourceGroupNameSchema(), "location": locationSchema(), @@ -629,3 +627,13 @@ func flattenAppServiceAppSettings(input *map[string]*string) map[string]string { return output } + +func validateAppServiceName(v interface{}, k string) (ws []string, es []error) { + value := v.(string) + + if matched := regexp.MustCompile(`^[0-9a-zA-Z-]+$`).Match([]byte(value)); !matched { + es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k)) + } + + return +} diff --git a/azurerm/resource_arm_app_service_plan.go b/azurerm/resource_arm_app_service_plan.go index dd681debf745..eb524e45db93 100644 --- a/azurerm/resource_arm_app_service_plan.go +++ b/azurerm/resource_arm_app_service_plan.go @@ -3,6 +3,7 @@ package azurerm import ( "fmt" "log" + "regexp" "github.com/Azure/azure-sdk-for-go/arm/web" "github.com/hashicorp/terraform/helper/schema" @@ -22,9 +23,10 @@ func resourceArmAppServicePlan() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateAppServicePlanName, }, "resource_group_name": resourceGroupNameSchema(), @@ -269,3 +271,13 @@ func flattenAppServiceProperties(props *web.AppServicePlanProperties) []interfac result = append(result, properties) return result } + +func validateAppServicePlanName(v interface{}, k string) (ws []string, es []error) { + value := v.(string) + + if matched := regexp.MustCompile(`^[0-9a-zA-Z-]+$`).Match([]byte(value)); !matched { + es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k)) + } + + return +} diff --git a/azurerm/resource_arm_app_service_plan_test.go b/azurerm/resource_arm_app_service_plan_test.go index 1fec9ca40617..ade1b5919df4 100644 --- a/azurerm/resource_arm_app_service_plan_test.go +++ b/azurerm/resource_arm_app_service_plan_test.go @@ -10,6 +10,46 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) +func TestAzureRMAppServicePlanName_validation(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "ab", + ErrCount: 0, + }, + { + Value: "abc", + ErrCount: 0, + }, + { + Value: "webapp1", + ErrCount: 0, + }, + { + Value: "hello-world", + ErrCount: 0, + }, + { + Value: "hello_world", + ErrCount: 1, + }, + { + Value: "helloworld21!", + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validateAppServicePlanName(tc.Value, "azurerm_app_service_plan") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the App Service Plan Name to trigger a validation error for '%s'", tc.Value) + } + } +} + func TestAccAzureRMAppServicePlan_basicWindows(t *testing.T) { ri := acctest.RandInt() config := testAccAzureRMAppServicePlan_basicWindows(ri, testLocation()) diff --git a/azurerm/resource_arm_app_service_test.go b/azurerm/resource_arm_app_service_test.go index 0a15e47f23f9..4f34f22f94ce 100644 --- a/azurerm/resource_arm_app_service_test.go +++ b/azurerm/resource_arm_app_service_test.go @@ -10,6 +10,46 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) +func TestAzureRMAppServiceName_validation(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "ab", + ErrCount: 0, + }, + { + Value: "abc", + ErrCount: 0, + }, + { + Value: "webapp1", + ErrCount: 0, + }, + { + Value: "hello-world", + ErrCount: 0, + }, + { + Value: "hello_world", + ErrCount: 1, + }, + { + Value: "helloworld21!", + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validateAppServiceName(tc.Value, "azurerm_app_service") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the App Service Name to trigger a validation error for '%s'", tc.Value) + } + } +} + func TestAccAzureRMAppService_basic(t *testing.T) { resourceName := "azurerm_app_service.test" ri := acctest.RandInt()