Skip to content

Commit cd81117

Browse files
authored
direct: Add waiting for the app after create same as in TF implementation (#3484)
## Changes Copied an implementation from TF so both behaviours are in line https://github.com/databricks/terraform-provider-databricks/blob/9346a78bde642b3ee4e8caec80439a71fa681a9b/internal/providers/pluginfw/products/app/resource_app.go#L161 ## Why Behaviour in TF and direct should be same, currently difference causing app related acceptance test to fail ## Tests Existing acceptance/bundle/run/app-with-job should pass <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent 5053e0e commit cd81117

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

bundle/terranova/tnresources/app.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package tnresources
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/databricks/cli/bundle/config/resources"
78
"github.com/databricks/cli/libs/log"
89
"github.com/databricks/databricks-sdk-go"
10+
"github.com/databricks/databricks-sdk-go/retries"
911
"github.com/databricks/databricks-sdk-go/service/apps"
1012
)
1113

@@ -36,8 +38,6 @@ func (r *ResourceApp) DoCreate(ctx context.Context) (string, error) {
3638
return "", err
3739
}
3840

39-
// TODO: Store waiter for Wait method
40-
4141
return waiter.Response.Name, nil
4242
}
4343

@@ -64,11 +64,41 @@ func DeleteApp(ctx context.Context, client *databricks.WorkspaceClient, id strin
6464
}
6565

6666
func (r *ResourceApp) WaitAfterCreate(ctx context.Context) error {
67-
// Intentional no-op
67+
_, err := r.waitForApp(ctx, r.client, r.config.Name)
68+
if err != nil {
69+
return err
70+
}
6871
return nil
6972
}
7073

7174
func (r *ResourceApp) WaitAfterUpdate(ctx context.Context) error {
7275
// Intentional no-op
7376
return nil
7477
}
78+
79+
// waitForApp waits for the app to reach the target state. The target state is either ACTIVE or STOPPED.
80+
// Apps with no_compute set to true will reach the STOPPED state, otherwise they will reach the ACTIVE state.
81+
// We can't use the default waiter from SDK because it only waits on ACTIVE state but we need also STOPPED state.
82+
// Ideally this should be done in Go SDK but currently only ACTIVE is marked as terminal state
83+
// so this would need to be addressed by Apps service team first in their proto.
84+
func (r *ResourceApp) waitForApp(ctx context.Context, w *databricks.WorkspaceClient, name string) (*apps.App, error) {
85+
retrier := retries.New[apps.App](retries.WithTimeout(-1), retries.WithRetryFunc(shouldRetry))
86+
return retrier.Run(ctx, func(ctx context.Context) (*apps.App, error) {
87+
app, err := w.Apps.GetByName(ctx, name)
88+
if err != nil {
89+
return nil, retries.Halt(err)
90+
}
91+
status := app.ComputeStatus.State
92+
statusMessage := app.ComputeStatus.Message
93+
switch status {
94+
case apps.ComputeStateActive, apps.ComputeStateStopped:
95+
return app, nil
96+
case apps.ComputeStateError:
97+
err := fmt.Errorf("failed to reach %s or %s, got %s: %s",
98+
apps.ComputeStateActive, apps.ComputeStateStopped, status, statusMessage)
99+
return nil, retries.Halt(err)
100+
default:
101+
return nil, retries.Continues(statusMessage)
102+
}
103+
})
104+
}

bundle/terranova/tnresources/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package tnresources
22

33
import (
44
"reflect"
5+
6+
"github.com/databricks/databricks-sdk-go/retries"
57
)
68

79
// filterFields creates a new slice with fields present only in the provided type.
@@ -18,3 +20,16 @@ func filterFields[T any](fields []string) []string {
1820

1921
return result
2022
}
23+
24+
// This is copied from the retries package of the databricks-sdk-go. It should be made public,
25+
// but for now, I'm copying it here.
26+
func shouldRetry(err error) bool {
27+
if err == nil {
28+
return false
29+
}
30+
e := err.(*retries.Err)
31+
if e == nil {
32+
return false
33+
}
34+
return !e.Halt
35+
}

0 commit comments

Comments
 (0)