Skip to content

Commit 4a4ade5

Browse files
pieternnfx
authored andcommitted
Simplify dashboard API wrapper
This commit makes it match the style of the visualization and widget resources, after they were converted to use composite IDs.
1 parent 16a8932 commit 4a4ade5

File tree

2 files changed

+19
-57
lines changed

2 files changed

+19
-57
lines changed

sqlanalytics/resource_dashboard.go

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package sqlanalytics
22

33
import (
44
"context"
5-
"strings"
5+
"fmt"
66

77
"github.com/databrickslabs/terraform-provider-databricks/common"
88
"github.com/databrickslabs/terraform-provider-databricks/sqlanalytics/api"
@@ -59,50 +59,30 @@ type DashboardAPI struct {
5959
context context.Context
6060
}
6161

62-
func (a DashboardAPI) buildPath(path ...string) string {
63-
out := "/preview/sql/dashboards"
64-
if len(path) == 1 {
65-
out = out + "/" + strings.Join(path, "/")
66-
}
67-
return out
68-
}
69-
7062
// Create ...
71-
func (a DashboardAPI) Create(d *api.Dashboard) (*api.Dashboard, error) {
72-
var dout api.Dashboard
73-
err := a.client.Post(a.context, a.buildPath(), d, &dout)
74-
if err != nil {
75-
return nil, err
76-
}
77-
78-
return &dout, err
63+
func (a DashboardAPI) Create(d *api.Dashboard) error {
64+
return a.client.Post(a.context, "/preview/sql/dashboards", d, &d)
7965
}
8066

8167
// Read ...
82-
func (a DashboardAPI) Read(d *api.Dashboard) (*api.Dashboard, error) {
83-
var dout api.Dashboard
84-
err := a.client.Get(a.context, a.buildPath(d.ID), nil, &dout)
68+
func (a DashboardAPI) Read(dashboardID string) (*api.Dashboard, error) {
69+
var d api.Dashboard
70+
err := a.client.Get(a.context, fmt.Sprintf("/preview/sql/dashboards/%s", dashboardID), nil, &d)
8571
if err != nil {
8672
return nil, err
8773
}
8874

89-
return &dout, nil
75+
return &d, nil
9076
}
9177

9278
// Update ...
93-
func (a DashboardAPI) Update(d *api.Dashboard) (*api.Dashboard, error) {
94-
var dout api.Dashboard
95-
err := a.client.Post(a.context, a.buildPath(d.ID), d, &dout)
96-
if err != nil {
97-
return nil, err
98-
}
99-
100-
return &dout, nil
79+
func (a DashboardAPI) Update(dashboardID string, d *api.Dashboard) error {
80+
return a.client.Post(a.context, fmt.Sprintf("/preview/sql/dashboards/%s", dashboardID), d, nil)
10181
}
10282

10383
// Delete ...
104-
func (a DashboardAPI) Delete(d *api.Dashboard) error {
105-
return a.client.Delete(a.context, a.buildPath(d.ID), nil)
84+
func (a DashboardAPI) Delete(dashboardID string) error {
85+
return a.client.Delete(a.context, fmt.Sprintf("/preview/sql/dashboards/%s", dashboardID), nil)
10686
}
10787

10888
// ResourceDashboard ...
@@ -121,29 +101,24 @@ func ResourceDashboard() *schema.Resource {
121101
return err
122102
}
123103

124-
adNew, err := NewDashboardAPI(ctx, c).Create(ad)
104+
err = NewDashboardAPI(ctx, c).Create(ad)
125105
if err != nil {
126106
return err
127107
}
128108

129109
// No need to set anything because the resource is going to be
130110
// read immediately after being created.
131-
data.SetId(adNew.ID)
111+
data.SetId(ad.ID)
132112
return nil
133113
},
134114
Read: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
135-
var d DashboardEntity
136-
ad, err := d.toAPIObject(s, data)
115+
ad, err := NewDashboardAPI(ctx, c).Read(data.Id())
137116
if err != nil {
138117
return err
139118
}
140119

141-
adNew, err := NewDashboardAPI(ctx, c).Read(ad)
142-
if err != nil {
143-
return err
144-
}
145-
146-
return d.fromAPIObject(adNew, s, data)
120+
var d DashboardEntity
121+
return d.fromAPIObject(ad, s, data)
147122
},
148123
Update: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
149124
var d DashboardEntity
@@ -152,23 +127,10 @@ func ResourceDashboard() *schema.Resource {
152127
return err
153128
}
154129

155-
_, err = NewDashboardAPI(ctx, c).Update(ad)
156-
if err != nil {
157-
return err
158-
}
159-
160-
// No need to set anything because the resource is going to be
161-
// read immediately after being created.
162-
return nil
130+
return NewDashboardAPI(ctx, c).Update(data.Id(), ad)
163131
},
164132
Delete: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
165-
var d DashboardEntity
166-
ad, err := d.toAPIObject(s, data)
167-
if err != nil {
168-
return err
169-
}
170-
171-
return NewDashboardAPI(ctx, c).Delete(ad)
133+
return NewDashboardAPI(ctx, c).Delete(data.Id())
172134
},
173135
Schema: s,
174136
}.ToResource()

sqlanalytics/resource_widget.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (a WidgetAPI) Create(w *api.Widget) error {
203203

204204
// Read ...
205205
func (a WidgetAPI) Read(dashboardID, widgetID string) (*api.Widget, error) {
206-
d, err := NewDashboardAPI(a.context, a.client).Read(&api.Dashboard{ID: dashboardID})
206+
d, err := NewDashboardAPI(a.context, a.client).Read(dashboardID)
207207
if err != nil {
208208
return nil, err
209209
}

0 commit comments

Comments
 (0)