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 (
2728const (
2829 resourceName = "app"
2930 resourceNamePlural = "apps"
31+ appDeletionTimeout = 10 * time .Minute
3032)
3133
3234type 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
219216func 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
354363func (a * resourceApp ) ImportState (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
0 commit comments