@@ -2,7 +2,9 @@ package wait
22
33import (
44 "context"
5+ "errors"
56 "fmt"
7+ "net/http"
68 "time"
79
810 "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
@@ -68,28 +70,26 @@ func CreateInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, p
6870}
6971
7072// PartialUpdateInstanceWaitHandler will wait for instance update
71- func PartialUpdateInstanceWaitHandler (ctx context.Context , a postgresflex.DefaultAPI , projectId , region , instanceId string ) * wait.AsyncActionHandler [postgresflex.InstanceResponse ] {
72- handler := wait .New (func () (waitFinished bool , response * postgresflex.InstanceResponse , err error ) {
73- s , err := a .GetInstance (ctx , projectId , region , instanceId ).Execute ()
74- if err != nil {
75- return false , nil , err
76- }
77- if s == nil || s .Item == nil || s .Item .Id == nil || * s .Item .Id != instanceId || s .Item .Status == nil {
78- return false , nil , nil
79- }
80- switch * s .Item .Status {
81- default :
82- return true , s , fmt .Errorf ("instance with id %s has unexpected status %s" , instanceId , * s .Item .Status )
83- case InstanceStateEmpty :
84- return false , nil , nil
85- case InstanceStateProgressing :
86- return false , nil , nil
87- case InstanceStateSuccess :
88- return true , s , nil
89- case InstanceStateFailed :
90- return true , s , fmt .Errorf ("update failed for instance with id %s" , instanceId )
91- }
92- })
73+ func PartialUpdateInstanceWaitHandler (ctx context.Context , client postgresflex.DefaultAPI , projectId , region , instanceId string ) * wait.AsyncActionHandler [postgresflex.InstanceResponse ] {
74+ waitConfig := wait.WaiterHelper [postgresflex.InstanceResponse , string ]{
75+ FetchInstance : client .GetInstance (ctx , projectId , region , instanceId ).Execute ,
76+ GetState : func (response * postgresflex.InstanceResponse ) (string , error ) {
77+ if response == nil {
78+ return "" , errors .New ("empty response" )
79+ }
80+ if response .Item == nil {
81+ return "" , errors .New ("empty instance" )
82+ }
83+ if response .Item .Status == nil {
84+ return "" , errors .New ("status is missing" )
85+ }
86+ return * response .Item .Status , nil
87+ },
88+ ActiveState : []string {InstanceStateSuccess },
89+ ErrorState : []string {InstanceStateFailed },
90+ }
91+
92+ handler := wait .New (waitConfig .Wait ())
9393 handler .SetTimeout (45 * time .Minute )
9494 return handler
9595}
@@ -118,21 +118,22 @@ func DeleteInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, p
118118}
119119
120120// ForceDeleteInstanceWaitHandler will wait for instance deletion
121- func ForceDeleteInstanceWaitHandler (ctx context.Context , a postgresflex.DefaultAPI , projectId , region , instanceId string ) * wait.AsyncActionHandler [struct {}] {
122- handler := wait .New (func () (waitFinished bool , response * struct {}, err error ) {
123- _ , err = a .GetInstance (ctx , projectId , region , instanceId ).Execute ()
124- if err == nil {
125- return false , nil , nil
126- }
127- oapiErr , ok := err .(* oapierror.GenericOpenAPIError ) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
128- if ! ok {
129- return false , nil , err
130- }
131- if oapiErr .StatusCode != 404 {
132- return false , nil , err
133- }
134- return true , nil , nil
135- })
121+ func ForceDeleteInstanceWaitHandler (ctx context.Context , client postgresflex.DefaultAPI , projectId , region , instanceId string ) * wait.AsyncActionHandler [postgresflex.InstanceResponse ] {
122+ waitConfig := wait.WaiterHelper [postgresflex.InstanceResponse , string ]{
123+ FetchInstance : client .GetInstance (ctx , projectId , instanceId , region ).Execute ,
124+ GetState : func (response * postgresflex.InstanceResponse ) (string , error ) {
125+ if response == nil {
126+ return "" , errors .New ("empty response" )
127+ }
128+ if response .Item .Status == nil {
129+ return "" , errors .New ("status is missing in response" )
130+ }
131+ return * response .Item .Status , nil
132+ },
133+ ErrorState : []string {InstanceStateFailed },
134+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound },
135+ }
136+ handler := wait .New (waitConfig .Wait ())
136137 handler .SetTimeout (15 * time .Minute )
137138 return handler
138139}
0 commit comments