Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding validation for both the App Service/App Service Plan resources names #528

Merged
merged 1 commit into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions azurerm/resource_arm_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),

Expand Down Expand Up @@ -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
}
18 changes: 15 additions & 3 deletions azurerm/resource_arm_app_service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down Expand Up @@ -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
}
40 changes: 40 additions & 0 deletions azurerm/resource_arm_app_service_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
40 changes: 40 additions & 0 deletions azurerm/resource_arm_app_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down