-
Notifications
You must be signed in to change notification settings - Fork 265
/
dashboard.go
471 lines (406 loc) · 12.6 KB
/
dashboard.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package modconfig
import (
"fmt"
typehelpers "github.com/turbot/go-kit/types"
"github.com/zclconf/go-cty/cty"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/stevenle/topsort"
"github.com/turbot/steampipe/pkg/utils"
)
const rootRuntimeDependencyNode = "rootRuntimeDependencyNode"
const RuntimeDependencyDashboardScope = "self"
// Dashboard is a struct representing the Dashboard resource
type Dashboard struct {
ResourceWithMetadataImpl
ModTreeItemImpl
WithProviderImpl
// required to allow partial decoding
Remain hcl.Body `hcl:",remain" json:"-"`
Width *int `cty:"width" hcl:"width" column:"width,text"`
Display *string `cty:"display" hcl:"display" column:"display,text"`
Inputs []*DashboardInput `cty:"inputs" column:"inputs,jsonb"`
UrlPath string `cty:"url_path" column:"url_path,jsonb"`
Base *Dashboard `hcl:"base"`
// store children in a way which can be serialised via cty
ChildNames []string `cty:"children" column:"children,jsonb"`
// map of all inputs in our resource tree
selfInputsMap map[string]*DashboardInput
runtimeDependencyGraph *topsort.Graph
}
func NewDashboard(block *hcl.Block, mod *Mod, shortName string) HclResource {
fullName := fmt.Sprintf("%s.%s.%s", mod.ShortName, block.Type, shortName)
c := &Dashboard{
ModTreeItemImpl: ModTreeItemImpl{
HclResourceImpl: HclResourceImpl{
ShortName: shortName,
FullName: fullName,
UnqualifiedName: fmt.Sprintf("%s.%s", block.Type, shortName),
DeclRange: block.DefRange,
blockType: block.Type,
},
Mod: mod,
},
}
c.SetAnonymous(block)
c.setUrlPath()
return c
}
// NewQueryDashboard creates a dashboard to wrap a query/control
// this is used for snapshot generation
func NewQueryDashboard(qp QueryProvider) (*Dashboard, error) {
parsedName, title, err := getQueryDashboardName(qp)
if err != nil {
return nil, err
}
// for query dashboard use generated title, for control use original title
if qp.BlockType() != BlockTypeQuery {
title = qp.GetTitle()
}
var dashboard = &Dashboard{
ResourceWithMetadataImpl: ResourceWithMetadataImpl{
metadata: &ResourceMetadata{},
},
ModTreeItemImpl: ModTreeItemImpl{
HclResourceImpl: HclResourceImpl{
ShortName: parsedName.Name,
FullName: parsedName.ToFullName(),
UnqualifiedName: parsedName.ToResourceName(),
Title: utils.ToStringPointer(title),
Description: utils.ToStringPointer(qp.GetDescription()),
Documentation: utils.ToStringPointer(qp.GetDocumentation()),
Tags: qp.GetTags(),
blockType: BlockTypeDashboard,
},
Mod: qp.GetMod(),
},
}
dashboard.setUrlPath()
table, err := NewQueryDashboardTable(qp)
if err != nil {
return nil, err
}
dashboard.children = []ModTreeItem{table}
return dashboard, nil
}
func getQueryDashboardName(qp QueryProvider) (*ParsedResourceName, string, error) {
var sql string
if q := qp.GetQuery(); q != nil {
sql = typehelpers.SafeString(q.GetSQL())
} else {
sql = typehelpers.SafeString(qp.GetSQL())
}
hash, err := utils.Base36Hash(sql, 8)
if err != nil {
return nil, "", err
}
dashboardName := fmt.Sprintf("custom.dashboard.sql_%s", hash)
parsed, err := ParseResourceName(dashboardName)
if err != nil {
return nil, "", err
}
title := getQueryDashboardTitle(hash)
return parsed, title, nil
}
func getQueryDashboardTitle(queryHash string) string {
return fmt.Sprintf("Custom Query [%s]", queryHash)
}
func (d *Dashboard) setUrlPath() {
d.UrlPath = fmt.Sprintf("/%s", d.FullName)
}
func (d *Dashboard) Equals(other *Dashboard) bool {
diff := d.Diff(other)
return !diff.HasChanges()
}
// OnDecoded implements HclResource
func (d *Dashboard) OnDecoded(block *hcl.Block, resourceMapProvider ResourceMapsProvider) hcl.Diagnostics {
d.setBaseProperties()
d.ChildNames = make([]string, len(d.children))
for i, child := range d.children {
d.ChildNames[i] = child.Name()
}
return nil
}
// GetWidth implements DashboardLeafNode
func (d *Dashboard) GetWidth() int {
if d.Width == nil {
return 0
}
return *d.Width
}
// GetDisplay implements DashboardLeafNode
func (d *Dashboard) GetDisplay() string {
return typehelpers.SafeString(d.Display)
}
// GetType implements DashboardLeafNode
func (d *Dashboard) GetType() string {
return ""
}
func (d *Dashboard) Diff(other *Dashboard) *DashboardTreeItemDiffs {
res := &DashboardTreeItemDiffs{
Item: d,
Name: d.Name(),
}
if !utils.SafeStringsEqual(d.FullName, other.FullName) {
res.AddPropertyDiff("Name")
}
if !utils.SafeStringsEqual(d.Title, other.Title) {
res.AddPropertyDiff("Title")
}
if !utils.SafeIntEqual(d.Width, other.Width) {
res.AddPropertyDiff("Width")
}
if len(d.Tags) != len(other.Tags) {
res.AddPropertyDiff("Tags")
} else {
for k, v := range d.Tags {
if otherVal := other.Tags[k]; v != otherVal {
res.AddPropertyDiff("Tags")
}
}
}
if !utils.SafeStringsEqual(d.Description, other.Description) {
res.AddPropertyDiff("Description")
}
if !utils.SafeStringsEqual(d.Documentation, other.Documentation) {
res.AddPropertyDiff("Documentation")
}
res.populateChildDiffs(d, other)
return res
}
func (d *Dashboard) SetChildren(children []ModTreeItem) {
d.children = children
}
func (d *Dashboard) AddChild(child ModTreeItem) {
d.children = append(d.children, child)
switch c := child.(type) {
case *DashboardInput:
d.Inputs = append(d.Inputs, c)
case *DashboardWith:
d.AddWith(c)
}
}
func (d *Dashboard) WalkResources(resourceFunc func(resource HclResource) (bool, error)) error {
for _, child := range d.children {
continueWalking, err := resourceFunc(child.(HclResource))
if err != nil {
return err
}
if !continueWalking {
break
}
if container, ok := child.(*DashboardContainer); ok {
if err := container.WalkResources(resourceFunc); err != nil {
return err
}
}
}
return nil
}
func (d *Dashboard) ValidateRuntimeDependencies(workspace ResourceMapsProvider) error {
d.runtimeDependencyGraph = topsort.NewGraph()
// add root node - this will depend on all other nodes
d.runtimeDependencyGraph.AddNode(rootRuntimeDependencyNode)
// define a walk function which determines whether the resource has runtime dependencies and if so,
// add to the graph
resourceFunc := func(resource HclResource) (bool, error) {
wp, ok := resource.(WithProvider)
if !ok {
// continue walking
return true, nil
}
if err := d.validateRuntimeDependenciesForResource(resource, workspace); err != nil {
return false, err
}
// if the query provider has any 'with' blocks, add these dependencies as well
for _, with := range wp.GetWiths() {
if err := d.validateRuntimeDependenciesForResource(with, workspace); err != nil {
return false, err
}
}
// continue walking
return true, nil
}
if err := d.WalkResources(resourceFunc); err != nil {
return err
}
// ensure that dependencies can be resolved
if _, err := d.runtimeDependencyGraph.TopSort(rootRuntimeDependencyNode); err != nil {
return fmt.Errorf("runtime depedencies cannot be resolved: %s", err.Error())
}
return nil
}
func (d *Dashboard) validateRuntimeDependenciesForResource(resource HclResource, workspace ResourceMapsProvider) error {
// TODO [node_reuse] re-add parse time validation https://github.com/turbot/steampipe/issues/2925
return nil
//rdp := resource.(RuntimeDependencyProvider)
//// WHAT ABOUT CHILDREN
//if len(runtimeDependencies) == 0 {
// return nil
//}
//name := resource.Name()
//if !d.runtimeDependencyGraph.ContainsNode(name) {
// d.runtimeDependencyGraph.AddNode(name)
//}
//
//for _, dependency := range runtimeDependencies {
// // try to resolve the dependency source resource
// if err := dependency.ValidateSource(d, workspace); err != nil {
// return err
// }
// if err := d.runtimeDependencyGraph.AddEdge(rootRuntimeDependencyNode, name); err != nil {
// return err
// }
// depString := dependency.String()
// if !d.runtimeDependencyGraph.ContainsNode(depString) {
// d.runtimeDependencyGraph.AddNode(depString)
// }
// if err := d.runtimeDependencyGraph.AddEdge(name, dependency.String()); err != nil {
// return err
// }
//}
//return nil
}
func (d *Dashboard) GetInput(name string) (*DashboardInput, bool) {
input, found := d.selfInputsMap[name]
return input, found
}
func (d *Dashboard) GetInputs() map[string]*DashboardInput {
return d.selfInputsMap
}
func (d *Dashboard) InitInputs() hcl.Diagnostics {
// add all our direct child inputs to a map
// (we must do this before adding child container inputs to detect dupes)
duplicates := d.setInputMap()
// add child containers and dashboard inputs
resourceFunc := func(resource HclResource) (bool, error) {
if container, ok := resource.(*DashboardContainer); ok {
for _, i := range container.Inputs {
// check we do not already have this input
if _, ok := d.selfInputsMap[i.UnqualifiedName]; ok {
duplicates = append(duplicates, i.Name())
}
d.Inputs = append(d.Inputs, i)
d.selfInputsMap[i.UnqualifiedName] = i
}
}
// continue walking
return true, nil
}
if err := d.WalkResources(resourceFunc); err != nil {
return hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Dashboard '%s' WalkResources failed", d.Name()),
Detail: err.Error(),
Subject: &d.DeclRange,
}}
}
if len(duplicates) > 0 {
return hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Dashboard '%s' contains duplicate input names for: %s", d.Name(), strings.Join(duplicates, ",")),
Subject: &d.DeclRange,
}}
}
var diags hcl.Diagnostics
// ensure they inputs not have cyclical dependencies
if err := d.validateInputDependencies(d.Inputs); err != nil {
return hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Failed to resolve input dependency order for dashboard '%s'", d.Name()),
Detail: err.Error(),
Subject: &d.DeclRange,
}}
}
// now 'claim' all inputs and add to mod
for _, input := range d.Inputs {
input.SetDashboard(d)
moreDiags := d.Mod.AddResource(input)
diags = append(diags, moreDiags...)
}
return diags
}
// populate our input map
func (d *Dashboard) setInputMap() []string {
var duplicates []string
d.selfInputsMap = make(map[string]*DashboardInput)
for _, i := range d.Inputs {
if _, ok := d.selfInputsMap[i.UnqualifiedName]; ok {
duplicates = append(duplicates, i.UnqualifiedName)
} else {
d.selfInputsMap[i.UnqualifiedName] = i
}
}
return duplicates
}
// CtyValue implements CtyValueProvider
func (d *Dashboard) CtyValue() (cty.Value, error) {
return GetCtyValue(d)
}
func (d *Dashboard) setBaseProperties() {
if d.Base == nil {
return
}
// copy base into the HclResourceImpl 'base' property so it is accessible to all nested structs
d.base = d.Base
// call into parent nested struct setBaseProperties
d.ModTreeItemImpl.setBaseProperties()
if d.Width == nil {
d.Width = d.Base.Width
}
if len(d.children) == 0 {
d.children = d.Base.children
d.ChildNames = d.Base.ChildNames
}
d.addBaseInputs(d.Base.Inputs)
}
func (d *Dashboard) addBaseInputs(baseInputs []*DashboardInput) {
if len(baseInputs) == 0 {
return
}
// rebuild Inputs and children
inheritedInputs := make([]*DashboardInput, len(baseInputs))
inheritedChildren := make([]ModTreeItem, len(baseInputs))
for i, baseInput := range baseInputs {
input := baseInput.Clone()
input.SetDashboard(d)
// add to mod
d.Mod.AddResource(input)
// add to our inputs
inheritedInputs[i] = input
inheritedChildren[i] = input
}
// add inputs to beginning of our existing inputs (if any)
d.Inputs = append(inheritedInputs, d.Inputs...)
// add inputs to beginning of our children
d.children = append(inheritedChildren, d.children...)
d.setInputMap()
}
// ensure that dependencies between inputs are resolveable
func (d *Dashboard) validateInputDependencies(inputs []*DashboardInput) error {
dependencyGraph := topsort.NewGraph()
rootDependencyNode := "dashboard"
dependencyGraph.AddNode(rootDependencyNode)
for _, i := range inputs {
for _, runtimeDep := range i.GetRuntimeDependencies() {
depName := runtimeDep.PropertyPath.ToResourceName()
to := depName
from := i.UnqualifiedName
if !dependencyGraph.ContainsNode(from) {
dependencyGraph.AddNode(from)
}
if !dependencyGraph.ContainsNode(to) {
dependencyGraph.AddNode(to)
}
if err := dependencyGraph.AddEdge(from, to); err != nil {
return err
}
if err := dependencyGraph.AddEdge(rootDependencyNode, i.UnqualifiedName); err != nil {
return err
}
}
}
// now verify we can get a dependency order
_, err := dependencyGraph.TopSort(rootDependencyNode)
return err
}