Skip to content

Commit ea01065

Browse files
authored
fix(instance): list server-type all pages (#2996)
1 parent 304a84c commit ea01065

17 files changed

+8523
-5328
lines changed

internal/namespaces/instance/v1/custom_server_create.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
250250
logger.Warningf("cannot get image %s: %s", serverReq.Image, err)
251251
}
252252

253-
serverType := getServeType(apiInstance, serverReq.Zone, serverReq.CommercialType)
253+
serverType := getServerType(apiInstance, serverReq.Zone, serverReq.CommercialType)
254254

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

318318
// Validate total local volume sizes.
319-
if serverType != nil {
320-
if err := validateLocalVolumeSizes(volumes, serverType, serverReq.CommercialType); err != nil {
319+
if serverType != nil && getImageResponse != nil {
320+
if err := validateLocalVolumeSizes(volumes, serverType, serverReq.CommercialType, getImageResponse.Image.RootVolume.Size); err != nil {
321321
return nil, err
322322
}
323323
} else {
@@ -575,7 +575,7 @@ func validateImageServerTypeCompatibility(image *instance.Image, serverType *ins
575575
}
576576

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

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

594594
if localVolumeTotalSize < volumeConstraint.MinSize || localVolumeTotalSize > volumeConstraint.MaxSize {
@@ -677,13 +677,13 @@ func instanceServerCreateImageAutoCompleteFunc(ctx context.Context, prefix strin
677677
return suggestions
678678
}
679679

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

684684
serverTypesRes, err := apiInstance.ListServersTypes(&instance.ListServersTypesRequest{
685685
Zone: zone,
686-
})
686+
}, scw.WithAllPages())
687687
if err != nil {
688688
logger.Warningf("cannot get server types: %s", err)
689689
} else {

internal/namespaces/instance/v1/custom_server_create_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func Test_CreateServerErrors(t *testing.T) {
270270
////
271271
t.Run("Error: invalid total local volumes size: too low 1", core.Test(&core.TestConfig{
272272
Commands: GetCommands(),
273-
Cmd: "scw instance server create image=ubuntu_bionic root-volume=l:10GB",
273+
Cmd: "scw instance server create image=ubuntu_bionic root-volume=l:5GB",
274274
Check: core.TestCheckCombine(
275275
core.TestCheckGolden(),
276276
core.TestCheckExitCode(1),
@@ -280,7 +280,7 @@ func Test_CreateServerErrors(t *testing.T) {
280280

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

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

311312
t.Run("Error: invalid total local volumes size: too high 2", core.Test(&core.TestConfig{
312313
Commands: GetCommands(),
313-
Cmd: "scw instance server create image=ubuntu_bionic additional-volumes.0=local:10GB",
314+
Cmd: "scw instance server create image=ubuntu_bionic additional-volumes.0=local:20GB",
314315
Check: core.TestCheckCombine(
315316
core.TestCheckGolden(),
316317
core.TestCheckExitCode(1),

internal/namespaces/instance/v1/custom_server_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func serverTypeListBuilder(c *core.Command) *core.Command {
7474

7575
// Get server types.
7676
request := argsI.(*instance.ListServersTypesRequest)
77-
listServersTypesResponse, err := api.ListServersTypes(request)
77+
listServersTypesResponse, err := api.ListServersTypes(request, scw.WithAllPages())
7878
if err != nil {
7979
return nil, err
8080
}

0 commit comments

Comments
 (0)