blob: d1540380108b347d83f0b4ec476010e4e9952927 [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,
61 OkCodes: []int{200},
Joe Topjiandee32222015-02-09 23:56:26 +000062 })
63 return res
64}
65
66// Delete requests the deletion of a previous allocated FloatingIP.
67func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
68 var res DeleteResult
Ash Wilsondecfed72015-02-13 09:14:55 -050069 _, res.Err = client.Request("DELETE", deleteURL(client, id), gophercloud.RequestOpts{
70 OkCodes: []int{202},
Joe Topjiandee32222015-02-09 23:56:26 +000071 })
72 return res
73}
74
75// association / disassociation
76
77// Associate pairs an allocated floating IP with an instance
78func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult {
79 var res AssociateResult
80
81 addFloatingIp := make(map[string]interface{})
82 addFloatingIp["address"] = fip
83 reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp}
84
Ash Wilsondecfed72015-02-13 09:14:55 -050085 _, res.Err = client.Request("POST", associateURL(client, serverId), gophercloud.RequestOpts{
86 JSONBody: reqBody,
87 OkCodes: []int{202},
Joe Topjiandee32222015-02-09 23:56:26 +000088 })
89 return res
90}
91
92// Disassociate decouples an allocated floating IP from an instance
93func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult {
94 var res DisassociateResult
95
96 removeFloatingIp := make(map[string]interface{})
97 removeFloatingIp["address"] = fip
98 reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp}
99
Ash Wilsondecfed72015-02-13 09:14:55 -0500100 _, res.Err = client.Request("POST", disassociateURL(client, serverId), gophercloud.RequestOpts{
101 JSONBody: reqBody,
102 OkCodes: []int{202},
Joe Topjiandee32222015-02-09 23:56:26 +0000103 })
104 return res
105}