github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/lang/blocktoattr/variables.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package blocktoattr 5 6 import ( 7 "github.com/hashicorp/hcl/v2" 8 "github.com/hashicorp/hcl/v2/ext/dynblock" 9 "github.com/hashicorp/hcl/v2/hcldec" 10 "github.com/terramate-io/tf/configs/configschema" 11 ) 12 13 // ExpandedVariables finds all of the global variables referenced in the 14 // given body with the given schema while taking into account the possibilities 15 // both of "dynamic" blocks being expanded and the possibility of certain 16 // attributes being written instead as nested blocks as allowed by the 17 // FixUpBlockAttrs function. 18 // 19 // This function exists to allow variables to be analyzed prior to dynamic 20 // block expansion while also dealing with the fact that dynamic block expansion 21 // might in turn produce nested blocks that are subject to FixUpBlockAttrs. 22 // 23 // This is intended as a drop-in replacement for dynblock.VariablesHCLDec, 24 // which is itself a drop-in replacement for hcldec.Variables. 25 func ExpandedVariables(body hcl.Body, schema *configschema.Block) []hcl.Traversal { 26 rootNode := dynblock.WalkVariables(body) 27 return walkVariables(rootNode, body, schema) 28 } 29 30 func walkVariables(node dynblock.WalkVariablesNode, body hcl.Body, schema *configschema.Block) []hcl.Traversal { 31 givenRawSchema := hcldec.ImpliedSchema(schema.DecoderSpec()) 32 ambiguousNames := ambiguousNames(schema) 33 effectiveRawSchema := effectiveSchema(givenRawSchema, body, ambiguousNames, false) 34 vars, children := node.Visit(effectiveRawSchema) 35 36 for _, child := range children { 37 if blockS, exists := schema.BlockTypes[child.BlockTypeName]; exists { 38 vars = append(vars, walkVariables(child.Node, child.Body(), &blockS.Block)...) 39 } else if attrS, exists := schema.Attributes[child.BlockTypeName]; exists && attrS.Type.IsCollectionType() && attrS.Type.ElementType().IsObjectType() { 40 // ☝️Check for collection type before element type, because if this is a mis-placed reference, 41 // a panic here will prevent other useful diags from being elevated to show the user what to fix 42 synthSchema := SchemaForCtyElementType(attrS.Type.ElementType()) 43 vars = append(vars, walkVariables(child.Node, child.Body(), synthSchema)...) 44 } 45 } 46 47 return vars 48 }