Skip to content

Commit a64aa96

Browse files
authored
feat(postgresflex): refactor wait handler to user helper struct (#7726)
relates to STACKITSDK-386
1 parent 0e2f677 commit a64aa96

4 files changed

Lines changed: 48 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@
356356
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
357357
- [v1.8.0](services/postgresflex/CHANGELOG.md#v180)
358358
- **Feature:** Added `_UNKNOWN_DEFAULT_OPEN_API` fallback value to all enums to handle unknown API values gracefully.
359+
- [v1.9.0](services/postgresflex/CHANGELOG.md#v190)
360+
- `v2api`:
361+
- **Improvement**: Use new `WaiterHelper` struct in the PostgreSQL Flex WaitHandler
362+
- **Breaking change:** Change return type of `wait.ForceDeleteInstanceWaitHandler()` to `*wait.AsyncActionHandler[postgresflex.InstanceResponse]`
359363
- `rabbitmq`:
360364
- [v0.28.3](services/rabbitmq/CHANGELOG.md#v0283)
361365
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`

services/postgresflex/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v1.9.0
2+
- `v2api`:
3+
- **Improvement**: Use new `WaiterHelper` struct in the PostgreSQL Flex WaitHandler
4+
- **Breaking change:** Change return type of `wait.ForceDeleteInstanceWaitHandler()` to `*wait.AsyncActionHandler[postgresflex.InstanceResponse]`
5+
16
## v1.8.0
27
- **Feature:** Added `_UNKNOWN_DEFAULT_OPEN_API` fallback value to all enums to handle unknown API values gracefully.
38

services/postgresflex/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.8.0
1+
v1.9.0

services/postgresflex/v2api/wait/wait.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package wait
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
7+
"net/http"
68
"time"
79

810
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
@@ -68,28 +70,26 @@ func CreateInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, p
6870
}
6971

7072
// PartialUpdateInstanceWaitHandler will wait for instance update
71-
func PartialUpdateInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] {
72-
handler := wait.New(func() (waitFinished bool, response *postgresflex.InstanceResponse, err error) {
73-
s, err := a.GetInstance(ctx, projectId, region, instanceId).Execute()
74-
if err != nil {
75-
return false, nil, err
76-
}
77-
if s == nil || s.Item == nil || s.Item.Id == nil || *s.Item.Id != instanceId || s.Item.Status == nil {
78-
return false, nil, nil
79-
}
80-
switch *s.Item.Status {
81-
default:
82-
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, *s.Item.Status)
83-
case InstanceStateEmpty:
84-
return false, nil, nil
85-
case InstanceStateProgressing:
86-
return false, nil, nil
87-
case InstanceStateSuccess:
88-
return true, s, nil
89-
case InstanceStateFailed:
90-
return true, s, fmt.Errorf("update failed for instance with id %s", instanceId)
91-
}
92-
})
73+
func PartialUpdateInstanceWaitHandler(ctx context.Context, client postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] {
74+
waitConfig := wait.WaiterHelper[postgresflex.InstanceResponse, string]{
75+
FetchInstance: client.GetInstance(ctx, projectId, region, instanceId).Execute,
76+
GetState: func(response *postgresflex.InstanceResponse) (string, error) {
77+
if response == nil {
78+
return "", errors.New("empty response")
79+
}
80+
if response.Item == nil {
81+
return "", errors.New("empty instance")
82+
}
83+
if response.Item.Status == nil {
84+
return "", errors.New("status is missing")
85+
}
86+
return *response.Item.Status, nil
87+
},
88+
ActiveState: []string{InstanceStateSuccess},
89+
ErrorState: []string{InstanceStateFailed},
90+
}
91+
92+
handler := wait.New(waitConfig.Wait())
9393
handler.SetTimeout(45 * time.Minute)
9494
return handler
9595
}
@@ -118,21 +118,22 @@ func DeleteInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, p
118118
}
119119

120120
// ForceDeleteInstanceWaitHandler will wait for instance deletion
121-
func ForceDeleteInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[struct{}] {
122-
handler := wait.New(func() (waitFinished bool, response *struct{}, err error) {
123-
_, err = a.GetInstance(ctx, projectId, region, instanceId).Execute()
124-
if err == nil {
125-
return false, nil, nil
126-
}
127-
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
128-
if !ok {
129-
return false, nil, err
130-
}
131-
if oapiErr.StatusCode != 404 {
132-
return false, nil, err
133-
}
134-
return true, nil, nil
135-
})
121+
func ForceDeleteInstanceWaitHandler(ctx context.Context, client postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] {
122+
waitConfig := wait.WaiterHelper[postgresflex.InstanceResponse, string]{
123+
FetchInstance: client.GetInstance(ctx, projectId, instanceId, region).Execute,
124+
GetState: func(response *postgresflex.InstanceResponse) (string, error) {
125+
if response == nil {
126+
return "", errors.New("empty response")
127+
}
128+
if response.Item.Status == nil {
129+
return "", errors.New("status is missing in response")
130+
}
131+
return *response.Item.Status, nil
132+
},
133+
ErrorState: []string{InstanceStateFailed},
134+
DeleteHttpErrorStatusCodes: []int{http.StatusNotFound},
135+
}
136+
handler := wait.New(waitConfig.Wait())
136137
handler.SetTimeout(15 * time.Minute)
137138
return handler
138139
}

0 commit comments

Comments
 (0)