Skip to content

Commit

Permalink
Fix child modules parse tfplan file
Browse files Browse the repository at this point in the history
  • Loading branch information
nirfirefly committed Oct 3, 2024
1 parent c17d26e commit d828c65
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 17 deletions.
7 changes: 4 additions & 3 deletions pkg/parser/json/tfplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
92 changes: 78 additions & 14 deletions pkg/parser/json/tfplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}{},
},
},
Expand All @@ -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,
},
},
},
Expand All @@ -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 {
Expand Down

0 comments on commit d828c65

Please sign in to comment.