Skip to content

Commit d1f8909

Browse files
committed
refac(opensearch): Use new WaitHelper for waiters
STACKITSDK-385 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 82ab1cc commit d1f8909

4 files changed

Lines changed: 39 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@
272272
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0`
273273
- [v0.27.2](services/opensearch/CHANGELOG.md#v0272)
274274
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
275+
- [v0.28.0](services/opensearch/CHANGELOG.md#v0280)
276+
- **Improvement:** Use new WaiterHelper for opensearch waiters
275277
- `postgresflex`:
276278
- [v1.6.3](services/postgresflex/CHANGELOG.md#v163)
277279
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`

services/opensearch/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.28.0
2+
- **Improvement:** Use new WaiterHelper for opensearch waiters
3+
14
## v0.27.2
25
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
36

services/opensearch/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.27.2
1+
v0.28.0

services/opensearch/v1api/wait/wait.go

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wait
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"strings"
@@ -27,44 +28,44 @@ const (
2728

2829
// CreateInstanceWaitHandler will wait for instance creation
2930
func CreateInstanceWaitHandler(ctx context.Context, a opensearch.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[opensearch.Instance] {
30-
handler := wait.New(func() (waitFinished bool, response *opensearch.Instance, err error) {
31-
s, err := a.GetInstance(ctx, projectId, instanceId).Execute()
32-
if err != nil {
33-
return false, nil, err
34-
}
35-
if s.Status == nil {
36-
return false, nil, fmt.Errorf("create failed for instance with id %s. The response is not valid: the status are missing", instanceId)
37-
}
38-
switch *s.Status {
39-
case INSTANCESTATUS_ACTIVE:
40-
return true, s, nil
41-
case INSTANCESTATUS_FAILED:
42-
return true, s, fmt.Errorf("create failed for instance with id %s: %s", instanceId, s.LastOperation.Description)
43-
}
44-
return false, nil, nil
45-
})
31+
waitConfig := wait.WaiterHelper[opensearch.Instance, string]{
32+
FetchInstance: a.GetInstance(ctx, projectId, instanceId).Execute,
33+
GetState: func(s *opensearch.Instance) (string, error) {
34+
if s == nil {
35+
return "", errors.New("empty response")
36+
}
37+
if s.Status == nil {
38+
return "", errors.New("status is missing")
39+
}
40+
return *s.Status, nil
41+
},
42+
ActiveState: []string{INSTANCESTATUS_ACTIVE},
43+
ErrorState: []string{INSTANCESTATUS_FAILED},
44+
}
45+
46+
handler := wait.New(waitConfig.Wait())
4647
handler.SetTimeout(45 * time.Minute)
4748
return handler
4849
}
4950

5051
// PartialUpdateInstanceWaitHandler will wait for instance update
5152
func PartialUpdateInstanceWaitHandler(ctx context.Context, a opensearch.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[opensearch.Instance] {
52-
handler := wait.New(func() (waitFinished bool, response *opensearch.Instance, err error) {
53-
s, err := a.GetInstance(ctx, projectId, instanceId).Execute()
54-
if err != nil {
55-
return false, nil, err
56-
}
57-
if s.Status == nil {
58-
return false, nil, fmt.Errorf("update failed for instance with id %s. The response is not valid: the instance id or the status are missing", instanceId)
59-
}
60-
switch *s.Status {
61-
case INSTANCESTATUS_ACTIVE:
62-
return true, s, nil
63-
case INSTANCESTATUS_FAILED:
64-
return true, s, fmt.Errorf("update failed for instance with id %s: %s", instanceId, s.LastOperation.Description)
65-
}
66-
return false, nil, nil
67-
})
53+
waitConfig := wait.WaiterHelper[opensearch.Instance, string]{
54+
FetchInstance: a.GetInstance(ctx, projectId, instanceId).Execute,
55+
GetState: func(s *opensearch.Instance) (string, error) {
56+
if s == nil {
57+
return "", errors.New("empty response")
58+
}
59+
if s.Status == nil {
60+
return "", errors.New("status is missing")
61+
}
62+
return *s.Status, nil
63+
},
64+
ActiveState: []string{INSTANCESTATUS_ACTIVE},
65+
ErrorState: []string{INSTANCESTATUS_FAILED},
66+
}
67+
68+
handler := wait.New(waitConfig.Wait())
6869
handler.SetTimeout(45 * time.Minute)
6970
return handler
7071
}

0 commit comments

Comments
 (0)