@@ -17,6 +17,7 @@ import (
1717 log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1818 "github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/namesgenerator"
1919 "github.com/scaleway/scaleway-cli/vendor/github.com/dustin/go-humanize"
20+ "github.com/scaleway/scaleway-cli/vendor/github.com/moul/anonuuid"
2021)
2122
2223// ScalewayResolvedIdentifier represents a list of matching identifier for a specifier pattern
@@ -283,23 +284,51 @@ func InspectIdentifiers(api *ScalewayAPI, ci chan ScalewayResolvedIdentifier, cj
283284 close (cj )
284285}
285286
287+ type ConfigCreateServer struct {
288+ ImageName string
289+ Name string
290+ Bootscript string
291+ Env string
292+ AdditionalVolumes string
293+ DynamicIpRequired bool
294+ IP string
295+ }
296+
286297// CreateServer creates a server using API based on typical server fields
287- func CreateServer (api * ScalewayAPI , imageName string , name string , bootscript string , env string , additionalVolumes string , dynamicIPRequired bool ) (string , error ) {
288- if name == "" {
289- name = strings .Replace (namesgenerator .GetRandomName (0 ), "_" , "-" , - 1 )
298+ func CreateServer (api * ScalewayAPI , c * ConfigCreateServer ) (string , error ) {
299+ if c . Name == "" {
300+ c . Name = strings .Replace (namesgenerator .GetRandomName (0 ), "_" , "-" , - 1 )
290301 }
291302
292303 var server ScalewayServerDefinition
293304 server .Volumes = make (map [string ]string )
294305
295- server .DynamicIPRequired = & dynamicIPRequired
296-
306+ server .DynamicIPRequired = & c .DynamicIpRequired
307+ if c .IP != "" {
308+ if anonuuid .IsUUID (c .IP ) == nil {
309+ server .PublicIP = c .IP
310+ } else {
311+ ips , err := api .GetIPS ()
312+ if err != nil {
313+ return "" , err
314+ }
315+ for _ , ip := range ips .IPS {
316+ if ip .Address == c .IP {
317+ server .PublicIP = ip .ID
318+ break
319+ }
320+ }
321+ if server .PublicIP == "" {
322+ return "" , fmt .Errorf ("IP address %v not found" , c .IP )
323+ }
324+ }
325+ }
297326 server .Tags = []string {}
298- if env != "" {
299- server .Tags = strings .Split (env , " " )
327+ if c . Env != "" {
328+ server .Tags = strings .Split (c . Env , " " )
300329 }
301- if additionalVolumes != "" {
302- volumes := strings .Split (additionalVolumes , " " )
330+ if c . AdditionalVolumes != "" {
331+ volumes := strings .Split (c . AdditionalVolumes , " " )
303332 for i := range volumes {
304333 volumeID , err := CreateVolumeFromHumanSize (api , volumes [i ])
305334 if err != nil {
@@ -310,17 +339,17 @@ func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript st
310339 server .Volumes [volumeIDx ] = * volumeID
311340 }
312341 }
313- server .Name = name
314- if bootscript != "" {
315- bootscript := api .GetBootscriptID (bootscript )
342+ server .Name = c . Name
343+ if c . Bootscript != "" {
344+ bootscript := api .GetBootscriptID (c . Bootscript )
316345 server .Bootscript = & bootscript
317346 }
318347
319348 inheritingVolume := false
320- _ , err := humanize .ParseBytes (imageName )
349+ _ , err := humanize .ParseBytes (c . ImageName )
321350 if err == nil {
322351 // Create a new root volume
323- volumeID , err := CreateVolumeFromHumanSize (api , imageName )
352+ volumeID , err := CreateVolumeFromHumanSize (api , c . ImageName )
324353 if err != nil {
325354 return "" , err
326355 }
@@ -329,11 +358,11 @@ func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript st
329358 // Use an existing image
330359 // FIXME: handle snapshots
331360 inheritingVolume = true
332- image := api .GetImageID (imageName , false )
361+ image := api .GetImageID (c . ImageName , false )
333362 if image != "" {
334363 server .Image = & image
335364 } else {
336- snapshotID := api .GetSnapshotID (imageName )
365+ snapshotID := api .GetSnapshotID (c . ImageName )
337366 snapshot , err := api .GetSnapshot (snapshotID )
338367 if err != nil {
339368 return "" , err
0 commit comments