2016-02-22 14:44:12 -05:00
|
|
|
package godo
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
const floatingBasePath = "v2/floating_ips"
|
|
|
|
|
|
|
|
// FloatingIPsService is an interface for interfacing with the floating IPs
|
|
|
|
// endpoints of the Digital Ocean API.
|
|
|
|
// See: https://developers.digitalocean.com/documentation/v2#floating-ips
|
|
|
|
type FloatingIPsService interface {
|
2017-04-08 15:52:57 -04:00
|
|
|
List(context.Context, *ListOptions) ([]FloatingIP, *Response, error)
|
|
|
|
Get(context.Context, string) (*FloatingIP, *Response, error)
|
|
|
|
Create(context.Context, *FloatingIPCreateRequest) (*FloatingIP, *Response, error)
|
|
|
|
Delete(context.Context, string) (*Response, error)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FloatingIPsServiceOp handles communication with the floating IPs related methods of the
|
|
|
|
// DigitalOcean API.
|
|
|
|
type FloatingIPsServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ FloatingIPsService = &FloatingIPsServiceOp{}
|
|
|
|
|
|
|
|
// FloatingIP represents a Digital Ocean floating IP.
|
|
|
|
type FloatingIP struct {
|
|
|
|
Region *Region `json:"region"`
|
|
|
|
Droplet *Droplet `json:"droplet"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FloatingIP) String() string {
|
|
|
|
return Stringify(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
type floatingIPsRoot struct {
|
|
|
|
FloatingIPs []FloatingIP `json:"floating_ips"`
|
|
|
|
Links *Links `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type floatingIPRoot struct {
|
|
|
|
FloatingIP *FloatingIP `json:"floating_ip"`
|
|
|
|
Links *Links `json:"links,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FloatingIPCreateRequest represents a request to create a floating IP.
|
|
|
|
// If DropletID is not empty, the floating IP will be assigned to the
|
|
|
|
// droplet.
|
|
|
|
type FloatingIPCreateRequest struct {
|
|
|
|
Region string `json:"region"`
|
|
|
|
DropletID int `json:"droplet_id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// List all floating IPs.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (f *FloatingIPsServiceOp) List(ctx context.Context, opt *ListOptions) ([]FloatingIP, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
path := floatingBasePath
|
|
|
|
path, err := addOptions(path, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
req, err := f.client.NewRequest(ctx, "GET", path, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(floatingIPsRoot)
|
|
|
|
resp, err := f.client.Do(req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.FloatingIPs, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an individual floating IP.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (f *FloatingIPsServiceOp) Get(ctx context.Context, ip string) (*FloatingIP, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
path := fmt.Sprintf("%s/%s", floatingBasePath, ip)
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
req, err := f.client.NewRequest(ctx, "GET", path, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(floatingIPRoot)
|
|
|
|
resp, err := f.client.Do(req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.FloatingIP, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a floating IP. If the DropletID field of the request is not empty,
|
|
|
|
// the floating IP will also be assigned to the droplet.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (f *FloatingIPsServiceOp) Create(ctx context.Context, createRequest *FloatingIPCreateRequest) (*FloatingIP, *Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
path := floatingBasePath
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
req, err := f.client.NewRequest(ctx, "POST", path, createRequest)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(floatingIPRoot)
|
|
|
|
resp, err := f.client.Do(req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.FloatingIP, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a floating IP.
|
2017-04-08 15:52:57 -04:00
|
|
|
func (f *FloatingIPsServiceOp) Delete(ctx context.Context, ip string) (*Response, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
path := fmt.Sprintf("%s/%s", floatingBasePath, ip)
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
req, err := f.client.NewRequest(ctx, "DELETE", path, nil)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := f.client.Do(req, nil)
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|