Skip to content

Commit e8ce30b

Browse files
Address review: use apierr.IsMissing in Exists, SDK LRO in DoCreate/DoUpdate
Co-authored-by: Isaac
1 parent 3d3510d commit e8ce30b

2 files changed

Lines changed: 16 additions & 39 deletions

File tree

bundle/config/resources/app_spaces.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/databricks/cli/libs/log"
88
"github.com/databricks/databricks-sdk-go"
9+
"github.com/databricks/databricks-sdk-go/apierr"
910
"github.com/databricks/databricks-sdk-go/marshal"
1011
"github.com/databricks/databricks-sdk-go/service/apps"
1112
)
@@ -28,7 +29,10 @@ func (s AppSpace) MarshalJSON() ([]byte, error) {
2829
func (s *AppSpace) Exists(ctx context.Context, w *databricks.WorkspaceClient, id string) (bool, error) {
2930
_, err := w.Apps.GetSpace(ctx, apps.GetSpaceRequest{Name: id})
3031
if err != nil {
31-
log.Debugf(ctx, "app space %s does not exist", id)
32+
log.Debugf(ctx, "app space with id %s does not exist: %v", id, err)
33+
if apierr.IsMissing(err) {
34+
return false, nil
35+
}
3236
return false, err
3337
}
3438
return true, nil

bundle/direct/dresources/app_space.go

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package dresources
22

33
import (
44
"context"
5-
"fmt"
6-
"time"
75

86
"github.com/databricks/cli/bundle/config/resources"
97
"github.com/databricks/databricks-sdk-go"
108
"github.com/databricks/databricks-sdk-go/common/types/fieldmask"
11-
"github.com/databricks/databricks-sdk-go/retries"
129
"github.com/databricks/databricks-sdk-go/service/apps"
1310
)
1411

@@ -29,53 +26,29 @@ func (r *ResourceAppSpace) DoRead(ctx context.Context, id string) (*apps.Space,
2926
}
3027

3128
func (r *ResourceAppSpace) DoCreate(ctx context.Context, config *apps.Space) (string, *apps.Space, error) {
32-
// Kick off the create request. Wait for the space to become active in
33-
// WaitAfterCreate so that parallel creates are not blocked here.
34-
_, err := r.client.Apps.CreateSpace(ctx, apps.CreateSpaceRequest{
29+
waiter, err := r.client.Apps.CreateSpace(ctx, apps.CreateSpaceRequest{
3530
Space: *config,
3631
})
3732
if err != nil {
3833
return "", nil, err
3934
}
40-
return config.Name, nil, nil
35+
space, err := waiter.Wait(ctx)
36+
if err != nil {
37+
return "", nil, err
38+
}
39+
return space.Name, space, nil
4140
}
4241

4342
func (r *ResourceAppSpace) DoUpdate(ctx context.Context, id string, config *apps.Space, _ *PlanEntry) (*apps.Space, error) {
44-
_, err := r.client.Apps.UpdateSpace(ctx, apps.UpdateSpaceRequest{
43+
waiter, err := r.client.Apps.UpdateSpace(ctx, apps.UpdateSpaceRequest{
4544
Name: id,
4645
Space: *config,
4746
UpdateMask: fieldmask.FieldMask{Paths: []string{"description", "resources", "user_api_scopes", "usage_policy_id"}},
4847
})
49-
return nil, err
50-
}
51-
52-
func (r *ResourceAppSpace) WaitAfterCreate(ctx context.Context, config *apps.Space) (*apps.Space, error) {
53-
return r.waitForSpaceActive(ctx, config.Name)
54-
}
55-
56-
func (r *ResourceAppSpace) WaitAfterUpdate(ctx context.Context, config *apps.Space) (*apps.Space, error) {
57-
return r.waitForSpaceActive(ctx, config.Name)
58-
}
59-
60-
func (r *ResourceAppSpace) waitForSpaceActive(ctx context.Context, name string) (*apps.Space, error) {
61-
retrier := retries.New[apps.Space](retries.WithTimeout(20 * time.Minute))
62-
return retrier.Run(ctx, func(ctx context.Context) (*apps.Space, error) {
63-
space, err := r.client.Apps.GetSpace(ctx, apps.GetSpaceRequest{Name: name})
64-
if err != nil {
65-
return nil, retries.Halt(err)
66-
}
67-
if space.Status == nil {
68-
return nil, retries.Continues("waiting for status")
69-
}
70-
switch space.Status.State {
71-
case apps.SpaceStatusSpaceStateSpaceActive:
72-
return space, nil
73-
case apps.SpaceStatusSpaceStateSpaceError:
74-
return nil, retries.Halt(fmt.Errorf("space %s is in ERROR state: %s", name, space.Status.Message))
75-
default:
76-
return nil, retries.Continues(fmt.Sprintf("space state: %s", space.Status.State))
77-
}
78-
})
48+
if err != nil {
49+
return nil, err
50+
}
51+
return waiter.Wait(ctx)
7952
}
8053

8154
func (r *ResourceAppSpace) DoDelete(ctx context.Context, id string) error {

0 commit comments

Comments
 (0)