@@ -3,11 +3,9 @@ package wait
33import (
44 "context"
55 "errors"
6- "fmt"
76 "net/http"
87 "time"
98
10- "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
119 "github.com/stackitcloud/stackit-sdk-go/core/wait"
1210 intake "github.com/stackitcloud/stackit-sdk-go/services/intake/v1betaapi"
1311)
@@ -27,133 +25,117 @@ const (
2725 INTAKEUSERRESPONSESTATE_DELETING = "deleting"
2826)
2927
30- func CreateOrUpdateIntakeRunnerWaitHandler (ctx context.Context , a intake.DefaultAPI , projectId , region , intakeRunnerId string ) * wait.AsyncActionHandler [intake.IntakeRunnerResponse ] {
31- handler := wait .New (func () (waitFinished bool , response * intake.IntakeRunnerResponse , err error ) {
32- runner , err := a .GetIntakeRunner (ctx , projectId , region , intakeRunnerId ).Execute ()
33- if err != nil {
34- return false , nil , err
35- }
36-
37- if runner == nil {
38- return false , nil , fmt .Errorf ("API returned a nil response for Intake Runner %s" , intakeRunnerId )
39- }
40-
41- if runner .Id == intakeRunnerId && runner .State == INTAKERUNNERRESPONSESTATE_ACTIVE {
42- return true , runner , nil
43- }
44-
28+ func CreateOrUpdateIntakeRunnerWaitHandler (ctx context.Context , client intake.DefaultAPI , projectId , region , intakeRunnerId string ) * wait.AsyncActionHandler [intake.IntakeRunnerResponse ] {
29+ waitConfig := wait.WaiterHelper [intake.IntakeRunnerResponse , string ]{
30+ FetchInstance : client .GetIntakeRunner (ctx , projectId , region , intakeRunnerId ).Execute ,
31+ GetState : func (response * intake.IntakeRunnerResponse ) (string , error ) {
32+ if response == nil {
33+ return "" , errors .New ("empty response" )
34+ }
35+ return response .State , nil
36+ },
37+ ActiveState : []string {INTAKERUNNERRESPONSESTATE_ACTIVE },
38+ ErrorState : []string {},
4539 // The API does not have a dedicated failure state for this resource,
4640 // so we rely on the timeout for cases where it never becomes active.
47- return false , nil , nil
48- })
41+ }
42+
43+ handler := wait .New (waitConfig .Wait ())
4944 handler .SetTimeout (15 * time .Minute )
5045 return handler
5146}
5247
53- func DeleteIntakeRunnerWaitHandler (ctx context.Context , a intake.DefaultAPI , projectId , region , intakeRunnerId string ) * wait.AsyncActionHandler [intake.IntakeRunnerResponse ] {
54- handler := wait .New (func () (waitFinished bool , response * intake.IntakeRunnerResponse , err error ) {
55- _ , err = a .GetIntakeRunner (ctx , projectId , region , intakeRunnerId ).Execute ()
56- if err == nil {
57- // Resource still exists
58- return false , nil , nil
59- }
60-
61- var oapiError * oapierror.GenericOpenAPIError
62- if errors .As (err , & oapiError ) {
63- if oapiError .StatusCode == http .StatusNotFound {
64- // Success: Resource is gone
65- return true , nil , nil
48+ func DeleteIntakeRunnerWaitHandler (ctx context.Context , client intake.DefaultAPI , projectId , region , intakeRunnerId string ) * wait.AsyncActionHandler [intake.IntakeRunnerResponse ] {
49+ waitConfig := wait.WaiterHelper [intake.IntakeRunnerResponse , string ]{
50+ FetchInstance : client .GetIntakeRunner (ctx , projectId , region , intakeRunnerId ).Execute ,
51+ GetState : func (response * intake.IntakeRunnerResponse ) (string , error ) {
52+ if response == nil {
53+ return "" , errors .New ("empty response" )
6654 }
67- }
68- // An unexpected error occurred
69- return false , nil , err
70- })
55+ return response .State , nil
56+ },
57+ ActiveState : []string {},
58+ ErrorState : []string {},
59+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound },
60+ }
61+
62+ handler := wait .New (waitConfig .Wait ())
7163 handler .SetTimeout (15 * time .Minute )
7264 return handler
7365}
7466
75- func CreateOrUpdateIntakeWaitHandler (ctx context.Context , a intake.DefaultAPI , projectId , region , intakeId string ) * wait.AsyncActionHandler [intake.IntakeResponse ] {
76- handler := wait .New (func () (waitFinished bool , response * intake.IntakeResponse , err error ) {
77- ik , err := a .GetIntake (ctx , projectId , region , intakeId ).Execute ()
78- if err != nil {
79- return false , nil , err
80- }
81-
82- if ik == nil {
83- return false , nil , fmt .Errorf ("API returned a nil response for Intake %s" , intakeId )
84- }
85-
86- if ik .Id == intakeId && ik .State == INTAKERESPONSESTATE_ACTIVE {
87- return true , ik , nil
88- }
89-
90- if ik .Id == intakeId && ik .State == INTAKERESPONSESTATE_FAILED {
91- return true , ik , fmt .Errorf ("create/update failed for Intake %s" , intakeId )
92- }
67+ func CreateOrUpdateIntakeWaitHandler (ctx context.Context , client intake.DefaultAPI , projectId , region , intakeId string ) * wait.AsyncActionHandler [intake.IntakeResponse ] {
68+ waitConfig := wait.WaiterHelper [intake.IntakeResponse , string ]{
69+ FetchInstance : client .GetIntake (ctx , projectId , region , intakeId ).Execute ,
70+ GetState : func (response * intake.IntakeResponse ) (string , error ) {
71+ if response == nil {
72+ return "" , errors .New ("empty response" )
73+ }
74+ return response .State , nil
75+ },
76+ ActiveState : []string {INTAKERUNNERRESPONSESTATE_ACTIVE },
77+ ErrorState : []string {INTAKERESPONSESTATE_FAILED },
78+ }
9379
94- return false , nil , nil
95- })
80+ handler := wait .New (waitConfig .Wait ())
9681 handler .SetTimeout (10 * time .Minute )
9782 return handler
9883}
9984
100- func DeleteIntakeWaitHandler (ctx context.Context , a intake.DefaultAPI , projectId , region , intakeId string ) * wait.AsyncActionHandler [intake.IntakeResponse ] {
101- handler := wait .New (func () (waitFinished bool , response * intake.IntakeResponse , err error ) {
102- _ , err = a .GetIntake (ctx , projectId , region , intakeId ).Execute ()
103- if err == nil {
104- return false , nil , nil
105- }
106-
107- var oapiError * oapierror.GenericOpenAPIError
108- if errors .As (err , & oapiError ) {
109- if oapiError .StatusCode == http .StatusNotFound {
110- return true , nil , nil
85+ func DeleteIntakeWaitHandler (ctx context.Context , client intake.DefaultAPI , projectId , region , intakeId string ) * wait.AsyncActionHandler [intake.IntakeResponse ] {
86+ waitConfig := wait.WaiterHelper [intake.IntakeResponse , string ]{
87+ FetchInstance : client .GetIntake (ctx , projectId , region , intakeId ).Execute ,
88+ GetState : func (response * intake.IntakeResponse ) (string , error ) {
89+ if response == nil {
90+ return "" , errors .New ("empty response" )
11191 }
112- }
113- return false , nil , err
114- })
92+ return response .State , nil
93+ },
94+ ActiveState : []string {},
95+ ErrorState : []string {},
96+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound },
97+ }
98+
99+ handler := wait .New (waitConfig .Wait ())
115100 handler .SetTimeout (10 * time .Minute )
116101 return handler
117102}
118103
119- func CreateOrUpdateIntakeUserWaitHandler (ctx context.Context , a intake.DefaultAPI , projectId , region , intakeId , intakeUserId string ) * wait.AsyncActionHandler [intake.IntakeUserResponse ] {
120- handler := wait .New (func () (waitFinished bool , response * intake.IntakeUserResponse , err error ) {
121- user , err := a .GetIntakeUser (ctx , projectId , region , intakeId , intakeUserId ).Execute ()
122- if err != nil {
123- return false , nil , err
124- }
125-
126- if user == nil {
127- return false , nil , fmt .Errorf ("API returned a nil response for Intake User %s" , intakeUserId )
128- }
129-
130- if user .Id == intakeUserId && user .State == INTAKEUSERRESPONSESTATE_ACTIVE {
131- return true , user , nil
132- }
133-
134- // The API does not have a dedicated failure state for this resource, we rely on the timeout for cases where
135- // it never becomes active.
136- return false , nil , nil
137- })
104+ func CreateOrUpdateIntakeUserWaitHandler (ctx context.Context , client intake.DefaultAPI , projectId , region , intakeId , intakeUserId string ) * wait.AsyncActionHandler [intake.IntakeUserResponse ] {
105+ waitConfig := wait.WaiterHelper [intake.IntakeUserResponse , string ]{
106+ FetchInstance : client .GetIntakeUser (ctx , projectId , region , intakeId , intakeUserId ).Execute ,
107+ GetState : func (response * intake.IntakeUserResponse ) (string , error ) {
108+ if response == nil {
109+ return "" , errors .New ("empty response" )
110+ }
111+ return response .State , nil
112+ },
113+ ActiveState : []string {INTAKEUSERRESPONSESTATE_ACTIVE },
114+ ErrorState : []string {},
115+ // The API does not have a dedicated failure state for this resource,
116+ // so we rely on the timeout for cases where it never becomes active.
117+ }
118+
119+ handler := wait .New (waitConfig .Wait ())
138120 handler .SetTimeout (5 * time .Minute )
139121 return handler
140122}
141123
142- func DeleteIntakeUserWaitHandler (ctx context.Context , a intake.DefaultAPI , projectId , region , intakeId , intakeUserId string ) * wait.AsyncActionHandler [intake.IntakeUserResponse ] {
143- handler := wait .New (func () (waitFinished bool , response * intake.IntakeUserResponse , err error ) {
144- _ , err = a .GetIntakeUser (ctx , projectId , region , intakeId , intakeUserId ).Execute ()
145- if err == nil {
146- return false , nil , nil
147- }
148-
149- var oapiError * oapierror.GenericOpenAPIError
150- if errors .As (err , & oapiError ) {
151- if oapiError .StatusCode == http .StatusNotFound {
152- return true , nil , nil
124+ func DeleteIntakeUserWaitHandler (ctx context.Context , client intake.DefaultAPI , projectId , region , intakeId , intakeUserId string ) * wait.AsyncActionHandler [intake.IntakeUserResponse ] {
125+ waitConfig := wait.WaiterHelper [intake.IntakeUserResponse , string ]{
126+ FetchInstance : client .GetIntakeUser (ctx , projectId , region , intakeId , intakeUserId ).Execute ,
127+ GetState : func (response * intake.IntakeUserResponse ) (string , error ) {
128+ if response == nil {
129+ return "" , errors .New ("empty response" )
153130 }
154- }
155- return false , nil , err
156- })
131+ return response .State , nil
132+ },
133+ ActiveState : []string {},
134+ ErrorState : []string {},
135+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound },
136+ }
137+
138+ handler := wait .New (waitConfig .Wait ())
157139 handler .SetTimeout (5 * time .Minute )
158140 return handler
159141}
0 commit comments