From d34dcc49d0444c21c5d219d4da8758f28bed4e6e Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 18 Mar 2021 16:36:28 +0100 Subject: [PATCH] Support multiple values for dashboard level parameters --- sqlanalytics/api/widget.go | 14 +++-- sqlanalytics/resource_widget.go | 16 +++++- sqlanalytics/resource_widget_test.go | 85 ++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 8 deletions(-) diff --git a/sqlanalytics/api/widget.go b/sqlanalytics/api/widget.go index bb2f24d48f..280bbeab33 100644 --- a/sqlanalytics/api/widget.go +++ b/sqlanalytics/api/widget.go @@ -14,19 +14,23 @@ type Widget struct { VisualizationID *int `json:"visualization_id"` Text *string `json:"text"` + // Options apply to both visualization and text widgets. + Options WidgetOptions `json:"options"` + // This field is no longer in use, but is still required as part of the schema. // It's OK that the field value is 0 everywhere. Width int `json:"width"` - Options struct { - ParameterMapping map[string]WidgetParameterMapping `json:"parameterMappings"` - Position *WidgetPosition `json:"position,omitempty"` - } `json:"options"` - // Fields below are set only when retrieving an existing widget. Visualization json.RawMessage `json:"visualization,omitempty"` } +// WidgetOptions ... +type WidgetOptions struct { + ParameterMapping map[string]WidgetParameterMapping `json:"parameterMappings"` + Position *WidgetPosition `json:"position,omitempty"` +} + // WidgetPosition ... type WidgetPosition struct { AutoHeight bool `json:"autoHeight"` diff --git a/sqlanalytics/resource_widget.go b/sqlanalytics/resource_widget.go index 2ef6f450ed..b9c5c105e2 100644 --- a/sqlanalytics/resource_widget.go +++ b/sqlanalytics/resource_widget.go @@ -34,7 +34,10 @@ type WidgetParameter struct { Type string `json:"type"` MapTo string `json:"map_to,omitempty"` Title string `json:"title,omitempty"` - Value string `json:"value,omitempty"` + + // Mutually exclusive. + Value string `json:"value,omitempty"` + Values []string `json:"values,omitempty"` } 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 if len(w.Parameter) > 0 { aw.Options.ParameterMapping = make(map[string]api.WidgetParameterMapping) for _, wp := range w.Parameter { - aw.Options.ParameterMapping[wp.Name] = api.WidgetParameterMapping{ + wpm := api.WidgetParameterMapping{ Name: wp.Name, Type: wp.Type, MapTo: wp.MapTo, Title: wp.Title, - Value: wp.Value, } + + if len(wp.Values) > 0 { + wpm.Value = wp.Values + } else { + wpm.Value = wp.Value + } + + aw.Options.ParameterMapping[wp.Name] = wpm } } diff --git a/sqlanalytics/resource_widget_test.go b/sqlanalytics/resource_widget_test.go index da441dde1f..3fba5cc497 100644 --- a/sqlanalytics/resource_widget_test.go +++ b/sqlanalytics/resource_widget_test.go @@ -67,3 +67,88 @@ func TestWidgetCreate(t *testing.T) { assert.Equal(t, "12345", d.Id(), "Resource ID should not be empty") assert.Equal(t, "678", d.Get("visualization_id")) } + +func TestWidgetCreateWithParamValue(t *testing.T) { + i678 := 678 + + d, err := qa.ResourceFixture{ + Fixtures: []qa.HTTPFixture{ + { + Method: "POST", + Resource: "/api/2.0/preview/sql/widgets", + ExpectedRequest: api.Widget{ + DashboardID: "some-uuid", + VisualizationID: &i678, + Options: api.WidgetOptions{ + ParameterMapping: map[string]api.WidgetParameterMapping{ + "p1": { + Name: "p1", + Type: "dashboard-level", + Value: "v1", + }, + "p2": { + Name: "p2", + Type: "dashboard-level", + Value: []string{"v2_0", "v2_1"}, + }, + }, + }, + }, + Response: api.Widget{ + ID: 12345, + DashboardID: "some-uuid", + VisualizationID: &i678, + }, + }, + { + Method: "GET", + Resource: "/api/2.0/preview/sql/dashboards/some-uuid", + Response: api.Dashboard{ + ID: "some-uuid", + Widgets: []json.RawMessage{ + json.RawMessage(` + { + "id": 12344, + "visualization_id": null + } + `), + json.RawMessage(` + { + "id": 12345, + "visualization_id": 678 + } + `), + json.RawMessage(` + { + "id": 12345, + "visualization_id": null + } + `), + }, + }, + }, + }, + Resource: ResourceWidget(), + Create: true, + HCL: ` + dashboard_id = "some-uuid" + visualization_id = 678 + + parameter { + name = "p1" + type = "dashboard-level" + value = "v1" + } + + parameter { + name = "p2" + type = "dashboard-level" + values = ["v2_0", "v2_1"] + } + `, + }.Apply(t) + + assert.NoError(t, err, err) + assert.Equal(t, "12345", d.Id(), "Resource ID should not be empty") + assert.Equal(t, "678", d.Get("visualization_id")) +}