Skip to content

Commit f7e091b

Browse files
authored
feat(dashboard) Expose legend configuration on panel (#256)
* feat(dashboard): expose legend configuration for timechart panel * feat(dashboard): minor docs change
1 parent 58e16c7 commit f7e091b

File tree

3 files changed

+234
-8
lines changed

3 files changed

+234
-8
lines changed

sysdig/resource_sysdig_monitor_dashboard.go

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,32 @@ func resourceSysdigMonitorDashboard() *schema.Resource {
208208
},
209209
},
210210
},
211+
"legend": {
212+
Type: schema.TypeSet,
213+
Optional: true,
214+
MaxItems: 1,
215+
Elem: &schema.Resource{
216+
Schema: map[string]*schema.Schema{
217+
"enabled": {
218+
Type: schema.TypeBool,
219+
Optional: true,
220+
Default: true,
221+
},
222+
"show_current": {
223+
Type: schema.TypeBool,
224+
Required: true,
225+
},
226+
"position": {
227+
Type: schema.TypeString,
228+
Required: true,
229+
},
230+
"layout": {
231+
Type: schema.TypeString,
232+
Required: true,
233+
},
234+
},
235+
},
236+
},
211237
},
212238
},
213239
},
@@ -373,6 +399,31 @@ func panelsFromResourceData(data *schema.ResourceData) (panels []*model.Panels,
373399
return
374400
}
375401

402+
func legendFromResourceData(data interface{}) *model.LegendConfiguration {
403+
defaultLegend := &model.LegendConfiguration{
404+
Position: "bottom",
405+
Layout: "table",
406+
}
407+
408+
if data == nil {
409+
return defaultLegend
410+
}
411+
412+
legendList := data.(*schema.Set).List()
413+
414+
if len(legendList) == 0 {
415+
return defaultLegend
416+
}
417+
418+
legend := legendList[0].(map[string]interface{})
419+
return &model.LegendConfiguration{
420+
Enabled: legend["enabled"].(bool),
421+
Position: legend["position"].(string),
422+
Layout: legend["layout"].(string),
423+
ShowCurrent: legend["show_current"].(bool),
424+
}
425+
}
426+
376427
func timechartPanelFromResourceData(panelInfo map[string]interface{}) (*model.Panels, error) {
377428
panel := &model.Panels{
378429
ID: 0,
@@ -408,14 +459,7 @@ func timechartPanelFromResourceData(panelInfo map[string]interface{}) (*model.Pa
408459
Scale: "linear",
409460
},
410461
},
411-
LegendConfiguration: &model.LegendConfiguration{
412-
Enabled: true,
413-
Position: "right",
414-
Layout: "table",
415-
ShowCurrent: true,
416-
Width: nil,
417-
Height: nil,
418-
},
462+
LegendConfiguration: legendFromResourceData(panelInfo["legend"]),
419463
MarkdownSource: nil,
420464
PanelTitleVisible: false,
421465
TextAutosized: false,
@@ -724,9 +768,21 @@ func timechartPanelToResourceData(panel *model.Panels, panelLayout *model.Layout
724768
"description": panel.Description,
725769
"type": "timechart",
726770
"query": queries,
771+
"legend": []map[string]interface{}{
772+
legendConfigurationToResourceData(panel.LegendConfiguration),
773+
},
727774
}, nil
728775
}
729776

