From ac2a870ea018f4721dd67ed7cb4e624ee8ab78b3 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 3 Sep 2021 13:53:52 -0400 Subject: [PATCH] allow json output to marshal ConfigModeAttr blocks In order to marshal config blocks using ConfigModeAttr, we need to insert the fixup body to map hcl blocks to the attribute in the schema. --- internal/command/jsonconfig/expression.go | 4 +++ .../command/jsonconfig/expression_test.go | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/internal/command/jsonconfig/expression.go b/internal/command/jsonconfig/expression.go index 0244d73d0b..fa443fc3ea 100644 --- a/internal/command/jsonconfig/expression.go +++ b/internal/command/jsonconfig/expression.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/configs/configschema" "github.com/hashicorp/terraform/internal/lang" + "github.com/hashicorp/terraform/internal/lang/blocktoattr" "github.com/zclconf/go-cty/cty" ctyjson "github.com/zclconf/go-cty/cty/json" ) @@ -96,6 +97,9 @@ func marshalExpressions(body hcl.Body, schema *configschema.Block) expressions { // (lowSchema is an hcl.BodySchema: // https://godoc.org/github.com/hashicorp/hcl/v2/hcl#BodySchema ) + // fix any ConfigModeAttr blocks present from legacy providers + body = blocktoattr.FixUpBlockAttrs(body, schema) + // Use the low-level schema with the body to decode one level We'll just // ignore any additional content that's not covered by the schema, which // will effectively ignore "dynamic" blocks, and may also ignore other diff --git a/internal/command/jsonconfig/expression_test.go b/internal/command/jsonconfig/expression_test.go index 971fb78d4d..58af11dda5 100644 --- a/internal/command/jsonconfig/expression_test.go +++ b/internal/command/jsonconfig/expression_test.go @@ -80,6 +80,29 @@ func TestMarshalExpressions(t *testing.T) { }, }, }, + { + hcltest.MockBody(&hcl.BodyContent{ + Blocks: hcl.Blocks{ + { + Type: "block_to_attr", + Body: hcltest.MockBody(&hcl.BodyContent{ + + Attributes: hcl.Attributes{ + "foo": { + Name: "foo", + Expr: hcltest.MockExprTraversalSrc(`module.foo.bar`), + }, + }, + }), + }, + }, + }), + expressions{ + "block_to_attr": expression{ + References: []string{"module.foo.bar", "module.foo"}, + }, + }, + }, } for _, test := range tests { @@ -89,6 +112,11 @@ func TestMarshalExpressions(t *testing.T) { Type: cty.String, Optional: true, }, + "block_to_attr": { + Type: cty.List(cty.Object(map[string]cty.Type{ + "foo": cty.String, + })), + }, }, }