github.com/rvichery/terraform@v0.11.10/configs/module_call.go (about)

     1  package configs
     2  
     3  import (
     4  	"github.com/hashicorp/hcl2/gohcl"
     5  	"github.com/hashicorp/hcl2/hcl"
     6  	"github.com/hashicorp/hcl2/hcl/hclsyntax"
     7  )
     8  
     9  // ModuleCall represents a "module" block in a module or file.
    10  type ModuleCall struct {
    11  	Name string
    12  
    13  	SourceAddr      string
    14  	SourceAddrRange hcl.Range
    15  	SourceSet       bool
    16  
    17  	Config hcl.Body
    18  
    19  	Version VersionConstraint
    20  
    21  	Count   hcl.Expression
    22  	ForEach hcl.Expression
    23  
    24  	DependsOn []hcl.Traversal
    25  
    26  	DeclRange hcl.Range
    27  }
    28  
    29  func decodeModuleBlock(block *hcl.Block, override bool) (*ModuleCall, hcl.Diagnostics) {
    30  	mc := &ModuleCall{
    31  		Name:      block.Labels[0],
    32  		DeclRange: block.DefRange,
    33  	}
    34  
    35  	schema := moduleBlockSchema
    36  	if override {
    37  		schema = schemaForOverrides(schema)
    38  	}
    39  
    40  	content, remain, diags := block.Body.PartialContent(schema)
    41  	mc.Config = remain
    42  
    43  	if !hclsyntax.ValidIdentifier(mc.Name) {
    44  		diags = append(diags, &hcl.Diagnostic{
    45  			Severity: hcl.DiagError,
    46  			Summary:  "Invalid module instance name",
    47  			Detail:   badIdentifierDetail,
    48  			Subject:  &block.LabelRanges[0],
    49  		})
    50  	}
    51  
    52  	if attr, exists := content.Attributes["source"]; exists {
    53  		valDiags := gohcl.DecodeExpression(attr.Expr, nil, &mc.SourceAddr)
    54  		diags = append(diags, valDiags...)
    55  		mc.SourceAddrRange = attr.Expr.Range()
    56  		mc.SourceSet = true
    57  	}
    58  
    59  	if attr, exists := content.Attributes["version"]; exists {
    60  		var versionDiags hcl.Diagnostics
    61  		mc.Version, versionDiags = decodeVersionConstraint(attr)
    62  		diags = append(diags, versionDiags...)
    63  	}
    64  
    65  	if attr, exists := content.Attributes["count"]; exists {
    66  		mc.Count = attr.Expr
    67  	}
    68  
    69  	if attr, exists := content.Attributes["for_each"]; exists {
    70  		mc.ForEach = attr.Expr
    71  	}
    72  
    73  	if attr, exists := content.Attributes["depends_on"]; exists {
    74  		deps, depsDiags := decodeDependsOn(attr)
    75  		diags = append(diags, depsDiags...)
    76  		mc.DependsOn = append(mc.DependsOn, deps...)
    77  	}
    78  
    79  	return mc, diags
    80  }
    81  
    82  var moduleBlockSchema = &hcl.BodySchema{
    83  	Attributes: []hcl.AttributeSchema{
    84  		{
    85  			Name:     "source",
    86  			Required: true,
    87  		},
    88  		{
    89  			Name: "version",
    90  		},
    91  		{
    92  			Name: "count",
    93  		},
    94  		{
    95  			Name: "for_each",
    96  		},
    97  		{
    98  			Name: "depends_on",
    99  		},
   100  	},
   101  }