-
Notifications
You must be signed in to change notification settings - Fork 30
feat(sqlserverflex): refactor wait handler to use helper struct #7448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GokceGK
wants to merge
3
commits into
main
Choose a base branch
from
feat/STACKITSDK-394-sqlserverflex-refactor-wait-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| v1.11.0 | ||
| v1.12.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,9 @@ package wait | |
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/wait" | ||
| sqlserverflex "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v2api" | ||
| ) | ||
|
|
@@ -21,76 +18,58 @@ const ( | |
| InstanceStateFailed = "Failed" | ||
| ) | ||
|
|
||
| // CreateInstanceWaitHandler will wait for instance creation | ||
| func CreateInstanceWaitHandler(ctx context.Context, a sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) { | ||
| s, err := a.GetInstance(ctx, projectId, instanceId, region).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 strings.ToLower(*s.Item.Status) { | ||
| case strings.ToLower(InstanceStateSuccess): | ||
| return true, s, nil | ||
| case strings.ToLower(InstanceStateUnknown), strings.ToLower(InstanceStateFailed): | ||
| return true, s, fmt.Errorf("create failed for instance with id %s", instanceId) | ||
| default: | ||
| return false, s, nil | ||
| } | ||
| }) | ||
| handler.SetTimeout(45 * time.Minute) | ||
| func createOrUpdateInstanceWaitHandler(ctx context.Context, client sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| waitConfig := wait.WaiterHelper[sqlserverflex.GetInstanceResponse, string]{ | ||
| FetchInstance: client.GetInstance(ctx, projectId, instanceId, region).Execute, | ||
| GetState: func(response *sqlserverflex.GetInstanceResponse) (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{InstanceStateUnknown, InstanceStateFailed, InstanceStateEmpty}, | ||
| } | ||
|
|
||
| handler := wait.New(waitConfig.Wait()) | ||
| handler.SetSleepBeforeWait(5 * time.Second) | ||
| handler.SetTimeout(45 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| // UpdateInstanceWaitHandler will wait for instance update | ||
| func UpdateInstanceWaitHandler(ctx context.Context, a sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) { | ||
| s, err := a.GetInstance(ctx, projectId, instanceId, region).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 strings.ToLower(*s.Item.Status) { | ||
| case strings.ToLower(InstanceStateSuccess): | ||
| return true, s, nil | ||
| case strings.ToLower(InstanceStateUnknown), strings.ToLower(InstanceStateFailed): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, what about this "toLower" here? Is this based on an API issue? |
||
| return true, s, fmt.Errorf("update failed for instance with id %s", instanceId) | ||
| default: | ||
| return false, s, nil | ||
| } | ||
| }) | ||
| handler.SetSleepBeforeWait(2 * time.Second) | ||
| handler.SetTimeout(45 * time.Minute) | ||
| return handler | ||
| // CreateInstanceWaitHandler will wait for instance creation | ||
| func CreateInstanceWaitHandler(ctx context.Context, client sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| return createOrUpdateInstanceWaitHandler(ctx, client, projectId, instanceId, region) | ||
| } | ||
|
|
||
| // PartialUpdateInstanceWaitHandler will wait for instance update | ||
| func PartialUpdateInstanceWaitHandler(ctx context.Context, a sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| return UpdateInstanceWaitHandler(ctx, a, projectId, instanceId, region) | ||
| // UpdateInstanceWaitHandler will wait for instance update | ||
| func UpdateInstanceWaitHandler(ctx context.Context, client sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| return createOrUpdateInstanceWaitHandler(ctx, client, projectId, instanceId, region) | ||
| } | ||
|
|
||
| // DeleteInstanceWaitHandler will wait for instance deletion | ||
| func DeleteInstanceWaitHandler(ctx context.Context, a sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[struct{}] { | ||
| handler := wait.New(func() (waitFinished bool, response *struct{}, err error) { | ||
| _, err = a.GetInstance(ctx, projectId, instanceId, region).Execute() | ||
| if err == nil { | ||
| return false, nil, nil | ||
| } | ||
| var oapiErr *oapierror.GenericOpenAPIError | ||
| ok := errors.As(err, &oapiErr) | ||
| if !ok { | ||
| return false, nil, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError") | ||
| } | ||
| if oapiErr.StatusCode != http.StatusNotFound { | ||
| return false, nil, err | ||
| } | ||
| return true, nil, nil | ||
| }) | ||
| func DeleteInstanceWaitHandler(ctx context.Context, client sqlserverflex.DefaultAPI, projectId, instanceId, region string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| waitConfig := wait.WaiterHelper[sqlserverflex.GetInstanceResponse, string]{ | ||
| FetchInstance: client.GetInstance(ctx, projectId, instanceId, region).Execute, | ||
| GetState: func(response *sqlserverflex.GetInstanceResponse) (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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to adjust to new structure (enum change)