Skip to content

Commit 185defd

Browse files
committed
feat(sfs): refactored and tested sfs resource pool wait handler to use wait helper
1 parent 4d4e5e6 commit 185defd

2 files changed

Lines changed: 96 additions & 119 deletions

File tree

services/sfs/wait/wait.go

Lines changed: 92 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ package wait
44
import (
55
"context"
66
"errors"
7-
"fmt"
8-
"net/http"
97
"time"
108

11-
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
129
"github.com/stackitcloud/stackit-sdk-go/core/wait"
1310
"github.com/stackitcloud/stackit-sdk-go/services/sfs"
1411
)
@@ -50,154 +47,134 @@ type APIClientInterface interface {
5047

5148
// Deprecated: Will be removed after 2026-09-30. Move to the packages generated for each available API version instead
5249
func CreateResourcePoolWaitHandler(ctx context.Context, api APIClientInterface, projectId, region, resourcePoolId string) *wait.AsyncActionHandler[sfs.GetResourcePoolResponse] {
53-
handler := wait.New(func() (waitFinished bool, resourcePool *sfs.GetResourcePoolResponse, err error) {
54-
resourcePool, err = api.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
55-
if err != nil {
56-
return false, resourcePool, err
57-
}
58-
if resourcePool == nil ||
59-
resourcePool.ResourcePool == nil ||
60-
resourcePool.ResourcePool.Id == nil ||
61-
resourcePool.ResourcePool.State == nil {
62-
return false, resourcePool, fmt.Errorf("create failed for resourcepool with id %s, the response is not valid (state missing)", resourcePoolId)
63-
}
64-
if *resourcePool.ResourcePool.Id == resourcePoolId {
65-
switch *resourcePool.ResourcePool.State {
66-
case ResourcePoolStateCreated:
67-
return true, resourcePool, err
68-
default:
69-
return false, resourcePool, err
70-
}
71-
}
72-
73-
return false, nil, nil
74-
})
50+
waitConfig := wait.WaiterHelper[sfs.GetResourcePoolResponse, string]{
51+
FetchInstance: func() (*sfs.GetResourcePoolResponse, error) {
52+
return api.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
53+
},
54+
GetState: getStateResourcePool,
55+
ActiveState: []string{ResourcePoolStateCreated},
56+
ErrorState: []string{},
57+
}
58+
59+
handler := wait.New(waitConfig.Wait())
7560

7661
handler.SetTimeout(10 * time.Minute)
7762
return handler
7863
}
7964

8065
// Deprecated: Will be removed after 2026-09-30. Move to the packages generated for each available API version instead
8166
func UpdateResourcePoolWaitHandler(ctx context.Context, api APIClientInterface, projectId, region, resourcePoolId string) *wait.AsyncActionHandler[sfs.GetResourcePoolResponse] {
82-
handler := wait.New(func() (waitFinished bool, resourcePool *sfs.GetResourcePoolResponse, err error) {
83-
resourcePool, err = api.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
84-
if err != nil {
85-
return false, resourcePool, err
86-
}
87-
if resourcePool == nil ||
88-
resourcePool.ResourcePool == nil ||
89-
resourcePool.ResourcePool.Id == nil ||
90-
resourcePool.ResourcePool.State == nil {
91-
return false, resourcePool, fmt.Errorf("update failed for resourcepool with id %s, the response is not valid (state missing)", resourcePoolId)
92-
}
93-
if *resourcePool.ResourcePool.Id == resourcePoolId {
94-
switch *resourcePool.ResourcePool.State {
95-
case ResourcePoolStateCreated:
96-
return true, resourcePool, err
97-
default:
98-
return false, resourcePool, err
99-
}
100-
}
101-
102-
return false, nil, nil
103-
})
67+
waitConfig := wait.WaiterHelper[sfs.GetResourcePoolResponse, string]{
68+
FetchInstance: func() (*sfs.GetResourcePoolResponse, error) {
69+
return api.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
70+
},
71+
GetState: getStateResourcePool,
72+
ActiveState: []string{ResourcePoolStateCreated},
73+
ErrorState: []string{},
74+
}
75+
76+
handler := wait.New(waitConfig.Wait())
10477

10578
handler.SetTimeout(10 * time.Minute)
10679
return handler
10780
}
10881

10982
// Deprecated: Will be removed after 2026-09-30. Move to the packages generated for each available API version instead
11083
func DeleteResourcePoolWaitHandler(ctx context.Context, api APIClientInterface, projectId, region, resourcePoolId string) *wait.AsyncActionHandler[sfs.GetResourcePoolResponse] {
111-
handler := wait.New(func() (waitFinished bool, resourcePool *sfs.GetResourcePoolResponse, err error) {
112-
resourcePool, err = api.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
113-
if err != nil {
114-
var oapiError *oapierror.GenericOpenAPIError
115-
if errors.As(err, &oapiError) {
116-
if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
117-
return true, resourcePool, nil
118-
}
119-
}
120-
}
121-
return false, nil, nil
122-
})
84+
waitConfig := wait.WaiterHelper[sfs.GetResourcePoolResponse, string]{
85+
FetchInstance: func() (*sfs.GetResourcePoolResponse, error) {
86+
return api.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
87+
},
88+
GetState: getStateResourcePool,
89+
ActiveState: []string{},
90+
ErrorState: []string{},
91+
}
92+
93+
handler := wait.New(waitConfig.Wait())
12394

12495
handler.SetTimeout(10 * time.Minute)
12596
return handler
12697
}
12798

