@@ -2,12 +2,11 @@ package wait
22
33import (
44 "context"
5+ "errors"
56 "fmt"
6- "net/http"
77 "strings"
88 "time"
99
10- "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1110 "github.com/stackitcloud/stackit-sdk-go/core/wait"
1211 loadbalancer "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api"
1312)
@@ -23,58 +22,42 @@ const (
2322
2423// CreateLoadBalancerWaitHandler will wait for load balancer creation
2524func CreateLoadBalancerWaitHandler (ctx context.Context , a loadbalancer.DefaultAPI , projectId , region , instanceName string ) * wait.AsyncActionHandler [loadbalancer.LoadBalancer ] {
26- handler := wait .New (func () (waitFinished bool , response * loadbalancer.LoadBalancer , err error ) {
27- s , err := a .GetLoadBalancer (ctx , projectId , region , instanceName ).Execute ()
28- if err != nil {
29- return false , nil , err
30- }
31- if s == nil || s .Name == nil || * s .Name != instanceName || s .Status == nil {
32- return false , nil , nil
33- }
34-
35- var errors []string
36- if len (s .Errors ) > 0 {
37- for _ , err := range s .Errors {
38- errors = append (errors , fmt .Sprintf ("%s: %s" , * err .Type , * err .Description ))
25+ waitConfig := wait.WaiterHelper [loadbalancer.LoadBalancer , string ]{
26+ FetchInstance : a .GetLoadBalancer (ctx , projectId , region , instanceName ).Execute ,
27+ GetState : func (r * loadbalancer.LoadBalancer ) (string , error ) {
28+ if r == nil || r .Status == nil {
29+ return "" , errors .New ("response or status is nil" )
3930 }
40- return true , s , fmt .Errorf ("create failed for instance with name %s, got status %s and errors: %s" , instanceName , * s .Status , strings .Join (errors , ";" ))
41- }
42-
43- switch * s .Status {
44- case LOADBALANCERSTATUS_READY :
45- return true , s , nil
46- case LOADBALANCERSTATUS_UNSPECIFIED :
47- return false , nil , nil
48- case LOADBALANCERSTATUS_PENDING :
49- return false , nil , nil
50- case LOADBALANCERSTATUS_TERMINATING :
51- return true , s , fmt .Errorf ("create failed for instance with name %s, got status %s" , instanceName , LOADBALANCERSTATUS_TERMINATING )
52- case LOADBALANCERSTATUS_ERROR :
53- return true , s , fmt .Errorf ("create failed for instance with name %s, got status %s" , instanceName , LOADBALANCERSTATUS_ERROR )
54- default :
55- return true , s , fmt .Errorf ("instance with name %s has unexpected status %s" , instanceName , * s .Status )
56- }
57- })
31+ var sb strings.Builder
32+ if r .Errors != nil {
33+ for _ , err := range r .Errors {
34+ sb .WriteString (fmt .Sprintf ("%s: %s; " , * err .Type , * err .Description ))
35+ }
36+ return "" , fmt .Errorf ("create failed for instance with name %s, got status %s and errors: %s" , instanceName , * r .Status , sb .String ())
37+ }
38+ return * r .Status , nil
39+ },
40+ ActiveState : []string {LOADBALANCERSTATUS_READY },
41+ ErrorState : []string {LOADBALANCERSTATUS_TERMINATING , LOADBALANCERSTATUS_ERROR },
42+ }
43+ handler := wait .New (waitConfig .Wait ())
5844 handler .SetTimeout (45 * time .Minute )
5945 return handler
6046}
6147
6248// DeleteLoadBalancerWaitHandler will wait for load balancer deletion
63- func DeleteLoadBalancerWaitHandler (ctx context.Context , a loadbalancer.DefaultAPI , projectId , region , instanceId string ) * wait.AsyncActionHandler [struct {}] {
64- handler := wait .New (func () (waitFinished bool , response * struct {}, err error ) {
65- _ , err = a .GetLoadBalancer (ctx , projectId , region , instanceId ).Execute ()
66- if err == nil {
67- return false , nil , nil
68- }
69- 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
70- if ! ok {
71- return false , nil , fmt .Errorf ("could not convert error to oapierror.GenericOpenAPIError" )
72- }
73- if oapiErr .StatusCode != http .StatusNotFound {
74- return false , nil , err
75- }
76- return true , nil , nil
77- })
49+ func DeleteLoadBalancerWaitHandler (ctx context.Context , a loadbalancer.DefaultAPI , projectId , region , instanceId string ) * wait.AsyncActionHandler [loadbalancer.LoadBalancer ] {
50+ waitConfig := wait.WaiterHelper [loadbalancer.LoadBalancer , string ]{
51+ FetchInstance : a .GetLoadBalancer (ctx , projectId , region , instanceId ).Execute ,
52+ GetState : func (l * loadbalancer.LoadBalancer ) (string , error ) {
53+ if l == nil || l .Status == nil {
54+ return "" , errors .New ("response or status is nil" )
55+ }
56+ return * l .Status , nil
57+ },
58+ ErrorState : []string {LOADBALANCERSTATUS_ERROR },
59+ }
60+ handler := wait .New (waitConfig .Wait ())
7861 handler .SetTimeout (15 * time .Minute )
7962 return handler
8063}
0 commit comments