@@ -3,11 +3,8 @@ package wait
33import (
44 "context"
55 "errors"
6- "fmt"
7- "net/http"
86 "time"
97
10- "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
118 "github.com/stackitcloud/stackit-sdk-go/core/wait"
129 alb "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
1310)
@@ -21,44 +18,43 @@ const (
2118)
2219
2320func CreateOrUpdateLoadbalancerWaitHandler (ctx context.Context , client alb.DefaultAPI , projectId , region , name string ) * wait.AsyncActionHandler [alb.LoadBalancer ] {
24- handler := wait .New (func () (bool , * alb.LoadBalancer , error ) {
25- response , err := client .GetLoadBalancer (ctx , projectId , region , name ).Execute ()
26- if err != nil {
27- return false , nil , err
28- }
29- if response .HasStatus () {
30- switch * response .Status {
31- case LOADBALANCERSTATUS_PENDING :
32- return false , nil , nil
33- case LOADBALANCERSTATUS_UNSPECIFIED :
34- return false , nil , nil
35- case LOADBALANCERSTATUS_ERROR :
36- return true , response , fmt .Errorf ("loadbalancer in error: %s" , * response .Status )
37- default :
38- return true , response , nil
21+ waitConfig := wait.WaiterHelper [alb.LoadBalancer , string ]{
22+ FetchInstance : client .GetLoadBalancer (ctx , projectId , region , name ).Execute ,
23+ GetState : func (response * alb.LoadBalancer ) (string , error ) {
24+ if response == nil {
25+ return "" , errors .New ("empty response" )
3926 }
40- }
27+ if response .Status == nil {
28+ return "" , errors .New ("status is missing in response" )
29+ }
30+ return * response .Status , nil
31+ },
32+ ActiveState : []string {LOADBALANCERSTATUS_READY },
33+ ErrorState : []string {LOADBALANCERSTATUS_ERROR },
34+ }
4135
42- return false , nil , nil
43- })
36+ handler := wait .New (waitConfig .Wait ())
4437 handler .SetTimeout (10 * time .Minute )
4538 return handler
4639}
4740
4841func DeleteLoadbalancerWaitHandler (ctx context.Context , client alb.DefaultAPI , projectId , region , name string ) * wait.AsyncActionHandler [alb.LoadBalancer ] {
49- handler := wait .New ( func () ( bool , * alb.LoadBalancer , error ) {
50- _ , err := client .GetLoadBalancer (ctx , projectId , region , name ).Execute ()
51- if err != nil {
52- var apiErr * oapierror. GenericOpenAPIError
53- if errors .As ( err , & apiErr ) {
54- if statusCode := apiErr . StatusCode ; statusCode == http . StatusNotFound || statusCode == http . StatusGone {
55- return true , nil , nil
56- }
42+ waitConfig := wait.WaiterHelper [ alb.LoadBalancer , string ] {
43+ FetchInstance : client .GetLoadBalancer (ctx , projectId , region , name ).Execute ,
44+ GetState : func ( response * alb. LoadBalancer ) ( string , error ) {
45+ if response == nil {
46+ return "" , errors .New ( "empty response" )
47+ }
48+ if response . Status == nil {
49+ return "" , errors . New ( "status is missing in response" )
5750 }
58- return true , nil , err
59- }
60- return false , nil , nil
61- })
51+ return * response .Status , nil
52+ },
53+ ActiveState : []string {},
54+ ErrorState : []string {LOADBALANCERSTATUS_ERROR },
55+ }
56+
57+ handler := wait .New (waitConfig .Wait ())
6258 handler .SetTimeout (10 * time .Minute )
6359 return handler
6460}
0 commit comments