2018-01-22 02:24:00 +00:00
|
|
|
package dynblock
|
|
|
|
|
|
|
|
import (
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2019-09-09 22:39:31 +00:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2018-01-22 02:24:00 +00:00
|
|
|
)
|
|
|
|
|
2019-03-18 23:28:30 +00:00
|
|
|
// 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.
|
2018-01-22 02:24:00 +00:00
|
|
|
//
|
2019-03-18 23:28:30 +00:00
|
|
|
// 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)
|
2018-01-22 02:24:00 +00:00
|
|
|
return walkVariablesWithHCLDec(rootNode, spec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func walkVariablesWithHCLDec(node WalkVariablesNode, spec hcldec.Spec) []hcl.Traversal {
|
|
|
|
vars, children := node.Visit(hcldec.ImpliedSchema(spec))
|
|
|
|
|
|
|
|
if len(children) > 0 {
|
|
|
|
childSpecs := hcldec.ChildBlockTypes(spec)
|
|
|
|
for _, child := range children {
|
|
|
|
if childSpec, exists := childSpecs[child.BlockTypeName]; exists {
|
|
|
|
vars = append(vars, walkVariablesWithHCLDec(child.Node, childSpec)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vars
|
|
|
|
}
|