Skip to content

ips #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 13, 2016
Merged

ips #379

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 11 additions & 25 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion pkg/cli/x_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,19 +17,22 @@ var cmdIPS = &Command{
$ scw _ips --new
$ scw _ips --attach IP_ID SERVER_ID
$ scw _ips --delete IP_ID
$ scw _ips --detach IP_ID
`,
}

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 {
Expand All @@ -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 {
Expand Down