Skip to content

Commit 5198379

Browse files
fix(dashboards) prevent false drift with optional legend and query format (#449)
1 parent 9211e8e commit 5198379

File tree

3 files changed

+88
-36
lines changed

3 files changed

+88
-36
lines changed

sysdig/internal/client/v2/model_dashboard.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func NewFormat(
120120
}
121121
}
122122

123-
func newPercentFormat() Format {
123+
func NewPercentFormat() Format {
124124
return *NewFormat(
125125
FormatUnitPercentage,
126126
"0-100",
@@ -132,7 +132,7 @@ func newPercentFormat() Format {
132132
)
133133
}
134134

135-
func newDataFormat() Format {
135+
func NewDataFormat() Format {
136136
return *NewFormat(
137137
FormatUnitData,
138138
"B",
@@ -144,7 +144,7 @@ func newDataFormat() Format {
144144
)
145145
}
146146

147-
func newDataRateFormat() Format {
147+
func NewDataRateFormat() Format {
148148
return *NewFormat(
149149
FormatUnitDataRate,
150150
"B/s",
@@ -156,7 +156,7 @@ func newDataRateFormat() Format {
156156
)
157157
}
158158

159-
func newNumberFormat() Format {
159+
func NewNumberFormat() Format {
160160
return *NewFormat(
161161
FormatUnitNumber,
162162
"1",
@@ -168,7 +168,7 @@ func newNumberFormat() Format {
168168
)
169169
}
170170

171-
func newNumberRateFormat() Format {
171+
func NewNumberRateFormat() Format {
172172
return *NewFormat(
173173
FormatUnitNumberRate,
174174
"/s",
@@ -180,7 +180,7 @@ func newNumberRateFormat() Format {
180180
)
181181
}
182182

183-
func newTimeFormat() Format {
183+
func NewTimeFormat() Format {
184184
return *NewFormat(
185185
FormatUnitTime,
186186
"ns",
@@ -209,7 +209,7 @@ func NewPromqlQuery(query string, parentPanel *Panels, displayInfo DisplayInfo)
209209
TimeSeriesDisplayNameTemplate: displayInfo.TimeSeriesDisplayNameTemplate,
210210
Type: displayInfo.Type,
211211
},
212-
Format: newPercentFormat(),
212+
Format: NewPercentFormat(),
213213
Query: query,
214214
ID: 0,
215215
ParentPanel: parentPanel,
@@ -263,37 +263,37 @@ func (q *AdvancedQueries) updateFormat(f *Format) {
263263
}
264264

265265
func (q *AdvancedQueries) WithPercentFormat(f *Format) *AdvancedQueries {
266-
q.Format = newPercentFormat()
266+
q.Format = NewPercentFormat()
267267
q.updateFormat(f)
268268
return q
269269
}
270270

271271
func (q *AdvancedQueries) WithDataFormat(f *Format) *AdvancedQueries {
272-
q.Format = newDataFormat()
272+
q.Format = NewDataFormat()
273273
q.updateFormat(f)
274274
return q
275275
}
276276

277277
func (q *AdvancedQueries) WithDataRateFormat(f *Format) *AdvancedQueries {
278-
q.Format = newDataRateFormat()
278+
q.Format = NewDataRateFormat()
279279
q.updateFormat(f)
280280
return q
281281
}
282282

283283
func (q *AdvancedQueries) WithNumberFormat(f *Format) *AdvancedQueries {
284-
q.Format = newNumberFormat()
284+
q.Format = NewNumberFormat()
285285
q.updateFormat(f)
286286
return q
287287
}
288288

289289
func (q *AdvancedQueries) WithNumberRateFormat(f *Format) *AdvancedQueries {
290-
q.Format = newNumberRateFormat()
290+
q.Format = NewNumberRateFormat()
291291
q.updateFormat(f)
292292
return q
293293
}
294294

295295
func (q *AdvancedQueries) WithTimeFormat(f *Format) *AdvancedQueries {
296-
q.Format = newTimeFormat()
296+
q.Format = NewTimeFormat()
297297
q.updateFormat(f)
298298
return q
299299
}

sysdig/resource_sysdig_monitor_dashboard.go

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"reflect"
78
"strconv"
89
"time"
910

@@ -450,20 +451,24 @@ func panelsFromResourceData(data *schema.ResourceData) (panels []*v2.Panels, err
450451
return
451452
}
452453

453-
func legendFromResourceData(data interface{}) *v2.LegendConfiguration {
454-
defaultLegend := &v2.LegendConfiguration{
455-
Position: "bottom",
456-
Layout: "table",
454+
func defaultLegendConfiguration() *v2.LegendConfiguration {
455+
return &v2.LegendConfiguration{
456+
Enabled: false,
457+
Position: "bottom",
458+
Layout: "table",
459+
ShowCurrent: false,
457460
}
461+
}
458462

463+
func legendFromResourceData(data interface{}) *v2.LegendConfiguration {
459464
if data == nil {
460-
return defaultLegend
465+
return defaultLegendConfiguration()
461466
}
462467

463468
legendList := data.(*schema.Set).List()
464469

465470
if len(legendList) == 0 {
466-
return defaultLegend
471+
return defaultLegendConfiguration()
467472
}
468473

469474
legend := legendList[0].(map[string]interface{})
@@ -727,8 +732,14 @@ func dashboardToResourceData(dashboard *v2.Dashboard, data *schema.ResourceData)
727732
_ = data.Set("public_token", dashboard.PublicToken)
728733

729734
var panels []map[string]interface{}
730-
for _, panel := range dashboard.Panels {
731-
dPanel, err := panelToResourceData(panel, dashboard.Layout)
735+
for i, panel := range dashboard.Panels {
736+
panelsData := data.Get("panel").(*schema.Set).List()
737+
panelData := map[string]interface{}{}
738+
if len(panelsData) > i {
739+
panelData = panelsData[i].(map[string]interface{})
740+
}
741+
742+
dPanel, err := panelToResourceData(panel, dashboard.Layout, panelData)
732743
if err != nil {
733744
return err
734745
}
@@ -824,7 +835,7 @@ func scopeFromResourceData(data *schema.ResourceData) ([]*v2.ScopeExpressionList
824835
return scopes, nil
825836
}
826837

827-
func panelToResourceData(panel *v2.Panels, layout []*v2.Layout) (map[string]interface{}, error) {
838+
func panelToResourceData(panel *v2.Panels, layout []*v2.Layout, panelData map[string]interface{}) (map[string]interface{}, error) {
828839
var panelLayout *v2.Layout
829840

830841
for _, l := range layout {
@@ -838,18 +849,18 @@ func panelToResourceData(panel *v2.Panels, layout []*v2.Layout) (map[string]inte
838849

839850
switch panel.Type {
840851
case v2.PanelTypeTimechart:
841-
return timechartPanelToResourceData(panel, panelLayout)
852+
return timechartPanelToResourceData(panel, panelLayout, panelData)
842853
case v2.PanelTypeNumber:
843-
return numberPanelToResourceData(panel, panelLayout)
854+
return numberPanelToResourceData(panel, panelLayout, panelData)
844855
case v2.PanelTypeText:
845856
return textPanelToResourceData(panel, panelLayout)
846857
default:
847858
return nil, fmt.Errorf("unsupported panel type %s", panel.Type)
848859
}
849860
}
850861

851-
func timechartPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout) (map[string]interface{}, error) {
852-
queries, err := queriesToResourceData(panel.AdvancedQueries)
862+
func timechartPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout, panelData map[string]interface{}) (map[string]interface{}, error) {
863+
queries, err := queriesToResourceData(panel.AdvancedQueries, panelData)
853864
if err != nil {
854865
return nil, err
855866
}
@@ -863,23 +874,31 @@ func timechartPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout) (map
863874
"description": panel.Description,
864875
"type": "timechart",
865876
"query": queries,
866-
"legend": []map[string]interface{}{
867-
legendConfigurationToResourceData(panel.LegendConfiguration),
868-
},
877+
"legend": legendConfigurationToResourceData(panel.LegendConfiguration, panelData),
869878
}, nil
870879
}
871880

872-
func legendConfigurationToResourceData(legend *v2.LegendConfiguration) map[string]interface{} {
873-
return map[string]interface{}{
881+
func legendConfigurationToResourceData(legend *v2.LegendConfiguration, panelData map[string]interface{}) []map[string]interface{} {
882+
883+
legendData := panelData["legend"]
884+
// If legend is not defined in the user configuration and the dashboard legend is the same as the default one
885+
// we don't set the legend in the resource data to avoid drifts
886+
if legendData == nil || len(legendData.(*schema.Set).List()) == 0 {
887+
if *legend == *defaultLegendConfiguration() {
888+
return nil
889+
}
890+
}
891+
892+
return []map[string]interface{}{{
874893
"enabled": legend.Enabled,
875894
"show_current": legend.ShowCurrent,
876895
"position": legend.Position,
877896
"layout": legend.Layout,
878-
}
897+
}}
879898
}
880899

881-
func numberPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout) (map[string]interface{}, error) {
882-
queries, err := queriesToResourceData(panel.AdvancedQueries)
900+
func numberPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout, panelData map[string]interface{}) (map[string]interface{}, error) {
901+
queries, err := queriesToResourceData(panel.AdvancedQueries, panelData)
883902
if err != nil {
884903
return nil, err
885904
}
@@ -911,23 +930,30 @@ func textPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout) (map[stri
911930
}, nil
912931
}
913932

914-
func queriesToResourceData(advancedQueries []*v2.AdvancedQueries) ([]map[string]interface{}, error) {
933+
func queriesToResourceData(advancedQueries []*v2.AdvancedQueries, panelsData map[string]interface{}) ([]map[string]interface{}, error) {
915934
var queries []map[string]interface{}
916-
for _, query := range advancedQueries {
935+
for queryIndex, query := range advancedQueries {
917936
unit := ""
937+
var defaultFormat v2.Format
918938
switch query.Format.Unit {
919939
case v2.FormatUnitPercentage:
920940
unit = "percent"
941+
defaultFormat = v2.NewPercentFormat()
921942
case v2.FormatUnitData:
922943
unit = "data"
944+
defaultFormat = v2.NewDataFormat()
923945
case v2.FormatUnitDataRate:
924946
unit = "data rate"
947+
defaultFormat = v2.NewDataRateFormat()
925948
case v2.FormatUnitNumber:
926949
unit = "number"
950+
defaultFormat = v2.NewNumberFormat()
927951
case v2.FormatUnitNumberRate:
928952
unit = "number rate"
953+
defaultFormat = v2.NewNumberRateFormat()
929954
case v2.FormatUnitTime:
930955
unit = "time"
956+
defaultFormat = v2.NewTimeFormat()
931957
default:
932958
return nil, fmt.Errorf("unsupported query format unit: %s", query.Format.Unit)
933959
}
@@ -954,6 +980,21 @@ func queriesToResourceData(advancedQueries []*v2.AdvancedQueries) ([]map[string]
954980
"y_axis": query.Format.YAxis,
955981
}}
956982

983+
queriesData := panelsData["query"]
984+
queryData := map[string]interface{}{}
985+
if queriesData != nil && queriesData.(*schema.Set).Len() > queryIndex {
986+
queryData = queriesData.(*schema.Set).List()[queryIndex].(map[string]interface{})
987+
}
988+
formatData := queryData["format"]
989+
990+
// If format is not defined in the user configuration and the dashboard format is the same as the default one
991+
// we don't set the format in the resource data to avoid drifts
992+
if formatData == nil || formatData.(*schema.Set).Len() == 0 {
993+
if reflect.DeepEqual(query.Format, defaultFormat) {
994+
q["format"] = nil
995+
}
996+
}
997+
957998
queries = append(queries, q)
958999
}
9591000
return queries, nil

sysdig/resource_sysdig_monitor_dashboard_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ resource "sysdig_monitor_dashboard" "dashboard_2" {
384384
unit = "percent"
385385
386386
format {
387+
decimals= 2
387388
display_format = "auto"
388389
input_format = "0-100"
389390
y_axis = "auto"
@@ -439,6 +440,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
439440
unit = "percent"
440441
441442
format {
443+
decimals= 2
442444
display_format = "auto"
443445
input_format = "0-100"
444446
y_axis = "auto"
@@ -450,6 +452,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
450452
unit = "number"
451453
452454
format {
455+
decimals= 2
453456
display_format = "auto"
454457
input_format = "1"
455458
y_axis = "auto"
@@ -472,6 +475,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
472475
unit = "time"
473476
474477
format {
478+
decimals= 2
475479
display_format = "auto"
476480
input_format = "ns"
477481
y_axis = "auto"
@@ -522,6 +526,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
522526
unit = "percent"
523527
524528
format {
529+
decimals= 2
525530
display_format = "auto"
526531
input_format = "0-100"
527532
y_axis = "auto"
@@ -533,6 +538,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
533538
unit = "number"
534539
535540
format {
541+
decimals= 2
536542
display_format = "auto"
537543
input_format = "1"
538544
y_axis = "auto"
@@ -607,6 +613,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
607613
unit = "percent"
608614
609615
format {
616+
decimals= 2
610617
display_format = "auto"
611618
input_format = "0-100"
612619
y_axis = "auto"
@@ -674,6 +681,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
674681
}
675682
676683
format {
684+
decimals= 2
677685
display_format = "auto"
678686
input_format = "0-100"
679687
y_axis = "auto"
@@ -689,6 +697,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
689697
}
690698
691699
format {
700+
decimals= 2
692701
display_format = "auto"
693702
input_format = "1"
694703
y_axis = "auto"
@@ -711,6 +720,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
711720
unit = "time"
712721
713722
format {
723+
decimals= 2
714724
display_format = "auto"
715725
input_format = "ns"
716726
y_axis = "auto"
@@ -785,6 +795,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
785795
}
786796
787797
format {
798+
decimals= 2
788799
display_format = "auto"
789800
input_format = "0-100"
790801
y_axis = "auto"

0 commit comments

Comments
 (0)