2018-02-02 05:33:06 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
2018-02-03 02:22:25 +01:00
|
|
|
"github.com/hashicorp/hcl2/gohcl"
|
2018-02-02 05:33:06 +01:00
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
2018-02-03 02:22:25 +01:00
|
|
|
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
2018-02-02 05:33:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ModuleCall represents a "module" block in a module or file.
|
|
|
|
type ModuleCall struct {
|
2018-02-03 02:22:25 +01:00
|
|
|
Name string
|
|
|
|
|
|
|
|
SourceAddr string
|
|
|
|
SourceAddrRange hcl.Range
|
2018-02-07 03:05:14 +01:00
|
|
|
SourceSet bool
|
2018-02-03 02:22:25 +01:00
|
|
|
|
|
|
|
Config hcl.Body
|
2018-02-02 05:33:06 +01:00
|
|
|
|
|
|
|
Version VersionConstraint
|
|
|
|
|
|
|
|
Count hcl.Expression
|
|
|
|
ForEach hcl.Expression
|
|
|
|
|
2018-02-03 02:22:25 +01:00
|
|
|
DependsOn []hcl.Traversal
|
|
|
|
|
2018-02-02 05:33:06 +01:00
|
|
|
DeclRange hcl.Range
|
|
|
|
}
|
2018-02-03 02:22:25 +01:00
|
|
|
|
2018-02-07 03:05:14 +01:00
|
|
|
func decodeModuleBlock(block *hcl.Block, override bool) (*ModuleCall, hcl.Diagnostics) {
|
2018-02-03 02:22:25 +01:00
|
|
|
mc := &ModuleCall{
|
|
|
|
Name: block.Labels[0],
|
|
|
|
DeclRange: block.DefRange,
|
|
|
|
}
|
|
|
|
|
2018-02-07 03:05:14 +01:00
|
|
|
schema := moduleBlockSchema
|
|
|
|
if override {
|
|
|
|
schema = schemaForOverrides(schema)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, remain, diags := block.Body.PartialContent(schema)
|
2018-02-03 02:22:25 +01:00
|
|
|
mc.Config = remain
|
|
|
|
|
|
|
|
if !hclsyntax.ValidIdentifier(mc.Name) {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid module instance name",
|
|
|
|
Detail: badIdentifierDetail,
|
|
|
|
Subject: &block.LabelRanges[0],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, exists := content.Attributes["source"]; exists {
|
|
|
|
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &mc.SourceAddr)
|
|
|
|
diags = append(diags, valDiags...)
|
|
|
|
mc.SourceAddrRange = attr.Expr.Range()
|
2018-02-07 03:05:14 +01:00
|
|
|
mc.SourceSet = true
|
2018-02-03 02:22:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if attr, exists := content.Attributes["version"]; exists {
|
|
|
|
var versionDiags hcl.Diagnostics
|
|
|
|
mc.Version, versionDiags = decodeVersionConstraint(attr)
|
|
|
|
diags = append(diags, versionDiags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, exists := content.Attributes["count"]; exists {
|
|
|
|
mc.Count = attr.Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, exists := content.Attributes["for_each"]; exists {
|
|
|
|
mc.ForEach = attr.Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, exists := content.Attributes["depends_on"]; exists {
|
|
|
|
deps, depsDiags := decodeDependsOn(attr)
|
|
|
|
diags = append(diags, depsDiags...)
|
|
|
|
mc.DependsOn = append(mc.DependsOn, deps...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mc, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
var moduleBlockSchema = &hcl.BodySchema{
|
|
|
|
Attributes: []hcl.AttributeSchema{
|
|
|
|
{
|
|
|
|
Name: "source",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "version",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "count",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "for_each",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "depends_on",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|