File tree Expand file tree Collapse file tree 2 files changed +61
-5
lines changed Expand file tree Collapse file tree 2 files changed +61
-5
lines changed Original file line number Diff line number Diff line change 66 "fmt"
77 "log"
88 "reflect"
9+ "strconv"
910 "strings"
1011
1112 "github.com/databrickslabs/terraform-provider-databricks/common"
@@ -449,11 +450,11 @@ func (a QueryAPI) Create(q *api.Query) error {
449450 if err != nil {
450451 return err
451452 }
452- // TODO
453- // err = a.DeleteVisualization(&v )
454- // if err != nil {
455- // return nil, err
456- // }
453+ // This is a best effort -- don't fail if it doesn't work.
454+ err = NewVisualizationAPI ( a . context , a . client ). Delete ( strconv . Itoa ( v . ID ) )
455+ if err != nil {
456+ log . Printf ( "[WARN] Unable to delete automatically created visualization for query %s (%d)" , q . ID , v . ID )
457+ }
457458 }
458459 q .Visualizations = []json.RawMessage {}
459460 return nil
Original file line number Diff line number Diff line change 11package sqlanalytics
22
33import (
4+ "encoding/json"
45 "testing"
56
67 "github.com/databrickslabs/terraform-provider-databricks/qa"
@@ -281,6 +282,60 @@ func TestQueryCreateWithWeeklySchedule(t *testing.T) {
281282 assert .Equal (t , untilDate , d .Get ("schedule.0.weekly.0.until_date" ))
282283}
283284
285+ func TestQueryCreateDeletesDefaultVisualization (t * testing.T ) {
286+ _ , err := qa.ResourceFixture {
287+ Fixtures : []qa.HTTPFixture {
288+ {
289+ Method : "POST" ,
290+ Resource : "/api/2.0/preview/sql/queries" ,
291+ ExpectedRequest : api.Query {
292+ DataSourceID : "xyz" ,
293+ Name : "Query name" ,
294+ Query : "SELECT 1" ,
295+ },
296+ Response : api.Query {
297+ ID : "foo" ,
298+ DataSourceID : "xyz" ,
299+ Name : "Query name" ,
300+ Query : "SELECT 1" ,
301+
302+ // The automatically created visualization should be deleted.
303+ Visualizations : []json.RawMessage {
304+ json .RawMessage (`
305+ {
306+ "id": 12345
307+ }
308+ ` ),
309+ },
310+ },
311+ },
312+ {
313+ Method : "GET" ,
314+ Resource : "/api/2.0/preview/sql/queries/foo" ,
315+ Response : api.Query {
316+ ID : "foo" ,
317+ DataSourceID : "xyz" ,
318+ Name : "Query name" ,
319+ Query : "SELECT 1" ,
320+ },
321+ },
322+ {
323+ Method : "DELETE" ,
324+ Resource : "/api/2.0/preview/sql/visualizations/12345" ,
325+ },
326+ },
327+ Resource : ResourceQuery (),
328+ Create : true ,
329+ State : map [string ]interface {}{
330+ "data_source_id" : "xyz" ,
331+ "name" : "Query name" ,
332+ "query" : "SELECT 1" ,
333+ },
334+ }.Apply (t )
335+
336+ assert .NoError (t , err , err )
337+ }
338+
284339func TestQueryRead (t * testing.T ) {
285340 d , err := qa.ResourceFixture {
286341 Fixtures : []qa.HTTPFixture {
You can’t perform that action at this time.
0 commit comments