diff --git a/pkg/parser/json/tfplan.go b/pkg/parser/json/tfplan.go index 23f2a8aa5fa..f0187a5a04b 100644 --- a/pkg/parser/json/tfplan.go +++ b/pkg/parser/json/tfplan.go @@ -64,12 +64,13 @@ func readPlan(plan *hcl_plan.Plan) model.Document { func (kp *KicsPlan) readModule(module *hcl_plan.StateModule) { // initialize all the types interfaces for _, resource := range module.Resources { - convNamedRes := make(map[string]KicsPlanNamedResource) - kp.Resource[resource.Type] = convNamedRes + if _, ok := kp.Resource[resource.Type]; !ok { + kp.Resource[resource.Type] = make(map[string]KicsPlanNamedResource) + } } // fill in all the types interfaces for _, resource := range module.Resources { - kp.Resource[resource.Type][resource.Name] = resource.AttributeValues + kp.Resource[resource.Type][resource.Address] = resource.AttributeValues } for _, childModule := range module.ChildModules { diff --git a/pkg/parser/json/tfplan_test.go b/pkg/parser/json/tfplan_test.go index 46bff92cc41..deb5dd4bd84 100644 --- a/pkg/parser/json/tfplan_test.go +++ b/pkg/parser/json/tfplan_test.go @@ -11,6 +11,15 @@ func TestJson_parseTFPlan(t *testing.T) { type args struct { doc model.Document } + dummyValues := map[string]interface{}{ + "name": "Production DB", + "size": 256, + } + + dummyExpectedValues := map[string]interface{}{ + "name": "Production DB", + "size": float64(256), + } tests := []struct { name string @@ -29,16 +38,13 @@ func TestJson_parseTFPlan(t *testing.T) { "root_module": map[string]interface{}{ "resources": []map[string]interface{}{ { - "address": "fakewebservices_database.prod_db", - "mode": "managed", - "type": "fakewebservices_database", - "name": "prod_db", - "provider_name": "registry.terraform.io/hashicorp/fakewebservices", - "schema_version": 0, - "values": map[string]interface{}{ - "name": "Production DB", - "size": 256, - }, + "address": "fakewebservices_database.prod_db", + "mode": "managed", + "type": "fakewebservices_database", + "name": "prod_db", + "provider_name": "registry.terraform.io/hashicorp/fakewebservices", + "schema_version": 0, + "values": dummyValues, "sensitive_values": map[string]interface{}{}, }, }, @@ -51,10 +57,7 @@ func TestJson_parseTFPlan(t *testing.T) { want: model.Document{ "resource": map[string]interface{}{ "fakewebservices_database": map[string]interface{}{ - "prod_db": map[string]interface{}{ - "name": "Production DB", - "size": (float64)(256), - }, + "fakewebservices_database.prod_db": dummyExpectedValues, }, }, }, @@ -72,6 +75,67 @@ func TestJson_parseTFPlan(t *testing.T) { want: model.Document{}, wantErr: true, }, + { + name: "test - child module is parsed", + args: args{ + doc: model.Document{ + "format_version": "0.2", + "terraform_version": "1.0.5", + "variables": map[string]interface{}{}, + "planned_values": map[string]interface{}{ + "root_module": map[string]interface{}{ + "resources": []map[string]interface{}{ + { + "address": "fakewebservices_database.prod_db", + "mode": "managed", + "type": "fakewebservices_database", + "name": "prod_db", + "provider_name": "registry.terraform.io/hashicorp/fakewebservices", + "values": dummyValues, + }, + }, + "child_modules": []map[string]interface{}{ + { + "resources": []map[string]interface{}{ + { + "address": "module.ilovetests.fakewebservices_database.prod_db", + "mode": "managed", + "type": "fakewebservices_database", + "name": "prod_db", + "provider_name": "registry.terraform.io/hashicorp/fakewebservices", + "values": dummyValues, + }, + }, + "child_modules": []map[string]interface{}{ + { + "resources": []map[string]interface{}{ + { + "address": "module.ilovetests.module.ilovego.fakewebservices_database.prod_db", + "mode": "managed", + "type": "fakewebservices_database", + "name": "prod_db", + "provider_name": "registry.terraform.io/hashicorp/fakewebservices", + "values": dummyValues, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + want: model.Document{ + "resource": map[string]interface{}{ + "fakewebservices_database": map[string]interface{}{ + "fakewebservices_database.prod_db": dummyExpectedValues, + "module.ilovetests.fakewebservices_database.prod_db": dummyExpectedValues, + "module.ilovetests.module.ilovego.fakewebservices_database.prod_db": dummyExpectedValues, + }, + }, + }, + }, } for _, tt := range tests {