diff --git a/CHANGELOG.md b/CHANGELOG.md index 10eb0d817..361faa951 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -354,6 +354,8 @@ - **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0` - [v1.8.0](services/postgresflex/CHANGELOG.md#v180) - **Feature:** Added `_UNKNOWN_DEFAULT_OPEN_API` fallback value to all enums to handle unknown API values gracefully. + - [v1.9.0](services/postgresflex/CHANGELOG.md#v190) + - `v2api`: **Improvement**: Use new `WaiterHelper` struct in the PostgreSQL Flex WaitHandler - `rabbitmq`: - [v0.28.3](services/rabbitmq/CHANGELOG.md#v0283) - **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1` diff --git a/services/postgresflex/CHANGELOG.md b/services/postgresflex/CHANGELOG.md index 82a4e1ffb..ee952d76c 100644 --- a/services/postgresflex/CHANGELOG.md +++ b/services/postgresflex/CHANGELOG.md @@ -1,3 +1,6 @@ +## v1.9.0 +- `v2api`: **Improvement**: Use new `WaiterHelper` struct in the PostgreSQL Flex WaitHandler + ## v1.8.0 - **Feature:** Added `_UNKNOWN_DEFAULT_OPEN_API` fallback value to all enums to handle unknown API values gracefully. diff --git a/services/postgresflex/VERSION b/services/postgresflex/VERSION index 4e2cea3bb..e2f650c40 100644 --- a/services/postgresflex/VERSION +++ b/services/postgresflex/VERSION @@ -1 +1 @@ -v1.8.0 \ No newline at end of file +v1.9.0 \ No newline at end of file diff --git a/services/postgresflex/v2api/wait/wait.go b/services/postgresflex/v2api/wait/wait.go index 2b1d9ca87..bffac971b 100644 --- a/services/postgresflex/v2api/wait/wait.go +++ b/services/postgresflex/v2api/wait/wait.go @@ -2,7 +2,9 @@ package wait import ( "context" + "errors" "fmt" + "net/http" "time" "github.com/stackitcloud/stackit-sdk-go/core/oapierror" @@ -68,28 +70,26 @@ func CreateInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, p } // PartialUpdateInstanceWaitHandler will wait for instance update -func PartialUpdateInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] { - handler := wait.New(func() (waitFinished bool, response *postgresflex.InstanceResponse, err error) { - s, err := a.GetInstance(ctx, projectId, region, instanceId).Execute() - if err != nil { - return false, nil, err - } - if s == nil || s.Item == nil || s.Item.Id == nil || *s.Item.Id != instanceId || s.Item.Status == nil { - return false, nil, nil - } - switch *s.Item.Status { - default: - return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, *s.Item.Status) - case InstanceStateEmpty: - return false, nil, nil - case InstanceStateProgressing: - return false, nil, nil - case InstanceStateSuccess: - return true, s, nil - case InstanceStateFailed: - return true, s, fmt.Errorf("update failed for instance with id %s", instanceId) - } - }) +func PartialUpdateInstanceWaitHandler(ctx context.Context, client postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] { + waitConfig := wait.WaiterHelper[postgresflex.InstanceResponse, string]{ + FetchInstance: client.GetInstance(ctx, projectId, region, instanceId).Execute, + GetState: func(response *postgresflex.InstanceResponse) (string, error) { + if response == nil { + return "", errors.New("empty response") + } + if response.Item == nil { + return "", errors.New("empty instance") + } + if response.Item.Status == nil { + return "", errors.New("status is missing") + } + return *response.Item.Status, nil + }, + ActiveState: []string{InstanceStateSuccess}, + ErrorState: []string{InstanceStateFailed}, + } + + handler := wait.New(waitConfig.Wait()) handler.SetTimeout(45 * time.Minute) return handler } @@ -118,21 +118,22 @@ func DeleteInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, p } // ForceDeleteInstanceWaitHandler will wait for instance deletion -func ForceDeleteInstanceWaitHandler(ctx context.Context, a postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[struct{}] { - handler := wait.New(func() (waitFinished bool, response *struct{}, err error) { - _, err = a.GetInstance(ctx, projectId, region, instanceId).Execute() - if err == nil { - return false, nil, nil - } - 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 - if !ok { - return false, nil, err - } - if oapiErr.StatusCode != 404 { - return false, nil, err - } - return true, nil, nil - }) +func ForceDeleteInstanceWaitHandler(ctx context.Context, client postgresflex.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] { + waitConfig := wait.WaiterHelper[postgresflex.InstanceResponse, string]{ + FetchInstance: client.GetInstance(ctx, projectId, instanceId, region).Execute, + GetState: func(response *postgresflex.InstanceResponse) (string, error) { + if response == nil { + return "", errors.New("empty response") + } + if response.Item.Status == nil { + return "", errors.New("status is missing in response") + } + return *response.Item.Status, nil + }, + ErrorState: []string{InstanceStateFailed}, + DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, + } + handler := wait.New(waitConfig.Wait()) handler.SetTimeout(15 * time.Minute) return handler }