-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_block.go
167 lines (147 loc) · 3.2 KB
/
base_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
package golden
import (
"context"
"github.com/google/uuid"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)
type BaseBlock struct {
c Config
hb *HclBlock
name string
id string
blockAddress string
forEach *ForEach
hasExpanded bool
readyForRead bool
preConditions []PreCondition
}
func NewBaseBlock(c Config, hb *HclBlock) *BaseBlock {
n := ""
if hb != nil {
n = hb.Labels[1]
}
bb := &BaseBlock{
c: c,
hb: hb,
blockAddress: blockAddress(hb),
name: n,
id: uuid.NewString(),
}
return bb
}
func (bb *BaseBlock) Id() string {
if bb == nil {
return ""
}
return bb.id
}
func (bb *BaseBlock) Name() string {
if bb == nil {
return ""
}
return bb.name
}
func (bb *BaseBlock) HclBlock() *HclBlock {
if bb.hb == nil {
return &HclBlock{
Block: new(hclsyntax.Block),
}
}
return bb.hb
}
func (bb *BaseBlock) BaseValues() map[string]cty.Value {
return map[string]cty.Value{
"id": cty.StringVal(bb.id),
}
}
func (bb *BaseBlock) EvalContext() *hcl.EvalContext {
var ctx *hcl.EvalContext
if bb.c == nil {
ctx = new(hcl.EvalContext)
} else {
ctx = bb.c.EvalContext()
}
if bb.forEach != nil {
ctx = ctx.NewChild()
ctx.Variables = map[string]cty.Value{
"each": cty.ObjectVal(map[string]cty.Value{
"key": cty.StringVal(CtyValueToString(bb.forEach.key)),
"value": bb.forEach.value,
}),
}
}
return ctx
}
func (bb *BaseBlock) Address() string {
if bb == nil {
return ""
}
return bb.blockAddress
}
func (bb *BaseBlock) Context() context.Context {
if bb == nil || bb.c == nil {
return context.TODO()
}
return bb.c.Context()
}
func (bb *BaseBlock) PreConditionCheck(ctx *hcl.EvalContext) ([]PreCondition, error) {
var failedChecks []PreCondition
var err error
for _, cond := range bb.preConditions {
diag := gohcl.DecodeBody(cond.Body, ctx, &cond)
if diag.HasErrors() {
err = multierror.Append(err, diag.Errs()...)
continue
}
if !cond.Condition {
failedChecks = append(failedChecks, cond)
}
}
return failedChecks, err
}
func (bb *BaseBlock) Config() Config {
return bb.c
}
func (bb *BaseBlock) forEachDefined() bool {
_, forEach := bb.HclBlock().Body.Attributes["for_each"]
return forEach
}
func (bb *BaseBlock) getDownstreams() []Block {
var blocks []Block
children, _ := bb.c.GetChildren(bb.blockAddress)
for _, c := range children {
blocks = append(blocks, c.(Block))
}
return blocks
}
func (bb *BaseBlock) setForEach(each *ForEach) {
bb.forEach = each
}
func (bb *BaseBlock) getForEach() *ForEach {
return bb.forEach
}
func (bb *BaseBlock) setMetaNestedBlock() {
for _, nb := range bb.hb.Block.Body.Blocks {
if nb.Type == "precondition" {
bb.preConditions = append(bb.preConditions, PreCondition{
Body: nb.Body,
})
}
}
}
func (bb *BaseBlock) markExpanded() {
bb.hasExpanded = true
}
func (bb *BaseBlock) expandable() bool {
return bb.forEachDefined() && !bb.hasExpanded
}
func (bb *BaseBlock) markReady() {
bb.readyForRead = true
}
func (bb *BaseBlock) isReadyForRead() bool {
return bb.readyForRead
}