blob: 8abb72dcdec79cdc734e1e873686247c792e4cff [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
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010048 _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
49 OkCodes: []int{200},
Joe Topjiandee32222015-02-09 23:56:26 +000050 })
51 return res
52}
53
54// Get returns data about a previously created FloatingIP.
55func Get(client *gophercloud.ServiceClient, id string) GetResult {
56 var res GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010057 _, res.Err = client.Get(getURL(client, id), &res.Body, nil)
Joe Topjiandee32222015-02-09 23:56:26 +000058 return res
59}
60
61// Delete requests the deletion of a previous allocated FloatingIP.
62func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
63 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010064 _, res.Err = client.Delete(deleteURL(client, id), nil)
Joe Topjiandee32222015-02-09 23:56:26 +000065 return res
66}
67
68// association / disassociation
69
70// Associate pairs an allocated floating IP with an instance
71func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult {
72 var res AssociateResult
73
74 addFloatingIp := make(map[string]interface{})
75 addFloatingIp["address"] = fip
76 reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp}
77
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010078 _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil)
Joe Topjiandee32222015-02-09 23:56:26 +000079 return res
80}
81
82// Disassociate decouples an allocated floating IP from an instance
83func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult {
84 var res DisassociateResult
85
86 removeFloatingIp := make(map[string]interface{})
87 removeFloatingIp["address"] = fip
88 reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp}
89
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010090 _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil)
Joe Topjiandee32222015-02-09 23:56:26 +000091 return res
92}