Skip to content

Commit a778056

Browse files
changed the waiting to happen in the delete function
1 parent 8bd43f8 commit a778056

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

internal/providers/pluginfw/products/app/resource_app.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77
"slices"
8+
"time"
89

910
"github.com/databricks/databricks-sdk-go"
1011
"github.com/databricks/databricks-sdk-go/apierr"
@@ -27,6 +28,7 @@ import (
2728
const (
2829
resourceName = "app"
2930
resourceNamePlural = "apps"
31+
appDeletionTimeout = 10 * time.Minute
3032
)
3133

3234
type AppResource struct {
@@ -127,11 +129,6 @@ func (a *resourceApp) Create(ctx context.Context, req resource.CreateRequest, re
127129
return
128130
}
129131

130-
if err := waitForAppDeleted(ctx, w, appGoSdk.Name); err != nil {
131-
resp.Diagnostics.AddError("failed to wait for app deletion", err.Error())
132-
return
133-
}
134-
135132
// Create the app
136133
var forceSendFields []string
137134
if !app.NoCompute.IsNull() {
@@ -217,7 +214,7 @@ func (a *resourceApp) waitForApp(ctx context.Context, w *databricks.WorkspaceCli
217214
}
218215

219216
func waitForAppDeleted(ctx context.Context, w *databricks.WorkspaceClient, name string) error {
220-
retrier := retries.New[struct{}](retries.WithTimeout(-1), retries.WithRetryFunc(shouldRetry))
217+
retrier := retries.New[struct{}](retries.WithTimeout(appDeletionTimeout), retries.WithRetryFunc(shouldRetry))
221218
_, err := retrier.Run(ctx, func(ctx context.Context) (*struct{}, error) {
222219
app, err := w.Apps.GetByName(ctx, name)
223220
if apierr.IsMissing(err) {
@@ -226,10 +223,17 @@ func waitForAppDeleted(ctx context.Context, w *databricks.WorkspaceClient, name
226223
if err != nil {
227224
return nil, retries.Halt(err)
228225
}
229-
if app.ComputeStatus.State == apps.ComputeStateDeleting {
230-
return nil, retries.Continues(fmt.Sprintf("app %s is still deleting", name))
226+
if app.ComputeStatus == nil {
227+
return nil, retries.Continues("waiting for compute status")
228+
}
229+
switch app.ComputeStatus.State {
230+
case apps.ComputeStateDeleting:
231+
return nil, retries.Continues("app is deleting")
232+
case apps.ComputeStateActive, apps.ComputeStateStopped, apps.ComputeStateError:
233+
return nil, retries.Halt(fmt.Errorf("app %s was not deleted, current state: %s", name, app.ComputeStatus.State))
234+
default:
235+
return nil, retries.Continues(fmt.Sprintf("app is in %s state", app.ComputeStatus.State))
231236
}
232-
return &struct{}{}, nil
233237
})
234238
return err
235239
}
@@ -349,6 +353,11 @@ func (a *resourceApp) Delete(ctx context.Context, req resource.DeleteRequest, re
349353
resp.Diagnostics.AddError("failed to delete app", err.Error())
350354
return
351355
}
356+
357+
if err := waitForAppDeleted(ctx, w, app.Name.ValueString()); err != nil {
358+
resp.Diagnostics.AddError("failed to wait for app deletion", err.Error())
359+
return
360+
}
352361
}
353362

354363
func (a *resourceApp) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {

internal/providers/pluginfw/products/app/resource_app_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ func TestWaitForAppDeleted_AppDoesNotExist(t *testing.T) {
2525
assert.NoError(t, err)
2626
}
2727

28-
func TestWaitForAppDeleted_AppInDeletingState(t *testing.T) {
28+
func TestWaitForAppDeleted_WaitsUntilDeleted(t *testing.T) {
2929
ctx, cancel := context.WithCancel(context.Background())
3030
mockClient := mocks.NewMockWorkspaceClient(t)
3131
mockAppsAPI := mockClient.GetMockAppsAPI()
3232

3333
mockAppsAPI.EXPECT().GetByName(mock.Anything, "test-app").Return(&apps.App{
3434
Name: "test-app",
3535
ComputeStatus: &apps.ComputeStatus{
36-
State: apps.ComputeStateDeleting,
37-
Message: "App is being deleted",
36+
State: apps.ComputeStateDeleting,
3837
},
3938
}, nil).Once()
4039

@@ -49,23 +48,21 @@ func TestWaitForAppDeleted_AppInDeletingState(t *testing.T) {
4948
assert.NoError(t, err)
5049
}
5150

52-
func TestWaitForAppDeleted_AppNotInDeletingState_ReturnsImmediately(t *testing.T) {
51+
func TestWaitForAppDeleted_AppNotDeleted_Halts(t *testing.T) {
5352
ctx := context.Background()
5453
mockClient := mocks.NewMockWorkspaceClient(t)
5554
mockAppsAPI := mockClient.GetMockAppsAPI()
5655

57-
// If app exists but is not in DELETING state, return immediately.
58-
// The subsequent Create() call will fail with a proper API error.
5956
mockAppsAPI.EXPECT().GetByName(mock.Anything, "test-app").Return(&apps.App{
6057
Name: "test-app",
6158
ComputeStatus: &apps.ComputeStatus{
62-
State: apps.ComputeStateActive,
63-
Message: "App is active",
59+
State: apps.ComputeStateActive,
6460
},
6561
}, nil).Once()
6662

6763
err := waitForAppDeleted(ctx, mockClient.WorkspaceClient, "test-app")
68-
assert.NoError(t, err)
64+
assert.Error(t, err)
65+
assert.Contains(t, err.Error(), "was not deleted")
6966
}
7067

7168
func TestWaitForAppDeleted_APIError(t *testing.T) {

0 commit comments

Comments
 (0)