Skip to content

Commit

Permalink
Support multiple values for dashboard level parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Mar 18, 2021
1 parent 6081f49 commit d34dcc4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
14 changes: 9 additions & 5 deletions sqlanalytics/api/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
16 changes: 13 additions & 3 deletions sqlanalytics/resource_widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}

Expand Down
85 changes: 85 additions & 0 deletions sqlanalytics/resource_widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

0 comments on commit d34dcc4

Please sign in to comment.