From b26356045a3ab1203a6f9ceeda6bb5fb0fdde116 Mon Sep 17 00:00:00 2001 From: Gabriel Nepomuceno Date: Tue, 15 Sep 2020 11:39:10 +0100 Subject: [PATCH 1/4] Added azurerm_recovery_services_vault Including recover service vault plug genral resource to generate names that do not appear in any current defined resource. Correcting minimum length in randon length --- azurecaf/models.go | 3 +++ azurecaf/models_generated.go | 7 ++++++- azurecaf/resource_name.go | 2 +- resourceDefinition.json | 22 ++++++++++++++++++++++ resourceDefinition_out_of_docs.json | 23 +++++++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/azurecaf/models.go b/azurecaf/models.go index 7d426a8..e2b5842 100644 --- a/azurecaf/models.go +++ b/azurecaf/models.go @@ -62,6 +62,9 @@ var ( // Generate a random value to add to the resource names func randSeq(length int, seed *int64) string { + if length == 0 { + return "" + } // initialize random seed if seed == nil { value := time.Now().UnixNano() diff --git a/azurecaf/models_generated.go b/azurecaf/models_generated.go index 4660bf2..e0ed4a1 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-01 13:47:54.476599 +0800 +08 m=+0.003636662 +// 2020-09-15 11:34:10.0642392 +0100 BST m=+0.001246801 // using data from // resourceDefinition.json and resourceDefinition_out_of_docs.json @@ -164,6 +164,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_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" }, @@ -225,10 +227,12 @@ var ResourceDefinitions = map[string]ResourceStructure{ "databricks_cluster": {"databricks_cluster", "dbc", 3, 30, false, "[^a-zA-Z0-9-_]", "^[a-zA-Z0-9-_]{3,30}$", true, "parent" }, "databricks_high_concurrency_cluster": {"databricks_high_concurrency_cluster", "dbhcc", 3, 30, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9-_]{3,30}$", true, "parent" }, "databricks_standard_cluster": {"databricks_standard_cluster", "dbsc", 3, 30, false, "[^a-zA-Z0-9-_]", "^[a-zA-Z0-9-_]{3,30}$", true, "parent" }, + "general": {"general", "", 1, 250, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9-_]{1,250}$", true, "global" }, } // ResourceMaps are a map from the slug to the resource definition var ResourceMaps = map[string]string { + "": "general", "aa": "azurerm_automation_account", "aacert": "azurerm_automation_certificate", "aacred": "azurerm_automation_credential", @@ -383,6 +387,7 @@ var ResourceMaps = map[string]string { "rln": "azurerm_relay_namespace", "route": "azurerm_route_table", "rsv": "azurerm_hdinsight_rserver_cluster", + "rsvbp": "azurerm_recovery_services_vault_backup_police", "rt": "azurerm_route", "sb": "azurerm_servicebus_namespace", "sbar": "azurerm_servicebus_namespace_authorization_rule", diff --git a/azurecaf/resource_name.go b/azurecaf/resource_name.go index 8b551d5..f09497a 100644 --- a/azurecaf/resource_name.go +++ b/azurecaf/resource_name.go @@ -50,7 +50,7 @@ func resourceName() *schema.Resource { Type: schema.TypeInt, Optional: true, ForceNew: true, - ValidateFunc: validation.IntAtLeast(1), + ValidateFunc: validation.IntAtLeast(0), Default: 0, }, "result": { diff --git a/resourceDefinition.json b/resourceDefinition.json index 87e4feb..46820d9 100644 --- a/resourceDefinition.json +++ b/resourceDefinition.json @@ -760,6 +760,28 @@ "lowercase": false, "regex": "\"[^0-9A-Za-z<>*%:.?\\\\+\\\\/]\"" }, + { + "name": "azurerm_recovery_services_vault", + "min_length": 2, + "max_length": 50, + "validation_regex": "\"^[a-zA-Z][a-zA-Z0-9\\\\-]{1,49}$\"", + "scope": "resourceGroup", + "slug": "rsv", + "dashes": true, + "lowercase": false, + "regex": "\"[a-zA-Z0-9\\\\-]\"" + }, + { + "name": "azurerm_recovery_services_vault_backup_police", + "min_length": 3, + "max_length": 150, + "validation_regex": "\"^[a-zA-Z][a-zA-Z0-9\\\\-]{1,148}[a-zA-Z0-9]$\"", + "scope": "resourceGroup", + "slug": "rsvbp", + "dashes": true, + "lowercase": false, + "regex": "\"[a-zA-Z0-9\\\\-]\"" + }, { "name": "azurerm_data_factory_linked_service_mysql", "min_length": 1, diff --git a/resourceDefinition_out_of_docs.json b/resourceDefinition_out_of_docs.json index ef63349..38757a0 100644 --- a/resourceDefinition_out_of_docs.json +++ b/resourceDefinition_out_of_docs.json @@ -328,5 +328,28 @@ "dashes": true, "lowercase": false, "regex": "\"[^0-9A-Za-z_-]\"" + }, + { + "name": "general", + "min_length": 1, + "max_length": 250, + "validation_regex": "\"^[a-zA-Z0-9-_]{1,250}$\"", + "scope": "global", + "slug": "", + "dashes": true, + "lowercase": false, + "regex": "\"[^0-9A-Za-z_-]\"" + }, + { + "name": "general_safe", + "min_length": 1, + "max_length": 250, + "validation_regex": "\"^[a-z]{1,250}$\"", + "scope": "global", + "slug": "", + "dashes": false, + "lowercase": true, + "regex": "\"[^a-z]\"" } + ] \ No newline at end of file From f02e4d34a7c5ea1184d329880e37afd374ca50ed Mon Sep 17 00:00:00 2001 From: Gabriel Nepomuceno Date: Tue, 15 Sep 2020 13:10:07 +0100 Subject: [PATCH 2/4] Corrcting conditional GPG --- .github/workflows/go.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 3e2b2d3..f540ea4 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -48,6 +48,7 @@ jobs: - name: Import GPG key id: import_gpg uses: crazy-max/ghaction-import-gpg@v2 + if: startsWith(github.ref, 'refs/tags/') env: GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }} From 760cae3e4672040d9440b2c3198a61e1afc2854d Mon Sep 17 00:00:00 2001 From: Gabriel Nepomuceno Date: Wed, 16 Sep 2020 01:38:30 +0100 Subject: [PATCH 3/4] Adding missing resources Issues #36 #38 #29 --- azurecaf/models.go | 2 +- azurecaf/models_generated.go | 21 +++++- azurecaf/resource_name.go | 18 ++++- azurecaf/resource_name_test.go | 22 +++++- ...ource_naming_convention_cafclassic_test.go | 2 +- docs/resources/azurecaf_name.md | 37 ++++++---- resourceDefinition.json | 74 ++++++++++++++++++- 7 files changed, 147 insertions(+), 29 deletions(-) diff --git a/azurecaf/models.go b/azurecaf/models.go index e2b5842..1247de6 100644 --- a/azurecaf/models.go +++ b/azurecaf/models.go @@ -86,7 +86,7 @@ var Resources = map[string]ResourceStructure{ "acr": {"azure container registry", "acr", 5, 50, true, alphanum, "^[0-9A-Za-z]{5,50}$", true, "resourceGroup"}, "afw": {"azure firewall", "afw", 1, 80, false, alphanumhup, "^[a-zA-Z][0-9A-Za-z_.-]{0,79}$", true, "resourceGroup"}, "agw": {"application gateway", "agw", 1, 80, false, alphanumhup, "^[0-9a-zA-Z][0-9A-Za-z_.-]{0,78}[0-9a-zA-Z_]$", true, "resourceGroup"}, - "aks": {"azure kubernetes service", "aks", 1, 63, false, alphanumu, "^[0-9a-zA-Z][0-9A-Za-z_.-]{0,61}[0-9a-zA-Z]$", true, "resourceGroup"}, + "aks": {"azure kubernetes service", "aks", 1, 63, false, alphanumhu, "^[0-9a-zA-Z][0-9A-Za-z_.-]{0,61}[0-9a-zA-Z]$", true, "resourceGroup"}, "aksdns": {"aksdns prefix", "aksdns", 3, 45, false, alphanumh, "^[a-zA-Z][0-9A-Za-z-]{0,43}[0-9a-zA-Z]$", true, "resourceGroup"}, "aksnpl": {"aks node pool for Linux", "aksnpl", 2, 12, true, alphanum, "^[a-zA-Z][0-9a-z]{0,11}$", true, "resourceGroup"}, "aksnpw": {"aks node pool for Windows", "aksnpw", 2, 6, true, alphanum, "^[a-zA-Z][0-9a-z]{0,5}$", true, "resourceGroup"}, diff --git a/azurecaf/models_generated.go b/azurecaf/models_generated.go index e0ed4a1..c3fa64e 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-15 11:34:10.0642392 +0100 BST m=+0.001246801 +// 2020-09-16 01:36:06.5914436 +0100 BST m=+0.001222301 // using data from // resourceDefinition.json and resourceDefinition_out_of_docs.json @@ -8,10 +8,16 @@ package azurecaf // ResourceDefinitions are a map of difinitions for the resources supported var ResourceDefinitions = map[string]ResourceStructure{ + "aks_node_pool_linux": {"aks_node_pool_linux", "npl", 1, 12, false, "[^0-9a-z]", "^[a-z][0-9a-z]{0,11}$", false, "parent" }, + "aks_node_pool_windows": {"aks_node_pool_windows", "npw", 1, 6, false, "[^0-9a-z]", "^[a-z][0-9a-z]{0,5}$", false, "parent" }, "azurerm_analysis_services_server": {"azurerm_analysis_services_server", "as", 3, 63, true, "[^0-9a-z]", "^[a-z][a-z0-9]{2,62}$", false, "resourceGroup" }, "azurerm_api_management_service": {"azurerm_api_management_service", "apim", 1, 50, false, "[^0-9A-Za-z-]", "^[a-z][a-zA-Z0-9-][a-zA-Z0-9]{0,48}$", false, "global" }, "azurerm_app_configuration": {"azurerm_app_configuration", "appcg", 5, 50, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9_-]{5,50}$", true, "resourceGroup" }, + "azurerm_app_service": {"azurerm_app_service", "app", 2, 60, false, "[^0-9A-Za-z-]", "^[0-9A-Za-z][0-9A-Za-z-]{0,58}[0-9a-zA-Z]$", true, "global" }, + "azurerm_app_service_environment": {"azurerm_app_service_environment", "plan", 2, 37, false, "[^0-9A-Za-z-]", "^[0-9A-Za-z-]{2,37}$", true, "resourceGroup" }, + "azurerm_app_service_plan": {"azurerm_app_service_plan", "plan", 1, 40, false, "[^0-9A-Za-z-]", "^[0-9A-Za-z-]{1,40}$", true, "resourceGroup" }, "azurerm_application_gateway": {"azurerm_application_gateway", "agw", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$", true, "resourceGroup" }, + "azurerm_application_insights": {"azurerm_application_insights", "appi", 1, 260, false, "[^0-9A-Za-z_.-]", "^[^%&\\?/. ][^%&\\?/]{0,258}[^%&\\?/. ]$", true, "resourceGroup" }, "azurerm_application_security_group": {"azurerm_application_security_group", "asg", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$", true, "resourceGroup" }, "azurerm_automation_account": {"azurerm_automation_account", "aa", 6, 50, false, "[^0-9A-Za-z_-]", "^[a-zA-Z][a-zA-Z0-9-]{4,48}[a-zA-Z0-9]$", true, "resourceGroup" }, "azurerm_automation_certificate": {"azurerm_automation_certificate", "aacert", 1, 128, false, `[^-\w\._\(\)]`, "^[^<>*%:.?\\+\\/]{0,127}[^<>*%:.?\\+\\/ ]$", true, "parent" }, @@ -20,7 +26,7 @@ var ResourceDefinitions = map[string]ResourceStructure{ "azurerm_automation_schedule": {"azurerm_automation_schedule", "aasched", 1, 128, false, `[^-\w\._\(\)]`, "^[^<>*%:.?\\+\\/]{0,127}[^<>*%:.?\\+\\/ ]$", true, "parent" }, "azurerm_automation_variable": {"azurerm_automation_variable", "aavar", 1, 128, false, `[^-\w\._\(\)]`, "^[^<>*%:.?\\+\\/]{0,127}[^<>*%:.?\\+\\/ ]$", true, "parent" }, "azurerm_availability_set": {"azurerm_availability_set", "avail", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-_.]{0,78}[a-zA-Z0-9_]$", true, "resourceGroup" }, - "azurerm_bastion_host": {"azurerm_bastion_host", "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_bastion_host": {"azurerm_bastion_host", "bast", 1, 80, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$", true, "parent" }, "azurerm_batch_account": {"azurerm_batch_account", "ba", 3, 24, true, "[^0-9a-z]", "^[a-z0-9]{3,24}$", false, "region" }, "azurerm_batch_application": {"azurerm_batch_application", "baapp", 1, 64, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9_-]{1,64}$", true, "parent" }, "azurerm_batch_certificate": {"azurerm_batch_certificate", "bacert", 5, 45, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9_-]{5,45}$", true, "parent" }, @@ -110,7 +116,7 @@ var ResourceDefinitions = map[string]ResourceStructure{ "azurerm_key_vault_certificate": {"azurerm_key_vault_certificate", "kvc", 1, 127, false, "[^0-9A-Za-z-]", "^[a-zA-Z0-9-]{1,127}$", true, "parent" }, "azurerm_key_vault_key": {"azurerm_key_vault_key", "kvk", 1, 127, false, "[^0-9A-Za-z-]", "^[a-zA-Z0-9-]{1,127}$", true, "parent" }, "azurerm_key_vault_secret": {"azurerm_key_vault_secret", "kvs", 1, 127, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9-]{1,127}$", true, "parent" }, - "azurerm_kubernetes_cluster": {"azurerm_kubernetes_cluster", "aks", 1, 63, false, "[^0-9A-Za-z_.-]", "^[a-zA-Z0-9][a-zA-Z0-9-_.]{0,61}[a-zA-Z0-9]$", true, "resourceGroup" }, + "azurerm_kubernetes_cluster": {"azurerm_kubernetes_cluster", "aks", 1, 63, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]$", true, "resourceGroup" }, "azurerm_kusto_cluster": {"azurerm_kusto_cluster", "kc", 4, 22, false, "[^0-9A-Za-z]", "^[a-z][a-z0-9]{3,21}$", false, "global" }, "azurerm_kusto_database": {"azurerm_kusto_database", "kdb", 1, 260, false, "[^0-9A-Za-z- .]", "^[a-zA-Z0-9- .]{1,260}$", true, "parent" }, "azurerm_kusto_eventhub_data_connection": {"azurerm_kusto_eventhub_data_connection", "kehc", 1, 40, false, "[^0-9A-Za-z- .]", "^[a-zA-Z0-9- .]{1,40}$", true, "parent" }, @@ -228,6 +234,7 @@ var ResourceDefinitions = map[string]ResourceStructure{ "databricks_high_concurrency_cluster": {"databricks_high_concurrency_cluster", "dbhcc", 3, 30, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9-_]{3,30}$", true, "parent" }, "databricks_standard_cluster": {"databricks_standard_cluster", "dbsc", 3, 30, false, "[^a-zA-Z0-9-_]", "^[a-zA-Z0-9-_]{3,30}$", true, "parent" }, "general": {"general", "", 1, 250, false, "[^0-9A-Za-z_-]", "^[a-zA-Z0-9-_]{1,250}$", true, "global" }, + "general_safe": {"general_safe", "", 1, 250, true, "[^a-z]", "^[a-z]{1,250}$", false, "global" }, } // ResourceMaps are a map from the slug to the resource definition @@ -254,7 +261,9 @@ var ResourceMaps = map[string]string { "agw": "azurerm_application_gateway", "aks": "azurerm_kubernetes_cluster", "apim": "azurerm_api_management_service", + "app": "azurerm_app_service", "appcg": "azurerm_app_configuration", + "appi": "azurerm_application_insights", "as": "azurerm_analysis_services_server", "asa": "azurerm_stream_analytics_job", "asafunc": "azurerm_stream_analytics_function_javascript_udf", @@ -273,6 +282,7 @@ var ResourceMaps = map[string]string { "baapp": "azurerm_batch_application", "bacert": "azurerm_batch_certificate", "bapool": "azurerm_batch_pool", + "bast": "azurerm_bastion_host", "blob": "azurerm_storage_blob", "bot": "azurerm_bot_web_app", "botchan": "azurerm_bot_channels_registration", @@ -360,6 +370,8 @@ var ResourceMaps = map[string]string { "mysqlvn": "azurerm_mysql_virtual_network_rule", "nh": "azurerm_notification_hub", "nic": "azurerm_network_interface", + "npl": "aks_node_pool_linux", + "npw": "aks_node_pool_windows", "nsg": "azurerm_network_security_group", "nsgr": "azurerm_network_security_group_rule", "nw": "azurerm_network_watcher", @@ -370,6 +382,7 @@ var ResourceMaps = map[string]string { "pe": "azurerm_private_endpoint", "pip": "azurerm_public_ip", "pippf": "azurerm_public_ip_prefix", + "plan": "azurerm_app_service_environment", "pls": "azurerm_private_link_service", "ppg": "azurerm_proximity_placement_group", "prov": "azurerm_custom_provider", @@ -400,7 +413,7 @@ var ResourceMaps = map[string]string { "sgnlr": "azurerm_signalr_service", "si": "azurerm_shared_image", "sig": "azurerm_shared_image_gallery", - "snap": "azurerm_bastion_host", + "snap": "azurerm_snapshots", "snet": "azurerm_subnet", "spark": "azurerm_hdinsight_spark_cluster", "sql": "azurerm_mssql_server", diff --git a/azurecaf/resource_name.go b/azurecaf/resource_name.go index f09497a..f359374 100644 --- a/azurecaf/resource_name.go +++ b/azurecaf/resource_name.go @@ -102,6 +102,12 @@ func resourceName() *schema.Resource { Optional: true, ForceNew: true, }, + "use_slug": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + Default: true, + }, }, } } @@ -283,6 +289,7 @@ func getResourceName(resourceTypeName string, separator string, convention string, cleanInput bool, passthrough bool, + useSlug bool, namePrecedence []string) (string, error) { resource, err := getResource(resourceTypeName) @@ -294,7 +301,11 @@ func getResourceName(resourceTypeName string, separator string, return "", err } - slug := getSlug(resourceTypeName, convention) + slug := "" + if useSlug { + slug = getSlug(resourceTypeName, convention) + } + if cleanInput { prefixes = cleanSlice(prefixes, resource) suffixes = cleanSlice(suffixes, resource) @@ -331,6 +342,7 @@ func getNameResult(d *schema.ResourceData, meta interface{}) error { resourceTypes := convertInterfaceToString(d.Get("resource_types").([]interface{})) cleanInput := d.Get("clean_input").(bool) passthrough := d.Get("passthrough").(bool) + useSlug := d.Get("use_slug").(bool) randomLength := d.Get("random_length").(int) randomSeed := int64(d.Get("random_seed").(int)) @@ -345,7 +357,7 @@ func getNameResult(d *schema.ResourceData, meta interface{}) error { } if len(resourceType) > 0 { - resourceName, err := getResourceName(resourceType, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, namePrecedence) + resourceName, err := getResourceName(resourceType, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, useSlug, namePrecedence) if err != nil { return err } @@ -354,7 +366,7 @@ func getNameResult(d *schema.ResourceData, meta interface{}) error { resourceNames := make(map[string]string, len(resourceTypes)) for _, resourceTypeName := range resourceTypes { var err error - resourceNames[resourceTypeName], err = getResourceName(resourceTypeName, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, namePrecedence) + resourceNames[resourceTypeName], err = getResourceName(resourceTypeName, separator, prefixes, name, suffixes, randomSuffix, convention, cleanInput, passthrough, useSlug, namePrecedence) if err != nil { return err } diff --git a/azurecaf/resource_name_test.go b/azurecaf/resource_name_test.go index 200ba19..4433a67 100644 --- a/azurecaf/resource_name_test.go +++ b/azurecaf/resource_name_test.go @@ -212,7 +212,7 @@ func TestValidResourceType_invalidParameters(t *testing.T) { func TestGetResourceNameValid(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, namePrecedence) + resourceName, err := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, true, namePrecedence) expected := "a-b-rg-myrg-1234" if err != nil { @@ -224,9 +224,25 @@ func TestGetResourceNameValid(t *testing.T) { 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) + expected := "a-b-myrg-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 TestGetResourceNameInvalidResourceType(t *testing.T) { namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"} - resourceName, err := getResourceName("azurerm_invalid", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, namePrecedence) + resourceName, err := getResourceName("azurerm_invalid", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, false, true, namePrecedence) expected := "a-b-rg-myrg-1234" if err == nil { @@ -241,7 +257,7 @@ func TestGetResourceNameInvalidResourceType(t *testing.T) { func TestGetResourceNamePassthrough(t *testing.T) { namePrecedence := []string{"name", "slug", "random", "suffixes", "prefixes"} - resourceName, _ := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, true, namePrecedence) + resourceName, _ := getResourceName("azurerm_resource_group", "-", []string{"a", "b"}, "myrg", nil, "1234", "cafclassic", true, true, true, namePrecedence) expected := "myrg" if expected != resourceName { diff --git a/azurecaf/resource_naming_convention_cafclassic_test.go b/azurecaf/resource_naming_convention_cafclassic_test.go index 0b3081d..5ec1898 100644 --- a/azurecaf/resource_naming_convention_cafclassic_test.go +++ b/azurecaf/resource_naming_convention_cafclassic_test.go @@ -68,7 +68,7 @@ func TestAccCafNamingConvention_Classic(t *testing.T) { testAccCafNamingValidation( "azurecaf_naming_convention.classic_aks", "kubedemo", - 11, + 12, "aks"), regexMatch("azurecaf_naming_convention.classic_aks", regexp.MustCompile(Resources["aks"].ValidationRegExp), 1), testAccCafNamingValidation( diff --git a/docs/resources/azurecaf_name.md b/docs/resources/azurecaf_name.md index 8b645c0..9d96493 100644 --- a/docs/resources/azurecaf_name.md +++ b/docs/resources/azurecaf_name.md @@ -1,6 +1,6 @@ # azurecaf_name -The resource azurecaf_name implements a set of methodologies to apply consistent resource naming using the default Microsoft Cloud Adoption Framework for Azure recommendations as per https://docs.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging. +The resource azurecaf_name implements a set of methodologies to apply consistent resource naming using the default Microsoft Cloud Adoption Framework for Azure recommendations as per [naming-and-tagging](https://docs.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging). the azurecaf_name supersedes the previous azurecaf_naming_convention. This new resource provides more flexibility and will be updated on a regular basis as new Azure services are released. @@ -12,6 +12,7 @@ The azurecaf_name resource allows you to: * Allow passthrough mode (simply validate the output) ## Example usage + This example outputs one name, the result of the naming convention query. The result attribute returns the name based on the convention and parameters input. The example generates a 23 characters name compatible with the specification for an Azure Resource Group @@ -20,11 +21,11 @@ dev-aztfmod-001 ```hcl resource "azurecaf_name" "rg_example" { name = "demogroup" - resource_type = "azurerm_resource_group" - prefixes = ["a", "b"] - suffixes = ["y", "z"] - random_length = 5 - clean_input = true + resource_type = "azurerm_resource_group" + prefixes = ["a", "b"] + suffixes = ["y", "z"] + random_length = 5 + clean_input = true } resource "azurerm_resource_group" "demo" { @@ -37,7 +38,6 @@ The provider generates a name using the input parameters and automatically appen The example above would generate a name using the pattern [prefix]-[cafprefix]-[name]-[postfix]-[5_random_chars]: - ## Argument Reference The following arguments are supported: @@ -47,12 +47,13 @@ The following arguments are supported: * **suffixes** (optional) - a list of additional suffix added after the basename, this is can be used to append resource index (eg. vm-001). Suffixes are separated by the separator character * **random_length** (optional) - default to ``0`` : configure additional characters to append to the generated resource name. Random characters will remain compliant with the set of allowed characters per resources and will be appended after the suffixes * **resource_type** (optional) - describes the type of azure resource you are requesting a name from (eg. azure container registry: azurerm_container_registry). See the Resource Type section -* **resource_types** (optional) - a list of additional resource type should you want to use the same settings for a set of resources +* **resource_types** (optional) - a list of additional resource type should you want to use the same settings for a set of resources * **separator** (optional) - defaults to ``-``. The separator character to use between prefixes, resource type, name, suffixes, random character * **clean_input** (optional) - defaults to ``true``. The separator character to use between prefixes, resource type, name, suffixes, random character * **passthrough** (optional) - defaults to ``false``. Enables the passthrough mode - in that case only the clean input option is considered and the prefixes, suffixes, random, and are ignored. The resource prefixe is not added either to the resulting string -# Attributes Reference +## Attributes Reference + The following attributes are exported: * **id** - The id of the naming convention object @@ -61,13 +62,13 @@ The following attributes are exported: ## Resource types -We define resource types as per: https://docs.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging +We define resource types as per [naming-and-tagging](https://docs.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/naming-and-tagging) Current supported resource types: | Resource type | Resource type code (short) | minimum length | maximum length | lowercase only | validation regex | | ------------------------| ----------------------------|-----------------|-----------------|----------------|-------------------------------------------| -| azurerm_analysis_services_server| as| 3| 63| true| "^[a-z][a-z0-9]{2,62}$" | +| azurerm_analysis_services_server| as| 3| 63| true| "^[a-z][a-z0-9]{2,62}$"| | azurerm_api_management_service| apim| 1| 50| false| "^[a-z][a-zA-Z0-9-][a-zA-Z0-9]{0,48}$"| | azurerm_app_configuration| appcg| 5| 50| false| "^[a-zA-Z0-9_-]{5,50}$"| | azurerm_role_assignment| ra| 1| 64| false| "^[^%]{0,63}[^ %.]$"| @@ -107,7 +108,7 @@ Current supported resource types: | azurerm_containerGroups| cg| 1| 63| false| "^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$"| | azurerm_container_registry| cr| 1| 63| true| "^[a-zA-Z0-9]{1,63}$"| | azurerm_container_registry_webhook| crwh| 1| 50| false| "^[a-zA-Z0-9]{1,50}$"| -| azurerm_kubernetes_cluster| aks| 1| 63| false| "^[a-zA-Z0-9][a-zA-Z0-9-_.]{0,61}[a-zA-Z0-9]$"| +| azurerm_kubernetes_cluster| aks| 1| 63| false| "^[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]$"| | azurerm_cosmosdb_account| cosmos| 1| 63| false| "^[a-z0-9][a-zA-Z0-9-_.]{0,61}[a-zA-Z0-9]$"| | azurerm_custom_provider| prov| 3| 64| false| "^[^&%?\\/]{2,63}[^&%.?\\/ ]$"| | azurerm_mariadb_server| maria| 3| 63| false| "^[a-z0-9][a-zA-Z0-9-]{1,61}[a-z0-9]$"| @@ -136,6 +137,8 @@ Current supported resource types: | azurerm_data_factory_pipeline| adfpl| 1| 260| false| "^[a-zA-Z0-9][^<>*%:.?\\+\\/]{0,258}[a-zA-Z0-9]$"| | azurerm_data_factory_linked_service_data_lake_storage_gen2| adfsvst| 1| 260| false| "^[a-zA-Z0-9][^<>*%:.?\\+\\/]{0,259}$"| | azurerm_data_factory_linked_service_key_vault| adfsvkv| 1| 260| false| "^[a-zA-Z0-9][^<>*%:.?\\+\\/]{0,259}$"| +| azurerm_recovery_services_vault| rsv| 2| 50| false| "^[a-zA-Z][a-zA-Z0-9\\-]{1,49}$"| +| azurerm_recovery_services_vault_backup_police| rsvbp| 3| 150| false| "^[a-zA-Z][a-zA-Z0-9\\-]{1,148}[a-zA-Z0-9]$"| | azurerm_data_factory_linked_service_mysql| adfsvmysql| 1| 260| false| "^[a-zA-Z0-9][^<>*%:.?\\+\\/]{0,259}$"| | azurerm_data_factory_linked_service_postgresql| adfsvpsql| 1| 260| false| "^[a-zA-Z0-9][^<>*%:.?\\+\\/]{0,259}$"| | azurerm_data_factory_linked_service_sql_server| adfsvmssql| 1| 260| false| "^[a-zA-Z0-9][^<>*%:.?\\+\\/]{0,259}$"| @@ -209,7 +212,7 @@ Current supported resource types: | azurerm_storage_share_directory| sts| 3| 63| false| "^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$"| | azurerm_machine_learning_workspace| mlw| 1| 260| false| "^[^<>*%:.?\\+\\/]{0,259}[^<>*%:.?\\+\\/ ]$"| | azurerm_storage_blob| blob| 1| 1024| false| "^[^\\s\\/$#&]{1,1000}[^\\s\\/$#&]{0,24}$"| -| azurerm_bastion_host| snap| 1| 80| false| "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$"| +| azurerm_bastion_host| bast| 1| 80| false| "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$"| | azurerm_local_network_gateway| lgw| 1| 80| false| "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$"| | azurerm_application_gateway| agw| 1| 80| false| "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$"| | azurerm_express_route_gateway| ergw| 1| 80| false| "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$"| @@ -254,6 +257,12 @@ Current supported resource types: | azurerm_eventgrid_topic| egt| 3| 50| false| "^[a-zA-Z0-9-]{3,50}$"| | azurerm_relay_namespace| rln| 6| 50| false| "^[a-zA-Z][a-zA-Z0-9-]{4,48}[a-zA-Z0-9]$"| | azurerm_relay_hybrid_connection| rlhc| 1| 260| false| "^[a-zA-Z0-9][a-zA-Z0-9-._]{0,258}[a-zA-Z0-9]$"| +| azurerm_app_service| app| 2| 60| false| "^[0-9A-Za-z][0-9A-Za-z-]{0,58}[0-9a-zA-Z]$"| +| azurerm_app_service_plan| plan| 1| 40| false| "^[0-9A-Za-z-]{1,40}$"| +| azurerm_app_service_environment| plan| 2| 37| false| "^[0-9A-Za-z-]{2,37}$"| +| azurerm_application_insights| appi| 1| 260| false| "^[^%&\\?/. ][^%&\\?/]{0,258}[^%&\\?/. ]$"| +| aks_node_pool_linux| npl| 1| 12| false| "^[a-z][0-9a-z]{0,11}$"| +| aks_node_pool_windows| npw| 1| 6| false| "^[a-z][0-9a-z]{0,5}$"| cat resourceDefinition_out_of_docs.json | jq -r '.[] | "| \(.name)| \(.slug)| \(.min_length)| \(.max_length)| \(.lowercase)| \(.validation_regex)|"' | azurerm_private_endpoint| pe| 1| 80| false| "^[a-zA-Z0-9][a-zA-Z0-9\\-\\._]{0,78}[a-zA-Z0-9_]$"| | azurerm_private_service_connection| psc| 1| 80| false| "^[a-zA-Z0-9][a-zA-Z0-9\\-\\._]{0,78}[a-zA-Z0-9_]$"| @@ -285,3 +294,5 @@ cat resourceDefinition_out_of_docs.json | jq -r '.[] | "| \(.name)| \(.slug)| \( | databricks_cluster| dbc| 3| 30| false| "^[a-zA-Z0-9-_]{3,30}$"| | databricks_standard_cluster| dbsc| 3| 30| false| "^[a-zA-Z0-9-_]{3,30}$"| | databricks_high_concurrency_cluster| dbhcc| 3| 30| false| "^[a-zA-Z0-9-_]{3,30}$"| +| general| | 1| 250| false| "^[a-zA-Z0-9-_]{1,250}$"| +| general_safe| | 1| 250| true| "^[a-z]{1,250}$"| diff --git a/resourceDefinition.json b/resourceDefinition.json index 46820d9..a6d404b 100644 --- a/resourceDefinition.json +++ b/resourceDefinition.json @@ -445,12 +445,12 @@ "name": "azurerm_kubernetes_cluster", "min_length": 1, "max_length": 63, - "validation_regex": "\"^[a-zA-Z0-9][a-zA-Z0-9-_.]{0,61}[a-zA-Z0-9]$\"", + "validation_regex": "\"^[a-zA-Z0-9][a-zA-Z0-9-_]{0,61}[a-zA-Z0-9]$\"", "scope": "resourceGroup", "slug": "aks", "dashes": true, "lowercase": false, - "regex": "\"[^0-9A-Za-z_.-]\"" + "regex": "\"[^0-9A-Za-z_-]\"" }, { "name": "azurerm_cosmosdb_account", @@ -1592,13 +1592,13 @@ "lowercase": false, "regex": "\"[\\\\s\\\\/$#&]\"" }, - { + { "name": "azurerm_bastion_host", "min_length": 1, "max_length": 80, "validation_regex": "\"^[a-zA-Z0-9][a-zA-Z0-9-._]{0,78}[a-zA-Z0-9_]$\"", "scope": "parent", - "slug": "snap", + "slug": "bast", "dashes": true, "lowercase": false, "regex": "\"[^0-9A-Za-z_.-]\"" @@ -2087,5 +2087,71 @@ "dashes": true, "lowercase": false, "regex": "\"[^0-9A-Za-z_.-]\"" + }, + { + "name": "azurerm_app_service", + "min_length": 2, + "max_length": 60, + "validation_regex": "\"^[0-9A-Za-z][0-9A-Za-z-]{0,58}[0-9a-zA-Z]$\"", + "scope": "global", + "slug": "app", + "dashes": true, + "lowercase": false, + "regex": "\"[^0-9A-Za-z-]\"" + }, + { + "name": "azurerm_app_service_plan", + "min_length": 1, + "max_length": 40, + "validation_regex": "\"^[0-9A-Za-z-]{1,40}$\"", + "scope": "resourceGroup", + "slug": "plan", + "dashes": true, + "lowercase": false, + "regex": "\"[^0-9A-Za-z-]\"" + }, + { + "name": "azurerm_app_service_environment", + "min_length": 2, + "max_length": 37, + "validation_regex": "\"^[0-9A-Za-z-]{2,37}$\"", + "scope": "resourceGroup", + "slug": "plan", + "dashes": true, + "lowercase": false, + "regex": "\"[^0-9A-Za-z-]\"" + }, + { + "name": "azurerm_application_insights", + "min_length": 1, + "max_length": 260, + "validation_regex": "\"^[^%&\\\\?/. ][^%&\\\\?/]{0,258}[^%&\\\\?/. ]$\"", + "scope": "resourceGroup", + "slug": "appi", + "dashes": true, + "lowercase": false, + "regex": "\"[^0-9A-Za-z_.-]\"" + }, + { + "name": "aks_node_pool_linux", + "min_length": 1, + "max_length": 12, + "validation_regex": "\"^[a-z][0-9a-z]{0,11}$\"", + "scope": "parent", + "slug": "npl", + "dashes": false, + "lowercase": false, + "regex": "\"[^0-9a-z]\"" + }, + { + "name": "aks_node_pool_windows", + "min_length": 1, + "max_length": 6, + "validation_regex": "\"^[a-z][0-9a-z]{0,5}$\"", + "scope": "parent", + "slug": "npw", + "dashes": false, + "lowercase": false, + "regex": "\"[^0-9a-z]\"" } ] \ No newline at end of file From 34d679f00457a813c765bfe9a0e3cbfafc0b8ba3 Mon Sep 17 00:00:00 2001 From: Gabriel Nepomuceno Date: Wed, 16 Sep 2020 01:46:25 +0100 Subject: [PATCH 4/4] update docs --- docs/resources/azurecaf_name.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/resources/azurecaf_name.md b/docs/resources/azurecaf_name.md index 9d96493..6e26d36 100644 --- a/docs/resources/azurecaf_name.md +++ b/docs/resources/azurecaf_name.md @@ -51,6 +51,7 @@ The following arguments are supported: * **separator** (optional) - defaults to ``-``. The separator character to use between prefixes, resource type, name, suffixes, random character * **clean_input** (optional) - defaults to ``true``. The separator character to use between prefixes, resource type, name, suffixes, random character * **passthrough** (optional) - defaults to ``false``. Enables the passthrough mode - in that case only the clean input option is considered and the prefixes, suffixes, random, and are ignored. The resource prefixe is not added either to the resulting string +* **use_slug** (optional) - defaults to ``true``. If a slug should be added to the name - If you put false no slug (the few letters that identify the resource type) will be added to the name ## Attributes Reference