2016-01-29 20:53:56 +01:00
|
|
|
package godo
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/digitalocean/godo/context"
|
|
|
|
)
|
2016-01-29 20:53:56 +01:00
|
|
|
|
|
|
|
// FloatingIPActionsService is an interface for interfacing with the
|
|
|
|
// floating IPs actions endpoints of the Digital Ocean API.
|
|
|
|
// See: https://developers.digitalocean.com/documentation/v2#floating-ips-action
|
|
|
|
type FloatingIPActionsService interface {
|
2017-05-15 15:54:16 +02:00
|
|
|
Assign(ctx context.Context, ip string, dropletID int) (*Action, *Response, error)
|
|
|
|
Unassign(ctx context.Context, ip string) (*Action, *Response, error)
|
|
|
|
Get(ctx context.Context, ip string, actionID int) (*Action, *Response, error)
|
|
|
|
List(ctx context.Context, ip string, opt *ListOptions) ([]Action, *Response, error)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FloatingIPActionsServiceOp handles communication with the floating IPs
|
|
|
|
// action related methods of the DigitalOcean API.
|
|
|
|
type FloatingIPActionsServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign a floating IP to a droplet.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *FloatingIPActionsServiceOp) Assign(ctx context.Context, ip string, dropletID int) (*Action, *Response, error) {
|
2016-01-29 20:53:56 +01:00
|
|
|
request := &ActionRequest{
|
|
|
|
"type": "assign",
|
|
|
|
"droplet_id": dropletID,
|
|
|
|
}
|
2017-05-15 15:54:16 +02:00
|
|
|
return s.doAction(ctx, ip, request)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unassign a floating IP from the droplet it is currently assigned to.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *FloatingIPActionsServiceOp) Unassign(ctx context.Context, ip string) (*Action, *Response, error) {
|
2016-01-29 20:53:56 +01:00
|
|
|
request := &ActionRequest{"type": "unassign"}
|
2017-05-15 15:54:16 +02:00
|
|
|
return s.doAction(ctx, ip, request)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get an action for a particular floating IP by id.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *FloatingIPActionsServiceOp) Get(ctx context.Context, ip string, actionID int) (*Action, *Response, error) {
|
2016-01-29 20:53:56 +01:00
|
|
|
path := fmt.Sprintf("%s/%d", floatingIPActionPath(ip), actionID)
|
2017-05-15 15:54:16 +02:00
|
|
|
return s.get(ctx, path)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List the actions for a particular floating IP.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *FloatingIPActionsServiceOp) List(ctx context.Context, ip string, opt *ListOptions) ([]Action, *Response, error) {
|
2016-01-29 20:53:56 +01:00
|
|
|
path := floatingIPActionPath(ip)
|
|
|
|
path, err := addOptions(path, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
return s.list(ctx, path)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *FloatingIPActionsServiceOp) doAction(ctx context.Context, ip string, request *ActionRequest) (*Action, *Response, error) {
|
2016-01-29 20:53:56 +01:00
|
|
|
path := floatingIPActionPath(ip)
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
req, err := s.client.NewRequest(ctx, "POST", path, request)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(actionRoot)
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2017-02-15 15:35:41 +01:00
|
|
|
return root.Event, resp, err
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *FloatingIPActionsServiceOp) get(ctx context.Context, path string) (*Action, *Response, error) {
|
|
|
|
req, err := s.client.NewRequest(ctx, "GET", path, nil)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(actionRoot)
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2017-02-15 15:35:41 +01:00
|
|
|
return root.Event, resp, err
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *FloatingIPActionsServiceOp) list(ctx context.Context, path string) ([]Action, *Response, error) {
|
|
|
|
req, err := s.client.NewRequest(ctx, "GET", path, nil)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(actionsRoot)
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2016-01-29 20:53:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Actions, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func floatingIPActionPath(ip string) string {
|
|
|
|
return fmt.Sprintf("%s/%s/actions", floatingBasePath, ip)
|
|
|
|
}
|