@@ -7,7 +7,6 @@ package api
77import (
88 "errors"
99 "fmt"
10- "math"
1110 "os"
1211 "sort"
1312 "strings"
@@ -95,34 +94,29 @@ func CreateVolumeFromHumanSize(api *ScalewayAPI, size string) (*string, error) {
9594 return & volumeID , nil
9695}
9796
98- // VolumesFromSize returns a string of standard sized volumes from a given size
99- func VolumesFromSize (size uint64 ) string {
100- const DefaultVolumeSize float64 = 50000000000
101- StdVolumeSizes := []struct {
102- kind string
103- capacity float64
104- }{
105- {"150G" , 150000000000 },
106- {"100G" , 100000000000 },
107- {"50G" , 50000000000 },
108- }
109-
110- RequiredSize := float64 (size ) - DefaultVolumeSize
111- Volumes := ""
112- for _ , v := range StdVolumeSizes {
113- q := RequiredSize / v .capacity
114- r := math .Mod (RequiredSize , v .capacity )
115- RequiredSize = r
116-
117- if q > 0 {
118- Volumes += strings .Repeat (v .kind + " " , int (q ))
119- }
120- if r == 0 {
121- break
122- }
97+ func min (a , b uint64 ) uint64 {
98+ if a > b {
99+ return b
123100 }
101+ return a
102+ }
103+
104+ const Giga = 1000000000
124105
125- return strings .TrimSpace (Volumes )
106+ // VolumesFromSize returns a string of standard sized volumes from a given size
107+ func VolumesFromSize (rootVolumeSize , targetSize , perVolumeMaxSize uint64 ) string {
108+ if targetSize <= rootVolumeSize {
109+ return ""
110+ }
111+ targetSize -= rootVolumeSize
112+ q := targetSize / perVolumeMaxSize
113+ r := targetSize % perVolumeMaxSize
114+ humanSize := fmt .Sprintf ("%dG" , perVolumeMaxSize / Giga )
115+ volumes := strings .Repeat (humanSize + " " , int (q ))
116+ if r != 0 {
117+ volumes += fmt .Sprintf ("%dG" , r / Giga )
118+ }
119+ return strings .TrimSpace (volumes )
126120}
127121
128122// fillIdentifierCache fills the cache by fetching from the API
@@ -396,7 +390,7 @@ func CreateServer(api *ScalewayAPI, c *ConfigCreateServer) (string, error) {
396390 var server ScalewayServerDefinition
397391
398392 server .CommercialType = commercialType
399- server .Volumes = make (map [string ]string )
393+ server .Volumes = make (map [string ]ScalewayServerVolumeDefinition )
400394 server .DynamicIPRequired = & c .DynamicIPRequired
401395 server .EnableIPV6 = c .EnableIPV6
402396 server .BootType = c .BootType
@@ -435,21 +429,59 @@ func CreateServer(api *ScalewayAPI, c *ConfigCreateServer) (string, error) {
435429 if err != nil {
436430 return "" , fmt .Errorf ("Unknow commercial type %v: %v" , server .CommercialType , err )
437431 }
432+ //
433+ // Find the correct root size
434+ //
435+ // 1- the user define a custom root size
436+ // 2- (default) use the largest possible size ==> min(categoryMaxSize,volumeMaxSize)
437+ // 3- the user specify additional volumes ==> min(50G,volumeMaxSize)
438+ //
439+ isUserDefinedRootSize := true
440+ rootVolumeSize , err := humanize .ParseBytes (c .ImageName )
441+ if err != nil {
442+ isUserDefinedRootSize = false
443+ rootVolumeSize = min (offer .PerVolumesConstraint .LSsdConstraint .MaxSize , offer .VolumesConstraint .MaxSize )
444+ if c .AdditionalVolumes != "" {
445+ rootVolumeSize = min (50 * Giga , offer .VolumesConstraint .MaxSize ) // create a volume up to 50GB
446+ }
447+ }
448+
449+ if isUserDefinedRootSize {
450+ // create a new volume from scratch
451+ server .Volumes ["0" ] = & ScalewayServerVolumeDefinitionNew {
452+ OrganizationId : api .Organization ,
453+ VolumeType : "l_ssd" ,
454+ Name : "Volume-0" ,
455+ Size : rootVolumeSize ,
456+ }
457+ } else {
458+ // leverage compute image resizing
459+ server .Volumes ["0" ] = & ScalewayServerVolumeDefinitionResize {
460+ Size : rootVolumeSize ,
461+ }
462+ }
463+
438464 if offer .VolumesConstraint .MinSize > 0 && c .AdditionalVolumes == "" {
439- c .AdditionalVolumes = VolumesFromSize (offer .VolumesConstraint .MinSize )
440- log .Debugf ("%s needs at least %s. Automatically creates the following volumes: %s" ,
441- server .CommercialType , humanize .Bytes (offer .VolumesConstraint .MinSize ), c .AdditionalVolumes )
465+ c .AdditionalVolumes = VolumesFromSize (rootVolumeSize , offer .VolumesConstraint .MinSize , offer . PerVolumesConstraint . LSsdConstraint . MaxSize )
466+ log .Debugf ("%s needs at least %s. Automatically creates the following volumes: %dG % s" ,
467+ server .CommercialType , humanize .Bytes (offer .VolumesConstraint .MinSize ), rootVolumeSize / Giga , c .AdditionalVolumes )
442468 }
469+
443470 if c .AdditionalVolumes != "" {
444471 volumes := strings .Split (c .AdditionalVolumes , " " )
445472 for i := range volumes {
446- volumeID , err := CreateVolumeFromHumanSize ( api , volumes [i ])
473+ rootSize , err := humanize . ParseBytes ( volumes [i ])
447474 if err != nil {
448475 return "" , err
449476 }
450477
451478 volumeIDx := fmt .Sprintf ("%d" , i + 1 )
452- server .Volumes [volumeIDx ] = * volumeID
479+ server .Volumes [volumeIDx ] = & ScalewayServerVolumeDefinitionNew {
480+ OrganizationId : api .Organization ,
481+ VolumeType : "l_ssd" ,
482+ Name : "Volume-" + volumeIDx ,
483+ Size : rootSize ,
484+ }
453485 }
454486 }
455487
@@ -462,15 +494,8 @@ func CreateServer(api *ScalewayAPI, c *ConfigCreateServer) (string, error) {
462494 }
463495 server .Name = c .Name
464496 inheritingVolume := false
465- _ , err = humanize .ParseBytes (c .ImageName )
466- if err == nil {
467- // Create a new root volume
468- volumeID , errCreateVol := CreateVolumeFromHumanSize (api , c .ImageName )
469- if errCreateVol != nil {
470- return "" , errCreateVol
471- }
472- server .Volumes ["0" ] = * volumeID
473- } else {
497+
498+ if ! isUserDefinedRootSize {
474499 // Use an existing image
475500 inheritingVolume = true
476501 if anonuuid .IsUUID (c .ImageName ) == nil {
@@ -494,7 +519,7 @@ func CreateServer(api *ScalewayAPI, c *ConfigCreateServer) (string, error) {
494519 if snapshot .BaseVolume .Identifier == "" {
495520 return "" , fmt .Errorf ("snapshot %v does not have base volume" , snapshot .Name )
496521 }
497- server .Volumes ["0" ] = snapshot .BaseVolume .Identifier
522+ server .Volumes ["0" ] = ScalewayServerVolumeDefinitionFromId ( snapshot .BaseVolume .Identifier )
498523 }
499524 }
500525 }
0 commit comments