-
Notifications
You must be signed in to change notification settings - Fork 24
Add waiters for service enablement EnableService and DisableService #630
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 2 commits
Commits
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 |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## v0.2.0 (2024-07-12) | ||
|
|
||
| - **Feature**: New waiters `EnableServiceWaitHandler` and `DisableServiceWaitHandler` for async operations `EnableService` and `DisableService`, respectively. | ||
|
|
||
| ## v0.1.0 (2024-05-17) | ||
|
|
||
| - **New**: STACKIT Service Enablement module can be used to enable services |
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,74 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/wait" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/serviceenablement" | ||
| ) | ||
|
|
||
| const ( | ||
| ServiceStateEnabled = "ENABLED" | ||
| ServiceStateEnabling = "ENABLING" | ||
| ServiceStateDisabled = "DISABLED" | ||
| ServiceStateDisabling = "DISABLING" | ||
| ) | ||
|
|
||
| // Interface needed for tests | ||
| type APIClientInstanceInterface interface { | ||
| GetServiceStatusExecute(ctx context.Context, projectId, serviceId string) (*serviceenablement.ServiceStatus, error) | ||
| } | ||
|
|
||
| func EnableServiceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, serviceId string) *wait.AsyncActionHandler[serviceenablement.ServiceStatus] { | ||
| handler := wait.New(func() (waitFinished bool, response *serviceenablement.ServiceStatus, err error) { | ||
| s, err := a.GetServiceStatusExecute(ctx, projectId, serviceId) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
| if s == nil || s.State == nil { | ||
| return false, nil, nil | ||
| } | ||
| switch *s.State { | ||
| default: | ||
| return true, s, fmt.Errorf("service with id %s has unexpected state %s", serviceId, *s.State) | ||
| case ServiceStateEnabled: | ||
| return true, s, nil | ||
| case ServiceStateEnabling: | ||
| return false, nil, nil | ||
| case ServiceStateDisabled: | ||
| return true, s, fmt.Errorf("enabling failed for service with id %s", serviceId) | ||
| case ServiceStateDisabling: | ||
| return true, s, fmt.Errorf("service with id %s is in state %s", serviceId, *s.State) | ||
| } | ||
| }) | ||
| handler.SetTimeout(45 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func DisableServiceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, serviceId string) *wait.AsyncActionHandler[serviceenablement.ServiceStatus] { | ||
| handler := wait.New(func() (waitFinished bool, response *serviceenablement.ServiceStatus, err error) { | ||
| s, err := a.GetServiceStatusExecute(ctx, projectId, serviceId) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
| if s == nil || s.State == nil { | ||
| return false, nil, nil | ||
| } | ||
| switch *s.State { | ||
| default: | ||
| return true, s, fmt.Errorf("service with id %s has unexpected state %s", serviceId, *s.State) | ||
| case ServiceStateDisabled: | ||
| return true, s, nil | ||
| case ServiceStateDisabling: | ||
| return false, nil, nil | ||
| case ServiceStateEnabled: | ||
| return true, s, fmt.Errorf("disabling failed for service with id %s", serviceId) | ||
| case ServiceStateEnabling: | ||
| return true, s, fmt.Errorf("service with id %s is in state %s", serviceId, *s.State) | ||
| } | ||
| }) | ||
| handler.SetTimeout(45 * 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/serviceenablement" | ||
| ) | ||
|
|
||
| // Used for testing service operations | ||
| type apiClientServiceMocked struct { | ||
| serviceId string | ||
| serviceState string | ||
| getServiceFails bool | ||
| } | ||
|
|
||
| func (a *apiClientServiceMocked) GetServiceStatusExecute(_ context.Context, _, _ string) (*serviceenablement.ServiceStatus, error) { | ||
| if a.getServiceFails { | ||
| return nil, &oapierror.GenericOpenAPIError{ | ||
| StatusCode: 500, | ||
| } | ||
| } | ||
|
|
||
| return &serviceenablement.ServiceStatus{ | ||
| ServiceId: &a.serviceId, | ||
| State: &a.serviceState, | ||
| }, nil | ||
| } | ||
|
|
||
| func TestEnableServiceWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| desc string | ||
| getServiceFails bool | ||
| serviceState string | ||
| wantErr bool | ||
| wantResp bool | ||
| }{ | ||
| { | ||
| desc: "enable_succeeded", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateEnabled, | ||
| wantErr: false, | ||
| wantResp: true, | ||
| }, | ||
| { | ||
| desc: "enable_failed", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateDisabled, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| { | ||
| desc: "enable_failed_2", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateDisabling, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| { | ||
| desc: "timeout", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateEnabling, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| { | ||
| desc: "get_service_fails", | ||
| getServiceFails: true, | ||
| serviceState: ServiceStateEnabling, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.desc, func(t *testing.T) { | ||
| serviceId := "serviceId" | ||
|
|
||
| apiClient := &apiClientServiceMocked{ | ||
| serviceId: serviceId, | ||
| serviceState: tt.serviceState, | ||
| getServiceFails: tt.getServiceFails, | ||
| } | ||
|
|
||
| var wantRes *serviceenablement.ServiceStatus | ||
| if tt.wantResp { | ||
| wantRes = &serviceenablement.ServiceStatus{ | ||
| ServiceId: &serviceId, | ||
| State: &tt.serviceState, | ||
| } | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| handler := EnableServiceWaitHandler(ctx, apiClient, "projectId", serviceId) | ||
|
|
||
| gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(ctx) | ||
|
|
||
| if err != nil { | ||
| if !tt.wantErr { | ||
| t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| return | ||
| } | ||
| if !cmp.Equal(gotRes, wantRes) { | ||
| t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func TestDisableServiceWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| desc string | ||
| getServiceFails bool | ||
| serviceState string | ||
| wantErr bool | ||
| wantResp bool | ||
| }{ | ||
| { | ||
| desc: "disable_succeeded", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateDisabled, | ||
| wantErr: false, | ||
| wantResp: true, | ||
| }, | ||
| { | ||
| desc: "disable_failed", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateEnabled, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| { | ||
| desc: "disable_failed_2", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateEnabling, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| { | ||
| desc: "timeout", | ||
| getServiceFails: false, | ||
| serviceState: ServiceStateDisabling, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| { | ||
| desc: "get_service_fails", | ||
| getServiceFails: true, | ||
| serviceState: ServiceStateDisabling, | ||
| wantErr: true, | ||
| wantResp: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.desc, func(t *testing.T) { | ||
| serviceId := "serviceId" | ||
|
|
||
| apiClient := &apiClientServiceMocked{ | ||
| serviceId: serviceId, | ||
| serviceState: tt.serviceState, | ||
| getServiceFails: tt.getServiceFails, | ||
| } | ||
|
|
||
| var wantRes *serviceenablement.ServiceStatus | ||
| if tt.wantResp { | ||
| wantRes = &serviceenablement.ServiceStatus{ | ||
| ServiceId: &serviceId, | ||
| State: &tt.serviceState, | ||
| } | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| handler := DisableServiceWaitHandler(ctx, apiClient, "projectId", serviceId) | ||
|
|
||
| gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(ctx) | ||
|
|
||
| if err != nil { | ||
| if !tt.wantErr { | ||
| t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) | ||
| } | ||
| return | ||
| } | ||
| if !cmp.Equal(gotRes, wantRes) { | ||
| t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
| } |
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.