-
Notifications
You must be signed in to change notification settings - Fork 4
/
base_config.go
352 lines (317 loc) · 9.68 KB
/
base_config.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package golden
import (
"context"
"fmt"
"github.com/zclconf/go-cty/cty/function"
"path/filepath"
"sort"
"sync"
"github.com/hashicorp/hcl/v2"
"github.com/lonegunmanb/hclfuncs"
"github.com/spf13/afero"
"github.com/zclconf/go-cty/cty"
)
var configFs = afero.NewOsFs()
type NewBaseConfigArgs struct {
Basedir string
VarConfigDir *string
Ctx context.Context
DslAbbreviation string
DslFullName string
IgnoreUnknownVariables bool
CliFlagAssignedVariables []CliFlagAssignedVariables
}
type BaseConfig struct {
ctx context.Context
basedir string
varConfigDir *string
d *Dag
rawBlockAddresses map[string]struct{}
dslFullName string
dslAbbreviation string
cliFlagAssignedVariables []CliFlagAssignedVariables
inputVariables map[string]VariableValueRead
inputVariableReadsLoader *sync.Once
ignoreUnknownVariables bool
OverrideFunctions map[string]function.Function
}
func (c *BaseConfig) Context() context.Context {
return c.ctx
}
func (c *BaseConfig) DslFullName() string { return c.dslFullName }
func (c *BaseConfig) DslAbbreviation() string { return c.dslAbbreviation }
func (c *BaseConfig) EmptyEvalContext() *hcl.EvalContext {
return &hcl.EvalContext{
Functions: merge(hclfuncs.Functions(c.basedir), c.OverrideFunctions),
Variables: make(map[string]cty.Value),
}
}
func (c *BaseConfig) EvalContext() *hcl.EvalContext {
ctx := c.EmptyEvalContext()
for bt, bs := range c.blocksByTypes() {
sample := bs[0]
if s, ok := sample.(BlockCustomizedRefType); ok {
bt = s.CustomizedRefType()
}
if _, ok := sample.(SingleValueBlock); ok {
ctx.Variables[bt] = SingleValues(castBlock[SingleValueBlock](bs))
continue
}
ctx.Variables[bt] = Values(bs)
}
return ctx
}
func NewBasicConfigFromArgs(a NewBaseConfigArgs) *BaseConfig {
c := NewBasicConfig(a.Basedir, a.DslFullName, a.DslAbbreviation, a.VarConfigDir, a.CliFlagAssignedVariables, a.Ctx)
c.ignoreUnknownVariables = a.IgnoreUnknownVariables
return c
}
func NewBasicConfig(basedir, dslFullName, dslAbbreviation string, varConfigDir *string, cliFlagAssignedVariables []CliFlagAssignedVariables, ctx context.Context) *BaseConfig {
if ctx == nil {
ctx = context.Background()
}
c := &BaseConfig{
basedir: basedir,
varConfigDir: varConfigDir,
ctx: ctx,
dslAbbreviation: dslAbbreviation,
dslFullName: dslFullName,
cliFlagAssignedVariables: cliFlagAssignedVariables,
d: newDag(),
inputVariableReadsLoader: &sync.Once{},
rawBlockAddresses: make(map[string]struct{}),
}
return c
}
func (c *BaseConfig) RunPrePlan() error {
return c.runDag(prePlan)
}
func (c *BaseConfig) RunPlan() error {
return c.runDag(dagPlan)
}
func (c *BaseConfig) GetVertices() map[string]interface{} {
if c.d == nil {
return nil
}
return c.d.GetVertices()
}
func (c *BaseConfig) GetAncestors(id string) (map[string]interface{}, error) {
return c.d.GetAncestors(id)
}
func (c *BaseConfig) GetChildren(id string) (map[string]interface{}, error) {
return c.d.GetChildren(id)
}
func (c *BaseConfig) ValidBlockAddress(address string) bool {
if v, err := c.d.GetVertex(address); v != nil && err == nil {
return true
}
if _, ok := c.rawBlockAddresses[address]; ok {
return true
}
return false
}
func (c *BaseConfig) readInputVariables() (map[string]VariableValueRead, error) {
if c.inputVariables != nil {
return c.inputVariables, nil
}
var readErr error
c.inputVariableReadsLoader.Do(func() {
envVars := c.readVariablesFromEnv()
defaultFileVars, err := c.readVariablesFromDefaultVarFiles()
if err != nil {
readErr = err
return
}
autoFileVars, err := c.readVariablesFromAutoVarFiles()
if err != nil {
readErr = err
return
}
cliAssignedVariables, err := c.readCliAssignedVariables()
if err != nil {
readErr = err
return
}
c.inputVariables = merge(envVars, defaultFileVars, autoFileVars, cliAssignedVariables)
})
return c.inputVariables, readErr
}
func (c *BaseConfig) readVariablesFromEnv() map[string]VariableValueRead {
valuesFromEnv := make(map[string]VariableValueRead)
variables := Blocks[*VariableBlock](c)
for _, vb := range variables {
valuesFromEnv[vb.Name()] = vb.readValueFromEnv()
}
return valuesFromEnv
}
func (c *BaseConfig) readCliAssignedVariables() (map[string]VariableValueRead, error) {
r := make(map[string]VariableValueRead)
for _, assignedVariables := range c.cliFlagAssignedVariables {
reads, err := assignedVariables.Variables(c)
if err != nil {
return nil, err
}
r = merge(r, reads)
}
return r, nil
}
func (c *BaseConfig) readVariablesFromAutoVarFiles() (map[string]VariableValueRead, error) {
autoHclVarFilePattern := fmt.Sprintf("*.auto.%svars", c.dslAbbreviation)
autoJsonVarFilePattern := autoHclVarFilePattern + ".json"
hclMatches, err := afero.Glob(configFs, filepath.Join(c.variableConfigFilesDir(), autoHclVarFilePattern))
if err != nil {
return nil, fmt.Errorf("cannot list auto var files at %s: %+v", c.variableConfigFilesDir(), err)
}
jsonMatches, err := afero.Glob(configFs, filepath.Join(c.variableConfigFilesDir(), autoJsonVarFilePattern))
if err != nil {
return nil, fmt.Errorf("cannot list auto var files at %s: %+v", c.variableConfigFilesDir(), err)
}
matches := append(hclMatches, jsonMatches...)
sort.Strings(matches)
return c.readVariablesFromVarFiles(matches)
}
func (c *BaseConfig) readVariablesFromDefaultVarFiles() (map[string]VariableValueRead, error) {
defaultHclVarFilePath := filepath.Join(c.variableConfigFilesDir(), fmt.Sprintf("%s.%svars", c.dslFullName, c.dslAbbreviation))
defaultJsonVarFilePath := defaultHclVarFilePath + ".json"
return c.readVariablesFromVarFiles([]string{defaultHclVarFilePath, defaultJsonVarFilePath})
}
func (c *BaseConfig) readVariablesFromVarFiles(paths []string) (map[string]VariableValueRead, error) {
r := make(map[string]VariableValueRead)
for _, path := range paths {
vars, err := c.readVariablesFromVarFile(path)
if err != nil {
return nil, err
}
r = merge(r, vars)
}
return r, nil
}
func (c *BaseConfig) readVariablesFromVarFile(fileName string) (map[string]VariableValueRead, error) {
var m map[string]VariableValueRead
exist, err := afero.Exists(configFs, fileName)
if err != nil {
return nil, fmt.Errorf("cannot check existance of %s: %+v", fileName, err)
}
if !exist {
return nil, nil
}
content, err := afero.ReadFile(configFs, fileName)
if err != nil {
return nil, fmt.Errorf("cannot open %s: %+v", fileName, err)
}
m, err = c.ReadVariablesFromSingleVarFile(content, fileName)
if err != nil {
return nil, fmt.Errorf("cannot parse %s: %+v", fileName, err)
}
return m, nil
}
func (c *BaseConfig) ReadVariablesFromSingleVarFile(fileContent []byte, fileName string) (map[string]VariableValueRead, error) {
parser := &varFileParserImpl{dslAbbreviation: c.dslAbbreviation}
file, err := parser.ParseFile(fileContent, filepath.Base(fileName))
if err != nil {
return nil, err
}
attributes, diag := file.Body.JustAttributes()
if diag.HasErrors() {
return nil, diag
}
reads := make(map[string]VariableValueRead)
for _, attr := range attributes {
value, diag := attr.Expr.Value(nil)
var err error
if diag.HasErrors() {
err = diag
}
reads[attr.Name] = NewVariableValueRead(attr.Name, &value, err)
}
return reads, nil
}
func (c *BaseConfig) variableConfigFilesDir() string {
if c.varConfigDir != nil {
return *c.varConfigDir
}
return c.basedir
}
func (c *BaseConfig) blocksByTypes() map[string][]Block {
r := make(map[string][]Block)
for _, b := range blocks(c) {
bt := b.BlockType()
r[bt] = append(r[bt], b)
}
return r
}
func (c *BaseConfig) buildDag(blocks []Block) error {
for _, b := range blocks {
c.rawBlockAddresses[b.Address()] = struct{}{}
}
return c.d.buildDag(blocks)
}
func (c *BaseConfig) runDag(onReady func(Block) error) error {
return c.d.runDag(c, onReady)
}
func (c *BaseConfig) expandBlock(b Block) ([]Block, error) {
var expandedBlocks []Block
hclBlock := b.HclBlock()
attr, ok := hclBlock.Body.Attributes["for_each"]
if !ok || b.getForEach() != nil {
return nil, nil
}
forEachValue, diag := attr.Expr.Value(c.EvalContext())
if diag.HasErrors() {
return nil, diag
}
if !forEachValue.CanIterateElements() {
return nil, fmt.Errorf("invalid `for_each`, except set or map: %s", attr.Range().String())
}
address := b.Address()
upstreams, err := c.d.GetAncestors(address)
if err != nil {
return nil, err
}
downstreams, err := c.d.GetChildren(address)
if err != nil {
return nil, err
}
iterator := forEachValue.ElementIterator()
for iterator.Next() {
key, value := iterator.Element()
newBlock := NewHclBlock(hclBlock.Block, hclBlock.wb, NewForEach(key, value))
nb, err := wrapBlock(b.Config(), newBlock)
if err != nil {
return nil, err
}
nb.markExpanded()
expandedAddress := blockAddress(newBlock)
expandedBlocks = append(expandedBlocks, nb)
err = c.d.AddVertexByID(expandedAddress, nb)
if err != nil {
return nil, err
}
for upstreamAddress := range upstreams {
err := c.d.addEdge(upstreamAddress, expandedAddress)
if err != nil {
return nil, err
}
}
for downstreamAddress := range downstreams {
err := c.d.addEdge(expandedAddress, downstreamAddress)
if err != nil {
return nil, err
}
}
}
b.markExpanded()
return expandedBlocks, c.d.DeleteVertex(address)
}
func Traverse[T Block](c *BaseConfig, walker func(b T) error) error {
return traverse(c.d, walker)
}
func merge[TK, TV comparable](maps ...map[TK]TV) map[TK]TV {
r := make(map[TK]TV)
for _, m := range maps {
for k, v := range m {
r[k] = v
}
}
return r
}