Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(terraform): fix child modules parse tfplan file #7266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading