blob: ab681dc92fe24113ec0449c40fe2a6fb5a53a8e6 [file] [log] [blame]
Joe Topjiandee32222015-02-09 23:56:26 +00001package floatingip
2
3import (
4 "errors"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11// List returns a Pager that allows you to iterate over a collection of FloatingIPs.
12func List(client *gophercloud.ServiceClient) pagination.Pager {
13 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
14 return FloatingIPsPage{pagination.SinglePageBase(r)}
15 })
16}
17
18// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
19// CreateOpts struct in this package does.
20type CreateOptsBuilder interface {
21 ToFloatingIPCreateMap() (map[string]interface{}, error)
22}
23
24// CreateOpts specifies a Floating IP allocation request
25type CreateOpts struct {
26 // Pool is the pool of floating IPs to allocate one from
27 Pool string
28}
29
30// ToFloatingIPCreateMap constructs a request body from CreateOpts.
31func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) {
32 if opts.Pool == "" {
33 return nil, errors.New("Missing field required for floating IP creation: Pool")
34 }
35
36 return map[string]interface{}{"pool": opts.Pool}, nil
37}
38
39// Create requests the creation of a new floating IP
40func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
41 var res CreateResult
42
43 reqBody, err := opts.ToFloatingIPCreateMap()
44 if err != nil {
45 res.Err = err
46 return res
47 }
48
49 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
50 MoreHeaders: client.AuthenticatedHeaders(),
51 ReqBody: reqBody,
52 Results: &res.Body,
53 OkCodes: []int{200},
54 })
55 return res
56}
57
58// Get returns data about a previously created FloatingIP.
59func Get(client *gophercloud.ServiceClient, id string) GetResult {
60 var res GetResult
61 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
62 MoreHeaders: client.AuthenticatedHeaders(),
63 Results: &res.Body,
64 OkCodes: []int{200},
65 })
66 return res
67}
68
69// Delete requests the deletion of a previous allocated FloatingIP.
70func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
71 var res DeleteResult
72 _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
73 MoreHeaders: client.AuthenticatedHeaders(),
74 OkCodes: []int{202},
75 })
76 return res
77}
78
79// association / disassociation
80
81// Associate pairs an allocated floating IP with an instance
82func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult {
83 var res AssociateResult
84
85 addFloatingIp := make(map[string]interface{})
86 addFloatingIp["address"] = fip
87 reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp}
88
89 _, res.Err = perigee.Request("POST", associateURL(client, serverId), perigee.Options{
90 MoreHeaders: client.AuthenticatedHeaders(),
91 ReqBody: reqBody,
92 OkCodes: []int{202},
93 })
94 return res
95}
96
97// Disassociate decouples an allocated floating IP from an instance
98func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult {
99 var res DisassociateResult
100
101 removeFloatingIp := make(map[string]interface{})
102 removeFloatingIp["address"] = fip
103 reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp}
104
105 _, res.Err = perigee.Request("POST", disassociateURL(client, serverId), perigee.Options{
106 MoreHeaders: client.AuthenticatedHeaders(),
107 ReqBody: reqBody,
108 OkCodes: []int{202},
109 })
110 return res
111}