-
Notifications
You must be signed in to change notification settings - Fork 353
/
resource_sql_dashboard.go
145 lines (123 loc) · 4.31 KB
/
resource_sql_dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package sql
import (
"context"
"fmt"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/sql/api"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// DashboardEntity defines the parameters that can be set in the resource.
type DashboardEntity struct {
Name string `json:"name"`
Tags []string `json:"tags,omitempty"`
Parent string `json:"parent,omitempty" tf:"suppress_diff,force_new"`
CreatedAt string `json:"created_at,omitempty" tf:"computed"`
UpdatedAt string `json:"updated_at,omitempty" tf:"computed"`
RunAsRole string `json:"run_as_role,omitempty" tf:"suppress_diff"`
DashboardFiltersEnabled bool `json:"dashboard_filters_enabled,omitempty"`
}
func (d *DashboardEntity) toAPIObject(schema map[string]*schema.Schema, data *schema.ResourceData) (*api.Dashboard, error) {
// Extract from ResourceData.
common.DataToStructPointer(data, schema, d)
// Copy to API object.
var ad api.Dashboard
ad.ID = data.Id()
ad.Name = d.Name
ad.Tags = append([]string{}, d.Tags...)
ad.Parent = d.Parent
ad.DashboardFiltersEnabled = d.DashboardFiltersEnabled
ad.RunAsRole = d.RunAsRole
return &ad, nil
}
func (d *DashboardEntity) fromAPIObject(ad *api.Dashboard, schema map[string]*schema.Schema, data *schema.ResourceData) error {
// Copy from API object.
d.Name = ad.Name
d.Tags = append([]string{}, ad.Tags...)
d.Parent = ad.Parent
d.UpdatedAt = ad.UpdatedAt
d.CreatedAt = ad.CreatedAt
d.DashboardFiltersEnabled = ad.DashboardFiltersEnabled
d.RunAsRole = ad.RunAsRole
// Pass to ResourceData.
if err := common.StructToData(*d, schema, data); err != nil {
return err
}
// Overwrite `tags` in case they're empty on the server side.
// This would have been skipped by `common.StructToData` because of slice emptiness.
// Ideally, the reflection code also sets empty values, but we'd risk
// clobbering values we actually want to keep around in existing code.
data.Set("tags", ad.Tags)
return nil
}
// NewDashboardAPI ...
func NewDashboardAPI(ctx context.Context, m any) DashboardAPI {
return DashboardAPI{m.(*common.DatabricksClient), ctx}
}
// DashboardAPI ...
type DashboardAPI struct {
client *common.DatabricksClient
context context.Context
}
// Create ...
func (a DashboardAPI) Create(d *api.Dashboard) error {
return a.client.Post(a.context, "/preview/sql/dashboards", d, &d)
}
// Read ...
func (a DashboardAPI) Read(dashboardID string) (*api.Dashboard, error) {
var d api.Dashboard
err := a.client.Get(a.context, fmt.Sprintf("/preview/sql/dashboards/%s", dashboardID), nil, &d)
if err != nil {
return nil, err
}
return &d, nil
}
// Update ...
func (a DashboardAPI) Update(dashboardID string, d *api.Dashboard) error {
return a.client.Post(a.context, fmt.Sprintf("/preview/sql/dashboards/%s", dashboardID), d, nil)
}
// Delete ...
func (a DashboardAPI) Delete(dashboardID string) error {
return a.client.Delete(a.context, fmt.Sprintf("/preview/sql/dashboards/%s", dashboardID), nil)
}
func ResourceSqlDashboard() common.Resource {
s := common.StructToSchema(
DashboardEntity{},
common.NoCustomize)
return common.Resource{
Create: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
var d DashboardEntity
ad, err := d.toAPIObject(s, data)
if err != nil {
return err
}
err = NewDashboardAPI(ctx, c).Create(ad)
if err != nil {
return err
}
// No need to set anything because the resource is going to be
// read immediately after being created.
data.SetId(ad.ID)
return nil
},
Read: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
ad, err := NewDashboardAPI(ctx, c).Read(data.Id())
if err != nil {
return err
}
var d DashboardEntity
return d.fromAPIObject(ad, s, data)
},
Update: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
var d DashboardEntity
ad, err := d.toAPIObject(s, data)
if err != nil {
return err
}
return NewDashboardAPI(ctx, c).Update(data.Id(), ad)
},
Delete: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
return NewDashboardAPI(ctx, c).Delete(data.Id())
},
Schema: s,
}
}