Skip to content

Commit

Permalink
test case addition
Browse files Browse the repository at this point in the history
  • Loading branch information
HemanthDogiparthi12 committed Oct 16, 2024
1 parent e677aad commit 4012d8e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
51 changes: 28 additions & 23 deletions genesyscloud/tfexporter/genesyscloud_resource_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions genesyscloud/tfexporter/genesyscloud_resource_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand Down

0 comments on commit 4012d8e

Please sign in to comment.