-
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 3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module github.com/stackitcloud/stackit-sdk-go/examples/sqlserverflex | ||
|
|
||
| go 1.18 | ||
|
|
||
| require github.com/stackitcloud/stackit-sdk-go/core v0.12.0 | ||
|
|
||
| require ( | ||
| github.com/golang-jwt/jwt/v5 v5.2.1 // indirect | ||
| github.com/google/uuid v1.6.0 // indirect | ||
| github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.1.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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= | ||
| github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= | ||
| github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
| github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
| github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
| github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U= | ||
| github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= | ||
| github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.1.0 h1:zndnPbQLR9aeJXK7bWomQP1WcZgrzfPpDXlti4v4DP0= | ||
| github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.1.0/go.mod h1:27rHZe5pbIuDOdO2aZpUAWoRgOC2UB7rBJfmFnR1pBw= |
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,76 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/config" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/wait" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Specify the project ID | ||
| projectId := "16f49d71-37ad-4137-8b97-44d9c55c4094" | ||
|
|
||
| // Specify instance configuration options | ||
| // flavorId := "FLAVOR_ID" | ||
| // version := "VERSION" | ||
joaopalet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| flavorId := "4.16-Single" | ||
| version := "2022" | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| // Create a new API client, that uses default authentication and configuration | ||
| sqlserverflexClient, err := sqlserverflex.NewAPIClient( | ||
| config.WithRegion("eu01"), | ||
| ) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Get the MongoDB Flex instances for your project | ||
| getInstancesResp, err := sqlserverflexClient.ListInstances(ctx, projectId).Execute() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error when calling `GetInstances`: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| items := *getInstancesResp.Items | ||
| fmt.Printf("Number of instances: %v\n", len(items)) | ||
|
|
||
| // Create an instance | ||
| createInstancePayload := sqlserverflex.CreateInstancePayload{ | ||
| Name: utils.Ptr("my-instance-3"), | ||
| FlavorId: utils.Ptr(flavorId), | ||
| Version: utils.Ptr(version), | ||
| } | ||
| instance, err := sqlserverflexClient.CreateInstance(ctx, projectId).CreateInstancePayload(createInstancePayload).Execute() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error creating SQL Server Flex instance: %v\n", err) | ||
| } | ||
| instanceId := *instance.Id | ||
|
|
||
| _, err = wait.CreateInstanceWaitHandler(ctx, sqlserverflexClient, projectId, instanceId).WaitWithContext(ctx) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error when waiting for SQL Server Flex instance creation: %v\n", err) | ||
| } | ||
|
|
||
| fmt.Printf("Created SQL Server Flex instance \"%s\".\n", instanceId) | ||
|
|
||
| // Delete an instance | ||
| err = sqlserverflexClient.DeleteInstance(ctx, projectId, instanceId).Execute() | ||
|
|
||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error deleting SQL Server Flex instance: %v\n", err) | ||
| } | ||
|
|
||
| _, err = wait.DeleteInstanceWaitHandler(ctx, sqlserverflexClient, projectId, instanceId).WaitWithContext(ctx) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error when waiting for SQL Server Flex instance deletion: %v\n", err) | ||
| } | ||
|
|
||
| fmt.Printf("Deleted SQL Server Flex instance \"%s\".\n", instanceId) | ||
| } | ||
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.