@@ -2,7 +2,7 @@ package wait
22
33import (
44 "context"
5- "fmt "
5+ "errors "
66 "time"
77
88 "github.com/stackitcloud/stackit-sdk-go/core/wait"
@@ -16,55 +16,46 @@ const (
1616 SERVICESTATUSSTATE_DISABLING = "DISABLING"
1717)
1818
19+ // EnableServiceWaitHandler will wait for service enablement
1920func EnableServiceWaitHandler (ctx context.Context , a serviceenablement.DefaultAPI , region , projectId , serviceId string ) * wait.AsyncActionHandler [serviceenablement.ServiceStatus ] {
20- handler := wait .New (func () (waitFinished bool , response * serviceenablement.ServiceStatus , err error ) {
21- s , err := a .GetServiceStatusRegional (ctx , region , projectId , serviceId ).Execute ()
22- if err != nil {
23- return false , nil , err
24- }
25- if s == nil || s .State == nil {
26- return false , nil , nil
27- }
28- switch * s .State {
29- default :
30- return true , s , fmt .Errorf ("service with id %s has unexpected state %s" , serviceId , * s .State )
31- case SERVICESTATUSSTATE_ENABLED :
32- return true , s , nil
33- case SERVICESTATUSSTATE_ENABLING :
34- return false , nil , nil
35- case SERVICESTATUSSTATE_DISABLED :
36- return true , s , fmt .Errorf ("enabling failed for service with id %s" , serviceId )
37- case SERVICESTATUSSTATE_DISABLING :
38- return true , s , fmt .Errorf ("service with id %s is in state %s" , serviceId , * s .State )
39- }
40- })
21+ waitConfig := wait.WaiterHelper [serviceenablement.ServiceStatus , string ]{
22+ FetchInstance : a .GetServiceStatusRegional (ctx , region , projectId , serviceId ).Execute ,
23+ GetState : func (s * serviceenablement.ServiceStatus ) (string , error ) {
24+ if s == nil {
25+ return "" , errors .New ("empty response" )
26+ }
27+ if s .State == nil {
28+ return "" , errors .New ("state is missing" )
29+ }
30+ return * s .State , nil
31+ },
32+ ActiveState : []string {SERVICESTATUSSTATE_ENABLED },
33+ ErrorState : []string {SERVICESTATUSSTATE_DISABLED , SERVICESTATUSSTATE_DISABLING },
34+ }
4135
36+ handler := wait .New (waitConfig .Wait ())
4237 handler .SetTimeout (45 * time .Minute ).SetSleepBeforeWait (15 * time .Second )
4338 return handler
4439}
4540
41+ // DisableServiceWaitHandler will wait for service disablement
4642func DisableServiceWaitHandler (ctx context.Context , a serviceenablement.DefaultAPI , region , projectId , serviceId string ) * wait.AsyncActionHandler [serviceenablement.ServiceStatus ] {
47- handler := wait .New (func () (waitFinished bool , response * serviceenablement.ServiceStatus , err error ) {
48- s , err := a .GetServiceStatusRegional (ctx , region , projectId , serviceId ).Execute ()
49- if err != nil {
50- return false , nil , err
51- }
52- if s == nil || s .State == nil {
53- return false , nil , nil
54- }
55- switch * s .State {
56- default :
57- return true , s , fmt .Errorf ("service with id %s has unexpected state %s" , serviceId , * s .State )
58- case SERVICESTATUSSTATE_DISABLED :
59- return true , s , nil
60- case SERVICESTATUSSTATE_DISABLING :
61- return false , nil , nil
62- case SERVICESTATUSSTATE_ENABLED :
63- return true , s , fmt .Errorf ("disabling failed for service with id %s" , serviceId )
64- case SERVICESTATUSSTATE_ENABLING :
65- return true , s , fmt .Errorf ("service with id %s is in state %s" , serviceId , * s .State )
66- }
67- })
43+ waitConfig := wait.WaiterHelper [serviceenablement.ServiceStatus , string ]{
44+ FetchInstance : a .GetServiceStatusRegional (ctx , region , projectId , serviceId ).Execute ,
45+ GetState : func (s * serviceenablement.ServiceStatus ) (string , error ) {
46+ if s == nil {
47+ return "" , errors .New ("empty response" )
48+ }
49+ if s .State == nil {
50+ return "" , errors .New ("status is missing" )
51+ }
52+ return * s .State , nil
53+ },
54+ ActiveState : []string {SERVICESTATUSSTATE_DISABLED },
55+ ErrorState : []string {SERVICESTATUSSTATE_ENABLED , SERVICESTATUSSTATE_ENABLING },
56+ }
57+
58+ handler := wait .New (waitConfig .Wait ())
6859 handler .SetTimeout (45 * time .Minute ).SetSleepBeforeWait (15 * time .Second )
6960 return handler
7061}
0 commit comments