Skip to content

Commit 1e394ee

Browse files
pieternnfx
authored andcommitted
Refactor query and visualization resources to match pattern
1 parent 1f40444 commit 1e394ee

File tree

2 files changed

+154
-224
lines changed

2 files changed

+154
-224
lines changed

sqlanalytics/resource_query.go

Lines changed: 70 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
)
1313

14-
// Query ...
15-
type Query struct {
14+
// QueryEntity defines the parameters that can be set in the resource.
15+
type QueryEntity struct {
1616
ID string `json:"id,omitempty" tf:"computed"`
1717
DataSourceID string `json:"data_source_id"`
1818
Name string `json:"name"`
@@ -135,15 +135,9 @@ func newQueryParameterAllowMultiple(aq *api.QueryParameterMultipleValuesOptions)
135135
}
136136
}
137137

138-
type queryResource struct {
139-
schema map[string]*schema.Schema
140-
}
141-
142-
func (r *queryResource) toAPIObject(d *schema.ResourceData) (*api.Query, error) {
143-
var q Query
144-
145-
// Transform from ResourceData.
146-
if err := common.DataToStructPointer(d, r.schema, &q); err != nil {
138+
func (q *QueryEntity) toAPIObject(schema map[string]*schema.Schema, data *schema.ResourceData) (*api.Query, error) {
139+
// Extract from ResourceData.
140+
if err := common.DataToStructPointer(data, schema, q); err != nil {
147141
return nil, err
148142
}
149143

@@ -154,17 +148,14 @@ func (r *queryResource) toAPIObject(d *schema.ResourceData) (*api.Query, error)
154148
aq.Name = q.Name
155149
aq.Description = q.Description
156150
aq.Query = q.Query
151+
aq.Tags = append([]string{}, q.Tags...)
157152

158153
if s := q.Schedule; s != nil {
159154
aq.Schedule = &api.QuerySchedule{
160155
Interval: s.Interval,
161156
}
162157
}
163158

164-
if len(q.Tags) > 0 {
165-
aq.Tags = append(aq.Tags, q.Tags...)
166-
}
167-
168159
if len(q.Parameter) > 0 {
169160
aq.Options = &api.QueryOptions{}
170161
for _, p := range q.Parameter {
@@ -251,27 +242,24 @@ func (r *queryResource) toAPIObject(d *schema.ResourceData) (*api.Query, error)
251242
return &aq, nil
252243
}
253244

254-
func (r *queryResource) fromAPIObject(aq *api.Query, d *schema.ResourceData) error {
255-
var q Query
256-
257-
// Transform from API object.
245+
func (q *QueryEntity) fromAPIObject(aq *api.Query, schema map[string]*schema.Schema, data *schema.ResourceData) error {
246+
// Copy from API object.
258247
q.ID = aq.ID
259248
q.DataSourceID = aq.DataSourceID
260249
q.Name = aq.Name
261250
q.Description = aq.Description
262251
q.Query = aq.Query
252+
q.Tags = append([]string{}, aq.Tags...)
263253

264254
if s := aq.Schedule; s != nil {
265255
q.Schedule = &QuerySchedule{
266256
Interval: s.Interval,
267257
}
268258
}
269259

270-
if len(aq.Tags) > 0 {
271-
q.Tags = append(q.Tags, aq.Tags...)
272-
}
273-
274260
if aq.Options != nil {
261+
q.Parameter = nil
262+
275263
for _, ap := range aq.Options.Parameters {
276264
var p QueryParameter
277265
switch apv := ap.(type) {
@@ -356,103 +344,78 @@ func (r *queryResource) fromAPIObject(aq *api.Query, d *schema.ResourceData) err
356344
}
357345

358346
// Transform to ResourceData.
359-
if err := common.StructToData(q, r.schema, d); err != nil {
360-
return err
361-
}
362-
363-
return nil
364-
}
365-
366-
func (r *queryResource) create(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
367-
aq, err := r.toAPIObject(d)
368-
if err != nil {
347+
if err := common.StructToData(*q, schema, data); err != nil {
369348
return err
370349
}
371350

372-
var w = api.NewWrapper(ctx, c)
373-
aqNew, err := w.CreateQuery(aq)
374-
if err != nil {
375-
return err
376-
}
377-
378-
err = r.fromAPIObject(aqNew, d)
379-
if err != nil {
380-
return err
381-
}
382-
383-
d.SetId(aqNew.ID)
384351
return nil
385352
}
386353

387-
func (r *queryResource) read(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
388-
aq, err := r.toAPIObject(d)
389-
if err != nil {
390-
return err
391-
}
392-
393-
var w = api.NewWrapper(ctx, c)
394-
aqNew, err := w.ReadQuery(aq)
395-
if err != nil {
396-
return err
397-
}
398-
399-
err = r.fromAPIObject(aqNew, d)
400-
if err != nil {
401-
return err
402-
}
403-
404-
return nil
405-
}
354+
// ResourceQuery ...
355+
func ResourceQuery() *schema.Resource {
356+
s := common.StructToSchema(
357+
QueryEntity{},
358+
func(m map[string]*schema.Schema) map[string]*schema.Schema {
359+
return m
360+
})
406361

407-
func (r *queryResource) update(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
408-
aq, err := r.toAPIObject(d)
409-
if err != nil {
410-
return err
411-
}
362+
return common.Resource{
363+
Create: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
364+
var q QueryEntity
365+
aq, err := q.toAPIObject(s, data)
366+
if err != nil {
367+
return err
368+
}
412369

413-
var w = api.NewWrapper(ctx, c)
414-
aqNew, err := w.UpdateQuery(aq)
415-
if err != nil {
416-
return err
417-
}
370+
aqNew, err := api.NewWrapper(ctx, c).CreateQuery(aq)
371+
if err != nil {
372+
return err
373+
}
418374

419-
err = r.fromAPIObject(aqNew, d)
420-
if err != nil {
421-
return err
422-
}
375+
// No need to set anything because the resource is going to be
376+
// read immediately after being created.
377+
data.SetId(aqNew.ID)
378+
return nil
379+
},
380+
Read: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
381+
var q QueryEntity
382+
aq, err := q.toAPIObject(s, data)
383+
if err != nil {
384+
return err
385+
}
423386

424-
return nil
425-
}
387+
aqNew, err := api.NewWrapper(ctx, c).ReadQuery(aq)
388+
if err != nil {
389+
return err
390+
}
426391

427-
func (r *queryResource) delete(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
428-
aq, err := r.toAPIObject(d)
429-
if err != nil {
430-
return err
431-
}
392+
return q.fromAPIObject(aqNew, s, data)
393+
},
394+
Update: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
395+
var q QueryEntity
396+
aq, err := q.toAPIObject(s, data)
397+
if err != nil {
398+
return err
399+
}
432400

433-
var w = api.NewWrapper(ctx, c)
434-
err = w.DeleteQuery(aq)
435-
if err != nil {
436-
return err
437-
}
401+
_, err = api.NewWrapper(ctx, c).UpdateQuery(aq)
402+
if err != nil {
403+
return err
404+
}
438405

439-
return nil
440-
}
406+
// No need to set anything because the resource is going to be
407+
// read immediately after being created.
408+
return nil
409+
},
410+
Delete: func(ctx context.Context, data *schema.ResourceData, c *common.DatabricksClient) error {
411+
var q QueryEntity
412+
aq, err := q.toAPIObject(s, data)
413+
if err != nil {
414+
return err
415+
}
441416

442-
// ResourceQuery ...
443-
func ResourceQuery() *schema.Resource {
444-
r := queryResource{
445-
common.StructToSchema(
446-
Query{},
447-
func(m map[string]*schema.Schema) map[string]*schema.Schema {
448-
return m
449-
}),
450-
}
451-
return common.Resource{
452-
Schema: r.schema,
453-
Create: r.create,
454-
Read: r.read,
455-
Update: r.update,
456-
Delete: r.delete,
417+
return api.NewWrapper(ctx, c).DeleteQuery(aq)
418+
},
419+
Schema: s,
457420
}.ToResource()
458421
}

0 commit comments

Comments
 (0)