|
| 1 | +package baremetal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + "github.com/scaleway/scaleway-cli/v2/internal/core" |
| 9 | + flexibleip "github.com/scaleway/scaleway-cli/v2/internal/namespaces/flexibleip/v1alpha1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1" |
| 11 | + fip "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1" |
| 12 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 13 | +) |
| 14 | + |
| 15 | +type serverAddFlexibleIPRequest struct { |
| 16 | + ServerID string |
| 17 | + Description string |
| 18 | + IPType string |
| 19 | + Tags []string |
| 20 | + Zone scw.Zone |
| 21 | +} |
| 22 | + |
| 23 | +func serverAddFlexibleIP() *core.Command { |
| 24 | + return &core.Command{ |
| 25 | + Short: "Attach a new flexible IP to a server", |
| 26 | + Long: "Create and attach a new flexible IP to a server", |
| 27 | + Namespace: "baremetal", |
| 28 | + Resource: "server", |
| 29 | + Verb: "add-flexible-ip", |
| 30 | + Groups: []string{"utility"}, |
| 31 | + ArgsType: reflect.TypeOf(serverAddFlexibleIPRequest{}), |
| 32 | + ArgSpecs: core.ArgSpecs{ |
| 33 | + { |
| 34 | + Name: "server-id", |
| 35 | + Short: `ID of the server to which the newly created flexible IP will be attached.`, |
| 36 | + Required: true, |
| 37 | + Deprecated: false, |
| 38 | + Positional: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + Name: "description", |
| 42 | + Short: `Flexible IP description (max. of 255 characters)`, |
| 43 | + Required: false, |
| 44 | + Deprecated: false, |
| 45 | + Positional: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "ip-type", |
| 49 | + Short: `Define whether the flexible IP is an IPv4 or IPv6`, |
| 50 | + Required: false, |
| 51 | + Deprecated: false, |
| 52 | + Positional: false, |
| 53 | + EnumValues: ipTypeOption, |
| 54 | + ValidateFunc: func(_ *core.ArgSpec, value interface{}) error { |
| 55 | + if value == "IPv4" || value == "IPv6" || value == "" { |
| 56 | + return nil |
| 57 | + } |
| 58 | + return &core.CliError{ |
| 59 | + Err: fmt.Errorf("error looks like the IP type isn't correct"), |
| 60 | + Hint: "Two type available : IPv6 or IPv4", |
| 61 | + } |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + Name: "tags.{index}", |
| 66 | + Short: `Tags to associate to the flexible IP`, |
| 67 | + Required: false, |
| 68 | + Deprecated: false, |
| 69 | + Positional: false, |
| 70 | + }, |
| 71 | + core.ZoneArgSpec(scw.ZoneFrPar1, scw.ZoneFrPar2, scw.ZoneNlAms1), |
| 72 | + }, |
| 73 | + Run: func(ctx context.Context, argsI interface{}) (interface{}, error) { |
| 74 | + request := argsI.(*serverAddFlexibleIPRequest) |
| 75 | + client := core.ExtractClient(ctx) |
| 76 | + api := baremetal.NewAPI(client) |
| 77 | + server, err := api.GetServer(&baremetal.GetServerRequest{ |
| 78 | + Zone: request.Zone, |
| 79 | + ServerID: request.ServerID, |
| 80 | + }, scw.WithContext(ctx)) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + args := request |
| 85 | + if args.IPType == "" { |
| 86 | + args, err = promptIPFlexibleServer(ctx, request) |
| 87 | + } |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + apiFip := fip.NewAPI(client) |
| 92 | + IsIPv6 := args.IPType == "IPv6" |
| 93 | + flexibleIP, err := apiFip.CreateFlexibleIP(&fip.CreateFlexibleIPRequest{ |
| 94 | + ServerID: &server.ID, |
| 95 | + Zone: server.Zone, |
| 96 | + ProjectID: server.ProjectID, |
| 97 | + Description: args.Description, |
| 98 | + Tags: args.Tags, |
| 99 | + IsIPv6: IsIPv6, |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + return apiFip.WaitForFlexibleIP(&fip.WaitForFlexibleIPRequest{ |
| 105 | + FipID: flexibleIP.ID, |
| 106 | + Zone: flexibleIP.Zone, |
| 107 | + Timeout: scw.TimeDurationPtr(flexibleip.FlexibleIPTimeout), |
| 108 | + RetryInterval: core.DefaultRetryInterval, |
| 109 | + }) |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
0 commit comments