Skip to content

Commit

Permalink
provider/azurerm: Add support for storage container name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stack72 committed May 24, 2016
1 parent a764238 commit 70410f6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
26 changes: 23 additions & 3 deletions builtin/providers/azurerm/resource_arm_storage_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/hashicorp/terraform/helper/schema"
"regexp"
)

func resourceArmStorageContainer() *schema.Resource {
Expand All @@ -18,9 +19,10 @@ func resourceArmStorageContainer() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageContainerName,
},
"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -47,6 +49,24 @@ func resourceArmStorageContainer() *schema.Resource {
}
}

func validateArmStorageContainerName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only lowercase alphanumeric characters and hyphens allowed in %q: %q",
k, value))
}
if len(value) < 3 || len(value) > 63 {
errors = append(errors, fmt.Errorf(
"%q must be between 3 and 63 characters: %q", k, value))
}
if regexp.MustCompile(`^-`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot begin with a hyphen: %q", k, value))
}
return
}

func validateArmStorageContainerAccessType(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))
validTypes := map[string]struct{}{
Expand Down
28 changes: 28 additions & 0 deletions builtin/providers/azurerm/resource_arm_storage_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error {
return nil
}

func TestValidateArmStorageContainerName(t *testing.T) {
validNames := []string{
"valid-name",
"valid02-name",
}
for _, v := range validNames {
_, errors := validateArmStorageContainerName(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid Storage Container Name: %q", v, errors)
}
}

invalidNames := []string{
"InvalidName1",
"-invalidname1",
"invalid_name",
"invalid!",
"ww",
strings.Repeat("w", 65),
}
for _, v := range invalidNames {
_, errors := validateArmStorageContainerName(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid Storage Container Name", v)
}
}
}

var testAccAzureRMStorageContainer_basic = `
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
Expand Down

0 comments on commit 70410f6

Please sign in to comment.