provider/azurerm: Add support for storage container name validation (#6852)
This commit is contained in:
parent
e0d13f82d2
commit
d997b3fb5e
|
@ -5,6 +5,8 @@ import (
|
|||
"log"
|
||||
"strings"
|
||||
|
||||
"regexp"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/storage"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
@ -18,9 +20,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,
|
||||
|
@ -47,6 +50,25 @@ func resourceArmStorageContainer() *schema.Resource {
|
|||
}
|
||||
}
|
||||
|
||||
//Following the naming convention as laid out in the docs
|
||||
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{}{
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue