Skip to content
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

Update core to v0.2 on Load Balancer #78

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/loadbalancer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/google/go-cmp v0.5.9
github.com/stackitcloud/stackit-sdk-go/core v0.1.1
github.com/stackitcloud/stackit-sdk-go/core v0.2.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions services/loadbalancer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/stackitcloud/stackit-sdk-go/core v0.1.1 h1:zePWTYnLi0HKViCRSDSF5u10zXH1yAk2yI+0N7phSbA=
github.com/stackitcloud/stackit-sdk-go/core v0.1.1/go.mod h1:2LEfP04fooOahpKVwPTUSMNvBC1Cc0ky5FKtgN7pJ4M=
github.com/stackitcloud/stackit-sdk-go/core v0.2.0 h1:dv9pMtGN6p5HbbpaB1PjncOdDPDyP4mXfXBxk7j9x0c=
github.com/stackitcloud/stackit-sdk-go/core v0.2.0/go.mod h1:Bne56SlX8V2GbONcofEmZz5lVLuuZgsc015FPIUYXy4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
Expand Down
26 changes: 7 additions & 19 deletions services/loadbalancer/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"net/http"

"github.com/stackitcloud/stackit-sdk-go/core/utils"
oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/core/wait"
)

Expand Down Expand Up @@ -34,24 +34,12 @@ type APIClientInterface interface {
GetStatusExecute(ctx context.Context, projectId string) (*StatusResponse, error)
}

func handleError(reqErr error) (res interface{}, done bool, err error) {
oapiErr, ok := reqErr.(*GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if !ok {
return nil, false, fmt.Errorf("could not convert error to GenericOpenApiError")
}
// Some APIs may return temporary errors and the request should be retried
if utils.Contains(wait.RetryHttpErrorStatusCodes, oapiErr.statusCode) {
return nil, false, nil
}
return nil, false, reqErr
}

// CreateInstanceWaitHandler will wait for creation
func CreateInstanceWaitHandler(ctx context.Context, a APIClientInterface, projectId, instanceName string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := a.GetLoadBalancerExecute(ctx, projectId, instanceName)
if err != nil {
return handleError(err)
return nil, false, err
}
if s == nil || s.Name == nil || *s.Name != instanceName || s.Status == nil {
return s, false, nil
Expand Down Expand Up @@ -80,12 +68,12 @@ func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInterface, projec
if err == nil {
return s, false, nil
}
oapiErr, ok := err.(*GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
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 nil, false, fmt.Errorf("could not convert error to GenericOpenApiError")
return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError")
}
if oapiErr.statusCode != http.StatusNotFound {
return handleError(err)
if oapiErr.StatusCode != http.StatusNotFound {
return nil, false, err
}
return nil, true, nil
})
Expand All @@ -96,7 +84,7 @@ func EnableLoadBalancingWaitHandler(ctx context.Context, a APIClientInterface, p
return wait.New(func() (res interface{}, done bool, err error) {
s, err := a.GetStatusExecute(ctx, projectId)
if err != nil {
return handleError(err)
return nil, false, err
}
if s == nil || s.Status == nil {
return s, false, nil
Expand Down
75 changes: 7 additions & 68 deletions services/loadbalancer/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package loadbalancer

import (
"context"
"fmt"
"net/http"
"testing"
"time"

"github.com/google/go-cmp/cmp"
oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
)

// Used for testing instance operations
Expand All @@ -22,14 +21,14 @@ type apiClientMocked struct {

func (a *apiClientMocked) GetLoadBalancerExecute(_ context.Context, _, _ string) (*LoadBalancer, error) {
if a.instanceGetFails {
return nil, &GenericOpenAPIError{
statusCode: 500,
return nil, &oapiError.GenericOpenAPIError{
StatusCode: 500,
}
}

if a.instanceIsDeleted {
return nil, &GenericOpenAPIError{
statusCode: 404,
return nil, &oapiError.GenericOpenAPIError{
StatusCode: 404,
}
}

Expand All @@ -40,8 +39,8 @@ func (a *apiClientMocked) GetLoadBalancerExecute(_ context.Context, _, _ string)
}
func (a *apiClientMocked) GetStatusExecute(_ context.Context, _ string) (*StatusResponse, error) {
if a.functionalityStatusGetFails {
return nil, &GenericOpenAPIError{
statusCode: 500,
return nil, &oapiError.GenericOpenAPIError{
StatusCode: 500,
}
}

Expand All @@ -50,66 +49,6 @@ func (a *apiClientMocked) GetStatusExecute(_ context.Context, _ string) (*Status
}, nil
}

func TestHandleError(t *testing.T) {
tests := []struct {
desc string
reqErr error
wantRes interface{}
wantDone bool
wantErr bool
}{
{
desc: "handle_oapi_error",
reqErr: &GenericOpenAPIError{
statusCode: 500,
},
wantRes: nil,
wantDone: false,
wantErr: true,
},
{
desc: "not_generic_oapi_error",
reqErr: fmt.Errorf("some error"),
wantRes: nil,
wantDone: false,
wantErr: true,
},
{
desc: "bad_gateway_error",
reqErr: &GenericOpenAPIError{
statusCode: http.StatusBadGateway,
},
wantRes: nil,
wantDone: false,
wantErr: false,
},
{
desc: "gateway_timeout_error",
reqErr: &GenericOpenAPIError{
statusCode: http.StatusBadGateway,
},
wantRes: nil,
wantDone: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
gotRes, gotDone, err := handleError(tt.reqErr)
if (err != nil) != tt.wantErr {
t.Errorf("handleError() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(gotRes, tt.wantRes) {
t.Errorf("handleError() gotRes = %v, want %v", gotRes, tt.wantRes)
}
if gotDone != tt.wantDone {
t.Errorf("handleError() gotDone = %v, want %v", gotDone, tt.wantDone)
}
})
}
}

func TestCreateInstanceWaitHandler(t *testing.T) {
tests := []struct {
desc string
Expand Down