@@ -2,6 +2,7 @@ package dresources
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "time"
78
@@ -31,21 +32,28 @@ func (r *ResourceApp) DoRead(ctx context.Context, id string) (*apps.App, error)
3132}
3233
3334func (r * ResourceApp ) DoCreate (ctx context.Context , config * apps.App ) (string , * apps.App , error ) {
34- if err := r .waitForDeletion (ctx , config .Name ); err != nil {
35- return "" , nil , err
36- }
37-
3835 request := apps.CreateAppRequest {
3936 App : * config ,
4037 NoCompute : true ,
4138 ForceSendFields : nil ,
4239 }
43- waiter , err := r .client .Apps .Create (ctx , request )
40+
41+ retrier := retries .New [apps.App ](retries .WithTimeout (time .Minute ), retries .WithRetryFunc (shouldRetry ))
42+ app , err := retrier .Run (ctx , func (ctx context.Context ) (* apps.App , error ) {
43+ waiter , err := r .client .Apps .Create (ctx , request )
44+ if err != nil {
45+ if errors .Is (err , apierr .ErrResourceAlreadyExists ) {
46+ return nil , retries .Continues ("app already exists, retrying" )
47+ }
48+ return nil , retries .Halt (err )
49+ }
50+ return waiter .Response , nil
51+ })
4452 if err != nil {
4553 return "" , nil , err
4654 }
4755
48- return waiter . Response .Name , nil , nil
56+ return app .Name , nil , nil
4957}
5058
5159func (r * ResourceApp ) DoUpdate (ctx context.Context , id string , config * apps.App , _ * Changes ) (* apps.App , error ) {
@@ -80,32 +88,6 @@ func (r *ResourceApp) WaitAfterCreate(ctx context.Context, config *apps.App) (*a
8088 return r .waitForApp (ctx , r .client , config .Name )
8189}
8290
83- func (r * ResourceApp ) waitForDeletion (ctx context.Context , name string ) error {
84- retrier := retries .New [struct {}](retries .WithTimeout (10 * time .Minute ), retries .WithRetryFunc (shouldRetry ))
85- _ , err := retrier .Run (ctx , func (ctx context.Context ) (* struct {}, error ) {
86- app , err := r .client .Apps .GetByName (ctx , name )
87- if err != nil {
88- if apierr .IsMissing (err ) {
89- return nil , nil
90- }
91- return nil , retries .Halt (err )
92- }
93-
94- if app .ComputeStatus == nil {
95- return nil , retries .Continues ("waiting for compute status" )
96- }
97-
98- switch app .ComputeStatus .State {
99- case apps .ComputeStateDeleting :
100- return nil , retries .Continues ("app is deleting" )
101- default :
102- err := fmt .Errorf ("app %s already exists" , name )
103- return nil , retries .Halt (err )
104- }
105- })
106- return err
107- }
108-
10991// waitForApp waits for the app to reach the target state. The target state is either ACTIVE or STOPPED.
11092// Apps with no_compute set to true will reach the STOPPED state, otherwise they will reach the ACTIVE state.
11193// We can't use the default waiter from SDK because it only waits on ACTIVE state but we need also STOPPED state.
0 commit comments