Skip to content
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

Add DetachIP helper #378

Merged
merged 1 commit into from
Jul 13, 2016
Merged
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
29 changes: 29 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,35 @@ func (s *ScalewayAPI) AttachIP(ipID, serverID string) error {
return err
}

// 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
}

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this code below

if resp != nil {
    defer resp.Body.Close()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done; see diff

if err != nil {
return err
}
defer resp.Body.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to add this code between s.PutResponse and if err != nil

if resp != nil {
    defer resp.Body.Close()
}

according to http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/#anameclose_http_resp_bodyaclosinghttpresponsebody

_, err = s.handleHTTPError([]int{200}, resp)
return err
}

// DeleteIP deletes an IP
func (s *ScalewayAPI) DeleteIP(ipID string) error {
resp, err := s.DeleteResponse(ComputeAPI, fmt.Sprintf("ips/%s", ipID))
Expand Down