Skip to content

Commit a7fad38

Browse files
committed
Merge pull request #236 from QuentinPerez/fix235
Support of --ip-address fix #235
2 parents dcffd74 + e637941 commit a7fad38

File tree

7 files changed

+77
-19
lines changed

7 files changed

+77
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Options:
309309
--bootscript="" Assign a bootscript
310310
-e, --env="" Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)
311311
-h, --help=false Print usage
312+
--ip-address="" Assign an IP
312313
--name="" Assign a name
313314
--tmp-ssh-key=false Access your server without uploading your SSH key to your account
314315
-v, --volume="" Attach additional volume (i.e., 50G)
@@ -679,8 +680,10 @@ Options:
679680
-e, --env="" Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)
680681
-g, --gateway="" Use a SSH gateway
681682
-h, --help=false Print usage
683+
--ip-address="" Assign an IP
682684
--name="" Assign a name
683685
--rm=false Automatically remove the server when it exits
686+
--show-boot=false Allows to show the boot
684687
--tmp-ssh-key=false Access your server without uploading your SSH key to your account
685688
-v, --volume="" Attach additional volume (i.e., 50G)
686689

@@ -1139,6 +1142,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11391142

11401143
### master (unreleased)
11411144

1145+
* Support of `scw create|run --ip-address` ([#235](https://github.com/scaleway/scaleway-cli/issues/235))
11421146
* Update gotty-client to 1.3.0
11431147
* Support of `scw run --show-boot` option ([#156](https://github.com/scaleway/scaleway-cli/issues/156))
11441148
* Remove go1.[34] support

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ type ScalewayServerDefinition struct {
575575

576576
// Organization is the owner of the server
577577
Organization string `json:"organization"`
578+
579+
PublicIP string `json:"public_ip,omitempty"`
578580
}
579581

580582
// ScalewayOneServer represents the response of a GET /servers/UUID API call

pkg/api/helpers.go

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

pkg/cli/cmd_create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func init() {
3030
cmdCreate.Flag.StringVar(&createBootscript, []string{"-bootscript"}, "", "Assign a bootscript")
3131
cmdCreate.Flag.StringVar(&createEnv, []string{"e", "-env"}, "", "Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)")
3232
cmdCreate.Flag.StringVar(&createVolume, []string{"v", "-volume"}, "", "Attach additional volume (i.e., 50G)")
33+
cmdCreate.Flag.StringVar(&createIPAddress, []string{"-ip-address"}, "", "Assign an IP")
3334
cmdCreate.Flag.BoolVar(&createHelp, []string{"h", "-help"}, false, "Print usage")
3435
cmdCreate.Flag.BoolVar(&createTmpSSHKey, []string{"-tmp-ssh-key"}, false, "Access your server without uploading your SSH key to your account")
3536
}
@@ -41,6 +42,7 @@ var createEnv string // -e, --env flag
4142
var createVolume string // -v, --volume flag
4243
var createHelp bool // -h, --help flag
4344
var createTmpSSHKey bool // --tmp-ssh-key flag
45+
var createIPAddress string // --ip-address flag
4446

4547
func runCreate(cmd *Command, rawArgs []string) error {
4648
if createHelp {
@@ -55,6 +57,7 @@ func runCreate(cmd *Command, rawArgs []string) error {
5557
Bootscript: createBootscript,
5658
Image: rawArgs[0],
5759
TmpSSHKey: createTmpSSHKey,
60+
IP: createIPAddress,
5861
}
5962

6063
if len(createEnv) > 0 {

pkg/cli/cmd_run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func init() {
3636
cmdRun.Flag.StringVar(&runCreateEnv, []string{"e", "-env"}, "", "Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)")
3737
cmdRun.Flag.StringVar(&runCreateVolume, []string{"v", "-volume"}, "", "Attach additional volume (i.e., 50G)")
3838
cmdRun.Flag.BoolVar(&runHelpFlag, []string{"h", "-help"}, false, "Print usage")
39+
cmdRun.Flag.StringVar(&runIPAddress, []string{"-ip-address"}, "", "Assign an IP")
3940
cmdRun.Flag.BoolVar(&runAttachFlag, []string{"a", "-attach"}, false, "Attach to serial console")
4041
cmdRun.Flag.BoolVar(&runDetachFlag, []string{"d", "-detach"}, false, "Run server in background and print server ID")
4142
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
@@ -51,6 +52,7 @@ var runAutoRemove bool // --rm flag
5152
var runCreateBootscript string // --bootscript flag
5253
var runCreateEnv string // -e, --env flag
5354
var runCreateVolume string // -v, --volume flag
55+
var runIPAddress string // --ip-address flag
5456
var runHelpFlag bool // -h, --help flag
5557
var runAttachFlag bool // -a, --attach flag
5658
var runDetachFlag bool // -d, --detach flag
@@ -98,6 +100,7 @@ func runRun(cmd *Command, rawArgs []string) error {
98100
AutoRemove: runAutoRemove,
99101
TmpSSHKey: runTmpSSHKey,
100102
ShowBoot: runShowBoot,
103+
IP: runIPAddress,
101104
// FIXME: DynamicIPRequired
102105
// FIXME: Timeout
103106
}

pkg/commands/create.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type CreateArgs struct {
1919
Volumes []string
2020
Image string
2121
TmpSSHKey bool
22+
IP string
2223
}
2324

2425
// RunCreate is the handler for 'scw create'
@@ -32,7 +33,15 @@ func RunCreate(ctx CommandContext, args CreateArgs) error {
3233

3334
env := strings.Join(args.Tags, " ")
3435
volume := strings.Join(args.Volumes, " ")
35-
serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, true)
36+
serverID, err := api.CreateServer(ctx.API, &api.ConfigCreateServer{
37+
ImageName: args.Image,
38+
Name: args.Name,
39+
Bootscript: args.Bootscript,
40+
Env: env,
41+
AdditionalVolumes: volume,
42+
DynamicIpRequired: args.IP == "",
43+
IP: args.IP,
44+
})
3645
if err != nil {
3746
return err
3847
}

pkg/commands/run.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type RunArgs struct {
2626
Gateway string
2727
Image string
2828
Name string
29+
IP string
2930
Tags []string
3031
Volumes []string
3132
AutoRemove bool
@@ -82,8 +83,15 @@ func Run(ctx CommandContext, args RunArgs) error {
8283

8384
// create IMAGE
8485
logrus.Info("Server creation ...")
85-
dynamicIPRequired := args.Gateway == ""
86-
serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, dynamicIPRequired)
86+
serverID, err := api.CreateServer(ctx.API, &api.ConfigCreateServer{
87+
ImageName: args.Image,
88+
Name: args.Name,
89+
Bootscript: args.Bootscript,
90+
Env: env,
91+
AdditionalVolumes: volume,
92+
DynamicIpRequired: args.Gateway == "",
93+
IP: args.IP,
94+
})
8795
if err != nil {
8896
return fmt.Errorf("failed to create server: %v", err)
8997
}

0 commit comments

Comments
 (0)