Onboard MongoDB Flex instance (#86)

* Onboard instance resource

* Add options.type as required field

* Implement resource unit tests

* Implement data source

* Implement acc tests

* Adjust update acc test

* Fix typo

* Adjust update unit tests

* Adjustments after review

* Minor adjustment for uniformity

* Adjustments after review
This commit is contained in:
João Palet 2023-10-17 11:20:22 +02:00 committed by GitHub
parent 6372434e56
commit ade77eb544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 2245 additions and 106 deletions

View file

@ -1,6 +1,8 @@
package postgresflex
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
@ -10,6 +12,19 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
)
type postgresFlexClientMocked struct {
returnError bool
getFlavorsResp *postgresflex.FlavorsResponse
}
func (c *postgresFlexClientMocked) GetFlavorsExecute(_ context.Context, _ string) (*postgresflex.FlavorsResponse, error) {
if c.returnError {
return nil, fmt.Errorf("get flavors failed")
}
return c.getFlavorsResp, nil
}
func TestMapFields(t *testing.T) {
tests := []struct {
description string
@ -507,3 +522,156 @@ func TestToUpdatePayload(t *testing.T) {
})
}
}
func TestLoadFlavorId(t *testing.T) {
tests := []struct {
description string
inputFlavor *flavorModel
mockedResp *postgresflex.FlavorsResponse
expected *flavorModel
getFlavorsFails bool
isValid bool
}{
{
"ok_flavor",
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
&postgresflex.FlavorsResponse{
Flavors: &[]postgresflex.InstanceFlavor{
{
Id: utils.Ptr("fid-1"),
Cpu: utils.Ptr(int32(2)),
Description: utils.Ptr("description"),
Memory: utils.Ptr(int32(8)),
},
},
},
&flavorModel{
Id: types.StringValue("fid-1"),
Description: types.StringValue("description"),
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
false,
true,
},
{
"ok_flavor_2",
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
&postgresflex.FlavorsResponse{
Flavors: &[]postgresflex.InstanceFlavor{
{
Id: utils.Ptr("fid-1"),
Cpu: utils.Ptr(int32(2)),
Description: utils.Ptr("description"),
Memory: utils.Ptr(int32(8)),
},
{
Id: utils.Ptr("fid-2"),
Cpu: utils.Ptr(int32(1)),
Description: utils.Ptr("description"),
Memory: utils.Ptr(int32(4)),
},
},
},
&flavorModel{
Id: types.StringValue("fid-1"),
Description: types.StringValue("description"),
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
false,
true,
},
{
"no_matching_flavor",
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
&postgresflex.FlavorsResponse{
Flavors: &[]postgresflex.InstanceFlavor{
{
Id: utils.Ptr("fid-1"),
Cpu: utils.Ptr(int32(1)),
Description: utils.Ptr("description"),
Memory: utils.Ptr(int32(8)),
},
{
Id: utils.Ptr("fid-2"),
Cpu: utils.Ptr(int32(1)),
Description: utils.Ptr("description"),
Memory: utils.Ptr(int32(4)),
},
},
},
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
false,
false,
},
{
"nil_response",
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
&postgresflex.FlavorsResponse{},
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
false,
false,
},
{
"error_response",
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
&postgresflex.FlavorsResponse{},
&flavorModel{
CPU: types.Int64Value(2),
RAM: types.Int64Value(8),
},
true,
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
client := &postgresFlexClientMocked{
returnError: tt.getFlavorsFails,
getFlavorsResp: tt.mockedResp,
}
model := &Model{
ProjectId: types.StringValue("pid"),
}
flavorModel := &flavorModel{
CPU: tt.inputFlavor.CPU,
RAM: tt.inputFlavor.RAM,
}
err := loadFlavorId(context.Background(), client, model, flavorModel)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(flavorModel, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}