12899
// Deprecated: Will be removed after 2026-09-30. Move to the packages generated for each available API version instead
129100
func CreateShareWaitHandler(ctx context.Context, api APIClientInterface, projectId, region, resourcePoolId, shareId string) *wait.AsyncActionHandler[sfs.GetShareResponse] {
130-
handler := wait.New(func() (waitFinished bool, share *sfs.GetShareResponse, err error) {
131-
share, err = api.GetShareExecute(ctx, projectId, region, resourcePoolId, shareId)
132-
if err != nil {
133-
return false, share, err
134-
}
135-
if share == nil ||
136-
share.Share == nil ||
137-
share.Share.Id == nil ||
138-
share.Share.State == nil {
139-
return false, share, fmt.Errorf("create failed for share with id %s %s, the response is not valid (state missing)", resourcePoolId, shareId)
140-
}
141-
if *share.Share.Id == shareId {
142-
switch *share.Share.State {
143-
case ShareStateCreated:
144-
return true, share, err
145-
default:
146-
return false, share, err
147-
}
148-
}
149-
150-
return false, nil, nil
151-
})
101+
waitConfig := wait.WaiterHelper[sfs.GetShareResponse, string]{
102+
FetchInstance: func() (*sfs.GetShareResponse, error) {
103+
return api.GetShareExecute(ctx, projectId, region, resourcePoolId, shareId)
104+
},
105+
GetState: getStateShare,
106+
ActiveState: []string{ShareStateCreated},
107+
ErrorState: []string{},
108+
}
109+
110+
handler := wait.New(waitConfig.Wait())
152111

153112
handler.SetTimeout(10 * time.Minute)
154113
return handler
155114
}
156115

157116
// Deprecated: Will be removed after 2026-09-30. Move to the packages generated for each available API version instead
158117
func UpdateShareWaitHandler(ctx context.Context, api APIClientInterface, projectId, region, resourcePoolId, shareId string) *wait.AsyncActionHandler[sfs.GetShareResponse] {
159-
handler := wait.New(func() (waitFinished bool, share *sfs.GetShareResponse, err error) {
160-
share, err = api.GetShareExecute(ctx, projectId, region, resourcePoolId, shareId)
161-
if err != nil {
162-
return false, share, err
163-
}
164-
if share == nil ||
165-
share.Share == nil ||
166-
share.Share.Id == nil ||
167-
share.Share.State == nil {
168-
return false, share, fmt.Errorf("update failed for resourcepool with id %s, the response is not valid (state missing)", resourcePoolId)
169-
}
170-
if *share.Share.Id == shareId {
171-
switch *share.Share.State {
172-
case ResourcePoolStateCreated:
173-
return true, share, err
174-
default:
175-
return false, share, err
176-
}
177-
}
178-
179-
return false, nil, nil
180-
})
118+
waitConfig := wait.WaiterHelper[sfs.GetShareResponse, string]{
119+
FetchInstance: func() (*sfs.GetShareResponse, error) {
120+
return api.GetShareExecute(ctx, projectId, region, resourcePoolId, shareId)
121+
},
122+
GetState: getStateShare,
123+
ActiveState: []string{ShareStateCreated},
124+
ErrorState: []string{},
125+
}
126+
127+
handler := wait.New(waitConfig.Wait())
181128

182129
handler.SetTimeout(10 * time.Minute)
183130
return handler
184131
}
185132

