From 076261af86f139a01a03e0578e672edb2e667283 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Mon, 18 Sep 2023 23:04:27 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20unmarshal=20for=20terrafor?= =?UTF-8?q?m=20plan=20`replace=5Fpaths`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migrate https://github.com/mondoohq/cnquery/pull/1511 Fixes https://github.com/mondoohq/cnquery/issues/1512 Note: These Terraform tests need a proper migration of their test as described in https://github.com/mondoohq/cnquery/issues/1787 Signed-off-by: Dominik Richter --- .../testdata/tfplan-replace-paths/tfplan.json | 36 +++++++++++++++++++ providers/terraform/provider/tfplan_test.go | 18 ++++++++++ providers/terraform/resources/tfplan.go | 4 +-- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 providers/terraform/provider/testdata/tfplan-replace-paths/tfplan.json diff --git a/providers/terraform/provider/testdata/tfplan-replace-paths/tfplan.json b/providers/terraform/provider/testdata/tfplan-replace-paths/tfplan.json new file mode 100644 index 0000000000..6ec6e9d0df --- /dev/null +++ b/providers/terraform/provider/testdata/tfplan-replace-paths/tfplan.json @@ -0,0 +1,36 @@ +{ + "format_version": "1.5", + "terraform_version": "1.5.0", + "resource_changes": [ + { + "address": "", + "module_address": "", + "mode": "managed", + "type": "google_service_account_iam_member", + "name": "cloud_deploy_service_account", + "provider_name": "registry.terraform.io/hashicorp/google", + "change": { + "actions": ["delete", "create"], + "before": { + "condition": [], + "etag": "BwYCXPmcVSE=", + "id": "", + "member": "", + "role": "", + "service_account_id": "" + }, + "after": { + "condition": [], + "member": "", + "role": "", + "service_account_id": "" + }, + "after_unknown": { "condition": [], "etag": true, "id": true }, + "before_sensitive": { "condition": [] }, + "after_sensitive": { "condition": [] }, + "replace_paths": [["member"]] + }, + "action_reason": "replace_because_cannot_update" + } + ] +} diff --git a/providers/terraform/provider/tfplan_test.go b/providers/terraform/provider/tfplan_test.go index 420553f04b..c14723aba7 100644 --- a/providers/terraform/provider/tfplan_test.go +++ b/providers/terraform/provider/tfplan_test.go @@ -96,3 +96,21 @@ func TestTerraformPlanParsing(t *testing.T) { assert.Equal(t, 1, len(pc.RootModule.Resources)) } + +// // FIXME: This test needs migration +// func TestTerraformPlanParsingReplacePaths(t *testing.T) { +// path := "./testdata/tfplan-replace-paths/tfplan.json" +// query := "terraform.plan.resourceChanges" +// res := testTerraformPlanQueryWithPath(t, query, path) +// require.NotEmpty(t, res) +// assert.Empty(t, res[0].Result().Error) + +// query = "terraform.plan.resourceChanges[0].change.replacePaths" +// res = testTerraformPlanQueryWithPath(t, query, path) +// require.NotEmpty(t, res) +// resArrayInterface, ok := res[0].Data.Value.([]interface{}) +// require.True(t, ok) +// resArrayStrings, ok := resArrayInterface[0].([]interface{}) +// require.True(t, ok) +// assert.Equal(t, "member", resArrayStrings[0].(string)) +// } diff --git a/providers/terraform/resources/tfplan.go b/providers/terraform/resources/tfplan.go index 35730afafa..93ce8ebe86 100644 --- a/providers/terraform/resources/tfplan.go +++ b/providers/terraform/resources/tfplan.go @@ -84,7 +84,7 @@ func (t *mqlTerraformPlan) resourceChanges() ([]interface{}, error) { } } - var replacePaths map[string]interface{} + var replacePaths []interface{} if rc.Change.ReplacePaths != nil { if err := json.Unmarshal(rc.Change.ReplacePaths, &replacePaths); err != nil { return nil, err @@ -99,7 +99,7 @@ func (t *mqlTerraformPlan) resourceChanges() ([]interface{}, error) { "afterUnknown": llx.MapData(afterUnknown, types.Any), "beforeSensitive": llx.MapData(beforeSensitive, types.Any), "afterSensitive": llx.MapData(afterSensitive, types.Any), - "replacePaths": llx.MapData(replacePaths, types.Any), + "replacePaths": llx.ArrayData(replacePaths, types.Any), }) if err != nil { return nil, err