Skip to content

Commit

Permalink
fix(instance): list server-type all pages (#2996)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax committed Apr 4, 2023
1 parent 304a84c commit ea01065
Show file tree
Hide file tree
Showing 17 changed files with 8,523 additions and 5,328 deletions.
16 changes: 8 additions & 8 deletions internal/namespaces/instance/v1/custom_server_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
logger.Warningf("cannot get image %s: %s", serverReq.Image, err)
}

serverType := getServeType(apiInstance, serverReq.Zone, serverReq.CommercialType)
serverType := getServerType(apiInstance, serverReq.Zone, serverReq.CommercialType)

if serverType != nil && getImageResponse != nil {
if err := validateImageServerTypeCompatibility(getImageResponse.Image, serverType, serverReq.CommercialType); err != nil {
Expand Down Expand Up @@ -316,8 +316,8 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
}

// Validate total local volume sizes.
if serverType != nil {
if err := validateLocalVolumeSizes(volumes, serverType, serverReq.CommercialType); err != nil {
if serverType != nil && getImageResponse != nil {
if err := validateLocalVolumeSizes(volumes, serverType, serverReq.CommercialType, getImageResponse.Image.RootVolume.Size); err != nil {
return nil, err
}
} else {
Expand Down Expand Up @@ -575,7 +575,7 @@ func validateImageServerTypeCompatibility(image *instance.Image, serverType *ins
}

// validateLocalVolumeSizes validates the total size of local volumes.
func validateLocalVolumeSizes(volumes map[string]*instance.VolumeServerTemplate, serverType *instance.ServerType, commercialType string) error {
func validateLocalVolumeSizes(volumes map[string]*instance.VolumeServerTemplate, serverType *instance.ServerType, commercialType string, defaultRootVolumeSize scw.Size) error {
// Calculate local volume total size.
var localVolumeTotalSize scw.Size
for _, volume := range volumes {
Expand All @@ -588,7 +588,7 @@ func validateLocalVolumeSizes(volumes map[string]*instance.VolumeServerTemplate,

// If no root volume provided, count the default root volume size added by the API.
if rootVolume := volumes["0"]; rootVolume == nil {
localVolumeTotalSize += volumeConstraint.MinSize
localVolumeTotalSize += defaultRootVolumeSize
}

if localVolumeTotalSize < volumeConstraint.MinSize || localVolumeTotalSize > volumeConstraint.MaxSize {
Expand Down Expand Up @@ -677,13 +677,13 @@ func instanceServerCreateImageAutoCompleteFunc(ctx context.Context, prefix strin
return suggestions
}

// getServeType is a util to get a instance.ServerType by its commercialType
func getServeType(apiInstance *instance.API, zone scw.Zone, commercialType string) *instance.ServerType {
// getServerType is a util to get a instance.ServerType by its commercialType
func getServerType(apiInstance *instance.API, zone scw.Zone, commercialType string) *instance.ServerType {
serverType := (*instance.ServerType)(nil)

serverTypesRes, err := apiInstance.ListServersTypes(&instance.ListServersTypesRequest{
Zone: zone,
})
}, scw.WithAllPages())
if err != nil {
logger.Warningf("cannot get server types: %s", err)
} else {
Expand Down
11 changes: 6 additions & 5 deletions internal/namespaces/instance/v1/custom_server_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func Test_CreateServerErrors(t *testing.T) {
////
t.Run("Error: invalid total local volumes size: too low 1", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw instance server create image=ubuntu_bionic root-volume=l:10GB",
Cmd: "scw instance server create image=ubuntu_bionic root-volume=l:5GB",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
Expand All @@ -280,7 +280,7 @@ func Test_CreateServerErrors(t *testing.T) {

t.Run("Error: invalid total local volumes size: too low 2", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw instance server create image=ubuntu_bionic root-volume=l:10GB additional-volumes.0=block:10GB",
Cmd: "scw instance server create image=ubuntu_bionic root-volume=l:5GB additional-volumes.0=block:10GB",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
Expand All @@ -289,8 +289,9 @@ func Test_CreateServerErrors(t *testing.T) {
}))

t.Run("Error: invalid total local volumes size: too low 3", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw instance server create image=ubuntu_bionic root-volume=block:20GB",
Commands: GetCommands(),
BeforeFunc: createVolume("Volume", 5, instance.VolumeVolumeTypeLSSD),
Cmd: "scw instance server create image=ubuntu_bionic root-volume={{ .Volume.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
Expand All @@ -310,7 +311,7 @@ func Test_CreateServerErrors(t *testing.T) {

t.Run("Error: invalid total local volumes size: too high 2", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw instance server create image=ubuntu_bionic additional-volumes.0=local:10GB",
Cmd: "scw instance server create image=ubuntu_bionic additional-volumes.0=local:20GB",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
Expand Down
2 changes: 1 addition & 1 deletion internal/namespaces/instance/v1/custom_server_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func serverTypeListBuilder(c *core.Command) *core.Command {

// Get server types.
request := argsI.(*instance.ListServersTypesRequest)
listServersTypesResponse, err := api.ListServersTypes(request)
listServersTypesResponse, err := api.ListServersTypes(request, scw.WithAllPages())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ea01065

Please sign in to comment.