-
Notifications
You must be signed in to change notification settings - Fork 24
Add waiters and example for instance operations in sqlserverflex module
#537
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
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,3 +1,7 @@ | ||
| ## v0.2.0 (2024-05-24) | ||
|
|
||
| - **Feature** Waiters for async operations `CreateInstanceWaitHandler`, `UpdateInstanceWaitHandler`, and `DeleteInstanceWaitHandler` | ||
|
|
||
| ## v0.1.0 (2024-05-22) | ||
|
|
||
| - Manage your STACKIT SQL Server Flex resources: `Instance`, `Flavors`, `Users`, `Databases`, `Backups` | ||
| - Manage your STACKIT SQL Server Flex resources: `Instance`, `Flavors`, `Users`, `Databases`, `Backups` |
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 |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/wait" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" | ||
| ) | ||
|
|
||
| const ( | ||
| InstanceStateEmpty = "" | ||
| InstanceStateProcessing = "Progressing" | ||
| InstanceStateUnknown = "Unknown" | ||
| InstanceStateSuccess = "Ready" | ||
| InstanceStateFailed = "Failed" | ||
| ) | ||
|
|
||
| // Interface needed for tests | ||
| type APIClientInstanceInterface interface { | ||
| GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*sqlserverflex.GetInstanceResponse, error) | ||
| } | ||
|
|
||
| // CreateInstanceWaitHandler will wait for instance creation | ||
| func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) { | ||
| s, err := a.GetInstanceExecute(ctx, projectId, instanceId) | ||
| 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, s, nil | ||
| case InstanceStateProcessing: | ||
| return false, s, nil | ||
| case InstanceStateUnknown: | ||
| return false, s, nil | ||
| case InstanceStateSuccess: | ||
| return true, s, nil | ||
| case InstanceStateFailed: | ||
| return true, s, fmt.Errorf("create failed for instance with id %s", instanceId) | ||
| } | ||
| }) | ||
| handler.SetTimeout(45 * time.Minute) | ||
| handler.SetSleepBeforeWait(5 * time.Second) | ||
| return handler | ||
| } | ||
|
|
||
| // UpdateInstanceWaitHandler will wait for instance update | ||
| func UpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) { | ||
| s, err := a.GetInstanceExecute(ctx, projectId, instanceId) | ||
| 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, s, nil | ||
| case InstanceStateProcessing: | ||
| return false, s, nil | ||
| case InstanceStateUnknown: | ||
| return false, s, nil | ||
| case InstanceStateSuccess: | ||
| return true, s, nil | ||
| case InstanceStateFailed: | ||
| return true, s, fmt.Errorf("update failed for instance with id %s", instanceId) | ||
| } | ||
| }) | ||
| handler.SetSleepBeforeWait(2 * time.Second) | ||
| handler.SetTimeout(45 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| // PartialUpdateInstanceWaitHandler will wait for instance update | ||
| func PartialUpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] { | ||
| return UpdateInstanceWaitHandler(ctx, a, projectId, instanceId) | ||
| } | ||
|
|
||
| // DeleteInstanceWaitHandler will wait for instance deletion | ||
| func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[struct{}] { | ||
| handler := wait.New(func() (waitFinished bool, response *struct{}, err error) { | ||
| _, err = a.GetInstanceExecute(ctx, projectId, instanceId) | ||
| 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, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError") | ||
| } | ||
| if oapiErr.StatusCode != http.StatusNotFound { | ||
| return false, nil, err | ||
| } | ||
| return true, nil, nil | ||
| }) | ||
| handler.SetTimeout(15 * time.Minute) | ||
| return handler | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.