Skip to content

Commit 253f50f

Browse files
authored
Merge pull request #379 from QuentinPerez/ips
ips
2 parents 4f05b56 + 82bc18b commit 253f50f

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,8 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11851185
### master (unreleased)
11861186

11871187
* `scw login` Add motd when you are already logged ([#371](https://github.com/scaleway/scaleway-cli/issues/371))
1188+
* `scw _ips` add --detach flag
1189+
* API add DetachIP method ([@nicolai86](https://github.com/scaleway/scaleway-cli/pull/378))
11881190
* Fix error message with `--commercial-type=c2m` ([#374](https://github.com/scaleway/scaleway-cli/issues/374))
11891191
* Add Logger Interface to avoid multiples dependencies in the API, thank you [@nicolai86](https://github.com/nicolai86) ([#369](https://github.com/scaleway/scaleway-cli/pull/369))
11901192
* `scw run` handle `--ipv6` flag

pkg/api/api.go

+11-25
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,13 @@ type ScalewayGetSecurityGroup struct {
419419

420420
// ScalewayIPDefinition represents the IP's fields
421421
type ScalewayIPDefinition struct {
422-
Organization string `json:"organization"`
423-
Reverse string `json:"reverse"`
424-
ID string `json:"id"`
425-
Server struct {
422+
Organization string `json:"organization"`
423+
Reverse *string `json:"reverse"`
424+
ID string `json:"id"`
425+
Server *struct {
426426
Identifier string `json:"id,omitempty"`
427427
Name string `json:"name,omitempty"`
428-
} `json:"server,omitempty"`
428+
} `json:"server"`
429429
Address string `json:"address"`
430430
}
431431

@@ -2315,29 +2315,18 @@ func (s *ScalewayAPI) AttachIP(ipID, serverID string) error {
23152315

23162316
// DetachIP detaches an IP from a server
23172317
func (s *ScalewayAPI) DetachIP(ipID string) error {
2318-
var update struct {
2319-
Address string `json:"address"`
2320-
ID string `json:"id"`
2321-
Organization string `json:"organization"`
2322-
}
2323-
23242318
ip, err := s.GetIP(ipID)
23252319
if err != nil {
23262320
return err
23272321
}
2328-
2329-
if ip.IP.Server.Identifier == "" {
2330-
return nil
2322+
ip.IP.Server = nil
2323+
resp, err := s.PutResponse(ComputeAPI, fmt.Sprintf("ips/%s", ipID), ip.IP)
2324+
if resp != nil {
2325+
defer resp.Body.Close()
23312326
}
2332-
2333-
update.Address = ip.IP.Address
2334-
update.ID = ip.IP.ID
2335-
update.Organization = ip.IP.Organization
2336-
resp, err := s.PutResponse(ComputeAPI, fmt.Sprintf("ips/%s", ipID), update)
23372327
if err != nil {
23382328
return err
23392329
}
2340-
defer resp.Body.Close()
23412330
_, err = s.handleHTTPError([]int{200}, resp)
23422331
return err
23432332
}
@@ -2351,11 +2340,8 @@ func (s *ScalewayAPI) DeleteIP(ipID string) error {
23512340
if err != nil {
23522341
return err
23532342
}
2354-
2355-
if _, err := s.handleHTTPError([]int{204}, resp); err != nil {
2356-
return err
2357-
}
2358-
return nil
2343+
_, err = s.handleHTTPError([]int{204}, resp)
2344+
return err
23592345
}
23602346

23612347
// GetIP returns a ScalewayGetIP

pkg/cli/x_ips.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package cli
66

77
var cmdIPS = &Command{
88
Exec: runIPS,
9-
UsageLine: "_ips [--new|--attach|--delete] [IP_ID [SERVER_ID]]",
9+
UsageLine: "_ips [OPTIONS] [IP_ID [SERVER_ID]]",
1010

1111
Description: "Interacts with your IPs",
1212
Hidden: true,
@@ -17,19 +17,22 @@ var cmdIPS = &Command{
1717
$ scw _ips --new
1818
$ scw _ips --attach IP_ID SERVER_ID
1919
$ scw _ips --delete IP_ID
20+
$ scw _ips --detach IP_ID
2021
`,
2122
}
2223

2324
func init() {
2425
cmdIPS.Flag.BoolVar(&ipHelp, []string{"h", "-help"}, false, "Print usage")
2526
cmdIPS.Flag.BoolVar(&ipNew, []string{"n", "-new"}, false, "Add a new IP")
2627
cmdIPS.Flag.BoolVar(&ipAttach, []string{"a", "-attach"}, false, "Attach an IP to a server")
28+
cmdIPS.Flag.BoolVar(&ipDetach, []string{"-detach"}, false, "Detach an IP from a server")
2729
cmdIPS.Flag.StringVar(&ipDelete, []string{"d", "-delete"}, "", "Detele an IP")
2830
}
2931

3032
var ipHelp bool // -h, --help flag
3133
var ipNew bool // -n, --new flag
3234
var ipAttach bool // -a, --attach flag
35+
var ipDetach bool // --detach flag
3336
var ipDelete string // -d, --delete flag
3437

3538
func runIPS(cmd *Command, args []string) error {
@@ -53,6 +56,9 @@ func runIPS(cmd *Command, args []string) error {
5356
}
5457
return cmd.API.AttachIP(args[0], args[1])
5558
}
59+
if ipDetach {
60+
return cmd.API.DetachIP(args[0])
61+
}
5662
if len(args) == 1 {
5763
ip, err := cmd.API.GetIP(args[0])
5864
if err != nil {

0 commit comments

Comments
 (0)