diff --git a/go.mod b/go.mod index 588593169..9fbc45aef 100644 --- a/go.mod +++ b/go.mod @@ -60,7 +60,7 @@ require ( github.com/hashicorp/go-version v1.1.0 github.com/hashicorp/golang-lru v0.5.0 // indirect github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f - github.com/hashicorp/hcl2 v0.0.0-20190315201941-956e03eb6dda + github.com/hashicorp/hcl2 v0.0.0-20190318232830-f9f92da699d8 github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 github.com/hashicorp/logutils v1.0.0 github.com/hashicorp/memberlist v0.1.0 // indirect diff --git a/go.sum b/go.sum index b5c87a6c9..d4b3a56a0 100644 --- a/go.sum +++ b/go.sum @@ -200,8 +200,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws= github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w= github.com/hashicorp/hcl2 v0.0.0-20181208003705-670926858200/go.mod h1:ShfpTh661oAaxo7VcNxg0zcZW6jvMa7Moy2oFx7e5dE= -github.com/hashicorp/hcl2 v0.0.0-20190315201941-956e03eb6dda h1:wgPsY2p0JDBRzc7fPEUidAhpXZzSPTIuSUDATOisbYs= -github.com/hashicorp/hcl2 v0.0.0-20190315201941-956e03eb6dda/go.mod h1:HtEzazM5AZ9fviNEof8QZB4T1Vz9UhHrGhnMPzl//Ek= +github.com/hashicorp/hcl2 v0.0.0-20190318232830-f9f92da699d8 h1:H4X4ZtK0svjPuIRh1NJttHjJyrB1d/3ArFA1GZbuy1o= +github.com/hashicorp/hcl2 v0.0.0-20190318232830-f9f92da699d8/go.mod h1:HtEzazM5AZ9fviNEof8QZB4T1Vz9UhHrGhnMPzl//Ek= github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590 h1:2yzhWGdgQUWZUCNK+AoO35V+HTsgEmcM4J9IkArh7PI= github.com/hashicorp/hil v0.0.0-20190212112733-ab17b08d6590/go.mod h1:n2TSygSNwsLJ76m8qFXTSc7beTb+auJxYdqrnoqwZWE= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= diff --git a/lang/eval.go b/lang/eval.go index b5aaa8588..e27ff30e1 100644 --- a/lang/eval.go +++ b/lang/eval.go @@ -25,7 +25,7 @@ import ( func (s *Scope) ExpandBlock(body hcl.Body, schema *configschema.Block) (hcl.Body, tfdiags.Diagnostics) { spec := schema.DecoderSpec() - traversals := dynblock.ForEachVariablesHCLDec(body, spec) + traversals := dynblock.ExpandVariablesHCLDec(body, spec) refs, diags := References(traversals) ctx, ctxDiags := s.EvalContext(refs) diff --git a/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables.go b/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables.go index 9cb67168f..90b4820d1 100644 --- a/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables.go +++ b/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables.go @@ -5,19 +5,31 @@ import ( "github.com/zclconf/go-cty/cty" ) -// WalkVariables begins the recursive process of walking the variables in the -// given body that are needed by any "for_each" or "labels" attributes in -// "dynamic" blocks. The result is a WalkVariablesNode, which can extract -// root-level variable traversals and produce a list of child nodes that -// also need to be processed by calling Visit. +// WalkVariables begins the recursive process of walking all expressions and +// nested blocks in the given body and its child bodies while taking into +// account any "dynamic" blocks. // // This function requires that the caller walk through the nested block // structure in the given body level-by-level so that an appropriate schema // can be provided at each level to inform further processing. This workflow // is thus easiest to use for calling applications that have some higher-level // schema representation available with which to drive this multi-step -// process. -func WalkForEachVariables(body hcl.Body) WalkVariablesNode { +// process. If your application uses the hcldec package, you may be able to +// use VariablesHCLDec instead for a more automatic approach. +func WalkVariables(body hcl.Body) WalkVariablesNode { + return WalkVariablesNode{ + body: body, + includeContent: true, + } +} + +// WalkExpandVariables is like Variables but it includes only the variables +// required for successful block expansion, ignoring any variables referenced +// inside block contents. The result is the minimal set of all variables +// required for a call to Expand, excluding variables that would only be +// needed to subsequently call Content or PartialContent on the expanded +// body. +func WalkExpandVariables(body hcl.Body) WalkVariablesNode { return WalkVariablesNode{ body: body, } @@ -26,6 +38,8 @@ func WalkForEachVariables(body hcl.Body) WalkVariablesNode { type WalkVariablesNode struct { body hcl.Body it *iteration + + includeContent bool } type WalkVariablesChild struct { @@ -50,6 +64,22 @@ func (n WalkVariablesNode) Visit(schema *hcl.BodySchema) (vars []hcl.Traversal, children = make([]WalkVariablesChild, 0, len(container.Blocks)) + if n.includeContent { + for _, attr := range container.Attributes { + for _, traversal := range attr.Expr.Variables() { + var ours, inherited bool + if n.it != nil { + ours = traversal.RootName() == n.it.IteratorName + _, inherited = n.it.Inherited[traversal.RootName()] + } + + if !(ours || inherited) { + vars = append(vars, traversal) + } + } + } + } + for _, block := range container.Blocks { switch block.Type { @@ -104,8 +134,9 @@ func (n WalkVariablesNode) Visit(schema *hcl.BodySchema) (vars []hcl.Traversal, children = append(children, WalkVariablesChild{ BlockTypeName: blockTypeName, Node: WalkVariablesNode{ - body: contentBlock.Body, - it: blockIt, + body: contentBlock.Body, + it: blockIt, + includeContent: n.includeContent, }, }) } @@ -114,8 +145,9 @@ func (n WalkVariablesNode) Visit(schema *hcl.BodySchema) (vars []hcl.Traversal, children = append(children, WalkVariablesChild{ BlockTypeName: block.Type, Node: WalkVariablesNode{ - body: block.Body, - it: n.it, + body: block.Body, + it: n.it, + includeContent: n.includeContent, }, }) diff --git a/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables_hcldec.go b/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables_hcldec.go index 480873a57..a078d915c 100644 --- a/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables_hcldec.go +++ b/vendor/github.com/hashicorp/hcl2/ext/dynblock/variables_hcldec.go @@ -5,15 +5,25 @@ import ( "github.com/hashicorp/hcl2/hcldec" ) -// ForEachVariablesHCLDec is a wrapper around WalkForEachVariables that -// uses the given hcldec specification to automatically drive the recursive -// walk through nested blocks in the given body. +// VariablesHCLDec is a wrapper around WalkVariables that uses the given hcldec +// specification to automatically drive the recursive walk through nested +// blocks in the given body. // -// This provides more convenient access to all of the "for_each" and "labels" -// dependencies in a body for applications that are already using hcldec -// as a more convenient way to recursively decode body contents. -func ForEachVariablesHCLDec(body hcl.Body, spec hcldec.Spec) []hcl.Traversal { - rootNode := WalkForEachVariables(body) +// This is a drop-in replacement for hcldec.Variables which is able to treat +// blocks of type "dynamic" in the same special way that dynblock.Expand would, +// exposing both the variables referenced in the "for_each" and "labels" +// arguments and variables used in the nested "content" block. +func VariablesHCLDec(body hcl.Body, spec hcldec.Spec) []hcl.Traversal { + rootNode := WalkVariables(body) + return walkVariablesWithHCLDec(rootNode, spec) +} + +// ExpandVariablesHCLDec is like VariablesHCLDec but it includes only the +// minimal set of variables required to call Expand, ignoring variables that +// are referenced only inside normal block contents. See WalkExpandVariables +// for more information. +func ExpandVariablesHCLDec(body hcl.Body, spec hcldec.Spec) []hcl.Traversal { + rootNode := WalkExpandVariables(body) return walkVariablesWithHCLDec(rootNode, spec) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 3f4ab3c9d..83e84c7da 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -307,7 +307,7 @@ github.com/hashicorp/hcl/hcl/scanner github.com/hashicorp/hcl/hcl/strconv github.com/hashicorp/hcl/json/scanner github.com/hashicorp/hcl/json/token -# github.com/hashicorp/hcl2 v0.0.0-20190315201941-956e03eb6dda +# github.com/hashicorp/hcl2 v0.0.0-20190318232830-f9f92da699d8 github.com/hashicorp/hcl2/hcl github.com/hashicorp/hcl2/hcl/hclsyntax github.com/hashicorp/hcl2/hcldec