fix: builder and sdk changes (#81)
## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: marcel.henselin <marcel.henselin@stackit.cloud>
Reviewed-on: #81
This commit is contained in:
parent
635a9abf20
commit
1033d7e034
145 changed files with 5944 additions and 5298 deletions
|
|
@ -9,8 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
|
||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/wait"
|
||||
|
|
@ -25,68 +24,60 @@ const (
|
|||
InstanceStateTerminating = "TERMINATING"
|
||||
InstanceStateUnknown = "UNKNOWN"
|
||||
InstanceStatePending = "PENDING"
|
||||
InstanceStateDeleted = "DELETED"
|
||||
)
|
||||
|
||||
// APIClientInstanceInterface Interface needed for tests
|
||||
type APIClientInstanceInterface interface {
|
||||
GetInstanceRequestExecute(ctx context.Context, projectId, region, instanceId string) (
|
||||
*postgresflex.GetInstanceResponse,
|
||||
error,
|
||||
)
|
||||
GetInstanceRequest(ctx context.Context, projectId, region, instanceId string) v3alpha1api.ApiGetInstanceRequestRequest
|
||||
|
||||
ListUsersRequestExecute(
|
||||
ListUsersRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) (*postgresflex.ListUserResponse, error)
|
||||
) v3alpha1api.ApiListUsersRequestRequest
|
||||
}
|
||||
|
||||
// APIClientUserInterface Interface needed for tests
|
||||
type APIClientUserInterface interface {
|
||||
GetUserRequestExecute(ctx context.Context, projectId, region, instanceId string, userId int32) (
|
||||
*postgresflex.GetUserResponse,
|
||||
error,
|
||||
)
|
||||
GetUserRequest(ctx context.Context, projectId, region, instanceId string, userId int32) v3alpha1api.ApiGetUserRequestRequest
|
||||
}
|
||||
|
||||
GetDatabaseRequestExecute(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
databaseId int32,
|
||||
) (*postgresflex.GetDatabaseResponse, error)
|
||||
// APIClientDatabaseInterface Interface needed for tests
|
||||
type APIClientDatabaseInterface interface {
|
||||
GetDatabaseRequest(ctx context.Context, projectId string, region string, instanceId string, databaseId int32) v3alpha1api.ApiGetDatabaseRequestRequest
|
||||
}
|
||||
|
||||
// CreateInstanceWaitHandler will wait for instance creation
|
||||
func CreateInstanceWaitHandler(
|
||||
ctx context.Context, a APIClientInstanceInterface, projectId, region,
|
||||
instanceId string,
|
||||
) *wait.AsyncActionHandler[postgresflex.GetInstanceResponse] {
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetInstanceResponse] {
|
||||
instanceCreated := false
|
||||
var instanceGetResponse *postgresflex.GetInstanceResponse
|
||||
var instanceGetResponse *v3alpha1api.GetInstanceResponse
|
||||
maxWait := time.Minute * 45
|
||||
startTime := time.Now()
|
||||
extendedTimeout := 0
|
||||
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *postgresflex.GetInstanceResponse, err error) {
|
||||
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
|
||||
if !instanceCreated {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
if s == nil || s.Id == nil || *s.Id != instanceId || s.Status == nil {
|
||||
if s == nil || s.Id != instanceId {
|
||||
return false, nil, nil
|
||||
}
|
||||
tflog.Debug(
|
||||
ctx, "waiting for instance ready", map[string]interface{}{
|
||||
"status": *s.Status,
|
||||
"status": s.Status,
|
||||
},
|
||||
)
|
||||
switch *s.Status {
|
||||
switch s.Status {
|
||||
default:
|
||||
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, *s.Status)
|
||||
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, s.Status)
|
||||
case InstanceStateEmpty:
|
||||
return false, nil, nil
|
||||
case InstanceStatePending:
|
||||
|
|
@ -111,7 +102,7 @@ func CreateInstanceWaitHandler(
|
|||
extendedTimeout++
|
||||
if *s.Network.AccessScope == "SNA" {
|
||||
ready := true
|
||||
if s.Network == nil || s.Network.InstanceAddress == nil {
|
||||
if s.Network.InstanceAddress == nil {
|
||||
tflog.Warn(ctx, "Waiting for instance_address")
|
||||
ready = false
|
||||
}
|
||||
|
|
@ -123,16 +114,12 @@ func CreateInstanceWaitHandler(
|
|||
return false, nil, nil
|
||||
}
|
||||
}
|
||||
if s.IsDeletable == nil {
|
||||
tflog.Warn(ctx, "Waiting for is_deletable")
|
||||
return false, nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
instanceCreated = true
|
||||
instanceGetResponse = s
|
||||
case InstanceStateSuccess:
|
||||
if s.Network != nil && s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
|
||||
if s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
|
||||
if s.Network.InstanceAddress == nil {
|
||||
tflog.Warn(ctx, "Waiting for instance_address")
|
||||
return false, nil, nil
|
||||
|
|
@ -155,7 +142,7 @@ func CreateInstanceWaitHandler(
|
|||
tflog.Info(ctx, "Waiting for instance (calling list users")
|
||||
// // User operations aren't available right after an instance is deemed successful
|
||||
// // To check if they are, perform a users request
|
||||
_, err = a.ListUsersRequestExecute(ctx, projectId, region, instanceId)
|
||||
_, err = a.ListUsersRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err == nil {
|
||||
return true, instanceGetResponse, nil
|
||||
}
|
||||
|
|
@ -180,21 +167,21 @@ func CreateInstanceWaitHandler(
|
|||
|
||||
// PartialUpdateInstanceWaitHandler will wait for instance update
|
||||
func PartialUpdateInstanceWaitHandler(
|
||||
ctx context.Context, a APIClientInstanceInterface, projectId, region,
|
||||
instanceId string,
|
||||
) *wait.AsyncActionHandler[postgresflex.GetInstanceResponse] {
|
||||
ctx context.Context, a APIClientInstanceInterface, projectID, region,
|
||||
instanceID string,
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetInstanceResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *postgresflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
if s == nil || s.Id == nil || *s.Id != instanceId || s.Status == nil {
|
||||
if s == nil || s.Id != instanceID {
|
||||
return false, nil, nil
|
||||
}
|
||||
switch *s.Status {
|
||||
switch s.Status {
|
||||
default:
|
||||
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, *s.Status)
|
||||
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceID, s.Status)
|
||||
case InstanceStateEmpty:
|
||||
return false, nil, nil
|
||||
case InstanceStatePending:
|
||||
|
|
@ -208,7 +195,7 @@ func PartialUpdateInstanceWaitHandler(
|
|||
case InstanceStateUnknown:
|
||||
return false, nil, nil
|
||||
case InstanceStateFailed:
|
||||
return true, s, fmt.Errorf("update got status FAILURE for instance with id %s", instanceId)
|
||||
return true, s, fmt.Errorf("update got status FAILURE for instance with id %s", instanceID)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
|
@ -220,16 +207,16 @@ func PartialUpdateInstanceWaitHandler(
|
|||
func GetUserByIdWaitHandler(
|
||||
ctx context.Context,
|
||||
a APIClientUserInterface,
|
||||
projectId, instanceId, region string,
|
||||
userId int64,
|
||||
) *wait.AsyncActionHandler[postgresflex.GetUserResponse] {
|
||||
projectID, instanceID, region string,
|
||||
userID int64,
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetUserResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *postgresflex.GetUserResponse, err error) {
|
||||
if userId > math.MaxInt32 {
|
||||
return false, nil, fmt.Errorf("userId value is too big for int32")
|
||||
func() (waitFinished bool, response *v3alpha1api.GetUserResponse, err error) {
|
||||
if userID > math.MaxInt32 {
|
||||
return false, nil, fmt.Errorf("userID too large for int32")
|
||||
}
|
||||
userId32 := int32(userId) //nolint:gosec // we need to convert databaseId to int32 because API expects int32
|
||||
s, err := a.GetUserRequestExecute(ctx, projectId, region, instanceId, userId32)
|
||||
userID32 := int32(userID) //nolint:gosec // checked above
|
||||
s, err := a.GetUserRequest(ctx, projectID, region, instanceID, userID32).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
|
|
@ -258,14 +245,17 @@ func GetUserByIdWaitHandler(
|
|||
// GetDatabaseByIdWaitHandler will wait for instance creation
|
||||
func GetDatabaseByIdWaitHandler(
|
||||
ctx context.Context,
|
||||
a APIClientUserInterface,
|
||||
projectId, instanceId, region string,
|
||||
databaseId int64,
|
||||
) *wait.AsyncActionHandler[postgresflex.GetDatabaseResponse] {
|
||||
a APIClientDatabaseInterface,
|
||||
projectID, instanceID, region string,
|
||||
databaseID int64,
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetDatabaseResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *postgresflex.GetDatabaseResponse, err error) {
|
||||
dbId32 := int32(databaseId) //nolint:gosec // we need to convert databaseId to int32 because API expects int32
|
||||
s, err := a.GetDatabaseRequestExecute(ctx, projectId, region, instanceId, dbId32)
|
||||
func() (waitFinished bool, response *v3alpha1api.GetDatabaseResponse, err error) {
|
||||
if databaseID > math.MaxInt32 {
|
||||
return false, nil, fmt.Errorf("databaseID too large for int32")
|
||||
}
|
||||
dbId32 := int32(databaseID) //nolint:gosec // is checked above
|
||||
s, err := a.GetDatabaseRequest(ctx, projectID, region, instanceID, dbId32).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
|
|
@ -296,3 +286,46 @@ func GetDatabaseByIdWaitHandler(
|
|||
)
|
||||
return handler
|
||||
}
|
||||
|
||||
func DeleteInstanceWaitHandler(
|
||||
ctx context.Context,
|
||||
a APIClientInstanceInterface,
|
||||
projectID,
|
||||
region,
|
||||
instanceID string,
|
||||
timeout, sleepBeforeWait time.Duration,
|
||||
) error {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequest(ctx, projectID, region, instanceID).Execute()
|
||||
if err != 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("received error is no oapierror: %w", err)
|
||||
}
|
||||
if oapiErr.StatusCode == 404 {
|
||||
return true, nil, nil
|
||||
}
|
||||
return false, nil, fmt.Errorf("api returned error: %w", err)
|
||||
}
|
||||
switch s.Status {
|
||||
case InstanceStateDeleted:
|
||||
return true, nil, nil
|
||||
case InstanceStateEmpty, InstanceStatePending, InstanceStateUnknown, InstanceStateProgressing, InstanceStateSuccess:
|
||||
return false, nil, nil
|
||||
case InstanceStateFailed:
|
||||
return true, nil, fmt.Errorf("wait handler got status FAILURE for instance: %s", instanceID)
|
||||
default:
|
||||
return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceID, s.Status)
|
||||
}
|
||||
},
|
||||
).
|
||||
SetTimeout(timeout).
|
||||
SetSleepBeforeWait(sleepBeforeWait)
|
||||
|
||||
_, err := handler.WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,87 +10,34 @@ import (
|
|||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
)
|
||||
|
||||
// Used for testing instance operations
|
||||
type apiClientInstanceMocked struct {
|
||||
instanceId string
|
||||
instanceState string
|
||||
instanceNetwork postgresflex.InstanceNetwork
|
||||
instanceIsForceDeleted bool
|
||||
instanceGetFails bool
|
||||
usersGetErrorStatus int
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetInstanceRequestExecute(
|
||||
_ context.Context,
|
||||
_, _, _ string,
|
||||
) (*postgresflex.GetInstanceResponse, error) {
|
||||
if a.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
if a.instanceIsForceDeleted {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 404,
|
||||
}
|
||||
}
|
||||
|
||||
return &postgresflex.GetInstanceResponse{
|
||||
Id: &a.instanceId,
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(&a.instanceState),
|
||||
Network: postgresflex.GetInstanceResponseGetNetworkAttributeType(&a.instanceNetwork),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListUsersRequestExecute(
|
||||
_ context.Context,
|
||||
_, _, _ string,
|
||||
) (*postgresflex.ListUserResponse, error) {
|
||||
if a.usersGetErrorStatus != 0 {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: a.usersGetErrorStatus,
|
||||
}
|
||||
}
|
||||
|
||||
aux := int64(0)
|
||||
return &postgresflex.ListUserResponse{
|
||||
Pagination: &postgresflex.Pagination{
|
||||
TotalRows: &aux,
|
||||
},
|
||||
Users: &[]postgresflex.ListUser{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestCreateInstanceWaitHandler(t *testing.T) {
|
||||
tests := []struct {
|
||||
desc string
|
||||
instanceGetFails bool
|
||||
instanceState string
|
||||
instanceNetwork postgresflex.InstanceNetwork
|
||||
instanceNetwork v3alpha1api.InstanceNetwork
|
||||
usersGetErrorStatus int
|
||||
wantErr bool
|
||||
wantRes *postgresflex.GetInstanceResponse
|
||||
wantRes *v3alpha1api.GetInstanceResponse
|
||||
}{
|
||||
{
|
||||
desc: "create_succeeded",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateSuccess,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
RouterAddress: utils.Ptr("10.0.0.1"),
|
||||
},
|
||||
wantErr: false,
|
||||
wantRes: &postgresflex.GetInstanceResponse{
|
||||
Id: utils.Ptr("foo-bar"),
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(InstanceStateSuccess)),
|
||||
Network: &postgresflex.InstanceNetwork{
|
||||
wantRes: &v3alpha1api.GetInstanceResponse{
|
||||
Id: "foo-bar",
|
||||
Status: InstanceStateSuccess,
|
||||
Network: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -102,7 +49,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "create_failed",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateFailed,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -115,7 +62,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "create_failed_2",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateEmpty,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -134,7 +81,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "users_get_fails",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateSuccess,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -148,7 +95,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "users_get_fails_2",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateSuccess,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -156,10 +103,10 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
},
|
||||
usersGetErrorStatus: 400,
|
||||
wantErr: true,
|
||||
wantRes: &postgresflex.GetInstanceResponse{
|
||||
Id: utils.Ptr("foo-bar"),
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(InstanceStateSuccess)),
|
||||
Network: &postgresflex.InstanceNetwork{
|
||||
wantRes: &v3alpha1api.GetInstanceResponse{
|
||||
Id: "foo-bar",
|
||||
Status: InstanceStateSuccess,
|
||||
Network: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -171,8 +118,8 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "fail when response has no instance address",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateSuccess,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
AccessScope: postgresflex.InstanceNetworkGetAccessScopeAttributeType(utils.Ptr("SNA")),
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: (*v3alpha1api.InstanceNetworkAccessScope)(utils.Ptr("SNA")),
|
||||
Acl: nil,
|
||||
InstanceAddress: nil,
|
||||
RouterAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -184,8 +131,8 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "timeout",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateProgressing,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
AccessScope: postgresflex.InstanceNetworkGetAccessScopeAttributeType(utils.Ptr("SNA")),
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: (*v3alpha1api.InstanceNetworkAccessScope)(utils.Ptr("SNA")),
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
RouterAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -197,17 +144,44 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
instanceId := "foo-bar"
|
||||
instanceID := "foo-bar"
|
||||
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceId: instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
instanceNetwork: tt.instanceNetwork,
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
usersGetErrorStatus: tt.usersGetErrorStatus,
|
||||
listUsersMock := func(_ v3alpha1api.ApiListUsersRequestRequest) (*v3alpha1api.ListUserResponse, error) {
|
||||
if tt.usersGetErrorStatus != 0 {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: tt.usersGetErrorStatus,
|
||||
}
|
||||
}
|
||||
|
||||
aux := int32(0)
|
||||
return &v3alpha1api.ListUserResponse{
|
||||
Pagination: v3alpha1api.Pagination{
|
||||
TotalRows: aux,
|
||||
},
|
||||
Users: []v3alpha1api.ListUser{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", "", instanceId)
|
||||
getInstanceMock := func(_ v3alpha1api.ApiGetInstanceRequestRequest) (*v3alpha1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3alpha1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3alpha1api.Status(tt.instanceState),
|
||||
Network: tt.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
|
||||
apiClientMock := v3alpha1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &getInstanceMock,
|
||||
ListUsersRequestExecuteMock: &listUsersMock,
|
||||
}
|
||||
|
||||
handler := CreateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID)
|
||||
|
||||
gotRes, err := handler.SetTimeout(10 * time.Millisecond).SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
|
||||
if (err != nil) != tt.wantErr {
|
||||
|
|
@ -227,25 +201,25 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
|
|||
desc string
|
||||
instanceGetFails bool
|
||||
instanceState string
|
||||
instanceNetwork postgresflex.InstanceNetwork
|
||||
instanceNetwork v3alpha1api.InstanceNetwork
|
||||
wantErr bool
|
||||
wantRes *postgresflex.GetInstanceResponse
|
||||
wantRes *v3alpha1api.GetInstanceResponse
|
||||
}{
|
||||
{
|
||||
desc: "update_succeeded",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateSuccess,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
RouterAddress: utils.Ptr("10.0.0.1"),
|
||||
},
|
||||
wantErr: false,
|
||||
wantRes: &postgresflex.GetInstanceResponse{
|
||||
Id: utils.Ptr("foo-bar"),
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(InstanceStateSuccess)),
|
||||
Network: &postgresflex.InstanceNetwork{
|
||||
wantRes: &v3alpha1api.GetInstanceResponse{
|
||||
Id: "foo-bar",
|
||||
Status: v3alpha1api.Status(InstanceStateSuccess),
|
||||
Network: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -257,17 +231,17 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "update_failed",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateFailed,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
RouterAddress: utils.Ptr("10.0.0.1"),
|
||||
},
|
||||
wantErr: true,
|
||||
wantRes: &postgresflex.GetInstanceResponse{
|
||||
Id: utils.Ptr("foo-bar"),
|
||||
Status: postgresflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(InstanceStateFailed)),
|
||||
Network: &postgresflex.InstanceNetwork{
|
||||
wantRes: &v3alpha1api.GetInstanceResponse{
|
||||
Id: "foo-bar",
|
||||
Status: v3alpha1api.Status(InstanceStateFailed),
|
||||
Network: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -279,7 +253,7 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "update_failed_2",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateEmpty,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -298,7 +272,7 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
|
|||
desc: "timeout",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateProgressing,
|
||||
instanceNetwork: postgresflex.InstanceNetwork{
|
||||
instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
AccessScope: nil,
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -311,16 +285,38 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
instanceId := "foo-bar"
|
||||
instanceID := "foo-bar"
|
||||
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceId: instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
instanceNetwork: tt.instanceNetwork,
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
listUsersMock := func(_ v3alpha1api.ApiListUsersRequestRequest) (*v3alpha1api.ListUserResponse, error) {
|
||||
aux := int32(0)
|
||||
return &v3alpha1api.ListUserResponse{
|
||||
Pagination: v3alpha1api.Pagination{
|
||||
TotalRows: aux,
|
||||
},
|
||||
Users: []v3alpha1api.ListUser{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClient, "", "", instanceId)
|
||||
getInstanceMock := func(_ v3alpha1api.ApiGetInstanceRequestRequest) (*v3alpha1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3alpha1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3alpha1api.Status(tt.instanceState),
|
||||
Network: tt.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
|
||||
apiClientMock := v3alpha1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &getInstanceMock,
|
||||
ListUsersRequestExecuteMock: &listUsersMock,
|
||||
}
|
||||
|
||||
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClientMock, "", "", instanceID)
|
||||
|
||||
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
|
||||
if (err != nil) != tt.wantErr {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import (
|
|||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/wait"
|
||||
|
||||
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3alpha1api"
|
||||
)
|
||||
|
||||
// READY, PENDING, PROGRESSING, FAILURE, UNKNOWN,
|
||||
|
|
@ -28,45 +28,40 @@ const (
|
|||
|
||||
// APIClientInterface Interface needed for tests
|
||||
type APIClientInterface interface {
|
||||
GetInstanceRequestExecute(
|
||||
GetInstanceRequest(
|
||||
ctx context.Context,
|
||||
projectId, region, instanceId string,
|
||||
) (*sqlserverflex.GetInstanceResponse, error)
|
||||
GetDatabaseRequestExecute(
|
||||
) v3alpha1api.ApiGetInstanceRequestRequest
|
||||
|
||||
GetDatabaseRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
databaseName string,
|
||||
) (*sqlserverflex.GetDatabaseResponse, error)
|
||||
GetUserRequestExecute(
|
||||
) v3alpha1api.ApiGetDatabaseRequestRequest
|
||||
|
||||
GetUserRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
userId int64,
|
||||
) (*sqlserverflex.GetUserResponse, error)
|
||||
) v3alpha1api.ApiGetUserRequestRequest
|
||||
|
||||
ListRolesRequestExecute(
|
||||
ListRolesRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) (*sqlserverflex.ListRolesResponse, error)
|
||||
) v3alpha1api.ApiListRolesRequestRequest
|
||||
|
||||
ListUsersRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) sqlserverflex.ApiListUsersRequestRequest
|
||||
|
||||
ListUsersRequestExecute(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) (*sqlserverflex.ListUserResponse, error)
|
||||
) v3alpha1api.ApiListUsersRequestRequest
|
||||
}
|
||||
|
||||
// APIClientUserInterface Interface needed for tests
|
||||
|
|
@ -85,19 +80,19 @@ func CreateInstanceWaitHandler(
|
|||
ctx context.Context,
|
||||
a APIClientInterface,
|
||||
projectId, instanceId, region string,
|
||||
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetInstanceResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
if s == nil || s.Id == nil || *s.Id != instanceId || s.Status == nil {
|
||||
if s == nil || s.Id != instanceId {
|
||||
return false, nil, nil
|
||||
}
|
||||
switch strings.ToLower(string(*s.Status)) {
|
||||
switch strings.ToLower(string(s.Status)) {
|
||||
case strings.ToLower(InstanceStateSuccess):
|
||||
if s.Network != nil && s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
|
||||
if s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
|
||||
if s.Network.InstanceAddress == nil {
|
||||
tflog.Info(ctx, "Waiting for instance_address")
|
||||
return false, nil, nil
|
||||
|
|
@ -110,7 +105,7 @@ func CreateInstanceWaitHandler(
|
|||
|
||||
tflog.Info(ctx, "trying to get roles")
|
||||
time.Sleep(10 * time.Second)
|
||||
_, rolesErr := a.ListRolesRequestExecute(ctx, projectId, region, instanceId)
|
||||
_, rolesErr := a.ListRolesRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if rolesErr != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(rolesErr, &oapiErr)
|
||||
|
|
@ -134,7 +129,7 @@ func CreateInstanceWaitHandler(
|
|||
|
||||
tflog.Info(ctx, "trying to get users")
|
||||
time.Sleep(10 * time.Second)
|
||||
_, usersErr := a.ListUsersRequestExecute(ctx, projectId, region, instanceId)
|
||||
_, usersErr := a.ListUsersRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if usersErr != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(usersErr, &oapiErr)
|
||||
|
|
@ -161,7 +156,7 @@ func CreateInstanceWaitHandler(
|
|||
case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing):
|
||||
tflog.Info(
|
||||
ctx, "request is being handled", map[string]interface{}{
|
||||
"status": *s.Status,
|
||||
"status": s.Status,
|
||||
},
|
||||
)
|
||||
time.Sleep(10 * time.Second)
|
||||
|
|
@ -185,17 +180,17 @@ func UpdateInstanceWaitHandler(
|
|||
ctx context.Context,
|
||||
a APIClientInterface,
|
||||
projectId, instanceId, region string,
|
||||
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetInstanceResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
if s == nil || s.Id == nil || *s.Id != instanceId || s.Status == nil {
|
||||
if s == nil || s.Id != instanceId {
|
||||
return false, nil, nil
|
||||
}
|
||||
switch strings.ToLower(string(*s.Status)) {
|
||||
switch strings.ToLower(string(s.Status)) {
|
||||
case strings.ToLower(InstanceStateSuccess):
|
||||
return true, s, nil
|
||||
case strings.ToLower(InstanceStateUnknown), strings.ToLower(InstanceStateFailed):
|
||||
|
|
@ -203,7 +198,7 @@ func UpdateInstanceWaitHandler(
|
|||
case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing):
|
||||
tflog.Info(
|
||||
ctx, "request is being handled", map[string]interface{}{
|
||||
"status": *s.Status,
|
||||
"status": s.Status,
|
||||
},
|
||||
)
|
||||
return false, s, nil
|
||||
|
|
@ -226,10 +221,10 @@ func DeleteInstanceWaitHandler(
|
|||
ctx context.Context,
|
||||
a APIClientInterface,
|
||||
projectId, instanceId, region string,
|
||||
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetInstanceResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
func() (waitFinished bool, response *v3alpha1api.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err == nil {
|
||||
return false, s, nil
|
||||
}
|
||||
|
|
@ -253,10 +248,10 @@ func CreateDatabaseWaitHandler(
|
|||
ctx context.Context,
|
||||
a APIClientInterface,
|
||||
projectId, instanceId, region, databaseName string,
|
||||
) *wait.AsyncActionHandler[sqlserverflex.GetDatabaseResponse] {
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetDatabaseResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetDatabaseResponse, err error) {
|
||||
s, err := a.GetDatabaseRequestExecute(ctx, projectId, region, instanceId, databaseName)
|
||||
func() (waitFinished bool, response *v3alpha1api.GetDatabaseResponse, err error) {
|
||||
s, err := a.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
|
|
@ -271,7 +266,7 @@ func CreateDatabaseWaitHandler(
|
|||
}
|
||||
return false, nil, nil
|
||||
}
|
||||
if s == nil || s.Name == nil || *s.Name != databaseName {
|
||||
if s == nil || s.Name != databaseName {
|
||||
return false, nil, errors.New("response did return different result")
|
||||
}
|
||||
return true, s, nil
|
||||
|
|
@ -286,10 +281,10 @@ func CreateUserWaitHandler(
|
|||
a APIClientInterface,
|
||||
projectId, instanceId, region string,
|
||||
userId int64,
|
||||
) *wait.AsyncActionHandler[sqlserverflex.GetUserResponse] {
|
||||
) *wait.AsyncActionHandler[v3alpha1api.GetUserResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetUserResponse, err error) {
|
||||
s, err := a.GetUserRequestExecute(ctx, projectId, region, instanceId, userId)
|
||||
func() (waitFinished bool, response *v3alpha1api.GetUserResponse, err error) {
|
||||
s, err := a.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
|
|
@ -312,12 +307,12 @@ func WaitForUserWaitHandler(
|
|||
ctx context.Context,
|
||||
a APIClientInterface,
|
||||
projectId, instanceId, region, userName string,
|
||||
) *wait.AsyncActionHandler[sqlserverflex.ListUserResponse] {
|
||||
) *wait.AsyncActionHandler[v3alpha1api.ListUserResponse] {
|
||||
startTime := time.Now()
|
||||
timeOut := 2 * time.Minute
|
||||
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.ListUserResponse, err error) {
|
||||
func() (waitFinished bool, response *v3alpha1api.ListUserResponse, err error) {
|
||||
if time.Since(startTime) > timeOut {
|
||||
return false, nil, errors.New("ran into timeout")
|
||||
}
|
||||
|
|
@ -368,7 +363,7 @@ func DeleteUserWaitHandler(
|
|||
) *wait.AsyncActionHandler[struct{}] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *struct{}, err error) {
|
||||
_, err = a.GetUserRequestExecute(ctx, projectId, region, instanceId, userId)
|
||||
_, err = a.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err == nil {
|
||||
return false, nil, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,137 +9,35 @@ import (
|
|||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3alpha1api"
|
||||
)
|
||||
|
||||
// Used for testing instance operations
|
||||
type apiClientInstanceMocked struct {
|
||||
instanceId string
|
||||
instanceState string
|
||||
instanceNetwork sqlserverflex.InstanceNetwork
|
||||
instanceIsDeleted bool
|
||||
instanceGetFails bool
|
||||
}
|
||||
|
||||
type ListUsersRequestRequest struct{}
|
||||
|
||||
func (l ListUsersRequestRequest) Page(_ int64) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Size(_ int64) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Sort(_ sqlserverflex.UserSort) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Execute() (*sqlserverflex.ListUserResponse, error) {
|
||||
// TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListUsersRequest(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return ListUsersRequestRequest{}
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListRolesRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) (*sqlserverflex.ListRolesResponse, error) {
|
||||
return &sqlserverflex.ListRolesResponse{
|
||||
Roles: &[]string{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListUsersRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) (*sqlserverflex.ListUserResponse, error) {
|
||||
return &sqlserverflex.ListUserResponse{
|
||||
Pagination: nil,
|
||||
Users: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetDatabaseRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) (*sqlserverflex.GetDatabaseResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetUserRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
_ int64,
|
||||
) (*sqlserverflex.GetUserResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetInstanceRequestExecute(
|
||||
_ context.Context,
|
||||
_, _, _ string,
|
||||
) (*sqlserverflex.GetInstanceResponse, error) {
|
||||
if a.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
if a.instanceIsDeleted {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 404,
|
||||
}
|
||||
}
|
||||
|
||||
return &sqlserverflex.GetInstanceResponse{
|
||||
Id: &a.instanceId,
|
||||
Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(&a.instanceState),
|
||||
Network: &a.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
func TestCreateInstanceWaitHandler(t *testing.T) {
|
||||
instanceId := utils.Ptr("foo")
|
||||
instanceID := utils.Ptr("foo")
|
||||
tests := []struct {
|
||||
desc string
|
||||
instanceId string
|
||||
instanceID string
|
||||
instanceGetFails bool
|
||||
instanceState string
|
||||
instanceNetwork sqlserverflex.InstanceNetwork
|
||||
instanceNetwork v3alpha1api.InstanceNetwork
|
||||
usersGetErrorStatus int
|
||||
wantErr bool
|
||||
wantRes *sqlserverflex.GetInstanceResponse
|
||||
wantRes *v3alpha1api.GetInstanceResponse
|
||||
}{
|
||||
//{
|
||||
// desc: "create_succeeded",
|
||||
// instanceId: *instanceId,
|
||||
// instanceGetFails: false,
|
||||
// instanceState: *stateSuccess,
|
||||
// instanceNetwork: sqlserverflex.InstanceNetwork{
|
||||
// instanceNetwork: v3alpha1api.InstanceNetwork{
|
||||
// AccessScope: nil,
|
||||
// Acl: nil,
|
||||
// InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
// RouterAddress: utils.Ptr("10.0.0.2"),
|
||||
// },
|
||||
// wantErr: false,
|
||||
// wantRes: &sqlserverflex.GetInstanceResponse{
|
||||
// wantRes: &v3alpha1api.GetInstanceResponse{
|
||||
// BackupSchedule: nil,
|
||||
// Edition: nil,
|
||||
// Encryption: nil,
|
||||
|
|
@ -147,7 +45,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
// Id: instanceId,
|
||||
// IsDeletable: nil,
|
||||
// Name: nil,
|
||||
// Network: &sqlserverflex.InstanceNetwork{
|
||||
// Network: &v3alpha1api.InstanceNetwork{
|
||||
// AccessScope: nil,
|
||||
// Acl: nil,
|
||||
// InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
|
|
@ -155,14 +53,14 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
// },
|
||||
// Replicas: nil,
|
||||
// RetentionDays: nil,
|
||||
// Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(stateSuccess),
|
||||
// Status: v3alpha1api.GetInstanceResponseGetStatusAttributeType(stateSuccess),
|
||||
// Storage: nil,
|
||||
// Version: nil,
|
||||
// },
|
||||
// },
|
||||
{
|
||||
desc: "create_failed",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateFailed,
|
||||
wantErr: true,
|
||||
|
|
@ -170,7 +68,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
},
|
||||
{
|
||||
desc: "create_failed_2",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateEmpty,
|
||||
wantErr: true,
|
||||
|
|
@ -178,14 +76,14 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
},
|
||||
{
|
||||
desc: "instance_get_fails",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: true,
|
||||
wantErr: true,
|
||||
wantRes: nil,
|
||||
},
|
||||
{
|
||||
desc: "timeout",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateProcessing,
|
||||
wantErr: true,
|
||||
|
|
@ -195,13 +93,25 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceId: tt.instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
mockCall := func(_ v3alpha1api.ApiGetInstanceRequestRequest) (*v3alpha1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3alpha1api.GetInstanceResponse{
|
||||
Id: tt.instanceID,
|
||||
Status: v3alpha1api.Status(tt.instanceState),
|
||||
Network: tt.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
|
||||
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", tt.instanceId, "")
|
||||
apiClient := v3alpha1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &mockCall,
|
||||
}
|
||||
|
||||
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", tt.instanceID, "")
|
||||
|
||||
gotRes, err := handler.SetTimeout(10 * time.Millisecond).SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
|
||||
if (err != nil) != tt.wantErr {
|
||||
|
|
@ -217,7 +127,6 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUpdateInstanceWaitHandler(t *testing.T) {
|
||||
t.Skip("skipping - needs refactoring")
|
||||
tests := []struct {
|
||||
desc string
|
||||
instanceGetFails bool
|
||||
|
|
@ -263,23 +172,35 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
instanceId := "foo-bar"
|
||||
instanceID := "foo-bar"
|
||||
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceId: instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
mockCall := func(_ v3alpha1api.ApiGetInstanceRequestRequest) (*v3alpha1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3alpha1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3alpha1api.Status(tt.instanceState),
|
||||
//Network: tt.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var wantRes *sqlserverflex.GetInstanceResponse
|
||||
apiClient := v3alpha1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &mockCall,
|
||||
}
|
||||
|
||||
var wantRes *v3alpha1api.GetInstanceResponse
|
||||
if tt.wantResp {
|
||||
wantRes = &sqlserverflex.GetInstanceResponse{
|
||||
Id: &instanceId,
|
||||
Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(tt.instanceState)),
|
||||
wantRes = &v3alpha1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3alpha1api.Status(tt.instanceState),
|
||||
}
|
||||
}
|
||||
|
||||
handler := UpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceId, "")
|
||||
handler := UpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceID, "")
|
||||
|
||||
gotRes, err := handler.SetTimeout(10 * time.Millisecond).SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
|
||||
|
||||
|
|
@ -322,16 +243,33 @@ func TestDeleteInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
instanceId := "foo-bar"
|
||||
instanceID := "foo-bar"
|
||||
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
instanceIsDeleted: tt.instanceState == InstanceStateSuccess,
|
||||
instanceId: instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
mockCall := func(_ v3alpha1api.ApiGetInstanceRequestRequest) (*v3alpha1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
if tt.instanceState == InstanceStateSuccess {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 404,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3alpha1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3alpha1api.Status(tt.instanceState),
|
||||
//Network: tt.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
|
||||
handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceId, "")
|
||||
apiClient := v3alpha1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &mockCall,
|
||||
}
|
||||
|
||||
handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceID, "")
|
||||
|
||||
_, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import (
|
|||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/wait"
|
||||
|
||||
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
sqlserverflex "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3beta1api"
|
||||
)
|
||||
|
||||
// READY, PENDING, PROGRESSING, FAILURE, UNKNOWN,
|
||||
|
|
@ -28,31 +28,31 @@ const (
|
|||
|
||||
// APIClientInterface Interface needed for tests
|
||||
type APIClientInterface interface {
|
||||
GetInstanceRequestExecute(
|
||||
GetInstanceRequest(
|
||||
ctx context.Context,
|
||||
projectId, region, instanceId string,
|
||||
) (*sqlserverflex.GetInstanceResponse, error)
|
||||
GetDatabaseRequestExecute(
|
||||
) sqlserverflex.ApiGetInstanceRequestRequest
|
||||
GetDatabaseRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
databaseName string,
|
||||
) (*sqlserverflex.GetDatabaseResponse, error)
|
||||
GetUserRequestExecute(
|
||||
) sqlserverflex.ApiGetDatabaseRequestRequest
|
||||
GetUserRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
userId int64,
|
||||
) (*sqlserverflex.GetUserResponse, error)
|
||||
) sqlserverflex.ApiGetUserRequestRequest
|
||||
|
||||
ListRolesRequestExecute(
|
||||
ListRolesRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) (*sqlserverflex.ListRolesResponse, error)
|
||||
) sqlserverflex.ApiListRolesRequestRequest
|
||||
|
||||
ListUsersRequest(
|
||||
ctx context.Context,
|
||||
|
|
@ -60,13 +60,6 @@ type APIClientInterface interface {
|
|||
region string,
|
||||
instanceId string,
|
||||
) sqlserverflex.ApiListUsersRequestRequest
|
||||
|
||||
ListUsersRequestExecute(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) (*sqlserverflex.ListUserResponse, error)
|
||||
}
|
||||
|
||||
// APIClientUserInterface Interface needed for tests
|
||||
|
|
@ -88,7 +81,7 @@ func CreateInstanceWaitHandler(
|
|||
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
|
|
@ -102,12 +95,12 @@ func CreateInstanceWaitHandler(
|
|||
return false, nil, fmt.Errorf("api error: %w", err)
|
||||
}
|
||||
}
|
||||
if s == nil || s.Id == nil || *s.Id != instanceId || s.Status == nil {
|
||||
if s == nil || s.Id != instanceId {
|
||||
return false, nil, nil
|
||||
}
|
||||
switch strings.ToLower(string(*s.Status)) {
|
||||
switch strings.ToLower(string(s.Status)) {
|
||||
case strings.ToLower(InstanceStateSuccess):
|
||||
if s.Network != nil && s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
|
||||
if s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
|
||||
if s.Network.InstanceAddress == nil {
|
||||
tflog.Info(ctx, "Waiting for instance_address")
|
||||
return false, nil, nil
|
||||
|
|
@ -120,7 +113,7 @@ func CreateInstanceWaitHandler(
|
|||
|
||||
tflog.Info(ctx, "trying to get roles")
|
||||
time.Sleep(10 * time.Second)
|
||||
_, rolesErr := a.ListRolesRequestExecute(ctx, projectId, region, instanceId)
|
||||
_, rolesErr := a.ListRolesRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if rolesErr != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(rolesErr, &oapiErr)
|
||||
|
|
@ -144,7 +137,7 @@ func CreateInstanceWaitHandler(
|
|||
|
||||
tflog.Info(ctx, "trying to get users")
|
||||
time.Sleep(10 * time.Second)
|
||||
_, usersErr := a.ListUsersRequestExecute(ctx, projectId, region, instanceId)
|
||||
_, usersErr := a.ListUsersRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if usersErr != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(usersErr, &oapiErr)
|
||||
|
|
@ -181,7 +174,7 @@ func CreateInstanceWaitHandler(
|
|||
case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing):
|
||||
tflog.Info(
|
||||
ctx, "request is being handled", map[string]interface{}{
|
||||
"status": *s.Status,
|
||||
"status": s.Status,
|
||||
},
|
||||
)
|
||||
time.Sleep(10 * time.Second)
|
||||
|
|
@ -208,14 +201,14 @@ func UpdateInstanceWaitHandler(
|
|||
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
if s == nil || s.Id == nil || *s.Id != instanceId || s.Status == nil {
|
||||
if s == nil || s.Id != instanceId {
|
||||
return false, nil, nil
|
||||
}
|
||||
switch strings.ToLower(string(*s.Status)) {
|
||||
switch strings.ToLower(string(s.Status)) {
|
||||
case strings.ToLower(InstanceStateSuccess):
|
||||
return true, s, nil
|
||||
case strings.ToLower(InstanceStateUnknown), strings.ToLower(InstanceStateFailed):
|
||||
|
|
@ -223,7 +216,7 @@ func UpdateInstanceWaitHandler(
|
|||
case strings.ToLower(InstanceStatePending), strings.ToLower(InstanceStateProcessing):
|
||||
tflog.Info(
|
||||
ctx, "request is being handled", map[string]interface{}{
|
||||
"status": *s.Status,
|
||||
"status": s.Status,
|
||||
},
|
||||
)
|
||||
return false, s, nil
|
||||
|
|
@ -249,7 +242,7 @@ func DeleteInstanceWaitHandler(
|
|||
) *wait.AsyncActionHandler[sqlserverflex.GetInstanceResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetInstanceResponse, err error) {
|
||||
s, err := a.GetInstanceRequestExecute(ctx, projectId, region, instanceId)
|
||||
s, err := a.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err == nil {
|
||||
return false, s, nil
|
||||
}
|
||||
|
|
@ -276,7 +269,7 @@ func CreateDatabaseWaitHandler(
|
|||
) *wait.AsyncActionHandler[sqlserverflex.GetDatabaseResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetDatabaseResponse, err error) {
|
||||
s, err := a.GetDatabaseRequestExecute(ctx, projectId, region, instanceId, databaseName)
|
||||
s, err := a.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
|
|
@ -291,7 +284,7 @@ func CreateDatabaseWaitHandler(
|
|||
}
|
||||
return false, nil, nil
|
||||
}
|
||||
if s == nil || s.Name == nil || *s.Name != databaseName {
|
||||
if s == nil || s.Name != databaseName {
|
||||
return false, nil, errors.New("response did return different result")
|
||||
}
|
||||
return true, s, nil
|
||||
|
|
@ -309,7 +302,7 @@ func CreateUserWaitHandler(
|
|||
) *wait.AsyncActionHandler[sqlserverflex.GetUserResponse] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *sqlserverflex.GetUserResponse, err error) {
|
||||
s, err := a.GetUserRequestExecute(ctx, projectId, region, instanceId, userId)
|
||||
s, err := a.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
|
|
@ -388,7 +381,7 @@ func DeleteUserWaitHandler(
|
|||
) *wait.AsyncActionHandler[struct{}] {
|
||||
handler := wait.New(
|
||||
func() (waitFinished bool, response *struct{}, err error) {
|
||||
_, err = a.GetUserRequestExecute(ctx, projectId, region, instanceId, userId)
|
||||
_, err = a.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err == nil {
|
||||
return false, nil, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,160 +9,57 @@ import (
|
|||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3beta1api"
|
||||
)
|
||||
|
||||
// Used for testing instance operations
|
||||
type apiClientInstanceMocked struct {
|
||||
instanceId string
|
||||
instanceState string
|
||||
instanceNetwork sqlserverflex.InstanceNetwork
|
||||
instanceIsDeleted bool
|
||||
instanceGetFails bool
|
||||
}
|
||||
|
||||
type ListUsersRequestRequest struct{}
|
||||
|
||||
func (l ListUsersRequestRequest) Page(_ int64) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Size(_ int64) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Sort(_ sqlserverflex.UserSort) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Execute() (*sqlserverflex.ListUserResponse, error) {
|
||||
// TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListUsersRequest(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return ListUsersRequestRequest{}
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListRolesRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) (*sqlserverflex.ListRolesResponse, error) {
|
||||
return &sqlserverflex.ListRolesResponse{
|
||||
Roles: &[]string{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListUsersRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) (*sqlserverflex.ListUserResponse, error) {
|
||||
return &sqlserverflex.ListUserResponse{
|
||||
Pagination: nil,
|
||||
Users: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetDatabaseRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
) (*sqlserverflex.GetDatabaseResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetUserRequestExecute(
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ string,
|
||||
_ string,
|
||||
_ int64,
|
||||
) (*sqlserverflex.GetUserResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetInstanceRequestExecute(
|
||||
_ context.Context,
|
||||
_, _, _ string,
|
||||
) (*sqlserverflex.GetInstanceResponse, error) {
|
||||
if a.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
if a.instanceIsDeleted {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 404,
|
||||
}
|
||||
}
|
||||
|
||||
return &sqlserverflex.GetInstanceResponse{
|
||||
Id: &a.instanceId,
|
||||
Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(&a.instanceState),
|
||||
Network: &a.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
func TestCreateInstanceWaitHandler(t *testing.T) {
|
||||
instanceId := utils.Ptr("foo")
|
||||
instanceID := utils.Ptr("foo")
|
||||
tests := []struct {
|
||||
desc string
|
||||
instanceId string
|
||||
instanceID string
|
||||
instanceGetFails bool
|
||||
instanceState string
|
||||
instanceNetwork sqlserverflex.InstanceNetwork
|
||||
instanceNetwork v3beta1api.InstanceNetwork
|
||||
usersGetErrorStatus int
|
||||
wantErr bool
|
||||
wantRes *sqlserverflex.GetInstanceResponse
|
||||
wantRes *v3beta1api.GetInstanceResponse
|
||||
}{
|
||||
//{
|
||||
// desc: "create_succeeded",
|
||||
// instanceId: *instanceId,
|
||||
// instanceGetFails: false,
|
||||
// instanceState: *stateSuccess,
|
||||
// instanceNetwork: sqlserverflex.InstanceNetwork{
|
||||
// AccessScope: nil,
|
||||
// Acl: nil,
|
||||
// InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
// RouterAddress: utils.Ptr("10.0.0.2"),
|
||||
// },
|
||||
// wantErr: false,
|
||||
// wantRes: &sqlserverflex.GetInstanceResponse{
|
||||
// BackupSchedule: nil,
|
||||
// Edition: nil,
|
||||
// Encryption: nil,
|
||||
// FlavorId: nil,
|
||||
// Id: instanceId,
|
||||
// IsDeletable: nil,
|
||||
// Name: nil,
|
||||
// Network: &sqlserverflex.InstanceNetwork{
|
||||
// AccessScope: nil,
|
||||
// Acl: nil,
|
||||
// InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
// RouterAddress: utils.Ptr("10.0.0.2"),
|
||||
// },
|
||||
// Replicas: nil,
|
||||
// RetentionDays: nil,
|
||||
// Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(stateSuccess),
|
||||
// Storage: nil,
|
||||
// Version: nil,
|
||||
// },
|
||||
// },
|
||||
{
|
||||
desc: "create_succeeded_default_values",
|
||||
instanceID: "instance1",
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateSuccess,
|
||||
instanceNetwork: v3beta1api.InstanceNetwork{
|
||||
AccessScope: (*v3beta1api.InstanceNetworkAccessScope)(utils.Ptr("PUBLIC")),
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
RouterAddress: utils.Ptr("10.0.0.2"),
|
||||
},
|
||||
wantErr: false,
|
||||
wantRes: &v3beta1api.GetInstanceResponse{
|
||||
BackupSchedule: "",
|
||||
Edition: "",
|
||||
Encryption: nil,
|
||||
FlavorId: "",
|
||||
Id: "instance1",
|
||||
IsDeletable: false,
|
||||
Name: "",
|
||||
Network: v3beta1api.InstanceNetwork{
|
||||
AccessScope: (*v3beta1api.InstanceNetworkAccessScope)(utils.Ptr("PUBLIC")),
|
||||
Acl: nil,
|
||||
InstanceAddress: utils.Ptr("10.0.0.1"),
|
||||
RouterAddress: utils.Ptr("10.0.0.2"),
|
||||
},
|
||||
Replicas: 0,
|
||||
RetentionDays: 0,
|
||||
Status: v3beta1api.Status(InstanceStateSuccess),
|
||||
Storage: v3beta1api.Storage{},
|
||||
Version: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "create_failed",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateFailed,
|
||||
wantErr: true,
|
||||
|
|
@ -170,7 +67,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
},
|
||||
{
|
||||
desc: "create_failed_2",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateEmpty,
|
||||
wantErr: true,
|
||||
|
|
@ -178,14 +75,14 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
},
|
||||
{
|
||||
desc: "instance_get_fails",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: true,
|
||||
wantErr: true,
|
||||
wantRes: nil,
|
||||
},
|
||||
{
|
||||
desc: "timeout",
|
||||
instanceId: *instanceId,
|
||||
instanceID: *instanceID,
|
||||
instanceGetFails: false,
|
||||
instanceState: InstanceStateProcessing,
|
||||
wantErr: true,
|
||||
|
|
@ -195,19 +92,55 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceId: tt.instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
listRolesMock := func(_ v3beta1api.ApiListRolesRequestRequest) (*v3beta1api.ListRolesResponse, error) {
|
||||
return &v3beta1api.ListRolesResponse{
|
||||
Roles: []string{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", tt.instanceId, "")
|
||||
listUsersMock := func(_ v3beta1api.ApiListUsersRequestRequest) (*v3beta1api.ListUserResponse, error) {
|
||||
aux := int64(0)
|
||||
return &v3beta1api.ListUserResponse{
|
||||
Pagination: v3beta1api.Pagination{
|
||||
TotalRows: aux,
|
||||
},
|
||||
Users: []v3beta1api.ListUser{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
mockCall := func(_ v3beta1api.ApiGetInstanceRequestRequest) (*v3beta1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3beta1api.GetInstanceResponse{
|
||||
Id: tt.instanceID,
|
||||
Status: v3beta1api.Status(tt.instanceState),
|
||||
Network: tt.instanceNetwork,
|
||||
Storage: v3beta1api.Storage{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
apiClient := v3beta1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &mockCall,
|
||||
ListUsersRequestExecuteMock: &listUsersMock,
|
||||
ListRolesRequestExecuteMock: &listRolesMock,
|
||||
}
|
||||
|
||||
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", tt.instanceID, "")
|
||||
|
||||
gotRes, err := handler.SetTimeout(10 * time.Millisecond).SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
if diff := cmp.Diff(tt.wantRes, gotRes); diff != "" {
|
||||
t.Errorf("model mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(gotRes, tt.wantRes) {
|
||||
t.Fatalf("handler gotRes = %v, want %v", gotRes, tt.wantRes)
|
||||
}
|
||||
|
|
@ -217,7 +150,6 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUpdateInstanceWaitHandler(t *testing.T) {
|
||||
t.Skip("skipping - needs refactoring")
|
||||
tests := []struct {
|
||||
desc string
|
||||
instanceGetFails bool
|
||||
|
|
@ -263,23 +195,35 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
instanceId := "foo-bar"
|
||||
instanceID := "foo-bar"
|
||||
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceId: instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
mockCall := func(_ v3beta1api.ApiGetInstanceRequestRequest) (*v3beta1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3beta1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3beta1api.Status(tt.instanceState),
|
||||
//Network: tt.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var wantRes *sqlserverflex.GetInstanceResponse
|
||||
apiClient := v3beta1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &mockCall,
|
||||
}
|
||||
|
||||
var wantRes *v3beta1api.GetInstanceResponse
|
||||
if tt.wantResp {
|
||||
wantRes = &sqlserverflex.GetInstanceResponse{
|
||||
Id: &instanceId,
|
||||
Status: sqlserverflex.GetInstanceResponseGetStatusAttributeType(utils.Ptr(tt.instanceState)),
|
||||
wantRes = &v3beta1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3beta1api.Status(tt.instanceState),
|
||||
}
|
||||
}
|
||||
|
||||
handler := UpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceId, "")
|
||||
handler := UpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceID, "")
|
||||
|
||||
gotRes, err := handler.SetTimeout(10 * time.Millisecond).SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
|
||||
|
||||
|
|
@ -322,16 +266,33 @@ func TestDeleteInstanceWaitHandler(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.desc, func(t *testing.T) {
|
||||
instanceId := "foo-bar"
|
||||
instanceID := "foo-bar"
|
||||
|
||||
apiClient := &apiClientInstanceMocked{
|
||||
instanceGetFails: tt.instanceGetFails,
|
||||
instanceIsDeleted: tt.instanceState == InstanceStateSuccess,
|
||||
instanceId: instanceId,
|
||||
instanceState: tt.instanceState,
|
||||
mockCall := func(_ v3beta1api.ApiGetInstanceRequestRequest) (*v3beta1api.GetInstanceResponse, error) {
|
||||
if tt.instanceGetFails {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 500,
|
||||
}
|
||||
}
|
||||
|
||||
if tt.instanceState == InstanceStateSuccess {
|
||||
return nil, &oapierror.GenericOpenAPIError{
|
||||
StatusCode: 404,
|
||||
}
|
||||
}
|
||||
|
||||
return &v3beta1api.GetInstanceResponse{
|
||||
Id: instanceID,
|
||||
Status: v3beta1api.Status(tt.instanceState),
|
||||
//Network: tt.instanceNetwork,
|
||||
}, nil
|
||||
}
|
||||
|
||||
handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceId, "")
|
||||
apiClient := v3beta1api.DefaultAPIServiceMock{
|
||||
GetInstanceRequestExecuteMock: &mockCall,
|
||||
}
|
||||
|
||||
handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceID, "")
|
||||
|
||||
_, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue