Skip to content

Commit

Permalink
SOL-104922 | Bug fix to keep indetifier name consistent with HCL
Browse files Browse the repository at this point in the history
  • Loading branch information
LewisKSaint committed Oct 4, 2023
1 parent 4477fc0 commit ec14a04
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
7 changes: 1 addition & 6 deletions cmd/command/configgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions cmd/command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
64 changes: 64 additions & 0 deletions cmd/command/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
25 changes: 13 additions & 12 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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_", "")
Expand All @@ -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"
Expand Down Expand Up @@ -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"}

Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit ec14a04

Please sign in to comment.