From 0133dda3112f71b78ba6b228b97459c300c12988 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 31 May 2019 16:20:52 -0500 Subject: [PATCH] allow empty dynamic blocks with ConfigModeAttr If a dynamic block is evaluated zero times, the body content will contain 0 blocks. Allow the probe for ConfigModeAttr to accept that no blocks with a matching attribute should still be converted to a block if they are called with dynamicExpand. --- lang/blocktoattr/fixup_test.go | 14 ++++++++++++++ lang/blocktoattr/schema.go | 9 +++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lang/blocktoattr/fixup_test.go b/lang/blocktoattr/fixup_test.go index 20f71069e..ae650ee67 100644 --- a/lang/blocktoattr/fixup_test.go +++ b/lang/blocktoattr/fixup_test.go @@ -196,6 +196,20 @@ dynamic "foo" { }), }), }, + "dynamic block with empty iterator": { + src: ` +dynamic "foo" { + for_each = [] + content { + bar = foo.value + } +} +`, + schema: fooSchema, + want: cty.ObjectVal(map[string]cty.Value{ + "foo": cty.NullVal(fooSchema.Attributes["foo"].Type), + }), + }, "both attribute and block syntax": { src: ` foo = [] diff --git a/lang/blocktoattr/schema.go b/lang/blocktoattr/schema.go index 2f2463a5c..47a025659 100644 --- a/lang/blocktoattr/schema.go +++ b/lang/blocktoattr/schema.go @@ -55,10 +55,11 @@ func effectiveSchema(given *hcl.BodySchema, body hcl.Body, ambiguousNames map[st }, } content, _, _ = body.PartialContent(&probeSchema) - if len(content.Blocks) > 0 { - // No attribute present and at least one block present, so - // we'll need to rewrite this one as a block for a successful - // result. + if len(content.Blocks) > 0 || dynamicExpanded { + // A dynamic block with an empty iterator returns nothing. + // If there's no attribute and we have either a block or a + // dynamic expansion, we need to rewrite this one as a + // block for a successful result. appearsAsBlock[name] = struct{}{} } }