@@ -2,6 +2,7 @@ package wait
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "net/http"
78 "strings"
@@ -23,44 +24,38 @@ const (
2324
2425// CreateInstanceWaitHandler will wait for instance creation
2526func 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- })
27+ waitConfig := wait.WaiterHelper [mariadb.Instance , string ]{
28+ FetchInstance : a .GetInstance (ctx , projectId , instanceId ).Execute ,
29+ GetState : func (s * mariadb.Instance ) (string , error ) {
30+ if s == nil || s .Status == nil {
31+ return "" , errors .New ("response or status is nil" )
32+ }
33+ return * s .Status , nil
34+ },
35+ ActiveState : []string {INSTANCESTATUS_ACTIVE },
36+ ErrorState : []string {INSTANCESTATUS_FAILED },
37+ }
38+
39+ handler := wait .New (waitConfig .Wait ())
4240 handler .SetTimeout (45 * time .Minute )
4341 return handler
4442}
4543
4644// PartialUpdateInstanceWaitHandler will wait for instance update
4745func 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- })
46+ waitConfig := wait.WaiterHelper [mariadb.Instance , string ]{
47+ FetchInstance : a .GetInstance (ctx , projectId , instanceId ).Execute ,
48+ GetState : func (s * mariadb.Instance ) (string , error ) {
49+ if s == nil || s .Status == nil {
50+ return "" , errors .New ("response or status is nil" )
51+ }
52+ return * s .Status , nil
53+ },
54+ ActiveState : []string {INSTANCESTATUS_ACTIVE },
55+ ErrorState : []string {INSTANCESTATUS_FAILED },
56+ }
57+
58+ handler := wait .New (waitConfig .Wait ())
6459 handler .SetTimeout (45 * time .Minute )
6560 return handler
6661}
0 commit comments