@@ -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)
@@ -71,89 +68,76 @@ func DeleteResourcePoolWaitHandler(ctx context.Context, api sfs.DefaultAPI, proj
7168 return handler
7269}
7370
74- func GetStateResourcePool (response * sfs.GetResourcePoolResponse ) (string , error ) {
75- if response == nil {
76- return "" , errors .New ("empty response" )
77- }
78- if response .ResourcePool == nil {
79- return "" , errors .New ("resource pool is nil" )
80- }
81- if response .ResourcePool .State == nil {
82- return "" , errors .New ("resource pool state is nil" )
71+ func CreateShareWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId , shareId string ) * wait.AsyncActionHandler [sfs.GetShareResponse ] {
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 {},
8377 }
84- return * response .ResourcePool .State , nil
85- }
8678
87- func CreateShareWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId , shareId string ) * wait.AsyncActionHandler [sfs.GetShareResponse ] {
88- handler := wait .New (func () (waitFinished bool , share * sfs.GetShareResponse , err error ) {
89- share , err = api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ()
90- if err != nil {
91- return false , share , err
92- }
93- if share == nil ||
94- share .Share == nil ||
95- share .Share .Id == nil ||
96- share .Share .State == nil {
97- return false , share , fmt .Errorf ("create failed for share with id %s %s, the response is not valid (state missing)" , resourcePoolId , shareId )
98- }
99- if * share .Share .Id == shareId {
100- switch * share .Share .State {
101- case ShareStateCreated :
102- return true , share , err
103- default :
104- return false , share , err
105- }
106- }
107-
108- return false , nil , nil
109- })
79+ handler := wait .New (waitConfig .Wait ())
11080
11181 handler .SetTimeout (10 * time .Minute )
11282 return handler
11383}
11484
11585func UpdateShareWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId , shareId string ) * wait.AsyncActionHandler [sfs.GetShareResponse ] {
116- handler := wait .New (func () (waitFinished bool , share * sfs.GetShareResponse , err error ) {
117- share , err = api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ()
118- if err != nil {
119- return false , share , err
120- }
121- if share == nil ||
122- share .Share == nil ||
123- share .Share .Id == nil ||
124- share .Share .State == nil {
125- return false , share , fmt .Errorf ("update failed for resourcepool with id %s, the response is not valid (state missing)" , resourcePoolId )
126- }
127- if * share .Share .Id == shareId {
128- switch * share .Share .State {
129- case ResourcePoolStateCreated :
130- return true , share , err
131- default :
132- return false , share , err
133- }
134- }
135-
136- return false , nil , nil
137- })
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 {},
91+ }
92+
93+ handler := wait .New (waitConfig .Wait ())
13894
13995 handler .SetTimeout (10 * time .Minute )
14096 return handler
14197}
14298
14399func DeleteShareWaitHandler (ctx context.Context , api sfs.DefaultAPI , projectId , region , resourcePoolId , shareId string ) * wait.AsyncActionHandler [sfs.GetShareResponse ] {
144- handler := wait .New (func () (waitFinished bool , share * sfs.GetShareResponse , err error ) {
145- share , err = api .GetShare (ctx , projectId , region , resourcePoolId , shareId ).Execute ()
146- if err != nil {
147- var oapiError * oapierror.GenericOpenAPIError
148- if errors .As (err , & oapiError ) {
149- if statusCode := oapiError .StatusCode ; statusCode == http .StatusNotFound || statusCode == http .StatusGone {
150- return true , share , nil
151- }
152- }
153- }
154- return false , nil , nil
155- })
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 {},
105+ }
106+
107+ handler := wait .New (waitConfig .Wait ())
156108
157109 handler .SetTimeout (10 * time .Minute )
158110 return handler
159111}
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