-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.go
348 lines (316 loc) · 8.55 KB
/
block.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
package golden
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/sets/hashset"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/lonegunmanb/go-defaults"
"github.com/zclconf/go-cty/cty"
"reflect"
"strings"
)
type BlockType interface {
BlockType() string
}
type Block interface {
Id() string
Name() string
Type() string
BlockType() string
Address() string
HclBlock() *HclBlock
EvalContext() *hcl.EvalContext
BaseValues() map[string]cty.Value
PreConditionCheck(*hcl.EvalContext) ([]PreCondition, error)
AddressLength() int
CanExecutePrePlan() bool
Config() Config
getDownstreams() []Block
getForEach() *ForEach
markExpanded()
isReadyForRead() bool
markReady()
expandable() bool
}
func BlockToString(f Block) string {
if s, ok := f.(fmt.Stringer); ok {
return s.String()
}
marshal, _ := json.Marshal(f)
return string(marshal)
}
var MetaAttributeNames = hashset.New("for_each", "depends_on")
var MetaNestedBlockNames = hashset.New("precondition", "dynamic")
func Decode(b Block) error {
hb := b.HclBlock()
if err := verifyDependsOn(b); err != nil {
return err
}
zeroBlock(b)
evalContext := b.EvalContext()
if customDecode, ok := b.(CustomDecode); ok {
return customDecode.Decode(hb, evalContext)
}
if baseDecode, ok := b.(BaseDecode); ok {
err := baseDecode.BaseDecode(hb, evalContext)
if err != nil {
return err
}
}
diag := gohcl.DecodeBody(cleanBodyForDecode(hb.Body), evalContext, b)
if diag.HasErrors() {
return diag
}
err := decodeDynamicNestedBlock(b, b.HclBlock().Block, evalContext)
if err != nil {
return err
}
// we need set defaults again, since gohcl.DecodeBody might erase default value set on those attribute has null values.
defaults.SetDefaults(b)
return nil
}
func zeroBlock(b Block) {
nb, _ := wrapBlock(b.Config(), b.HclBlock())
v := reflect.ValueOf(b).Elem()
vnb := reflect.ValueOf(nb).Elem()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldNb := vnb.Field(i)
if !field.CanSet() {
continue
}
tag := v.Type().Field(i).Tag
if _, ok := tag.Lookup("hcl"); ok || strings.Contains(string(tag), "attribute") {
field.Set(fieldNb)
}
}
}
func verifyDependsOn(b Block) error {
dependsOn, ok := b.HclBlock().Attributes()["depends_on"]
if !ok {
return nil
}
exprString := strings.TrimSpace(dependsOn.ExprString())
if !strings.HasPrefix(exprString, "[") && !strings.HasSuffix(exprString, "]") {
return fmt.Errorf("`depends_on` must be a list of block address")
}
elements := strings.Split(strings.TrimSuffix(strings.TrimPrefix(exprString, "["), "]"), ",")
for _, element := range elements {
if !b.Config().ValidBlockAddress(element) {
return fmt.Errorf("`depends_on` must be a list of block address, invalid address: %s", element)
}
}
return nil
}
func decodeDynamicNestedBlock(b any, block *hclsyntax.Block, evalContext *hcl.EvalContext) error {
// Iterate over the blocks in the HCL block
for _, block := range block.Body.Blocks {
if block.Type != "dynamic" {
continue
}
forEachAttr, ok := block.Body.Attributes["for_each"]
if !ok {
return fmt.Errorf("`dynamic` block must has `for_each`")
}
forEachValue, diag := forEachAttr.Expr.Value(evalContext)
if diag.HasErrors() {
return diag
}
if !forEachValue.CanIterateElements() {
return fmt.Errorf("incorrect type for `for_each`, must be collection")
}
// Get the field in the parent block to append to
blockType := block.Labels[0]
parentField, ok := getFieldByTag(reflect.ValueOf(b).Elem(), blockType)
if !ok {
return fmt.Errorf("no such field: %s in block", blockType)
}
// Ensure we're working with a slice
if parentField.Kind() != reflect.Slice {
return fmt.Errorf("field: %s is not a slice", blockType)
}
// Append the parsed structs to the corresponding field in the parent block
for _, innerBlock := range block.Body.Blocks {
if innerBlock.Type != "content" {
return fmt.Errorf("dynamic should contains `content` block only: %s", innerBlock.DefRange().String())
}
iterator := forEachValue.ElementIterator()
iteratorName := blockType
for iterator.Next() {
key, value := iterator.Element()
newContext := evalContext.NewChild()
newContext.Variables = map[string]cty.Value{
iteratorName: cty.ObjectVal(map[string]cty.Value{
"key": key,
"value": value,
}),
}
err := decodeContentBlock(parentField, innerBlock, newContext)
if err != nil {
return err
}
}
}
}
return nil
}
// getFieldByTag returns the struct field with the given tag
func getFieldByTag(v reflect.Value, tag string) (reflect.Value, bool) {
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
hclTag := field.Tag.Get("hcl")
hclTagContents := strings.Split(hclTag, ",")
if hclTagContents[0] == tag {
return v.Field(i), true
}
}
return reflect.Value{}, false
}
func decodeContentBlock(parentField reflect.Value, innerBlock *hclsyntax.Block, evalContext *hcl.EvalContext) error {
// Decode the inner block into a new struct
newStructPtr := reflect.New(parentField.Type().Elem())
diags := gohcl.DecodeBody(bodyWithDynamicNestedBlocks(innerBlock.Body), evalContext, newStructPtr.Interface())
if diags.HasErrors() {
return diags
}
if err := decodeDynamicNestedBlock(newStructPtr.Interface(), innerBlock, evalContext); err != nil {
return err
}
// Append the new struct to the parent field
parentField.Set(reflect.Append(parentField, newStructPtr.Elem()))
return nil
}
func cleanBodyForDecode(hb *hclsyntax.Body) *hclsyntax.Body {
// Create a new hclsyntax.Body
newBody := &hclsyntax.Body{
Attributes: make(hclsyntax.Attributes),
}
// Iterate over the attributes of the original body
for attrName, attr := range hb.Attributes {
if MetaAttributeNames.Contains(attrName) {
continue
}
newBody.Attributes[attrName] = attr
}
for _, nb := range hb.Blocks {
if MetaNestedBlockNames.Contains(nb.Type) {
continue
}
newBody.Blocks = append(newBody.Blocks, nb)
}
return newBody
}
func bodyWithDynamicNestedBlocks(hb *hclsyntax.Body) *hclsyntax.Body {
newBody := &hclsyntax.Body{
Attributes: make(hclsyntax.Attributes),
}
// Iterate over the attributes of the original body
for attrName, attr := range hb.Attributes {
newBody.Attributes[attrName] = attr
}
for _, nb := range hb.Blocks {
if nb.Type != "dynamic" {
newBody.Blocks = append(newBody.Blocks, nb)
}
}
return newBody
}
func SingleValues(blocks []SingleValueBlock) cty.Value {
if len(blocks) == 0 {
return cty.EmptyObjectVal
}
res := map[string]cty.Value{}
for _, b := range blocks {
res[b.Name()] = b.Value()
}
return cty.ObjectVal(res)
}
func Values[T Block](blocks []T) cty.Value {
if len(blocks) == 0 {
return cty.EmptyObjectVal
}
res := map[string]cty.Value{}
valuesMap := map[string]map[string]cty.Value{}
for _, b := range blocks {
values, exists := valuesMap[b.Type()]
if !exists {
values = map[string]cty.Value{}
valuesMap[b.Type()] = values
}
blockVal := blockToCtyValue(b)
forEach := b.getForEach()
if forEach == nil {
values[b.Name()] = blockVal
} else {
m, ok := values[b.Name()]
if !ok {
m = cty.MapValEmpty(cty.EmptyObject)
}
nm := m.AsValueMap()
if nm == nil {
nm = make(map[string]cty.Value)
}
nm[CtyValueToString(forEach.key)] = blockVal
values[b.Name()] = cty.MapVal(nm)
}
valuesMap[b.Type()] = values
}
for t, m := range valuesMap {
res[t] = cty.ObjectVal(m)
}
return cty.ObjectVal(res)
}
func blockToCtyValue(b Block) cty.Value {
blockValues := map[string]cty.Value{}
baseCtyValues := b.BaseValues()
ctyValues := Value(b) //.Values()
for k, v := range ctyValues {
blockValues[k] = v
}
for k, v := range baseCtyValues {
blockValues[k] = v
}
blockVal := cty.ObjectVal(blockValues)
return blockVal
}
func concatLabels(labels []string) string {
sb := strings.Builder{}
for i, l := range labels {
if l == "" {
continue
}
sb.WriteString(l)
if i != len(labels)-1 {
sb.WriteString(".")
}
}
return sb.String()
}
func blockAddress(b *HclBlock) string {
if b == nil {
return ""
}
sb := strings.Builder{}
sb.WriteString(b.Block.Type)
sb.WriteString(".")
sb.WriteString(concatLabels(b.Block.Labels))
if b.ForEach != nil {
sb.WriteString(fmt.Sprintf("[%s]", CtyValueToString(b.ForEach.key)))
}
return sb.String()
}
// Not all `local` expression could be evaluated before for_each expansion, so we need to try to evaluate them.
func prePlan(b Block) error {
l, ok := b.(PrePlanBlock)
if !ok {
return nil
}
err := l.ExecuteBeforePlan()
if err != nil {
return err
}
b.markReady()
return nil
}