777+
func legendConfigurationToResourceData(legend *model.LegendConfiguration) map[string]interface{} {
778+
return map[string]interface{}{
779+
"enabled": legend.Enabled,
780+
"show_current": legend.ShowCurrent,
781+
"position": legend.Position,
782+
"layout": legend.Layout,
783+
}
784+
}
785+
730786
func numberPanelToResourceData(panel *model.Panels, panelLayout *model.Layout) (map[string]interface{}, error) {
731787
queries, err := queriesToResourceData(panel.AdvancedQueries)
732788
if err != nil {

sysdig/resource_sysdig_monitor_dashboard_test.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,68 @@ func TestAccDashboard(t *testing.T) {
5858
{
5959
Config: multiplePanelsDashboardWithDisplayInfo(rText()),
6060
},
61+
{
62+
Config: timeChartDashboardWithLegend(
63+
rText(),
64+
"true",
65+
"true",
66+
"bottom",
67+
"inline",
68+
),
69+
Check: resource.ComposeTestCheckFunc(
70+
resource.TestCheckResourceAttr(
71+
"sysdig_monitor_dashboard.dashboard",
72+
"panel.0.legend.0.enabled",
73+
"true",
74+
),
75+
resource.TestCheckResourceAttr(
76+
"sysdig_monitor_dashboard.dashboard",
77+
"panel.0.legend.0.show_current",
78+
"true",
79+
),
80+
resource.TestCheckResourceAttr(
81+
"sysdig_monitor_dashboard.dashboard",
82+
"panel.0.legend.0.layout",
83+
"inline",
84+
),
85+
resource.TestCheckResourceAttr(
86+
"sysdig_monitor_dashboard.dashboard",
87+
"panel.0.legend.0.position",
88+
"bottom",
89+
),
90+
),
91+
},
92+
{
93+
Config: timeChartDashboardWithLegend(
94+
rText(),
95+
"false",
96+
"false",
97+
"right",
98+
"table",
99+
),
100+
Check: resource.ComposeTestCheckFunc(
101+
resource.TestCheckResourceAttr(
102+
"sysdig_monitor_dashboard.dashboard",
103+
"panel.0.legend.0.enabled",
104+
"false",
105+
),
106+
resource.TestCheckResourceAttr(
107+
"sysdig_monitor_dashboard.dashboard",
108+
"panel.0.legend.0.show_current",
109+
"false",
110+
),
111+
resource.TestCheckResourceAttr(
112+
"sysdig_monitor_dashboard.dashboard",
113+
"panel.0.legend.0.layout",
114+
"table",
115+
),
116+
resource.TestCheckResourceAttr(
117+
"sysdig_monitor_dashboard.dashboard",
118+
"panel.0.legend.0.position",
119+
"right",
120+
),
121+
),
122+
},
61123
},
62124
})
63125
}
@@ -77,6 +139,12 @@ resource "sysdig_monitor_dashboard" "dashboard" {
77139
name = "example panel"
78140
description = "description"
79141
142+
legend {
143+
show_current = true
144+
position = "bottom"
145+
layout = "inline"
146+
}
147+
80148
query {
81149
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
82150
unit = "percent"
@@ -143,6 +211,12 @@ resource "sysdig_monitor_dashboard" "dashboard" {
143211
name = "example panel"
144212
description = "description"
145213
214+
legend {
215+
show_current = true
216+
position = "bottom"
217+
layout = "inline"
218+
}
219+
146220
query {
147221
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
148222
unit = "percent"
@@ -199,6 +273,12 @@ resource "sysdig_monitor_dashboard" "dashboard" {
199273
name = "example panel"
200274
description = "description"
201275
276+
legend {
277+
show_current = true
278+
position = "bottom"
279+
layout = "inline"
280+
}
281+
202282
query {
203283
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
204284
unit = "percent"
@@ -312,6 +392,12 @@ resource "sysdig_monitor_dashboard" "dashboard" {
312392
name = "example panel"
313393
description = "description"
314394
395+
legend {
396+
show_current = true
397+
position = "bottom"
398+
layout = "inline"
399+
}
400+
315401
query {
316402
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
317403
unit = "percent"
@@ -361,3 +447,65 @@ resource "sysdig_monitor_dashboard" "dashboard" {
361447
}
362448
`, name, name)
363449
}
450+
451+
func timeChartDashboardWithLegend(name, enabled, showCurrent, position, layout string) string {
452+
return fmt.Sprintf(`
453+
resource "sysdig_monitor_dashboard" "dashboard" {
454+
name = "TERRAFORM TEST - METRIC %[1]s"
455+
description = "TERRAFORM TEST - METRIC %[1]s"
456+
457+
scope {
458+
metric = "agent.id"
459+
comparator = "in"
460+
value = ["foo", "bar"]
461+
variable = "agent_id"
462+
}
463+
464+
scope {
465+
metric = "agent.name"
466+
comparator = "equals"
467+
value = ["name"]
468+
}
469+
470+
scope {
471+
metric = "kubernetes.namespace.name"
472+
variable = "k8_ns"
473+
}
474+
475+
panel {
476+
pos_x = 0
477+
pos_y = 0
478+
width = 12 # Maximum size: 24
479+
height = 6
480+
type = "timechart"
481+
name = "example panel"
482+
description = "description"
483+
484+
legend {
485+
enabled = %[2]s
486+
show_current = %[3]s
487+
position = "%[4]s"
488+
layout = "%[5]s"
489+
}
490+
491+
query {
492+
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
493+
unit = "percent"
494+
display_info {
495+
display_name = "hostname"
496+
time_series_display_name_template = "{{host_hostname}}"
497+
type = "lines"
498+
}
499+
}
500+
query {
501+
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent{ns_name=$k8s_ns}[$__interval]))"
502+
unit = "number"
503+
display_info {
504+
time_series_display_name_template = "{{host_hostname}}"
505+
type = "stackedArea"
506+
}
507+
}
508+
}
509+
}
510+
`, name, enabled, showCurrent, position, layout)
511+
}

website/docs/r/monitor_dashboard.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ resource "sysdig_monitor_dashboard" "dashboard" {
4040
name = "Example panel"
4141
description = "Description"
4242
43+
legend {
44+
enabled = true
45+
layout = "table"
46+
position = "bottom"
47+
show_current = true
48+
}
49+
4350
query {
4451
promql = "avg_over_time(sysdig_host_cpu_used_percent{host_name=$hostname}[$__interval])"
4552
unit = "percent"
4653
}
54+
4755
query {
4856
promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))"
4957
unit = "number"
@@ -158,6 +166,20 @@ The following arguments are supported:
158166
* `transparent_background` - (Optional) If true, the panel will have a transparent background.
159167
This field is ignored for all panel types except `text`.
160168

169+
### legend
170+
171+
Legend block is used to configure legend on the panel.
172+
173+
The following arguments are supported:
174+
175+
* `enabled` - (Optional) Whether to display the legend for the panel. Default: true.
176+
177+
* `layout` - (Required) Legend display layout. Can be one of: `table`, `inline`.
178+
179+
* `position` - (Required) Positioning of the legend related to the panel data. Can be one of: `bottom`, `right`.
180+
181+
* `show_current` - (Required) Whether to display the most recent datapoint value in the legend.
182+
161183
### query
162184

163185
To scope a panel built from a PromQL query, you must use a scope variable within the query. The variable will take the value of the referenced scope parameter, and the PromQL panel will change accordingly.

0 commit comments

Comments
 (0)