Skip to content

Commit 81caffb

Browse files
alexottnkvuong
andauthored
[Fix] Throw an error when creating databricks_query and databricks_alert with already existing names (#4697)
## Changes <!-- Summary of your changes that are easy to understand --> Right now, when an object with the same name already exists in a workspace, the Query and Alert APIs just create an object with a different name (by appending `(1)` or something like that). This leads to permanent configuration drift as TF will try to rename the object to the name specified in the HCL. P.S. Waiting for support this flag in the Update API as well... ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] using Go SDK - [ ] using TF Plugin Framework --------- Co-authored-by: vuong-nguyen <[email protected]>
1 parent fe6dae1 commit 81caffb

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

NEXT_CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bug Fixes
1010

11+
* Fail when creating `databricks_query` and `databricks_alert` with already existing names [#4697](https://github.com/databricks/terraform-provider-databricks/pull/4697).
12+
1113
### Documentation
1214

1315
### Exporter

sql/resource_alert.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func ResourceAlert() common.Resource {
5757
var a sql.CreateAlertRequestAlert
5858
common.DataToStructPointer(d, s, &a)
5959
apiAlert, err := w.Alerts.Create(ctx, sql.CreateAlertRequest{
60-
Alert: &a,
60+
AutoResolveDisplayName: false,
61+
Alert: &a,
62+
ForceSendFields: []string{"AutoResolveDisplayName"},
6163
})
6264
if err != nil {
6365
return err
@@ -107,9 +109,11 @@ func ResourceAlert() common.Resource {
107109
}
108110
common.DataToStructPointer(d, s, &a)
109111
_, err = w.Alerts.Update(ctx, sql.UpdateAlertRequest{
110-
Alert: &a,
111-
Id: d.Id(),
112-
UpdateMask: updateMask,
112+
Alert: &a,
113+
Id: d.Id(),
114+
UpdateMask: updateMask,
115+
AutoResolveDisplayName: false,
116+
ForceSendFields: []string{"AutoResolveDisplayName"},
113117
})
114118
return err
115119
},

sql/resource_alert_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
}
5353
}`
5454
createAlertRequest = sql.CreateAlertRequest{
55+
AutoResolveDisplayName: false,
5556
Alert: &sql.CreateAlertRequestAlert{
5657
QueryId: "123456",
5758
DisplayName: "TF new alert",
@@ -70,7 +71,9 @@ var (
7071
},
7172
},
7273
},
73-
}}
74+
},
75+
ForceSendFields: []string{"AutoResolveDisplayName"},
76+
}
7477
)
7578

7679
func TestAlertCreate(t *testing.T) {
@@ -104,13 +107,13 @@ func TestAlertCreate_BackendError(t *testing.T) {
104107
e := w.GetMockAlertsAPI().EXPECT()
105108
e.Create(mock.Anything, createAlertRequest).Return(nil, &apierr.APIError{
106109
StatusCode: http.StatusBadRequest,
107-
Message: "bad payload",
110+
Message: "Node named 'TF new alert' already exists",
108111
})
109112
},
110113
Resource: ResourceAlert(),
111114
Create: true,
112115
HCL: createHcl,
113-
}.ExpectError(t, "bad payload")
116+
}.ExpectError(t, "Node named 'TF new alert' already exists")
114117
}
115118

116119
func TestAlertCreate_ErrorMultipleValues(t *testing.T) {
@@ -191,8 +194,10 @@ func TestAlertUpdate(t *testing.T) {
191194
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
192195
e := w.GetMockAlertsAPI().EXPECT()
193196
e.Update(mock.Anything, sql.UpdateAlertRequest{
194-
Id: "7890",
195-
UpdateMask: "display_name,query_id,seconds_to_retrigger,condition,custom_body,custom_subject,owner_user_name,notify_on_ok",
197+
Id: "7890",
198+
UpdateMask: "display_name,query_id,seconds_to_retrigger,condition,custom_body,custom_subject,owner_user_name,notify_on_ok",
199+
AutoResolveDisplayName: false,
200+
ForceSendFields: []string{"AutoResolveDisplayName"},
196201
Alert: &sql.UpdateAlertRequestAlert{
197202
QueryId: "123456",
198203
DisplayName: "TF new alert",

sql/resource_query.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ func ResourceQuery() common.Resource {
100100
var q queryCreateStruct
101101
common.DataToStructPointer(d, s, &q)
102102
apiQuery, err := w.Queries.Create(ctx, sql.CreateQueryRequest{
103-
Query: &q.CreateQueryRequestQuery,
103+
AutoResolveDisplayName: false,
104+
Query: &q.CreateQueryRequestQuery,
105+
ForceSendFields: []string{"AutoResolveDisplayName"},
104106
})
105107
if err != nil {
106108
return err
@@ -149,9 +151,11 @@ func ResourceQuery() common.Resource {
149151
}
150152
}
151153
_, err = w.Queries.Update(ctx, sql.UpdateQueryRequest{
152-
Query: &q.UpdateQueryRequestQuery,
153-
Id: d.Id(),
154-
UpdateMask: updateMask,
154+
Query: &q.UpdateQueryRequestQuery,
155+
Id: d.Id(),
156+
UpdateMask: updateMask,
157+
AutoResolveDisplayName: false,
158+
ForceSendFields: []string{"AutoResolveDisplayName"},
155159
})
156160
return err
157161
},

sql/resource_query_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ var (
2727
owner_user_name = "[email protected]"
2828
`
2929
createQueryRequest = sql.CreateQueryRequest{
30+
AutoResolveDisplayName: false,
3031
Query: &sql.CreateQueryRequestQuery{
3132
WarehouseId: "123456",
3233
QueryText: "select 42 as value",
3334
DisplayName: "TF new query",
3435
ParentPath: "/Shared/Querys",
35-
}}
36+
},
37+
ForceSendFields: []string{"AutoResolveDisplayName"},
38+
}
3639
)
3740

3841
func TestQueryCreate(t *testing.T) {
@@ -66,13 +69,13 @@ func TestQueryCreate_Error(t *testing.T) {
6669
e := w.GetMockQueriesAPI().EXPECT()
6770
e.Create(mock.Anything, createQueryRequest).Return(nil, &apierr.APIError{
6871
StatusCode: http.StatusBadRequest,
69-
Message: "bad payload",
72+
Message: "Node named 'TF new query' already exists",
7073
})
7174
},
7275
Resource: ResourceQuery(),
7376
Create: true,
7477
HCL: createQueryHcl,
75-
}.ExpectError(t, "bad payload")
78+
}.ExpectError(t, "Node named 'TF new query' already exists")
7679
}
7780

7881
func TestQueryRead_Import(t *testing.T) {
@@ -125,8 +128,10 @@ func TestQueryUpdate(t *testing.T) {
125128
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
126129
e := w.GetMockQueriesAPI().EXPECT()
127130
e.Update(mock.Anything, sql.UpdateQueryRequest{
128-
Id: "7890",
129-
UpdateMask: "display_name,query_text,warehouse_id,parameters,owner_user_name",
131+
Id: "7890",
132+
UpdateMask: "display_name,query_text,warehouse_id,parameters,owner_user_name",
133+
AutoResolveDisplayName: false,
134+
ForceSendFields: []string{"AutoResolveDisplayName"},
130135
Query: &sql.UpdateQueryRequestQuery{
131136
WarehouseId: "123456",
132137
DisplayName: "TF new query",

0 commit comments

Comments
 (0)