@@ -2,6 +2,7 @@ package wait
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "net/http"
78 "strings"
@@ -27,44 +28,44 @@ const (
2728
2829// CreateInstanceWaitHandler will wait for instance creation
2930func CreateInstanceWaitHandler (ctx context.Context , a opensearch.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [opensearch.Instance ] {
30- handler := wait .New ( func () ( waitFinished bool , response * opensearch.Instance , err error ) {
31- s , err := a .GetInstance (ctx , projectId , instanceId ).Execute ()
32- if err != nil {
33- return false , nil , err
34- }
35- if s . Status == nil {
36- return false , nil , fmt . Errorf ( "create failed for instance with id %s. The response is not valid: the status are missing" , instanceId )
37- }
38- switch * s . Status {
39- case INSTANCESTATUS_ACTIVE :
40- return true , s , nil
41- case INSTANCESTATUS_FAILED :
42- return true , s , fmt . Errorf ( "create failed for instance with id %s: %s" , instanceId , s . LastOperation . Description )
43- }
44- return false , nil , nil
45- } )
31+ waitConfig := wait.WaiterHelper [ opensearch.Instance , string ] {
32+ FetchInstance : a .GetInstance (ctx , projectId , instanceId ).Execute ,
33+ GetState : func ( s * opensearch. Instance ) ( string , error ) {
34+ if s == nil {
35+ return "" , errors . New ( "empty response" )
36+ }
37+ if s . Status == nil {
38+ return "" , errors . New ( "status is missing" )
39+ }
40+ return * s . Status , nil
41+ },
42+ ActiveState : [] string { INSTANCESTATUS_ACTIVE },
43+ ErrorState : [] string { INSTANCESTATUS_FAILED },
44+ }
45+
46+ handler := wait . New ( waitConfig . Wait () )
4647 handler .SetTimeout (45 * time .Minute )
4748 return handler
4849}
4950
5051// PartialUpdateInstanceWaitHandler will wait for instance update
5152func PartialUpdateInstanceWaitHandler (ctx context.Context , a opensearch.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [opensearch.Instance ] {
52- handler := wait .New ( func () ( waitFinished bool , response * opensearch.Instance , err error ) {
53- s , err := a .GetInstance (ctx , projectId , instanceId ).Execute ()
54- if err != nil {
55- return false , nil , err
56- }
57- if s . Status == nil {
58- 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 )
59- }
60- switch * s . Status {
61- case INSTANCESTATUS_ACTIVE :
62- return true , s , nil
63- case INSTANCESTATUS_FAILED :
64- return true , s , fmt . Errorf ( "update failed for instance with id %s: %s" , instanceId , s . LastOperation . Description )
65- }
66- return false , nil , nil
67- } )
53+ waitConfig := wait.WaiterHelper [ opensearch.Instance , string ] {
54+ FetchInstance : a .GetInstance (ctx , projectId , instanceId ).Execute ,
55+ GetState : func ( s * opensearch. Instance ) ( string , error ) {
56+ if s == nil {
57+ return "" , errors . New ( "empty response" )
58+ }
59+ if s . Status == nil {
60+ return "" , errors . New ( "status is missing" )
61+ }
62+ return * s . Status , nil
63+ },
64+ ActiveState : [] string { INSTANCESTATUS_ACTIVE },
65+ ErrorState : [] string { INSTANCESTATUS_FAILED },
66+ }
67+
68+ handler := wait . New ( waitConfig . Wait () )
6869 handler .SetTimeout (45 * time .Minute )
6970 return handler
7071}
0 commit comments