-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathmodule.go
159 lines (139 loc) · 4.25 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package terraform
import (
"fmt"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
)
type Module struct {
Resources map[string]map[string]*Resource
Variables map[string]*Variable
Locals map[string]*Local
ModuleCalls map[string]*ModuleCall
SourceDir string
Sources map[string][]byte
Files map[string]*hcl.File
primaries []*hcl.File
overrides []*hcl.File
}
func NewEmptyModule() *Module {
return &Module{
Resources: map[string]map[string]*Resource{},
Variables: map[string]*Variable{},
Locals: map[string]*Local{},
ModuleCalls: map[string]*ModuleCall{},
SourceDir: "",
Sources: map[string][]byte{},
Files: map[string]*hcl.File{},
primaries: []*hcl.File{},
overrides: []*hcl.File{},
}
}
func (m *Module) build() hcl.Diagnostics {
body, diags := m.PartialContent(moduleSchema, nil)
if diags.HasErrors() {
return diags
}
for _, block := range body.Blocks {
switch block.Type {
case "resource":
r := decodeResourceBlock(block)
if _, exists := m.Resources[r.Type]; !exists {
m.Resources[r.Type] = map[string]*Resource{}
}
m.Resources[r.Type][r.Name] = r
case "variable":
v, valDiags := decodeVairableBlock(block)
diags = diags.Extend(valDiags)
m.Variables[v.Name] = v
case "module":
call, moduleDiags := decodeModuleBlock(block)
diags = diags.Extend(moduleDiags)
m.ModuleCalls[call.Name] = call
case "locals":
locals := decodeLocalsBlock(block)
for _, local := range locals {
m.Locals[local.Name] = local
}
}
}
return diags
}
// PartialContent extracts body content from Terraform configurations based on the passed schema.
// Basically, this function is a wrapper for hclext.PartialContent, but in some ways it reproduces
// Terraform language semantics.
//
// 1. Supports overriding files
// https://developer.hashicorp.com/terraform/language/files/override
// 2. Expands "dynamic" blocks
// https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks
// 3. Expands resource/module depends on the meta-arguments
// https://developer.hashicorp.com/terraform/language/meta-arguments/count
// https://developer.hashicorp.com/terraform/language/meta-arguments/for_each
//
// But 2 and 3 won't run if you didn't pass the evaluation context.
func (m *Module) PartialContent(schema *hclext.BodySchema, ctx *Evaluator) (*hclext.BodyContent, hcl.Diagnostics) {
content := &hclext.BodyContent{}
diags := hcl.Diagnostics{}
for _, f := range m.primaries {
expanded, d := ctx.ExpandBlock(f.Body, schema)
diags = diags.Extend(d)
c, d := hclext.PartialContent(expanded, schema)
diags = diags.Extend(d)
for name, attr := range c.Attributes {
content.Attributes[name] = attr
}
content.Blocks = append(content.Blocks, c.Blocks...)
}
for _, f := range m.overrides {
expanded, d := ctx.ExpandBlock(f.Body, schema)
diags = diags.Extend(d)
c, d := hclext.PartialContent(expanded, schema)
diags = diags.Extend(d)
for name, attr := range c.Attributes {
content.Attributes[name] = attr
}
content.Blocks = overrideBlocks(content.Blocks, c.Blocks)
}
return content, diags
}
// overrideBlocks changes the attributes in the passed primary blocks by override blocks recursively.
func overrideBlocks(primaries, overrides hclext.Blocks) hclext.Blocks {
dict := map[string]*hclext.Block{}
for _, primary := range primaries {
key := fmt.Sprintf("%s[%s]", primary.Type, strings.Join(primary.Labels, ","))
dict[key] = primary
}
for _, override := range overrides {
key := fmt.Sprintf("%s[%s]", override.Type, strings.Join(override.Labels, ","))
if primary, exists := dict[key]; exists {
for name, attr := range override.Body.Attributes {
primary.Body.Attributes[name] = attr
}
primary.Body.Blocks = overrideBlocks(primary.Body.Blocks, override.Body.Blocks)
}
}
return primaries
}
var moduleSchema = &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "resource",
LabelNames: []string{"type", "name"},
},
{
Type: "variable",
LabelNames: []string{"name"},
Body: variableBlockSchema,
},
{
Type: "module",
LabelNames: []string{"name"},
Body: moduleBlockSchema,
},
{
Type: "locals",
Body: localBlockSchema,
},
},
}