Skip to content

Commit 79322bb

Browse files
nkvuongalexott
andauthored
[Feature] Added no_wait option for warehouses to skip waiting to start on creation (#5014)
## Changes - Resolves #5006 ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [x] relevant change in `docs/` folder - [x] covered with integration tests in `internal/acceptance` - [x] using Go SDK - [x] has entry in `NEXT_CHANGELOG.md` file --------- Co-authored-by: Alex Ott <[email protected]>
1 parent 139f34e commit 79322bb

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

NEXT_CHANGELOG.md

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

77
### New Features and Improvements
88

9+
* Added `no_wait` option for `databricks_sql_endpoint` to skip waiting to start on warehouse creation
10+
911
### Bug Fixes
1012

1113
### Documentation

docs/resources/sql_endpoint.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The following arguments are supported:
4747
* `name` - Name of the Databricks SQL release channel. Possible values are: `CHANNEL_NAME_PREVIEW` and `CHANNEL_NAME_CURRENT`. Default is `CHANNEL_NAME_CURRENT`.
4848

4949
* `warehouse_type` - SQL warehouse type. See for [AWS](https://docs.databricks.com/sql/admin/sql-endpoints.html#switch-the-sql-warehouse-type-pro-classic-or-serverless) or [Azure](https://learn.microsoft.com/en-us/azure/databricks/sql/admin/create-sql-warehouse#--upgrade-a-pro-or-classic-sql-warehouse-to-a-serverless-sql-warehouse). Set to `PRO` or `CLASSIC`. If the field `enable_serverless_compute` has the value `true` either explicitly or through the default logic (see that field above for details), the default is `PRO`, which is required for serverless SQL warehouses. Otherwise, the default is `CLASSIC`.
50+
* `no_wait` - (Optional) Whether to skip waiting for the SQL warehouse to start after creation. Default is `false`. When set to `true`, Terraform will create the warehouse but won't wait for it to be in a running state before completing.
5051

5152
## Attribute reference
5253

sql/resource_sql_endpoint.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ func ResourceSqlEndpoint() common.Resource {
8383
common.CustomizeSchemaPath(m, "warehouse_type").
8484
SetSuppressDiff().
8585
SetValidateDiagFunc(validation.ToDiagFunc(validation.StringInSlice([]string{"PRO", "CLASSIC"}, false)))
86+
87+
// Add no_wait field to schema
88+
m["no_wait"] = &schema.Schema{
89+
Type: schema.TypeBool,
90+
Optional: true,
91+
Default: false,
92+
Description: "If true, skip waiting for the warehouse to start after creation.",
93+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
94+
if old == "" && new == "false" {
95+
return true
96+
}
97+
return old == new
98+
},
99+
}
100+
86101
return m
87102
})
88103
return common.Resource{
@@ -101,7 +116,17 @@ func ResourceSqlEndpoint() common.Resource {
101116
if err != nil {
102117
return fmt.Errorf("failed creating warehouse: %w", err)
103118
}
104-
resp, err := wait.Get()
119+
120+
d.SetId(wait.Id)
121+
122+
// Check if no_wait flag is set to true
123+
noWait, ok := d.GetOk("no_wait")
124+
if ok && noWait.(bool) {
125+
return nil
126+
}
127+
128+
// Wait for warehouse to start if no_wait is false or not set
129+
_, err = wait.Get()
105130
if err != nil {
106131
// Rollback by deleting the warehouse
107132
rollbackErr := w.Warehouses.DeleteById(ctx, wait.Id)
@@ -110,7 +135,6 @@ func ResourceSqlEndpoint() common.Resource {
110135
}
111136
return fmt.Errorf("failed waiting for warehouse to start: %w", err)
112137
}
113-
d.SetId(resp.Id)
114138
return nil
115139
},
116140
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {

sql/resource_sql_endpoint_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func TestResourceSQLEndpointCreate(t *testing.T) {
5757
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
5858
api := w.GetMockWarehousesAPI()
5959
api.EXPECT().Create(mock.Anything, createRequest).Return(&sql.WaitGetWarehouseRunning[sql.CreateWarehouseResponse]{
60+
Id: "abc", // Set the ID in the waiter
6061
Poll: poll.Simple(getResponse),
6162
}, nil)
6263
api.EXPECT().GetById(mock.Anything, "abc").Return(&getResponse, nil)
@@ -125,6 +126,7 @@ func TestResourceSQLEndpointCreate_ForceSendFields(t *testing.T) {
125126
SpotInstancePolicy: "COST_OPTIMIZED",
126127
ForceSendFields: c.expectedForceSendFields,
127128
}).Return(&sql.WaitGetWarehouseRunning[sql.CreateWarehouseResponse]{
129+
Id: "abc",
128130
Poll: poll.Simple(response),
129131
}, nil)
130132
api.EXPECT().GetById(mock.Anything, "abc").Return(&response, nil)
@@ -158,6 +160,7 @@ func TestResourceSQLEndpointCreateNoAutoTermination(t *testing.T) {
158160
SpotInstancePolicy: "COST_OPTIMIZED",
159161
ForceSendFields: []string{"AutoStopMins"},
160162
}).Return(&sql.WaitGetWarehouseRunning[sql.CreateWarehouseResponse]{
163+
Id: "abc",
161164
Poll: poll.Simple(getResponse),
162165
}, nil)
163166
e.GetById(mock.Anything, "abc").Return(&getResponse, nil)
@@ -288,7 +291,10 @@ func TestResourceSQLEndpointUpdate(t *testing.T) {
288291
MaxNumClusters: 1,
289292
EnablePhoton: true,
290293
SpotInstancePolicy: "COST_OPTIMIZED",
291-
}).Return(&sql.WaitGetWarehouseRunning[struct{}]{Poll: poll.Simple(getResponse)}, nil)
294+
}).Return(&sql.WaitGetWarehouseRunning[struct{}]{
295+
Id: "abc",
296+
Poll: poll.Simple(getResponse),
297+
}, nil)
292298
api.EXPECT().GetById(mock.Anything, "abc").Return(&getResponse, nil)
293299
addDataSourceListHttpFixture(mwc)
294300
},
@@ -409,3 +415,43 @@ func TestResolveDataSourceIDNotFound(t *testing.T) {
409415
require.Error(t, err)
410416
})
411417
}
418+
419+
func TestResourceSQLEndpointCreateNoWait(t *testing.T) {
420+
// Create a specific response for the no_wait test that shows a starting state
421+
getResponseStarting := sql.GetWarehouseResponse{
422+
Name: "foo",
423+
ClusterSize: "Small",
424+
Id: "abc",
425+
State: "STARTING", // Important: Warehouse is still starting
426+
Tags: &sql.EndpointTags{},
427+
MaxNumClusters: 1,
428+
NumClusters: 0, // No clusters running yet
429+
}
430+
431+
qa.ResourceFixture{
432+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
433+
api := w.GetMockWarehousesAPI()
434+
// The Create should return immediately without waiting due to no_wait=true
435+
api.EXPECT().Create(mock.Anything, createRequest).Return(&sql.WaitGetWarehouseRunning[sql.CreateWarehouseResponse]{
436+
Id: "abc", // Set the ID directly in the waiter
437+
Poll: poll.Simple(getResponseStarting), // Use starting state response
438+
}, nil)
439+
440+
// Read operation will still be called after create to refresh state
441+
api.EXPECT().GetById(mock.Anything, "abc").Return(&getResponseStarting, nil)
442+
addDataSourceListHttpFixture(w)
443+
},
444+
Resource: ResourceSqlEndpoint(),
445+
Create: true,
446+
HCL: `
447+
name = "foo"
448+
cluster_size = "Small"
449+
no_wait = true
450+
`,
451+
}.ApplyAndExpectData(t, map[string]interface{}{
452+
"id": "abc",
453+
"no_wait": true,
454+
"state": "STARTING",
455+
"data_source_id": "d7c9d05c-7496-4c69-b089-48823edad40c",
456+
})
457+
}

0 commit comments

Comments
 (0)