diff --git a/README.md b/README.md index 7e1ec0f722..67e420bffd 100644 --- a/README.md +++ b/README.md @@ -1185,6 +1185,8 @@ $ scw inspect myserver | jq '.[0].public_ip.address' ### master (unreleased) * `scw login` Add motd when you are already logged ([#371](https://github.com/scaleway/scaleway-cli/issues/371)) +* `scw _ips` add --detach flag +* API add DetachIP method ([@nicolai86](https://github.com/scaleway/scaleway-cli/pull/378)) * Fix error message with `--commercial-type=c2m` ([#374](https://github.com/scaleway/scaleway-cli/issues/374)) * 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)) * `scw run` handle `--ipv6` flag diff --git a/pkg/api/api.go b/pkg/api/api.go index d7147204bf..f9adf9c2f6 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -419,13 +419,13 @@ type ScalewayGetSecurityGroup struct { // ScalewayIPDefinition represents the IP's fields type ScalewayIPDefinition struct { - Organization string `json:"organization"` - Reverse string `json:"reverse"` - ID string `json:"id"` - Server struct { + Organization string `json:"organization"` + Reverse *string `json:"reverse"` + ID string `json:"id"` + Server *struct { Identifier string `json:"id,omitempty"` Name string `json:"name,omitempty"` - } `json:"server,omitempty"` + } `json:"server"` Address string `json:"address"` } @@ -2315,29 +2315,18 @@ func (s *ScalewayAPI) AttachIP(ipID, serverID string) error { // DetachIP detaches an IP from a server func (s *ScalewayAPI) DetachIP(ipID string) error { - var update struct { - Address string `json:"address"` - ID string `json:"id"` - Organization string `json:"organization"` - } - ip, err := s.GetIP(ipID) if err != nil { return err } - - if ip.IP.Server.Identifier == "" { - return nil + ip.IP.Server = nil + resp, err := s.PutResponse(ComputeAPI, fmt.Sprintf("ips/%s", ipID), ip.IP) + if resp != nil { + defer resp.Body.Close() } - - update.Address = ip.IP.Address - update.ID = ip.IP.ID - update.Organization = ip.IP.Organization - resp, err := s.PutResponse(ComputeAPI, fmt.Sprintf("ips/%s", ipID), update) if err != nil { return err } - defer resp.Body.Close() _, err = s.handleHTTPError([]int{200}, resp) return err } @@ -2351,11 +2340,8 @@ func (s *ScalewayAPI) DeleteIP(ipID string) error { if err != nil { return err } - - if _, err := s.handleHTTPError([]int{204}, resp); err != nil { - return err - } - return nil + _, err = s.handleHTTPError([]int{204}, resp) + return err } // GetIP returns a ScalewayGetIP diff --git a/pkg/cli/x_ips.go b/pkg/cli/x_ips.go index 5e8bbbc4e3..1a3cfaf9e1 100644 --- a/pkg/cli/x_ips.go +++ b/pkg/cli/x_ips.go @@ -6,7 +6,7 @@ package cli var cmdIPS = &Command{ Exec: runIPS, - UsageLine: "_ips [--new|--attach|--delete] [IP_ID [SERVER_ID]]", + UsageLine: "_ips [OPTIONS] [IP_ID [SERVER_ID]]", Description: "Interacts with your IPs", Hidden: true, @@ -17,6 +17,7 @@ var cmdIPS = &Command{ $ scw _ips --new $ scw _ips --attach IP_ID SERVER_ID $ scw _ips --delete IP_ID + $ scw _ips --detach IP_ID `, } @@ -24,12 +25,14 @@ func init() { cmdIPS.Flag.BoolVar(&ipHelp, []string{"h", "-help"}, false, "Print usage") cmdIPS.Flag.BoolVar(&ipNew, []string{"n", "-new"}, false, "Add a new IP") cmdIPS.Flag.BoolVar(&ipAttach, []string{"a", "-attach"}, false, "Attach an IP to a server") + cmdIPS.Flag.BoolVar(&ipDetach, []string{"-detach"}, false, "Detach an IP from a server") cmdIPS.Flag.StringVar(&ipDelete, []string{"d", "-delete"}, "", "Detele an IP") } var ipHelp bool // -h, --help flag var ipNew bool // -n, --new flag var ipAttach bool // -a, --attach flag +var ipDetach bool // --detach flag var ipDelete string // -d, --delete flag func runIPS(cmd *Command, args []string) error { @@ -53,6 +56,9 @@ func runIPS(cmd *Command, args []string) error { } return cmd.API.AttachIP(args[0], args[1]) } + if ipDetach { + return cmd.API.DetachIP(args[0]) + } if len(args) == 1 { ip, err := cmd.API.GetIP(args[0]) if err != nil {