@@ -2,6 +2,7 @@ package sqlanalytics
22
33import (
44 "context"
5+ "strings"
56
67 "github.com/databrickslabs/terraform-provider-databricks/common"
78 "github.com/databrickslabs/terraform-provider-databricks/sqlanalytics/api"
@@ -47,6 +48,63 @@ func (d *DashboardEntity) fromAPIObject(ad *api.Dashboard, schema map[string]*sc
4748 return nil
4849}
4950
51+ // NewDashboardAPI ...
52+ func NewDashboardAPI (ctx context.Context , m interface {}) DashboardAPI {
53+ return DashboardAPI {m .(* common.DatabricksClient ), ctx }
54+ }
55+
56+ // DashboardAPI ...
57+ type DashboardAPI struct {
58+ client * common.DatabricksClient
59+ context context.Context
60+ }
61+
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+
70+ // 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
79+ }
80+
81+ // 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 )
85+ if err != nil {
86+ return nil , err
87+ }
88+
89+ return & dout , nil
90+ }
91+
92+ // 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
101+ }
102+
103+ // Delete ...
104+ func (a DashboardAPI ) Delete (d * api.Dashboard ) error {
105+ return a .client .Delete (a .context , a .buildPath (d .ID ), nil )
106+ }
107+
50108// ResourceDashboard ...
51109func ResourceDashboard () * schema.Resource {
52110 s := common .StructToSchema (
@@ -63,7 +121,7 @@ func ResourceDashboard() *schema.Resource {
63121 return err
64122 }
65123
66- adNew , err := api . NewWrapper (ctx , c ).CreateDashboard (ad )
124+ adNew , err := NewDashboardAPI (ctx , c ).Create (ad )
67125 if err != nil {
68126 return err
69127 }
@@ -80,7 +138,7 @@ func ResourceDashboard() *schema.Resource {
80138 return err
81139 }
82140
83- adNew , err := api . NewWrapper (ctx , c ).ReadDashboard (ad )
141+ adNew , err := NewDashboardAPI (ctx , c ).Read (ad )
84142 if err != nil {
85143 return err
86144 }
@@ -94,7 +152,7 @@ func ResourceDashboard() *schema.Resource {
94152 return err
95153 }
96154
97- _ , err = api . NewWrapper (ctx , c ).UpdateDashboard (ad )
155+ _ , err = NewDashboardAPI (ctx , c ).Update (ad )
98156 if err != nil {
99157 return err
100158 }
@@ -110,7 +168,7 @@ func ResourceDashboard() *schema.Resource {
110168 return err
111169 }
112170
113- return api . NewWrapper (ctx , c ).DeleteDashboard (ad )
171+ return NewDashboardAPI (ctx , c ).Delete (ad )
114172 },
115173 Schema : s ,
116174 }.ToResource ()
0 commit comments