186133
// Deprecated: Will be removed after 2026-09-30. Move to the packages generated for each available API version instead
187134
func DeleteShareWaitHandler(ctx context.Context, api APIClientInterface, projectId, region, resourcePoolId, shareId string) *wait.AsyncActionHandler[sfs.GetShareResponse] {
188-
handler := wait.New(func() (waitFinished bool, share *sfs.GetShareResponse, err error) {
189-
share, err = api.GetShareExecute(ctx, projectId, region, resourcePoolId, shareId)
190-
if err != nil {
191-
var oapiError *oapierror.GenericOpenAPIError
192-
if errors.As(err, &oapiError) {
193-
if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
194-
return true, share, nil
195-
}
196-
}
197-
}
198-
return false, nil, nil
199-
})
135+
waitConfig := wait.WaiterHelper[sfs.GetShareResponse, string]{
136+
FetchInstance: func() (*sfs.GetShareResponse, error) {
137+
return api.GetShareExecute(ctx, projectId, region, resourcePoolId, shareId)
138+
},
139+
GetState: getStateShare,
140+
ActiveState: []string{},
141+
ErrorState: []string{},
142+
}
143+
144+
handler := wait.New(waitConfig.Wait())
200145

201146
handler.SetTimeout(10 * time.Minute)
202147
return handler
203148
}
149+
150+
func getStateResourcePool(response *sfs.GetResourcePoolResponse) (string, error) {
151+
if response == nil {
152+
return "", errors.New("empty response")
153+
}
154+
if response.ResourcePool == nil {
155+
return "", errors.New("resource pool is nil")
156+
}
157+
if response.ResourcePool.Id == nil {
158+
return "", errors.New("resource pool id is nil")
159+
}
160+
if response.ResourcePool.State == nil {
161+
return "", errors.New("resource pool state is nil")
162+
}
163+
return *response.ResourcePool.State, nil
164+
}
165+
166+
func getStateShare(response *sfs.GetShareResponse) (string, error) {
167+
if response == nil {
168+
return "", errors.New("empty response")
169+
}
170+
if response.Share == nil {
171+
return "", errors.New("share is nil")
172+
}
173+
if response.Share.Id == nil {
174+
return "", errors.New("share id is nil")
175+
}
176+
if response.Share.State == nil {
177+
return "", errors.New("share state is nil")
178+
}
179+
return *response.Share.State, nil
180+
}

services/sfs/wait/wait_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestCreateResourcePoolWaitHandler(t *testing.T) {
125125
{ResourcePoolStateCreating, 0},
126126
},
127127
},
128-
ResourcePoolStateCreating,
128+
"",
129129
true,
130130
},
131131
}
@@ -213,7 +213,7 @@ func TestUpdateResourcePoolWaitHandler(t *testing.T) {
213213
{ResourcePoolStateUpdating, 0},
214214
},
215215
},
216-
ResourcePoolStateUpdating,
216+
"",
217217
true,
218218
},
219219
}
@@ -375,7 +375,7 @@ func TestCreateShareWaitHandler(t *testing.T) {
375375
{ShareStateCreating, 0},
376376
},
377377
},
378-
ShareStateCreating,
378+
"",
379379
true,
380380
},
381381
}
@@ -463,7 +463,7 @@ func TestUpdateShareWaitHandler(t *testing.T) {
463463
{ShareStateCreating, 0},
464464
},
465465
},
466-
ShareStateCreating,
466+
"",
467467
true,
468468
},
469469
}

0 commit comments

Comments
 (0)