-
Notifications
You must be signed in to change notification settings - Fork 3
/
dashboard_input.go
207 lines (173 loc) · 5.94 KB
/
dashboard_input.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
package modconfig
import (
"github.com/hashicorp/hcl/v2"
typehelpers "github.com/turbot/go-kit/types"
"github.com/turbot/pipe-fittings/printers"
"github.com/turbot/pipe-fittings/utils"
"github.com/zclconf/go-cty/cty"
)
// DashboardInput is a struct representing a leaf dashboard node
type DashboardInput struct {
ResourceWithMetadataImpl
QueryProviderImpl
// required to allow partial decoding
Remain hcl.Body `hcl:",remain" json:"-"`
DashboardName string `column:"dashboard,string" json:"dashboard,omitempty"`
Label *string `cty:"label" hcl:"label" column:"label,string" json:"label,omitempty" snapshot:"label"`
Placeholder *string `cty:"placeholder" hcl:"placeholder" column:"placeholder,string" json:"placeholder,omitempty" snapshot:"placeholder"`
Options []*DashboardInputOption `cty:"options" hcl:"option,block" json:"options,omitempty" snapshot:"options"`
// tactical - exists purely so we can put "unqualified_name" in the snbapshot panel for the input
// TODO remove when input names are refactored https://github.com/turbot/steampipe/issues/2863
InputName string `cty:"input_name" json:"unqualified_name" snapshot:"unqualified_name"`
// these properties are JSON serialised by the parent LeafRun
Width *int `cty:"width" hcl:"width" column:"width,string" json:"width,omitempty"`
Type *string `cty:"type" hcl:"type" column:"type,string" json:"type,omitempty"`
Display *string `cty:"display" hcl:"display" json:"display,omitempty"`
Base *DashboardInput `hcl:"base" json:"-"`
dashboard *Dashboard
}
func NewDashboardInput(block *hcl.Block, mod *Mod, shortName string) HclResource {
// input cannot be anonymous
i := &DashboardInput{
QueryProviderImpl: NewQueryProviderImpl(block, mod, shortName),
}
// tactical set input name
i.InputName = i.UnqualifiedName
return i
}
// TODO remove https://github.com/turbot/steampipe/issues/2864
func (i *DashboardInput) Clone() *DashboardInput {
return &DashboardInput{
ResourceWithMetadataImpl: i.ResourceWithMetadataImpl,
QueryProviderImpl: i.QueryProviderImpl,
Width: i.Width,
Type: i.Type,
Label: i.Label,
Placeholder: i.Placeholder,
Display: i.Display,
Options: i.Options,
InputName: i.InputName,
dashboard: i.dashboard,
}
}
func (i *DashboardInput) Equals(other *DashboardInput) bool {
diff := i.Diff(other)
return !diff.HasChanges()
}
// OnDecoded implements HclResource
func (i *DashboardInput) OnDecoded(block *hcl.Block, resourceMapProvider ResourceMapsProvider) hcl.Diagnostics {
i.setBaseProperties()
return i.QueryProviderImpl.OnDecoded(block, resourceMapProvider)
}
func (i *DashboardInput) Diff(other *DashboardInput) *DashboardTreeItemDiffs {
res := &DashboardTreeItemDiffs{
Item: i,
Name: i.Name(),
}
if !utils.SafeStringsEqual(i.Type, other.Type) {
res.AddPropertyDiff("Type")
}
if !utils.SafeStringsEqual(i.Label, other.Label) {
res.AddPropertyDiff("Instance")
}
if !utils.SafeStringsEqual(i.Placeholder, other.Placeholder) {
res.AddPropertyDiff("Placeholder")
}
if len(i.Options) != len(other.Options) {
res.AddPropertyDiff("Options")
} else {
for idx, o := range i.Options {
if !other.Options[idx].Equals(o) {
res.AddPropertyDiff("Options")
}
}
}
res.populateChildDiffs(i, other)
res.queryProviderDiff(i, other)
res.dashboardLeafNodeDiff(i, other)
return res
}
// GetWidth implements DashboardLeafNode
func (i *DashboardInput) GetWidth() int {
if i.Width == nil {
return 0
}
return *i.Width
}
// GetDisplay implements DashboardLeafNode
func (i *DashboardInput) GetDisplay() string {
return typehelpers.SafeString(i.Display)
}
// GetType implements DashboardLeafNode
func (i *DashboardInput) GetType() string {
return typehelpers.SafeString(i.Type)
}
// SetDashboard sets the parent dashboard container
func (i *DashboardInput) SetDashboard(dashboard *Dashboard) {
i.dashboard = dashboard
i.DashboardName = dashboard.Name()
}
// ValidateQuery implements QueryProvider
func (i *DashboardInput) ValidateQuery() hcl.Diagnostics {
// inputs with placeholder or options, or text type do not need a query
if i.Placeholder != nil ||
len(i.Options) > 0 ||
typehelpers.SafeString(i.Type) == "text" {
return nil
}
return i.QueryProviderImpl.ValidateQuery()
}
// DependsOnInput returns whether this input has a runtime dependency on the given input¬
func (i *DashboardInput) DependsOnInput(changedInputName string) bool {
for _, r := range i.runtimeDependencies {
if r.SourceResourceName() == changedInputName {
return true
}
}
return false
}
// CtyValue implements CtyValueProvider
func (i *DashboardInput) CtyValue() (cty.Value, error) {
return GetCtyValue(i)
}
func (i *DashboardInput) setBaseProperties() {
if i.Base == nil {
return
}
// copy base into the HclResourceImpl 'base' property so it is accessible to all nested structs
i.base = i.Base
// call into parent nested struct setBaseProperties
i.QueryProviderImpl.setBaseProperties()
if i.Type == nil {
i.Type = i.Base.Type
}
if i.Display == nil {
i.Display = i.Base.Display
}
if i.Label == nil {
i.Label = i.Base.Label
}
if i.Placeholder == nil {
i.Placeholder = i.Base.Placeholder
}
if i.Width == nil {
i.Width = i.Base.Width
}
if i.Options == nil {
i.Options = i.Base.Options
}
}
// GetShowData implements printers.Showable
func (i *DashboardInput) GetShowData() *printers.RowData {
res := printers.NewRowData(
printers.NewFieldValue("Width", i.Width),
printers.NewFieldValue("Type", i.Type),
printers.NewFieldValue("Display", i.Display),
printers.NewFieldValue("Label", i.Label),
printers.NewFieldValue("Placeholder", i.Placeholder),
printers.NewFieldValue("DashboardName", i.DashboardName),
)
// merge fields from base, putting base fields first
res.Merge(i.QueryProviderImpl.GetShowData())
return res
}