diff --git a/.vscode/launch.json b/.vscode/launch.json index 35d1abf..c7161e3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,6 +8,10 @@ "request": "launch", "mode": "test", //"program": "${workspaceFolder}/main.go" + "args": [ + "check-go-version", + "false" + ], "program": "${file}" }, { diff --git a/azurecaf/models_generated.go b/azurecaf/models_generated.go index 3464619..886da01 100644 --- a/azurecaf/models_generated.go +++ b/azurecaf/models_generated.go @@ -1,6 +1,6 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2020-09-17 09:55:08.5334716 +0100 BST m=+0.001350401 +// 2020-09-17 13:18:28.6516898 +0100 BST m=+0.001429201 // using data from // resourceDefinition.json and resourceDefinition_out_of_docs.json @@ -82,7 +82,7 @@ var ResourceDefinitions = map[string]ResourceStructure{ "azurerm_eventgrid_domain": {"azurerm_eventgrid_domain", "egd", 3, 50, false, "[^0-9A-Za-z-]", "^[a-zA-Z0-9-]{3,50}$", true, "resourceGroup" }, "azurerm_eventgrid_domain_topic": {"azurerm_eventgrid_domain_topic", "egdt", 3, 50, false, "[^0-9A-Za-z-]", "^[a-zA-Z0-9-]{3,50}$", true, "parent" }, "azurerm_eventgrid_event_subscription": {"azurerm_eventgrid_event_subscription", "egs", 3, 64, false, "[^0-9A-Za-z-]", "^[a-zA-Z0-9-]{3,64}$", true, "resourceGroup" }, - "azurerm_eventgrid_topic": {"azurerm_eventgrid_topic", "egt", 3, 50, false, "[^0-9A-Za-]", "^[a-zA-Z0-9-]{3,50}$", true, "resourceGroup" }, + "azurerm_eventgrid_topic": {"azurerm_eventgrid_topic", "egt", 3, 50, false, `[^0-9A-Za-z-]`, "^[a-zA-Z0-9-]{3,50}$", true, "resourceGroup" }, "azurerm_eventhub": {"azurerm_eventhub", "evh", 1, 50, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,48}[a-zA-Z0-9]$", true, "parent" }, "azurerm_eventhub_authorization_rule": {"azurerm_eventhub_authorization_rule", "ehar", 1, 50, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,48}[a-zA-Z0-9]$", true, "parent" }, "azurerm_eventhub_consumer_group": {"azurerm_eventhub_consumer_group", "ehcg", 1, 50, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,48}[a-zA-Z0-9]$", true, "parent" }, @@ -170,8 +170,8 @@ var ResourceDefinitions = map[string]ResourceStructure{ "azurerm_proximity_placement_group": {"azurerm_proximity_placement_group", "ppg", 1, 80, false, "[^a-zA-Z0-9\\-\\._]", "^[a-zA-Z0-9][a-zA-Z0-9\\-\\._]{0,78}[a-zA-Z0-9_]$", true, "resourceGroup" }, "azurerm_public_ip": {"azurerm_public_ip", "pip", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$", true, "parent" }, "azurerm_public_ip_prefix": {"azurerm_public_ip_prefix", "pippf", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$", true, "parent" }, - "azurerm_recovery_services_vault": {"azurerm_recovery_services_vault", "rsv", 2, 50, false, "[a-zA-Z0-9\\-]", "^[a-zA-Z][a-zA-Z0-9\\-]{1,49}$", true, "resourceGroup" }, - "azurerm_recovery_services_vault_backup_police": {"azurerm_recovery_services_vault_backup_police", "rsvbp", 3, 150, false, "[a-zA-Z0-9\\-]", "^[a-zA-Z][a-zA-Z0-9\\-]{1,148}[a-zA-Z0-9]$", true, "resourceGroup" }, + "azurerm_recovery_services_vault": {"azurerm_recovery_services_vault", "rsv", 2, 50, false, `[^a-zA-Z0-9-]`, "^[a-zA-Z][a-zA-Z0-9-]{1,49}$", true, "resourceGroup" }, + "azurerm_recovery_services_vault_backup_police": {"azurerm_recovery_services_vault_backup_police", "rsvbp", 3, 150, false, `[^a-zA-Z0-9-]`, "^[a-zA-Z][a-zA-Z0-9\\-]{1,148}[a-zA-Z0-9]$", true, "resourceGroup" }, "azurerm_redis_cache": {"azurerm_redis_cache", "redis", 1, 63, false, "[^0-9A-Za-z-]", "^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$", true, "global" }, "azurerm_redis_firewall_rule": {"azurerm_redis_firewall_rule", "redisfw", 1, 256, false, "[^0-9A-Za-z]", "^[a-zA-Z0-9]{1,256}$", false, "parent" }, "azurerm_relay_hybrid_connection": {"azurerm_relay_hybrid_connection", "rlhc", 1, 260, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,258}[a-zA-Z0-9]$", true, "parent" }, @@ -196,7 +196,7 @@ var ResourceDefinitions = map[string]ResourceStructure{ "azurerm_snapshots": {"azurerm_snapshots", "snap", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$", true, "parent" }, "azurerm_sql_elasticpool": {"azurerm_sql_elasticpool", "sqlep", 1, 128, false, "[<>*%:.?\\+\\/]", "^[^<>*%:.?\\+\\/]{1,127}[^<>*%:.?\\+\\/ ]$", true, "parent" }, "azurerm_sql_failover_group": {"azurerm_sql_failover_group", "sqlfg", 1, 63, true, "[^0-9a-z-]", "^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$", true, "global" }, - "azurerm_sql_firewall_rule": {"azurerm_sql_firewall_rule", "sqlfw", 1, 128, false, "[^<>*%:?\\+\\/]", "^[^<>*%:?\\+\\/]{1,127}[^<>*%:.?\\+\\/]$", true, "parent" }, + "azurerm_sql_firewall_rule": {"azurerm_sql_firewall_rule", "sqlfw", 1, 128, false, `[<>*%:?\\+\\/]`, "^[^<>*%:?\\+\\/]{1,127}[^<>*%:.?\\+\\/]$", true, "parent" }, "azurerm_sql_server": {"azurerm_sql_server", "sql", 1, 63, true, "[^0-9a-z-]", "^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$", true, "global" }, "azurerm_storage_account": {"azurerm_storage_account", "st", 3, 24, true, "[^0-9a-z]", "^[a-z0-9]{3,24}$", false, "global" }, "azurerm_storage_blob": {"azurerm_storage_blob", "blob", 1, 1024, false, "[\\s\\/$#&]", "^[^\\s\\/$#&]{1,1000}[^\\s\\/$#&]{0,24}$", true, "parent" }, @@ -218,7 +218,7 @@ var ResourceDefinitions = map[string]ResourceStructure{ "azurerm_stream_analytics_stream_input_eventhub": {"azurerm_stream_analytics_stream_input_eventhub", "asaieh", 3, 63, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9-_]{3,63}$", true, "parent" }, "azurerm_stream_analytics_stream_input_iothub": {"azurerm_stream_analytics_stream_input_iothub", "asaiiot", 3, 63, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9-_]{3,63}$", true, "parent" }, "azurerm_subnet": {"azurerm_subnet", "snet", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$", true, "parent" }, - "azurerm_synapse_firewall_rule": {"azurerm_synapse_firewall_rule", "syfw", 1, 128, false, "[^<>*%:?\\+\\/]", "^[^<>*%:?\\+\\/]{1,127}[^<>*%:.?\\+\\/]$", true, "parent" }, + "azurerm_synapse_firewall_rule": {"azurerm_synapse_firewall_rule", "syfw", 1, 128, false, `[<>*%:?\\+\\/]`, "^[^<>*%:?\\+\\/]{1,127}[^<>*%:.?\\+\\/]$", true, "parent" }, "azurerm_synapse_spark_pool": {"azurerm_synapse_spark_pool", "sysp", 1, 15, true, "[^0-9a-zA-Z]", "^[0-9a-zA-Z]{1,15}$", false, "parent" }, "azurerm_synapse_workspace": {"azurerm_synapse_workspace", "syws", 1, 45, true, "[^0-9a-z]", "^[0-9a-z]{1,45}$", false, "resourceGroup" }, "azurerm_template_deployment": {"azurerm_template_deployment", "deploy", 1, 64, false, "[^0-9A-Za-z-._\\(\\)]", "^[a-zA-Z0-9-._\\(\\)]{1,64}$", true, "resourceGroup" }, diff --git a/azurecaf/models_generated_test.go b/azurecaf/models_generated_test.go index 07ad45a..804e6be 100644 --- a/azurecaf/models_generated_test.go +++ b/azurecaf/models_generated_test.go @@ -21,6 +21,22 @@ func TestCompileRegexValidation(t *testing.T) { } } +func TestStrimingNameRegexValidation(t *testing.T) { + for _, resource := range ResourceDefinitions { + reg, err := regexp.Compile(resource.RegEx) + if err != nil { + t.Logf("Error on the regex %s for the resource %s error %v", resource.RegEx, resource.ResourceTypeName, err.Error()) + t.Fail() + } + content := "abcde" + result := reg.ReplaceAllString(content, "") + if len(result) != 5 { + t.Logf("%s : expected not be clear anything startd with %s end with %s", resource.ResourceTypeName, content, result) + t.Fail() + } + } +} + func TestRegexValidationMinLength(t *testing.T) { content := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" contentBase := []rune(content) diff --git a/azurecaf/resource_name.go b/azurecaf/resource_name.go index d8ba02b..3c0a852 100644 --- a/azurecaf/resource_name.go +++ b/azurecaf/resource_name.go @@ -417,6 +417,7 @@ func getResourceName(resourceTypeName string, separator string, separator = cleanString(separator, resource) randomSuffix = cleanString(randomSuffix, resource) } + var resourceName string if passthrough { @@ -431,7 +432,7 @@ func getResourceName(resourceTypeName string, separator string, } if !validationRegEx.MatchString(resourceName) { - return "", fmt.Errorf("Invalid name for Random CAF naming %s %s, the pattern %s doesn't match %s", resource.ResourceTypeName, name, resource.ValidationRegExp, resourceName) + return "", fmt.Errorf("Invalid name for CAF naming %s %s, the pattern %s doesn't match %s", resource.ResourceTypeName, name, resource.ValidationRegExp, resourceName) } return resourceName, nil diff --git a/azurecaf/resource_name_test.go b/azurecaf/resource_name_test.go index 5678530..6272b4c 100644 --- a/azurecaf/resource_name_test.go +++ b/azurecaf/resource_name_test.go @@ -136,6 +136,27 @@ func TestAccResourceName_CafClassic(t *testing.T) { }) } +func TestAccResourceNameRsv_CafClassic(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccResourceNameCafClassicConfigRsv, + Check: resource.ComposeTestCheckFunc( + testAccCafNamingValidation( + "azurecaf_name.rsv", + "pr1-rsv-test-gm-su1", + 19, + ""), + regexMatch("azurecaf_name.rsv", regexp.MustCompile(ResourceDefinitions["azurerm_recovery_services_vault"].ValidationRegExp), 1), + ), + }, + }, + }) +} + func TestComposeName(t *testing.T) { namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"} prefixes := []string{"a", "b"} @@ -226,6 +247,21 @@ func TestGetResourceNameValid(t *testing.T) { } } +func TestGetResourceNameValidRsv(t *testing.T) { + namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"} + resourceName, err := getResourceName("azurerm_recovery_services_vault", "-", []string{"a", "b"}, "test", nil, "1234", "cafclassic", true, false, true, namePrecedence) + expected := "a-b-rsv-test-1234" + + if err != nil { + t.Logf("getResource Name generated an error %s", err.Error()) + t.Fail() + } + if expected != resourceName { + t.Logf("invalid name, expected %s got %s", expected, resourceName) + t.Fail() + } +} + func TestGetResourceNameValidNoSlug(t *testing.T) { namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"} resourceName, err := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, false, namePrecedence) @@ -298,6 +334,7 @@ resource "azurecaf_name" "classic_rg" { resource_type = "azurerm_resource_group" prefixes = ["pr1", "pr2"] suffixes = ["su1", "su2"] + random_seed = 1 random_length = 5 clean_input = true } @@ -307,6 +344,7 @@ resource "azurecaf_name" "classic_acr_invalid" { resource_type = "azurerm_container_registry" prefixes = ["pr1", "pr2"] suffixes = ["su1", "su2"] + random_seed = 1 random_length = 5 clean_input = true } @@ -316,8 +354,26 @@ resource "azurecaf_name" "passthrough" { resource_type = "azurerm_container_registry" prefixes = ["pr1", "pr2"] suffixes = ["su1", "su2"] + random_seed = 1 random_length = 5 clean_input = true passthrough = true } ` + +const testAccResourceNameCafClassicConfigRsv = ` + + +# Resource Group + +resource "azurecaf_name" "rsv" { + name = "test" + resource_type = "azurerm_recovery_services_vault" + prefixes = ["pr1"] + suffixes = ["su1"] + random_length = 2 + random_seed = 1 + clean_input = true + passthrough = false +} +` diff --git a/go.mod b/go.mod index 89c5342..d6cabbe 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,22 @@ go 1.13 require ( cloud.google.com/go v0.61.0 // indirect + github.com/go-delve/delve v1.5.0 // indirect + github.com/google/go-dap v0.3.0 // indirect github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 // indirect github.com/hashicorp/go-plugin v1.3.0 // indirect github.com/hashicorp/hcl/v2 v2.3.0 // indirect github.com/hashicorp/terraform-json v0.5.0 // indirect github.com/hashicorp/terraform-plugin-sdk v1.9.0 + github.com/mattn/go-colorable v0.1.7 // indirect + github.com/mattn/go-runewidth v0.0.9 // indirect github.com/mitchellh/go-testing-interface v1.0.4 // indirect + github.com/peterh/liner v1.2.0 // indirect + github.com/spf13/cobra v1.0.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/ulikunitz/xz v0.5.7 // indirect + go.starlark.net v0.0.0-20200901195727-6e684ef5eeee // indirect + golang.org/x/arch v0.0.0-20200826200359-b19915210f00 // indirect + golang.org/x/sys v0.0.0-20200917073148-efd3b9a0ff20 // indirect + gopkg.in/yaml.v2 v2.3.0 // indirect ) diff --git a/resourceDefinition.json b/resourceDefinition.json index da66cd0..55500d8 100644 --- a/resourceDefinition.json +++ b/resourceDefinition.json @@ -764,12 +764,12 @@ "name": "azurerm_recovery_services_vault", "min_length": 2, "max_length": 50, - "validation_regex": "\"^[a-zA-Z][a-zA-Z0-9\\\\-]{1,49}$\"", + "validation_regex": "\"^[a-zA-Z][a-zA-Z0-9-]{1,49}$\"", "scope": "resourceGroup", "slug": "rsv", "dashes": true, "lowercase": false, - "regex": "\"[a-zA-Z0-9\\\\-]\"" + "regex": "`[^a-zA-Z0-9-]`" }, { "name": "azurerm_recovery_services_vault_backup_police", @@ -780,7 +780,7 @@ "slug": "rsvbp", "dashes": true, "lowercase": false, - "regex": "\"[a-zA-Z0-9\\\\-]\"" + "regex": "`[^a-zA-Z0-9-]`" }, { "name": "azurerm_data_factory_linked_service_mysql", @@ -1744,7 +1744,7 @@ "slug": "sqlfw", "dashes": true, "lowercase": false, - "regex": "\"[^<>*%:?\\\\+\\\\/]\"" + "regex": "`[<>*%:?\\\\+\\\\/]`" }, { "name": "azurerm_log_analytics_workspace", @@ -2064,7 +2064,7 @@ "slug": "egt", "dashes": true, "lowercase": false, - "regex": "\"[^0-9A-Za-]\"" + "regex": "`[^0-9A-Za-z-]`" }, { "name": "azurerm_relay_namespace", @@ -2185,6 +2185,6 @@ "slug": "syfw", "dashes": true, "lowercase": false, - "regex": "\"[^<>*%:?\\\\+\\\\/]\"" + "regex": "`[<>*%:?\\\\+\\\\/]`" } ] \ No newline at end of file