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