@@ -2,9 +2,9 @@ package wait
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "net/http"
7- "strings"
88 "time"
99
1010 "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
@@ -23,76 +23,58 @@ const (
2323
2424// CreateInstanceWaitHandler will wait for instance creation
2525func CreateInstanceWaitHandler (ctx context.Context , a mariadb.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [mariadb.Instance ] {
26- handler := wait .New (func () (waitFinished bool , response * mariadb.Instance , err error ) {
27- s , err := a .GetInstance (ctx , projectId , instanceId ).Execute ()
28- if err != nil {
29- return false , nil , err
30- }
31- if s .Status == nil {
32- return false , nil , fmt .Errorf ("create failed for instance with id %s. The response is not valid: the status is missing" , instanceId )
33- }
34- switch * s .Status {
35- case INSTANCESTATUS_ACTIVE :
36- return true , s , nil
37- case INSTANCESTATUS_FAILED :
38- return true , s , fmt .Errorf ("create failed for instance with id %s: %s" , instanceId , s .LastOperation .Description )
39- }
40- return false , nil , nil
41- })
26+ waitConfig := wait.WaiterHelper [mariadb.Instance , string ]{
27+ FetchInstance : a .GetInstance (ctx , projectId , instanceId ).Execute ,
28+ GetState : func (s * mariadb.Instance ) (string , error ) {
29+ if s == nil || s .Status == nil {
30+ return "" , errors .New ("response or status is nil" )
31+ }
32+ return * s .Status , nil
33+ },
34+ ActiveState : []string {INSTANCESTATUS_ACTIVE },
35+ ErrorState : []string {INSTANCESTATUS_FAILED },
36+ }
37+
38+ handler := wait .New (waitConfig .Wait ())
4239 handler .SetTimeout (45 * time .Minute )
4340 return handler
4441}
4542
4643// PartialUpdateInstanceWaitHandler will wait for instance update
4744func PartialUpdateInstanceWaitHandler (ctx context.Context , a mariadb.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [mariadb.Instance ] {
48- handler := wait .New (func () (waitFinished bool , response * mariadb.Instance , err error ) {
49- s , err := a .GetInstance (ctx , projectId , instanceId ).Execute ()
50- if err != nil {
51- return false , nil , err
52- }
53- if s .Status == nil {
54- return false , nil , fmt .Errorf ("update failed for instance with id %s. The response is not valid: the instance id or the status are missing" , instanceId )
55- }
56- switch * s .Status {
57- case INSTANCESTATUS_ACTIVE :
58- return true , s , nil
59- case INSTANCESTATUS_FAILED :
60- return true , s , fmt .Errorf ("update failed for instance with id %s: %s" , instanceId , s .LastOperation .Description )
61- }
62- return false , nil , nil
63- })
45+ waitConfig := wait.WaiterHelper [mariadb.Instance , string ]{
46+ FetchInstance : a .GetInstance (ctx , projectId , instanceId ).Execute ,
47+ GetState : func (s * mariadb.Instance ) (string , error ) {
48+ if s == nil || s .Status == nil {
49+ return "" , errors .New ("response or status is nil" )
50+ }
51+ return * s .Status , nil
52+ },
53+ ActiveState : []string {INSTANCESTATUS_ACTIVE },
54+ ErrorState : []string {INSTANCESTATUS_FAILED },
55+ }
56+
57+ handler := wait .New (waitConfig .Wait ())
6458 handler .SetTimeout (45 * time .Minute )
6559 return handler
6660}
6761
6862// DeleteInstanceWaitHandler will wait for instance deletion
69- func DeleteInstanceWaitHandler (ctx context.Context , a mariadb.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [struct {}] {
70- handler := wait .New (func () (waitFinished bool , response * struct {}, err error ) {
71- s , err := a .GetInstance (ctx , projectId , instanceId ).Execute ()
72- if err == nil {
73- if s .Status == nil {
74- return false , nil , fmt .Errorf ("delete failed for instance with id %s. The response is not valid: The status is missing" , instanceId )
75- }
76- if * s .Status != INSTANCESTATUS_DELETING {
77- return false , nil , nil
63+ func DeleteInstanceWaitHandler (ctx context.Context , a mariadb.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [mariadb.Instance ] {
64+ waitConfig := wait.WaiterHelper [mariadb.Instance , string ]{
65+ FetchInstance : func () (* mariadb.Instance , error ) {
66+ return a .GetInstance (ctx , projectId , instanceId ).Execute ()
67+ },
68+ GetState : func (s * mariadb.Instance ) (string , error ) {
69+ if s == nil || s .Status == nil {
70+ return "" , errors .New ("response or status is nil" )
7871 }
79- if * s .Status == INSTANCESTATUS_ACTIVE {
80- if strings .Contains (s .LastOperation .Description , "DeleteFailed" ) || strings .Contains (s .LastOperation .Description , "failed" ) {
81- return true , nil , fmt .Errorf ("instance was deleted successfully but has errors: %s" , s .LastOperation .Description )
82- }
83- return true , nil , nil
84- }
85- return false , nil , nil
86- }
87- 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
88- if ! ok {
89- return false , nil , fmt .Errorf ("could not convert error to oapierror.GenericOpenAPIError" )
90- }
91- if oapiErr .StatusCode != http .StatusGone {
92- return false , nil , err
93- }
94- return true , nil , nil
95- })
72+ return * s .Status , nil
73+ },
74+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound , http .StatusGone },
75+ }
76+
77+ handler := wait .New (waitConfig .Wait ())
9678 handler .SetTimeout (15 * time .Minute )
9779 return handler
9880}
@@ -122,21 +104,22 @@ func CreateCredentialsWaitHandler(ctx context.Context, a mariadb.DefaultAPI, pro
122104}
123105
124106// DeleteCredentialsWaitHandler will wait for credentials deletion
125- func DeleteCredentialsWaitHandler (ctx context.Context , a mariadb.DefaultAPI , projectId , instanceId , credentialsId string ) * wait.AsyncActionHandler [struct {}] {
126- handler := wait .New (func () (waitFinished bool , response * struct {}, err error ) {
127- _ , err = a .GetCredentials (ctx , projectId , instanceId , credentialsId ).Execute ()
128- if err == nil {
129- return false , nil , nil
130- }
131- 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
132- if ! ok {
133- return false , nil , fmt .Errorf ("could not convert error to oapierror.GenericOpenAPIError" )
134- }
135- if oapiErr .StatusCode != http .StatusNotFound && oapiErr .StatusCode != http .StatusGone {
136- return false , nil , err
137- }
138- return true , nil , nil
139- })
107+ func DeleteCredentialsWaitHandler (ctx context.Context , a mariadb.DefaultAPI , projectId , instanceId , credentialsId string ) * wait.AsyncActionHandler [mariadb.CredentialsResponse ] {
108+ waitConfig := wait.WaiterHelper [mariadb.CredentialsResponse , string ]{
109+ FetchInstance : func () (* mariadb.CredentialsResponse , error ) {
110+ return a .GetCredentials (ctx , projectId , instanceId , credentialsId ).Execute ()
111+ },
112+ GetState : func (s * mariadb.CredentialsResponse ) (string , error ) {
113+ if s == nil || s .Id == "" {
114+ return "" , errors .New ("response or id is nil" )
115+ }
116+
117+ return s .Id , nil
118+ },
119+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound , http .StatusGone },
120+ }
121+
122+ handler := wait .New (waitConfig .Wait ())
140123 handler .SetTimeout (1 * time .Minute )
141124 return handler
142125}
0 commit comments