From ec14a04a6c42a4887895fea94f15e06234ee0ced Mon Sep 17 00:00:00 2001 From: LewisKSaint Date: Wed, 4 Oct 2023 18:06:32 -0400 Subject: [PATCH] SOL-104922 | Bug fix to keep indetifier name consistent with HCL --- cmd/command/configgenerator.go | 7 +--- cmd/command/util.go | 9 +++++ cmd/command/util_test.go | 64 ++++++++++++++++++++++++++++++++++ cmd/generate.go | 25 ++++++------- 4 files changed, 87 insertions(+), 18 deletions(-) diff --git a/cmd/command/configgenerator.go b/cmd/command/configgenerator.go index daad7b8a..95386598 100644 --- a/cmd/command/configgenerator.go +++ b/cmd/command/configgenerator.go @@ -210,16 +210,11 @@ func GetNameForResource(resourceTerraformName string, attributeResourceTerraform } if found { //sanitize name - value = strings.ReplaceAll(value, " ", "_") - value = strings.ReplaceAll(value, "#", "_") - value = strings.ReplaceAll(value, "\\", "_") - value = strings.ReplaceAll(value, "/", "_") - value = strings.ReplaceAll(value, "\"", "") resourceName = "_" + value } } } } } - return resourceName + return SanitizeHclIdentifierName(resourceName) } diff --git a/cmd/command/util.go b/cmd/command/util.go index d50aff70..69c42143 100644 --- a/cmd/command/util.go +++ b/cmd/command/util.go @@ -399,3 +399,12 @@ func hclFormatResource(resourceConfig ResourceConfig) string { config := b.String() return config } + +func SanitizeHclIdentifierName(name string) string { + name = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(strings.TrimSpace(name), "_") + if len(name) == 0 || (name[0] >= '0' && name[0] <= '9') || (len(name) == 1 && name[0] == '_') { + //just prepend static string to avoid checking all characters + name = "gn_" + name + } + return name +} diff --git a/cmd/command/util_test.go b/cmd/command/util_test.go index c69be077..941dd633 100644 --- a/cmd/command/util_test.go +++ b/cmd/command/util_test.go @@ -206,3 +206,67 @@ func Test_newAttributeInfo(t *testing.T) { }) } } + +func TestSanitizeHclIdentifierName(t *testing.T) { + type args struct { + name string + } + tests := []struct { + name string + args args + want string + }{ + { + "SanitizeTextStartingWithNumber", + args{name: "1testing"}, + "gn_1testing", + }, + { + "SanitizeTextContainingSpecialCharacters", + args{name: "*testing*"}, + "_testing_", + }, + { + "SanitizeTextContainingSpecialCharactersTwo", + args{name: "#testing/"}, + "_testing_", + }, + { + "SanitizeTextContainingSpecialCharactersThree", + args{name: "$testing\""}, + "_testing_", + }, + { + "SanitizeTextContainingSpecialCharactersFour", + args{name: "%testing^"}, + "_testing_", + }, + { + "SanitizeTextContainingSpecialCharactersFive", + args{name: "%testing^"}, + "_testing_", + }, + { + "SanitizeTextEmpty", + args{name: ""}, + "gn_", + }, + { + "SanitizeTextContainingEmpty", + args{name: " "}, + "gn_", + }, + { + "SanitizeTextOnlySpecialCharacter", + args{name: "#"}, + "gn__", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := SanitizeHclIdentifierName(tt.args.name); got != tt.want { + t.Errorf("SanitizeHclIdentifierName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/generate.go b/cmd/generate.go index 15f2b1fb..14dc824d 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -69,7 +69,7 @@ This command would create a file my-messagevpn.tf that contains a resource defin } providerSpecificIdentifier := cmd.Flags().Arg(1) if len(providerSpecificIdentifier) == 0 { - command.LogCLIError("Broker object not provided") + command.LogCLIError("Broker object not provided") _ = cmd.Help() os.Exit(1) } @@ -108,7 +108,8 @@ This command would create a file my-messagevpn.tf that contains a resource defin brokerObjectInstanceName := strings.ToLower(brokerObjectType) if strings.Contains(brokerObjectType, ".") { brokerObjectTypeName = strings.Split(brokerObjectType, ".")[0] - brokerObjectInstanceName = strings.Split(brokerObjectType, ".")[1] + //sanitize name + brokerObjectInstanceName = command.SanitizeHclIdentifierName(strings.Split(brokerObjectType, ".")[1]) } brokerObjectTerraformName := strings.ReplaceAll(brokerObjectTypeName, "solacebroker_", "") @@ -133,10 +134,10 @@ This command would create a file my-messagevpn.tf that contains a resource defin fmt.Println("\nReplacing hardcoded names of inter-object dependencies by references where required ...") fixInterObjectDependencies(brokerResources) - + // Format the results object.BrokerResources = command.ToFormattedHCL(brokerResources) - + registry, ok := os.LookupEnv("SOLACEBROKER_REGISTRY_OVERRIDE") if !ok { registry = "registry.terraform.io" @@ -231,13 +232,13 @@ func fixInterObjectDependencies(brokerResources []map[string]command.ResourceCon // this will modify the passed brokerResources object //temporal hard coding dependency graph fix not available in SEMP API - InterObjectDependencies := map[string][]string{"solacebroker_msg_vpn_authorization_group": {"solacebroker_msg_vpn_client_profile","solacebroker_msg_vpn_acl_profile"}, - "solacebroker_msg_vpn_client_username": {"solacebroker_msg_vpn_client_profile","solacebroker_msg_vpn_acl_profile"}, - "solacebroker_msg_vpn_rest_delivery_point": {"solacebroker_msg_vpn_client_profile"}, - "solacebroker_msg_vpn_acl_profile_client_connect_exception": {"solacebroker_msg_vpn_acl_profile"}, - "solacebroker_msg_vpn_acl_profile_publish_topic_exception": {"solacebroker_msg_vpn_acl_profile"}, + InterObjectDependencies := map[string][]string{"solacebroker_msg_vpn_authorization_group": {"solacebroker_msg_vpn_client_profile", "solacebroker_msg_vpn_acl_profile"}, + "solacebroker_msg_vpn_client_username": {"solacebroker_msg_vpn_client_profile", "solacebroker_msg_vpn_acl_profile"}, + "solacebroker_msg_vpn_rest_delivery_point": {"solacebroker_msg_vpn_client_profile"}, + "solacebroker_msg_vpn_acl_profile_client_connect_exception": {"solacebroker_msg_vpn_acl_profile"}, + "solacebroker_msg_vpn_acl_profile_publish_topic_exception": {"solacebroker_msg_vpn_acl_profile"}, "solacebroker_msg_vpn_acl_profile_subscribe_share_name_exception": {"solacebroker_msg_vpn_acl_profile"}, - "solacebroker_msg_vpn_acl_profile_subscribe_topic_exception": {"solacebroker_msg_vpn_acl_profile"}} + "solacebroker_msg_vpn_acl_profile_subscribe_topic_exception": {"solacebroker_msg_vpn_acl_profile"}} ObjectNameAttributes := map[string]string{"solacebroker_msg_vpn_client_profile": "client_profile_name", "solacebroker_msg_vpn_acl_profile": "acl_profile_name"} @@ -248,7 +249,7 @@ func fixInterObjectDependencies(brokerResources []map[string]command.ResourceCon var resourceType string // var resourceConfig command.ResourceConfig for resourceKey := range resources { - resourceType = strings.Split(resourceKey," ")[0] + resourceType = strings.Split(resourceKey, " ")[0] resourceDependencies, exists := InterObjectDependencies[resourceType] if !exists { continue @@ -265,7 +266,7 @@ func fixInterObjectDependencies(brokerResources []map[string]command.ResourceCon found := false for _, r := range brokerResources { for k := range r { - rName := strings.Split(k," ")[0] + rName := strings.Split(k, " ")[0] if rName != dependency { continue }