-
Notifications
You must be signed in to change notification settings - Fork 256
/
referenced_variables.go
66 lines (60 loc) · 2.29 KB
/
referenced_variables.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
package dashboardexecute
import (
"fmt"
"strings"
"github.com/turbot/steampipe/pkg/dashboard/dashboardtypes"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
"github.com/turbot/steampipe/pkg/workspace"
)
// GetReferencedVariables builds map of variables values containing only those mod variables which are referenced
// NOTE: we refer to variables in dependency mods in the format which is valid for an SPVARS filer, i.e.
// <mod>.<var-name>
// the VariableValues map will contain these variables with the name format <mod>.var.<var-name>,
// so we must convert the name
func GetReferencedVariables(root dashboardtypes.DashboardTreeRun, w *workspace.Workspace) map[string]string {
var referencedVariables = make(map[string]string)
addReferencedVars := func(refs []*modconfig.ResourceReference) {
for _, ref := range refs {
parts := strings.Split(ref.To, ".")
if len(parts) == 2 && parts[0] == "var" {
varName := parts[1]
varValueName := varName
// NOTE: if the ref is NOT for the workspace mod, then use the qualified variable name
// (e.g. aws_insights.var.v1)
if refMod := ref.GetMetadata().ModName; refMod != w.Mod.ShortName {
varValueName = fmt.Sprintf("%s.var.%s", refMod, varName)
varName = fmt.Sprintf("%s.%s", refMod, varName)
}
referencedVariables[varName] = w.VariableValues[varValueName]
}
}
}
switch r := root.(type) {
case *DashboardRun:
//nolint:errcheck // we don't care about errors here, since the callback does not return an error
r.dashboard.WalkResources(
func(resource modconfig.HclResource) (bool, error) {
if resourceWithMetadata, ok := resource.(modconfig.ResourceWithMetadata); ok {
addReferencedVars(resourceWithMetadata.GetReferences())
}
return true, nil
},
)
case *CheckRun:
switch n := r.resource.(type) {
case *modconfig.Benchmark:
//nolint:errcheck // we don't care about errors here, since the callback does not return an error
n.WalkResources(
func(resource modconfig.ModTreeItem) (bool, error) {
if resourceWithMetadata, ok := resource.(modconfig.ResourceWithMetadata); ok {
addReferencedVars(resourceWithMetadata.GetReferences())
}
return true, nil
},
)
case *modconfig.Control:
addReferencedVars(n.GetReferences())
}
}
return referencedVariables
}