-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag.go
382 lines (317 loc) · 11.4 KB
/
dag.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package hclconfig
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/jumppad-labs/hclconfig/convert"
"github.com/jumppad-labs/hclconfig/errors"
"github.com/jumppad-labs/hclconfig/resources"
"github.com/jumppad-labs/hclconfig/types"
"github.com/silas/dag"
"github.com/zclconf/go-cty/cty"
)
// doYaLikeDAGs? dags? yeah dags! oh dogs.
// https://www.youtube.com/watch?v=ZXILzUpVx7A&t=0s
func doYaLikeDAGs(c *Config) (*dag.AcyclicGraph, error) {
// create root node
graph := &dag.AcyclicGraph{}
// add a root node for the graph
root, _ := resources.DefaultResources().CreateResource(resources.TypeRoot, "root")
graph.Add(root)
// Loop over all resources and add to graph
for _, resource := range c.Resources {
// ignore variables
if resource.Metadata().Type != resources.TypeVariable {
graph.Add(resource)
}
}
// Add dependencies for all resources
for _, resource := range c.Resources {
hasDeps := false
// do nothing with variables
if resource.Metadata().Type == resources.TypeVariable {
continue
}
// we might not yet know if the resource is disabled, this could be due
// to the value being set from a variable or an interpolated value
// if disabled ignore any dependencies
if resource.GetDisabled() {
// add all disabled resources to the root
//fmt.Println("connect", "root", "to", resource.Metadata().ID)
graph.Connect(dag.BasicEdge(root, resource))
continue
}
// use a map to keep a unique list
dependencies := map[types.Resource]bool{}
// add links to dependencies
// this is here for now as we might need to process these two lists separately
// resource.SetDependsOn(append(resource.GetDependsOn(), resource.Metadata().Links...))
for _, d := range resource.Metadata().Links {
resource.AddDependency(d)
}
for _, d := range resource.GetDependencies() {
var err error
fqdn, err := resources.ParseFQRN(d)
if err != nil {
pe := &errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Message = fmt.Sprintf("invalid dependency: %s, error: %s", d, err)
pe.Level = errors.ParserErrorLevelError
return nil, pe
}
// when the dependency is a module, depend on all resources in the module
if fqdn.Type == resources.TypeModule {
// assume that all dependencies references have been written with no
// knowledge of their parent module. Therefore if the parent module is
// "module1" and the reference is "module.module2.resource.container.mine.id"
// then the reference should be modified to include the parent reference
// "module.module1.module2.resource.container.mine.id"
relFQDN := fqdn.AppendParentModule(resource.Metadata().Module)
deps, err := c.FindModuleResources(relFQDN.String(), true)
if err != nil {
pe := &errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Message = fmt.Sprintf("unable to find resources for module: %s, error: %s", fqdn.Module, err)
pe.Level = errors.ParserErrorLevelError
return nil, pe
}
for _, dep := range deps {
dependencies[dep] = true
}
}
// when the dependency is a resource, depend on the resource
if fqdn.Type != resources.TypeModule {
// assume that all dependencies references have been written with no
// knowledge of their parent module. Therefore if the parent module is
// "module1" and the reference is "module.module2.resource.container.mine.id"
// then the reference should be modified to include the parent reference
// "module.module1.module2.resource.container.mine.id"
relFQDN := fqdn.AppendParentModule(resource.Metadata().Module)
dep, err := c.FindResource(relFQDN.String())
if err != nil {
pe := &errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Message = fmt.Sprintf("unable to find dependent resource in module: '%s', error: '%s'", resource.Metadata().Module, err)
pe.Level = errors.ParserErrorLevelError
return nil, pe
}
dependencies[dep] = true
}
}
// if this resource is part of a module make it depend on that module
if resource.Metadata().Module != "" {
fqdnString := fmt.Sprintf("module.%s", resource.Metadata().Module)
d, err := c.FindResource(fqdnString)
if err != nil {
pe := &errors.ParserError{}
pe.Line = resource.Metadata().Line
pe.Column = resource.Metadata().Column
pe.Filename = resource.Metadata().File
pe.Message = fmt.Sprintf("unable to find resources parent module: '%s, error: %s", fqdnString, err)
pe.Level = errors.ParserErrorLevelError
return nil, pe
}
hasDeps = true
dependencies[d] = true
}
for d := range dependencies {
hasDeps = true
//fmt.Println("connect", resource.Metadata().ID, "to", d.Metadata().ID)
graph.Connect(dag.BasicEdge(d, resource))
}
// if no deps add to root node
if !hasDeps {
//fmt.Println("connect", resource.Metadata().ID, "to root")
graph.Connect(dag.BasicEdge(root, resource))
}
}
return graph, nil
}
// createCallback creates the internal callback that is called when a node in the
// dag is visited. This callback is responsible for processing the resource, setting
// any linked values and calling the user defined callback so that external work
// can be performed
func createCallback(c *Config, wf WalkCallback) func(v dag.Vertex) (diags dag.Diagnostics) {
return func(v dag.Vertex) (diags dag.Diagnostics) {
r, ok := v.(types.Resource)
// not a resource skip, this should never happen
if !ok {
panic("an item has been added to the graph that is not a resource")
}
// if this is the root module or is disabled skip or is a variable
if (r.Metadata().Type == resources.TypeRoot) || r.GetDisabled() {
return nil
}
bdy, err := c.getBody(r)
if err != nil {
panic(fmt.Sprintf(`no body found for resource "%s"`, r.Metadata().ID))
}
ctx, err := c.getContext(r)
if err != nil {
panic("no context found for resource")
}
// attempt to set the values in the resource links to the resource attribute
// all linked values should now have been processed as the graph
// will have handled them first
for _, v := range r.Metadata().Links {
fqrn, err := resources.ParseFQRN(v)
if err != nil {
pe := &errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Message = fmt.Sprintf("error parsing resource link %s", err)
pe.Level = errors.ParserErrorLevelError
return diags.Append(pe)
}
// get the value from the linked resource
l, err := c.FindRelativeResource(v, r.Metadata().Module)
if err != nil {
pe := &errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Message = fmt.Sprintf(`unable to find dependent resource "%s" %s`, v, err)
pe.Level = errors.ParserErrorLevelError
return diags.Append(pe)
}
var ctyRes cty.Value
// once we have found a resource convert it to a cty type and then
// set it on the context
switch l.Metadata().Type {
case resources.TypeLocal:
loc := l.(*resources.Local)
ctyRes = loc.CtyValue
case resources.TypeOutput:
out := l.(*resources.Output)
ctyRes = out.CtyValue
default:
ctyRes, err = convert.GoToCtyValue(l)
}
if err != nil {
pe := &errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Message = fmt.Sprintf(`unable to convert reference %s to context variable: %s`, v, err)
pe.Level = errors.ParserErrorLevelError
return diags.Append(pe)
}
// remove the attributes and to get a pure resource ref
fqrn.Attribute = ""
err = setContextVariableFromPath(ctx, fqrn.String(), ctyRes)
if err != nil {
pe := &errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Message = fmt.Sprintf(`unable to set context variable: %s`, err)
pe.Level = errors.ParserErrorLevelError
return diags.Append(pe)
}
}
// Process the raw resource now we have the context from the linked
// resources
ul := getContextLock(ctx)
defer ul()
diag := gohcl.DecodeBody(bdy, ctx, r)
if diag.HasErrors() {
pe := &errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Message = fmt.Sprintf(`unable to decode body: %s`, diag.Error())
// this error is set as warning as it is possible that the resource has
// interpolation that is not yet resolved
// check the error types and determine if we should set a warning or error
level := errors.ParserErrorLevelWarning
for _, e := range diag.Errs() {
err, ok := e.(*hcl.Diagnostic)
if !ok {
continue
}
if err.Summary == "Error in function call" {
level = errors.ParserErrorLevelError
break
}
}
pe.Level = level
return diags.Append(pe)
}
// if the type is a module the potentially we only just found out that we should be
// disabled
// as an additional check, set all module resources to disabled if the module is disabled
if r.GetDisabled() && r.Metadata().Type == resources.TypeModule {
// find all dependent resources
dr, err := c.FindModuleResources(r.Metadata().ID, true)
if err != nil {
// should not be here unless an internal error
pe := &errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Message = fmt.Sprintf(`unable to find disabled module resources "%s", %s"`, r.Metadata().ID, err)
pe.Level = errors.ParserErrorLevelError
return diags.Append(pe)
}
// set all the dependents to disabled
for _, d := range dr {
d.SetDisabled(true)
}
}
// if the type is a module we need to add the variables to the
// context
if r.Metadata().Type == resources.TypeModule {
mod := r.(*resources.Module)
var mapVars map[string]cty.Value
if att, ok := mod.Variables.(*hcl.Attribute); ok {
val, _ := att.Expr.Value(ctx)
mapVars = val.AsValueMap()
for k, v := range mapVars {
setContextVariable(mod.SubContext, k, v)
}
}
}
// if this is an output or local we need to convert the value into
// a go type
if r.Metadata().Type == resources.TypeOutput {
o := r.(*resources.Output)
if !o.CtyValue.IsNull() {
o.Value = castVar(o.CtyValue)
}
}
if r.Metadata().Type == resources.TypeLocal {
o := r.(*resources.Local)
if !o.CtyValue.IsNull() {
o.Value = castVar(o.CtyValue)
}
}
// if the config implements the processable interface call the resource process method
// and the resource is not disabled
//
// if disabled was set through interpolation, the value has only been set here
// we need to handle an additional check
if !r.GetDisabled() {
// call the callbacks
if wf != nil {
err := wf(r)
if err != nil {
pe := &errors.ParserError{}
pe.Filename = r.Metadata().File
pe.Line = r.Metadata().Line
pe.Column = r.Metadata().Column
pe.Message = fmt.Sprintf(`unable to create resource "%s": %s`, r.Metadata().ID, err)
pe.Level = errors.ParserErrorLevelError
return diags.Append(pe)
}
}
}
return nil
}
}