Skip to content

Commit 9ce2c5f

Browse files
committed
feat(baremetal): add support for add flexible ip
1 parent 17055b4 commit 9ce2c5f

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

docs/commands/baremetal.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Elastic Metal API.
2323
- [List the Private Networks of a server](#list-the-private-networks-of-a-server)
2424
- [Set multiple Private Networks on a server](#set-multiple-private-networks-on-a-server)
2525
- [Server management commands](#server-management-commands)
26+
- [Attach a new flexible IP to a server](#attach-a-new-flexible-ip-to-a-server)
2627
- [Create an Elastic Metal server](#create-an-elastic-metal-server)
2728
- [Delete an Elastic Metal server](#delete-an-elastic-metal-server)
2829
- [Get a specific Elastic Metal server](#get-a-specific-elastic-metal-server)
@@ -478,6 +479,28 @@ scw baremetal private-network set [arg=value ...]
478479
A server is a denomination of a type of instances provided by Scaleway.
479480

480481

482+
### Attach a new flexible IP to a server
483+
484+
Create and attach a new flexible IP to a server
485+
486+
**Usage:**
487+
488+
```
489+
scw baremetal server add-flexible-ip <server-id ...> [arg=value ...]
490+
```
491+
492+
493+
**Args:**
494+
495+
| Name | | Description |
496+
|------|---|-------------|
497+
| server-id | Required | ID of the server to which the newly created flexible IP will be attached. |
498+
| description | | Flexible IP description (max. of 255 characters) |
499+
| ip-type | | Define whether the flexible IP is an IPv4 or IPv6 |
500+
| tags.{index} | | Tags to associate to the flexible IP |
501+
502+
503+
481504
### Create an Elastic Metal server
482505

483506
Create a new Elastic Metal server. Once the server is created, proceed with the [installation of an OS](#post-3e949e).

internal/namespaces/baremetal/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func GetCommands() *core.Commands {
1111

1212
cmds.Merge(core.NewCommands(
1313
serverWaitCommand(),
14+
serverAddFlexibleIP(),
1415
))
1516

1617
human.RegisterMarshalerFunc(baremetal.ServerPingStatus(""), human.EnumMarshalFunc(serverPingStatusMarshalSpecs))
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package baremetal
2+
3+
import (
4+
"context"
5+
"log"
6+
"reflect"
7+
8+
"github.com/scaleway/scaleway-cli/v2/internal/core"
9+
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
10+
fip "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
12+
)
13+
14+
type serverAddFlexibleIPRequest struct {
15+
ServerID string
16+
Description string
17+
IPType string
18+
Tags []string
19+
}
20+
21+
func serverAddFlexibleIP() *core.Command {
22+
return &core.Command{
23+
Short: "Attach a new flexible IP to a server",
24+
Long: "Create and attach a new flexible IP to a server",
25+
Namespace: "baremetal",
26+
Resource: "server",
27+
Verb: "add-flexible-ip",
28+
ArgsType: reflect.TypeOf(serverAddFlexibleIPRequest{}),
29+
ArgSpecs: core.ArgSpecs{
30+
{
31+
Name: "server-id",
32+
Short: `ID of the server to which the newly created flexible IP will be attached.`,
33+
Required: true,
34+
Deprecated: false,
35+
Positional: true,
36+
},
37+
{
38+
Name: "description",
39+
Short: `Flexible IP description (max. of 255 characters)`,
40+
Required: false,
41+
Deprecated: false,
42+
Positional: false,
43+
},
44+
{
45+
Name: "ip-type",
46+
Short: `Define whether the flexible IP is an IPv4 or IPv6`,
47+
Required: false,
48+
Deprecated: false,
49+
Positional: false,
50+
},
51+
{
52+
Name: "tags.{index}",
53+
Short: `Tags to associate to the flexible IP`,
54+
Required: false,
55+
Deprecated: false,
56+
Positional: false,
57+
},
58+
},
59+
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
60+
request := argsI.(*serverAddFlexibleIPRequest)
61+
client := core.ExtractClient(ctx)
62+
api := baremetal.NewAPI(client)
63+
server, err := api.GetServer(&baremetal.GetServerRequest{
64+
Zone: "",
65+
ServerID: request.ServerID,
66+
}, scw.WithContext(ctx))
67+
if err != nil {
68+
return nil, err
69+
}
70+
args, err := promptIPFlexibleServer(ctx, request)
71+
if err != nil {
72+
return nil, err
73+
}
74+
apiFip := fip.NewAPI(client)
75+
IsIPv6 := args.IPType == "IPv6"
76+
log.Println("is IPv6 ", IsIPv6)
77+
return apiFip.CreateFlexibleIP(&fip.CreateFlexibleIPRequest{
78+
ServerID: &server.ID,
79+
Zone: server.Zone,
80+
ProjectID: server.ProjectID,
81+
Description: args.Description,
82+
Tags: args.Tags,
83+
IsIPv6: IsIPv6,
84+
})
85+
},
86+
}
87+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package baremetal
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/scaleway/scaleway-cli/v2/internal/core"
9+
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
10+
)
11+
12+
var ipTypeOption = []string{"IPv4", "IPv6"}
13+
14+
func promptIPFlexibleServer(ctx context.Context, req *serverAddFlexibleIPRequest) (*serverAddFlexibleIPRequest, error) {
15+
if !interactive.IsInteractive {
16+
if req.IPType == "" {
17+
return nil, &core.CliError{
18+
Err: fmt.Errorf("failed to create and attach a new flexible IP"),
19+
Hint: "Missing argument 'ip-type'",
20+
}
21+
}
22+
return req, nil
23+
}
24+
var err error
25+
req.IPType, err = promptChooseIPType(ctx)
26+
if err != nil {
27+
return nil, err
28+
}
29+
req.Description, err = promptAddDescriptionFip(ctx)
30+
if err != nil {
31+
return nil, err
32+
}
33+
req.Tags, err = promptAddTags(ctx)
34+
if err != nil {
35+
return nil, err
36+
}
37+
return req, nil
38+
}
39+
40+
func promptAddTags(ctx context.Context) ([]string, error) {
41+
_, _ = interactive.Println()
42+
promptConfig := interactive.PromptStringConfig{
43+
Ctx: ctx,
44+
Prompt: "Enter all the tags you want to associate to your flexible IP separate by a space",
45+
DefaultValue: "",
46+
}
47+
res, err := interactive.PromptStringWithConfig(&promptConfig)
48+
_, _ = interactive.Println()
49+
if err != nil {
50+
return nil, err
51+
}
52+
return strings.Split(res, " "), nil
53+
}
54+
55+
func promptAddDescriptionFip(ctx context.Context) (string, error) {
56+
_, _ = interactive.Println()
57+
promptConfig := interactive.PromptStringConfig{
58+
Ctx: ctx,
59+
Prompt: "Enter a description for you flexible ip (max 255 words)",
60+
DefaultValue: "",
61+
}
62+
res, err := interactive.PromptStringWithConfig(&promptConfig)
63+
if err != nil {
64+
return "", err
65+
}
66+
return res, nil
67+
}
68+
69+
func promptChooseIPType(ctx context.Context) (string, error) {
70+
prompt := interactive.ListPrompt{
71+
Prompt: "Choose your internet protocol",
72+
Choices: ipTypeOption,
73+
DefaultIndex: 0,
74+
}
75+
ipTypeIndex, err := prompt.Execute(ctx)
76+
if err != nil {
77+
return "", err
78+
}
79+
return ipTypeOption[ipTypeIndex], nil
80+
}

0 commit comments

Comments
 (0)