From c745fec19060d561d0c56783e387e434a139d790 Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 19 May 2021 21:25:40 -0400 Subject: [PATCH] Remove acr validator from scopemaps (#11784) Remove incorrect validator from scopemaps, as mentioned in #11783 --- .../container_registry_scope_map_resource.go | 2 +- .../container_registry_scope_map_name.go | 24 +++++++ .../container_registry_scope_map_name_test.go | 68 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 azurerm/internal/services/containers/validate/container_registry_scope_map_name.go create mode 100644 azurerm/internal/services/containers/validate/container_registry_scope_map_name_test.go diff --git a/azurerm/internal/services/containers/container_registry_scope_map_resource.go b/azurerm/internal/services/containers/container_registry_scope_map_resource.go index 3de73d874b139..9442aa4590f73 100644 --- a/azurerm/internal/services/containers/container_registry_scope_map_resource.go +++ b/azurerm/internal/services/containers/container_registry_scope_map_resource.go @@ -40,7 +40,7 @@ func resourceContainerRegistryScopeMap() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validate.ContainerRegistryName, + ValidateFunc: validate.ContainerRegistryScopeMapName, }, "description": { diff --git a/azurerm/internal/services/containers/validate/container_registry_scope_map_name.go b/azurerm/internal/services/containers/validate/container_registry_scope_map_name.go new file mode 100644 index 0000000000000..2a35b0c2989df --- /dev/null +++ b/azurerm/internal/services/containers/validate/container_registry_scope_map_name.go @@ -0,0 +1,24 @@ +package validate + +import ( + "fmt" + "regexp" +) + +func ContainerRegistryScopeMapName(v interface{}, k string) (warnings []string, errors []error) { + value := v.(string) + if !regexp.MustCompile(`^[a-zA-Z0-9\-]+$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "alpha numeric characters and hyphens only are allowed in %q: %q", k, value)) + } + + if 5 > len(value) { + errors = append(errors, fmt.Errorf("%q cannot be less than 5 characters: %q", k, value)) + } + + if len(value) >= 50 { + errors = append(errors, fmt.Errorf("%q cannot be longer than 50 characters: %q %d", k, value, len(value))) + } + + return warnings, errors +} diff --git a/azurerm/internal/services/containers/validate/container_registry_scope_map_name_test.go b/azurerm/internal/services/containers/validate/container_registry_scope_map_name_test.go new file mode 100644 index 0000000000000..234364a1f67aa --- /dev/null +++ b/azurerm/internal/services/containers/validate/container_registry_scope_map_name_test.go @@ -0,0 +1,68 @@ +package validate_test + +import ( + "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers/validate" +) + +func TestContainerRegistryScopeMapName(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "four", + ErrCount: 1, + }, + { + Value: "5five", + ErrCount: 0, + }, + { + Value: "five-123", + ErrCount: 0, + }, + { + Value: "hello-world", + ErrCount: 0, + }, + { + Value: "hello_world", + ErrCount: 1, + }, + { + Value: "helloWorld", + ErrCount: 0, + }, + { + Value: "helloworld12", + ErrCount: 0, + }, + { + Value: "hello@world", + ErrCount: 1, + }, + + { + Value: "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd3324120", + ErrCount: 0, + }, + { + Value: "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd33241202", + ErrCount: 0, + }, + { + Value: "qfvbdsbvipqdbwsbddbdcwqfjjfewsqwcdw21ddwqwd3324120fadfadf", + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validate.ContainerRegistryScopeMapName(tc.Value, "azurerm_container_registry_scope_map") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the Azure RM Container Registry Token Name to trigger a validation error: %v", errors) + } + } +}