diff --git a/genesyscloud/tfexporter/genesyscloud_resource_exporter.go b/genesyscloud/tfexporter/genesyscloud_resource_exporter.go index 33cb2c186..2c96e2796 100644 --- a/genesyscloud/tfexporter/genesyscloud_resource_exporter.go +++ b/genesyscloud/tfexporter/genesyscloud_resource_exporter.go @@ -442,40 +442,45 @@ func (g *GenesysCloudResourceExporter) updateInstanceStateAttributes(jsonResult resource resourceExporter.ResourceInfo) { for attr, val := range jsonResult { - switch val.(type) { + switch v := val.(type) { case []interface{}: - resources, _ := jsonResult["resources"].([]interface{}) - for _, res := range resources { - r, ok := res.(map[string]interface{}) - if !ok { - continue - } - resourceID := "" - for _, valt := range r { - pattern := regexp.MustCompile(`^resources\.(\d+)\.l.*$`) - for key, value := range resource.State.Attributes { - if matches := pattern.FindStringSubmatch(key); matches != nil && value == valt { - resourceID = matches[1] - } - } - } - for attrTemp, valTemp := range r { - if resourceID != "" { - key := fmt.Sprintf("resources." + resourceID + "." + attrTemp) - if valTemp != nil { - resource.State.Attributes[key] = valTemp.(string) + if resources, ok := jsonResult["resources"].([]interface{}); ok { + for _, res := range resources { + if resMap, ok := res.(map[string]interface{}); ok { + resourceID := findResourceID(resource, resMap) + + if resourceID != "" { + for attrTemp, valTemp := range resMap { + if valTempStr, ok := valTemp.(string); ok { + key := fmt.Sprintf("resources.%s.%s", resourceID, attrTemp) + resource.State.Attributes[key] = valTempStr + } + } } } } } case string: - if _, ok := resource.State.Attributes[attr]; !ok { - resource.State.Attributes[attr] = jsonResult[attr].(string) + // Directly add string attribute for rest of the flows/scripts/assets + if _, exists := resource.State.Attributes[attr]; !exists { + resource.State.Attributes[attr] = v } } } } +func findResourceID(resource resourceExporter.ResourceInfo, resMap map[string]interface{}) string { + pattern := regexp.MustCompile(`^resources\.(\d+)\.l.*$`) + for _, valt := range resMap { + for key, value := range resource.State.Attributes { + if matches := pattern.FindStringSubmatch(key); matches != nil && value == valt { + return matches[1] + } + } + } + return "" +} + func (g *GenesysCloudResourceExporter) updateSanitiseMap(exporters map[string]*resourceExporter.ResourceExporter, //Map of all of the exporters resource resourceExporter.ResourceInfo) { if exporters[resource.Type] != nil { diff --git a/genesyscloud/tfexporter/genesyscloud_resource_exporter_test.go b/genesyscloud/tfexporter/genesyscloud_resource_exporter_test.go index 2c393142f..b94668045 100644 --- a/genesyscloud/tfexporter/genesyscloud_resource_exporter_test.go +++ b/genesyscloud/tfexporter/genesyscloud_resource_exporter_test.go @@ -26,6 +26,60 @@ type PostProcessHclBytesTestCase struct { decodedMap map[string]string } +// Test case for updateInstanceStateAttributes +func TestUnitUpdateInstanceStateAttributes(t *testing.T) { + jsonResult := util.JsonMap{ + "resources": []interface{}{ + map[string]interface{}{ + "file_content_hash": "${filesha256(\"file.json\")}", + "file_name": "222", + "language": "en", + }, + map[string]interface{}{ + "file_content_hash": "${filesha256(\"file_fr.json\")}", + "file_name": "444", + "lang": "fr", + }, + }, + } + + // Mock initial resource attributes to simulate current state + initialAttributes := map[string]string{ + "resources.1234567.file_content_hash": "", + "resources.1234567.file_name": "", + "resources.1234567.language": "en", + "resources.3223344.file_content_hash": "", + "resources.3223344.file_name": "", + "resources.3223344.lang": "fr", + } + + // Create an instance of ResourceInfo + resources := []resourceExporter.ResourceInfo{ + { + Name: "testResourceName", + Type: "testResourceType", + State: &terraform.InstanceState{ + ID: "testResourceId", + Attributes: initialAttributes, + }, + }, + } + + exporter := GenesysCloudResourceExporter{} + exporter.updateInstanceStateAttributes(jsonResult, resources[0]) + + expectedAttributes := map[string]string{ + "resources.1234567.file_content_hash": "${filesha256(\"file.json\")}", + "resources.1234567.file_name": "222", + "resources.1234567.language": "en", + "resources.3223344.file_content_hash": "${filesha256(\"file_fr.json\")}", + "resources.3223344.file_name": "444", + "resources.3223344.lang": "fr", + } + + assert.Equal(t, expectedAttributes, resources[0].State.Attributes, "Attributes should be correctly updated") +} + func TestUnitTfExportPostProcessHclBytesFunc(t *testing.T) { testCase1 := PostProcessHclBytesTestCase{ original: `