@@ -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 sfs "github.com/stackitcloud/stackit-sdk-go/services/sfs/v1api"
1310)
@@ -30,149 +27,117 @@ const (
3027)
3128
3229func CreateResourcePoolWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId string ) * wait.AsyncActionHandler [sfs.GetResourcePoolResponse ] {
33- handler := wait .New (func () (waitFinished bool , resourcePool * sfs.GetResourcePoolResponse , err error ) {
34- resourcePool , err = api .GetResourcePool (ctx , projectId , region , resourcePoolId ).Execute ()
35- if err != nil {
36- return false , resourcePool , err
37- }
38- if resourcePool == nil ||
39- resourcePool .ResourcePool == nil ||
40- resourcePool .ResourcePool .Id == nil ||
41- resourcePool .ResourcePool .State == nil {
42- return false , resourcePool , fmt .Errorf ("create failed for resourcepool with id %s, the response is not valid (state missing)" , resourcePoolId )
43- }
44- if * resourcePool .ResourcePool .Id == resourcePoolId {
45- switch * resourcePool .ResourcePool .State {
46- case ResourcePoolStateCreated :
47- return true , resourcePool , err
48- default :
49- return false , resourcePool , err
50- }
51- }
52-
53- return false , nil , nil
54- })
30+ waitConfig := wait.WaiterHelper [sfs.GetResourcePoolResponse , string ]{
31+ FetchInstance : api .GetResourcePool (ctx , projectId , region , resourcePoolId ).Execute ,
32+ GetState : getStateResourcePool ,
33+ ActiveState : []string {ResourcePoolStateCreated },
34+ ErrorState : []string {ResourcePoolStateError },
35+ }
36+
37+ handler := wait .New (waitConfig .Wait ())
5538
5639 handler .SetTimeout (10 * time .Minute )
5740 return handler
5841}
5942
6043func UpdateResourcePoolWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId string ) * wait.AsyncActionHandler [sfs.GetResourcePoolResponse ] {
61- handler := wait .New (func () (waitFinished bool , resourcePool * sfs.GetResourcePoolResponse , err error ) {
62- resourcePool , err = api .GetResourcePool (ctx , projectId , region , resourcePoolId ).Execute ()
63- if err != nil {
64- return false , resourcePool , err
65- }
66- if resourcePool == nil ||
67- resourcePool .ResourcePool == nil ||
68- resourcePool .ResourcePool .Id == nil ||
69- resourcePool .ResourcePool .State == nil {
70- return false , resourcePool , fmt .Errorf ("update failed for resourcepool with id %s, the response is not valid (state missing)" , resourcePoolId )
71- }
72- if * resourcePool .ResourcePool .Id == resourcePoolId {
73- switch * resourcePool .ResourcePool .State {
74- case ResourcePoolStateCreated :
75- return true , resourcePool , err
76- default :
77- return false , resourcePool , err
78- }
79- }
80-
81- return false , nil , nil
82- })
44+ waitConfig := wait.WaiterHelper [sfs.GetResourcePoolResponse , string ]{
45+ FetchInstance : api .GetResourcePool (ctx , projectId , region , resourcePoolId ).Execute ,
46+ GetState : getStateResourcePool ,
47+ ActiveState : []string {ResourcePoolStateCreated },
48+ ErrorState : []string {ResourcePoolStateError },
49+ }
50+
51+ handler := wait .New (waitConfig .Wait ())
8352
8453 handler .SetTimeout (10 * time .Minute )
8554 return handler
8655}
8756
8857func DeleteResourcePoolWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId string ) * wait.AsyncActionHandler [sfs.GetResourcePoolResponse ] {
89- handler := wait .New (func () (waitFinished bool , resourcePool * sfs.GetResourcePoolResponse , err error ) {
90- resourcePool , err = api .GetResourcePool (ctx , projectId , region , resourcePoolId ).Execute ()
91- if err != nil {
92- var oapiError * oapierror.GenericOpenAPIError
93- if errors .As (err , & oapiError ) {
94- if statusCode := oapiError .StatusCode ; statusCode == http .StatusNotFound || statusCode == http .StatusGone {
95- return true , resourcePool , nil
96- }
97- }
98- }
99- return false , nil , nil
100- })
58+ waitConfig := wait.WaiterHelper [sfs.GetResourcePoolResponse , string ]{
59+ FetchInstance : api .GetResourcePool (ctx , projectId , region , resourcePoolId ).Execute ,
60+ GetState : getStateResourcePool ,
61+ ActiveState : []string {},
62+ ErrorState : []string {ResourcePoolStateError },
63+ }
64+
65+ handler := wait .New (waitConfig .Wait ())
10166
10267 handler .SetTimeout (10 * time .Minute )
10368 return handler
10469}
10570
10671func CreateShareWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId , shareId string ) * wait.AsyncActionHandler [sfs.GetShareResponse ] {
107- handler := wait .New (func () (waitFinished bool , share * sfs.GetShareResponse , err error ) {
108- share , err = api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ()
109- if err != nil {
110- return false , share , err
111- }
112- if share == nil ||
113- share .Share == nil ||
114- share .Share .Id == nil ||
115- share .Share .State == nil {
116- return false , share , fmt .Errorf ("create failed for share with id %s %s, the response is not valid (state missing)" , resourcePoolId , shareId )
117- }
118- if * share .Share .Id == shareId {
119- switch * share .Share .State {
120- case ShareStateCreated :
121- return true , share , err
122- default :
123- return false , share , err
124- }
125- }
126-
127- return false , nil , nil
128- })
72+ waitConfig := wait.WaiterHelper [sfs.GetShareResponse , string ]{
73+ FetchInstance : api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ,
74+ GetState : getStateShare ,
75+ ActiveState : []string {ShareStateCreated },
76+ ErrorState : []string {ShareStateError },
77+ }
78+
79+ handler := wait .New (waitConfig .Wait ())
12980
13081 handler .SetTimeout (10 * time .Minute )
13182 return handler
13283}
13384
13485func UpdateShareWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId , shareId string ) * wait.AsyncActionHandler [sfs.GetShareResponse ] {
135- handler := wait .New (func () (waitFinished bool , share * sfs.GetShareResponse , err error ) {
136- share , err = api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ()
137- if err != nil {
138- return false , share , err
139- }
140- if share == nil ||
141- share .Share == nil ||
142- share .Share .Id == nil ||
143- share .Share .State == nil {
144- return false , share , fmt .Errorf ("update failed for resourcepool with id %s, the response is not valid (state missing)" , resourcePoolId )
145- }
146- if * share .Share .Id == shareId {
147- switch * share .Share .State {
148- case ResourcePoolStateCreated :
149- return true , share , err
150- default :
151- return false , share , err
152- }
153- }
154-
155- return false , nil , nil
156- })
86+ waitConfig := wait.WaiterHelper [sfs.GetShareResponse , string ]{
87+ FetchInstance : api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ,
88+ GetState : getStateShare ,
89+ ActiveState : []string {ShareStateCreated },
90+ ErrorState : []string {ShareStateError },
91+ }
92+
93+ handler := wait .New (waitConfig .Wait ())
15794
15895 handler .SetTimeout (10 * time .Minute )
15996 return handler
16097}
16198
16299func DeleteShareWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId , shareId string ) * wait.AsyncActionHandler [sfs.GetShareResponse ] {
163- handler := wait .New (func () (waitFinished bool , share * sfs.GetShareResponse , err error ) {
164- share , err = api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ()
165- if err != nil {
166- var oapiError * oapierror.GenericOpenAPIError
167- if errors .As (err , & oapiError ) {
168- if statusCode := oapiError .StatusCode ; statusCode == http .StatusNotFound || statusCode == http .StatusGone {
169- return true , share , nil
170- }
171- }
172- }
173- return false , nil , nil
174- })
100+ waitConfig := wait.WaiterHelper [sfs.GetShareResponse , string ]{
101+ FetchInstance : api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ,
102+ GetState : getStateShare ,
103+ ActiveState : []string {},
104+ ErrorState : []string {ShareStateError },
105+ }
106+
107+ handler := wait .New (waitConfig .Wait ())
175108
176109 handler .SetTimeout (10 * time .Minute )
177110 return handler
178111}
112+
113+ func getStateResourcePool (response * sfs.GetResourcePoolResponse ) (string , error ) {
114+ if response == nil {
115+ return "" , errors .New ("empty response" )
116+ }
117+ if response .ResourcePool == nil {
118+ return "" , errors .New ("resource pool is nil" )
119+ }
120+ if response .ResourcePool .Id == nil {
121+ return "" , errors .New ("resource pool id is nil" )
122+ }
123+ if response .ResourcePool .State == nil {
124+ return "" , errors .New ("resource pool state is nil" )
125+ }
126+ return * response .ResourcePool .State , nil
127+ }
128+
129+ func getStateShare (response * sfs.GetShareResponse ) (string , error ) {
130+ if response == nil {
131+ return "" , errors .New ("empty response" )
132+ }
133+ if response .Share == nil {
134+ return "" , errors .New ("share is nil" )
135+ }
136+ if response .Share .Id == nil {
137+ return "" , errors .New ("share id is nil" )
138+ }
139+ if response .Share .State == nil {
140+ return "" , errors .New ("share state is nil" )
141+ }
142+ return * response .Share .State , nil
143+ }
0 commit comments