Skip to content

Commit 923f8eb

Browse files
pieternnfx
authored andcommitted
Support multiple values for dashboard level parameters
1 parent b5a3f6b commit 923f8eb

File tree

3 files changed

+107
-8
lines changed

3 files changed

+107
-8
lines changed

sqlanalytics/api/widget.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ type Widget struct {
1414
VisualizationID *int `json:"visualization_id"`
1515
Text *string `json:"text"`
1616

17+
// Options apply to both visualization and text widgets.
18+
Options WidgetOptions `json:"options"`
19+
1720
// This field is no longer in use, but is still required as part of the schema.
1821
// It's OK that the field value is 0 everywhere.
1922
Width int `json:"width"`
2023

21-
Options struct {
22-
ParameterMapping map[string]WidgetParameterMapping `json:"parameterMappings"`
23-
Position *WidgetPosition `json:"position,omitempty"`
24-
} `json:"options"`
25-
2624
// Fields below are set only when retrieving an existing widget.
2725
Visualization json.RawMessage `json:"visualization,omitempty"`
2826
}
2927

28+
// WidgetOptions ...
29+
type WidgetOptions struct {
30+
ParameterMapping map[string]WidgetParameterMapping `json:"parameterMappings"`
31+
Position *WidgetPosition `json:"position,omitempty"`
32+
}
33+
3034
// WidgetPosition ...
3135
type WidgetPosition struct {
3236
AutoHeight bool `json:"autoHeight"`

sqlanalytics/resource_widget.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ type WidgetParameter struct {
3434
Type string `json:"type"`
3535
MapTo string `json:"map_to,omitempty"`
3636
Title string `json:"title,omitempty"`
37-
Value string `json:"value,omitempty"`
37+
38+
// Mutually exclusive.
39+
Value string `json:"value,omitempty"`
40+
Values []string `json:"values,omitempty"`
3841
}
3942

4043
func (w *WidgetEntity) toAPIObject(schema map[string]*schema.Schema, data *schema.ResourceData) (*api.Widget, error) {
@@ -82,13 +85,20 @@ func (w *WidgetEntity) toAPIObject(schema map[string]*schema.Schema, data *schem
8285
if len(w.Parameter) > 0 {
8386
aw.Options.ParameterMapping = make(map[string]api.WidgetParameterMapping)
8487
for _, wp := range w.Parameter {
85-
aw.Options.ParameterMapping[wp.Name] = api.WidgetParameterMapping{
88+
wpm := api.WidgetParameterMapping{
8689
Name: wp.Name,
8790
Type: wp.Type,
8891
MapTo: wp.MapTo,
8992
Title: wp.Title,
90-
Value: wp.Value,
9193
}
94+
95+
if len(wp.Values) > 0 {
96+
wpm.Value = wp.Values
97+
} else {
98+
wpm.Value = wp.Value
99+
}
100+
101+
aw.Options.ParameterMapping[wp.Name] = wpm
92102
}
93103
}
94104

sqlanalytics/resource_widget_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,88 @@ func TestWidgetCreate(t *testing.T) {
6767
assert.Equal(t, "12345", d.Id(), "Resource ID should not be empty")
6868
assert.Equal(t, "678", d.Get("visualization_id"))
6969
}
70+
71+
func TestWidgetCreateWithParamValue(t *testing.T) {
72+
i678 := 678
73+
74+
d, err := qa.ResourceFixture{
75+
Fixtures: []qa.HTTPFixture{
76+
{
77+
Method: "POST",
78+
Resource: "/api/2.0/preview/sql/widgets",
79+
ExpectedRequest: api.Widget{
80+
DashboardID: "some-uuid",
81+
VisualizationID: &i678,
82+
Options: api.WidgetOptions{
83+
ParameterMapping: map[string]api.WidgetParameterMapping{
84+
"p1": {
85+
Name: "p1",
86+
Type: "dashboard-level",
87+
Value: "v1",
88+
},
89+
"p2": {
90+
Name: "p2",
91+
Type: "dashboard-level",
92+
Value: []string{"v2_0", "v2_1"},
93+
},
94+
},
95+
},
96+
},
97+
Response: api.Widget{
98+
ID: 12345,
99+
DashboardID: "some-uuid",
100+
VisualizationID: &i678,
101+
},
102+
},
103+
{
104+
Method: "GET",
105+
Resource: "/api/2.0/preview/sql/dashboards/some-uuid",
106+
Response: api.Dashboard{
107+
ID: "some-uuid",
108+
Widgets: []json.RawMessage{
109+
json.RawMessage(`
110+
{
111+
"id": 12344,
112+
"visualization_id": null
113+
}
114+
`),
115+
json.RawMessage(`
116+
{
117+
"id": 12345,
118+
"visualization_id": 678
119+
}
120+
`),
121+
json.RawMessage(`
122+
{
123+
"id": 12345,
124+
"visualization_id": null
125+
}
126+
`),
127+
},
128+
},
129+
},
130+
},
131+
Resource: ResourceWidget(),
132+
Create: true,
133+
HCL: `
134+
dashboard_id = "some-uuid"
135+
visualization_id = 678
136+
137+
parameter {
138+
name = "p1"
139+
type = "dashboard-level"
140+
value = "v1"
141+
}
142+
143+
parameter {
144+
name = "p2"
145+
type = "dashboard-level"
146+
values = ["v2_0", "v2_1"]
147+
}
148+
`,
149+
}.Apply(t)
150+
151+
assert.NoError(t, err, err)
152+
assert.Equal(t, "12345", d.Id(), "Resource ID should not be empty")
153+
assert.Equal(t, "678", d.Get("visualization_id"))
154+
}

0 commit comments

Comments
 (0)