-
Notifications
You must be signed in to change notification settings - Fork 24
Waiters for alb api #1734
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
Waiters for alb api #1734
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50f79ff
feat: implement waiter for alb api
bahkauv70 984ea70
feat: updated changelogs
bahkauv70 275f956
fix: set version to 0.2.0
bahkauv70 e7c5d55
feat: generalized openapi error constructors, which introduces new co…
bahkauv70 2b2a5eb
fix: integrated review findings
bahkauv70 6757c1b
fix: updated changelogs
bahkauv70 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,2 +1,5 @@ | ||
| ## v0.2.0 (2025-03-20) | ||
| - **Enhancement:** Provider waiter for loadbalancer api | ||
|
|
||
| ## v0.1.0 (2025-03-19) | ||
| - **New:** API for application load balancer |
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,68 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "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/alb" | ||
| ) | ||
|
|
||
| const ( | ||
| StatusUnspecified = "STATUS_UNSPECIFIED" | ||
| StatusPending = "STATUS_PENDING" | ||
| StatusReady = "STATUS_READY" | ||
| StatusError = "STATUS_ERROR" | ||
| StatusTerminating = "STATUS_TERMINATING" | ||
| ) | ||
|
|
||
| type APIClientLoadbalancerInterface interface { | ||
| GetLoadBalancerExecute(ctx context.Context, projectId string, region string, name string) (*alb.LoadBalancer, error) | ||
| } | ||
|
|
||
| func CreateOrUpdateLoadbalancerWaitHandler(ctx context.Context, client APIClientLoadbalancerInterface, projectId, region, name string) *wait.AsyncActionHandler[alb.LoadBalancer] { | ||
| handler := wait.New(func() (bool, *alb.LoadBalancer, error) { | ||
| response, err := client.GetLoadBalancerExecute(ctx, projectId, region, name) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
| if response.HasStatus() { | ||
| switch *response.Status { | ||
| case StatusPending: | ||
| return false, nil, nil | ||
| case StatusUnspecified: | ||
| return false, nil, nil | ||
| case StatusError: | ||
| return true, response, fmt.Errorf("loadbalancer in error: %s", *response.Status) | ||
| default: | ||
| return true, response, nil | ||
| } | ||
| } | ||
|
|
||
| return false, nil, nil | ||
| }) | ||
| handler.SetTimeout(10 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func DeleteLoadbalancerWaitHandler(ctx context.Context, client APIClientLoadbalancerInterface, projectId, region, name string) *wait.AsyncActionHandler[alb.LoadBalancer] { | ||
| handler := wait.New(func() (bool, *alb.LoadBalancer, error) { | ||
| _, err := client.GetLoadBalancerExecute(ctx, projectId, region, name) | ||
| if err != nil { | ||
| var apiErr *oapierror.GenericOpenAPIError | ||
| if errors.As(err, &apiErr) { | ||
| if statusCode := apiErr.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone { | ||
| return true, nil, nil | ||
| } | ||
| } | ||
| return true, nil, err | ||
| } | ||
| return false, nil, nil | ||
| }) | ||
| handler.SetTimeout(10 * 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,203 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/uuid" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/alb" | ||
| ) | ||
|
|
||
| var ( | ||
| testProject = uuid.NewString() | ||
| testRegion = "eu01" | ||
| testName = "testlb" | ||
| ) | ||
|
|
||
| var _ APIClientLoadbalancerInterface = (*apiClientLoadbalancerMocked)(nil) | ||
|
|
||
| type response struct { | ||
| loadbalancer *alb.LoadBalancer | ||
| err error | ||
| } | ||
|
|
||
| type apiClientLoadbalancerMocked struct { | ||
| n int | ||
| responses []response | ||
| } | ||
|
|
||
| // GetLoadBalancerExecute implements APIClientLoadbalancerInterface. | ||
| func (a *apiClientLoadbalancerMocked) GetLoadBalancerExecute(_ context.Context, _, _, _ string) (*alb.LoadBalancer, error) { | ||
| resp := a.responses[a.n] | ||
| a.n++ | ||
| a.n %= len(a.responses) | ||
| return resp.loadbalancer, resp.err | ||
| } | ||
|
|
||
| func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| responses []response | ||
| want *alb.LoadBalancer | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| "create succeeded immediately", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusReady)}, nil}, | ||
| }, | ||
| &alb.LoadBalancer{Status: utils.Ptr(StatusReady)}, | ||
| false, | ||
| }, | ||
| { | ||
| "create succeeded delayed", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusReady)}, nil}, | ||
| }, | ||
| &alb.LoadBalancer{Status: utils.Ptr(StatusReady)}, | ||
| false, | ||
| }, | ||
| { | ||
| "create failed delayed", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusError)}, nil}, | ||
| }, | ||
| &alb.LoadBalancer{Status: utils.Ptr(StatusError)}, | ||
| true, | ||
| }, | ||
| { | ||
| "timeout", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: alb.PtrString(StatusPending)}, nil}, | ||
| }, | ||
| nil, | ||
| true, | ||
| }, | ||
| { | ||
| "broken state", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: alb.PtrString("bogus")}, nil}, | ||
| }, | ||
| &alb.LoadBalancer{Status: alb.PtrString("bogus")}, | ||
| false, | ||
| }, | ||
| // no special update tests needed as the states are the same | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| client := &apiClientLoadbalancerMocked{ | ||
| responses: tt.responses, | ||
| } | ||
| handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName) | ||
| got, err := handler.SetTimeout(1 * time.Second). | ||
| SetThrottle(250 * time.Millisecond). | ||
| WaitWithContext(ctx) | ||
|
|
||
| if (err != nil) != tt.wantErr { | ||
| t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err) | ||
| } | ||
|
|
||
| if diff := cmp.Diff(tt.want, got); diff != "" { | ||
| t.Errorf("differing loadbalancer %s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDeleteLoadbalancerWaitHandler(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| responses []response | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| "Delete with '404' succeeded immediately", | ||
| []response{ | ||
| {nil, oapierror.NewError(http.StatusNotFound, "not found")}, | ||
| }, | ||
| false, | ||
| }, | ||
| { | ||
| "Delete with '404' delayed", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {nil, oapierror.NewError(http.StatusNotFound, "not found")}, | ||
| }, | ||
| false, | ||
| }, | ||
| { | ||
| "Delete with 'gone' succeeded immediately", | ||
| []response{ | ||
| {nil, oapierror.NewError(http.StatusGone, "gone")}, | ||
| }, | ||
| false, | ||
| }, | ||
| { | ||
| "Delete with 'gone' delayed", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {nil, oapierror.NewError(http.StatusGone, "not found")}, | ||
| }, | ||
| false, | ||
| }, | ||
| { | ||
| "Delete with error delayed", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(string(StatusError))}, oapierror.NewError(http.StatusInternalServerError, "kapow")}, | ||
| }, | ||
| true, | ||
| }, | ||
| { | ||
| "Cannot delete", | ||
| []response{ | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil}, | ||
| {&alb.LoadBalancer{Status: utils.Ptr(string(StatusError))}, oapierror.NewError(http.StatusOK, "ok")}, | ||
| }, | ||
| true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| client := &apiClientLoadbalancerMocked{ | ||
| responses: tt.responses, | ||
| } | ||
| handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName) | ||
| _, err := handler.SetTimeout(1 * time.Second). | ||
| SetThrottle(250 * time.Millisecond). | ||
| WaitWithContext(ctx) | ||
|
|
||
| if tt.wantErr != (err != nil) { | ||
| t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err) | ||
| } | ||
| if tt.wantErr { | ||
| var apiErr *oapierror.GenericOpenAPIError | ||
| if !errors.As(err, &apiErr) { | ||
| t.Fatalf("expected openapi error, got %v", err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.