Skip to content

Commit

Permalink
r/storage_share_directory: adding validation for the name field
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Jul 9, 2019
1 parent 7214179 commit 472d6eb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
24 changes: 24 additions & 0 deletions azurerm/helpers/validate/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package validate

import (
"fmt"
"regexp"
"strings"
)

func StorageShareDirectoryName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

// File share names can contain only lowercase letters, numbers, and hyphens,
// and must begin and end with a letter or a number.
if !regexp.MustCompile(`^[a-z0-9][a-z0-9-]+[a-z0-9]$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%s must contain only lowercase alphanumeric characters, numbers and hyphens. It must start and end with a letter and end only with a number or letter", k))
}

// The name cannot contain two consecutive hyphens.
if strings.Contains(value, "--") {
errors = append(errors, fmt.Errorf("%s cannot contain two concecutive hyphens", k))
}

return warnings, errors
}
53 changes: 53 additions & 0 deletions azurerm/helpers/validate/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package validate

import "testing"

func TestValidateStorageShareDirectoryName(t *testing.T) {
testCases := []struct {
Input string
Expected bool
}{
{
Input: "",
Expected: false,
},
{
Input: "abc123",
Expected: true,
},
{
Input: "123abc",
Expected: true,
},
{
Input: "123-abc",
Expected: true,
},
{
Input: "-123-abc",
Expected: false,
},
{
Input: "123-abc-",
Expected: false,
},
{
Input: "123--abc",
Expected: false,
},
}

for _, v := range testCases {
t.Logf("[DEBUG] Test Input %q", v.Input)

warnings, errors := StorageShareDirectoryName(v.Input, "name")
if len(warnings) != 0 {
t.Fatalf("Expected no warnings but got %d", len(warnings))
}

result := len(errors) == 0
if result != v.Expected {
t.Fatalf("Expected the result to be %t but got %t (and %d errors)", v.Expected, result, len(errors))
}
}
}
7 changes: 4 additions & 3 deletions azurerm/resource_arm_storage_share_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func resourceArmStorageShareDirectory() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.StorageShareDirectoryName,
},
"share_name": {
Type: schema.TypeString,
Expand Down

0 comments on commit 472d6eb

Please sign in to comment.