Skip to content

Commit a78915f

Browse files
pieternnfx
authored andcommitted
Delete automatically created visualization for new queries
1 parent b36b67f commit a78915f

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

sqlanalytics/resource_query.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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

sqlanalytics/resource_query_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqlanalytics
22

33
import (
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+
284339
func TestQueryRead(t *testing.T) {
285340
d, err := qa.ResourceFixture{
286341
Fixtures: []qa.HTTPFixture{

0 commit comments

Comments
 (0)