blob: 82064629774135685756e11babd705473a7ac06d [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
Joe Topjiand97fe9b2015-09-17 02:08:38 +000029// AssociateOpts specifies the required information to associate or disassociate a floating IP to an instance
30type AssociateOpts struct {
31 // ServerID is the UUID of the server
32 ServerID string
33
34 // FixedIP is an optional fixed IP address of the server
35 FixedIP string
36
37 // FloatingIP is the floating IP to associate with an instance
38 FloatingIP string
39}
40
Joe Topjiandee32222015-02-09 23:56:26 +000041// ToFloatingIPCreateMap constructs a request body from CreateOpts.
42func (opts CreateOpts) ToFloatingIPCreateMap() (map[string]interface{}, error) {
43 if opts.Pool == "" {
44 return nil, errors.New("Missing field required for floating IP creation: Pool")
45 }
46
47 return map[string]interface{}{"pool": opts.Pool}, nil
48}
49
Joe Topjiand97fe9b2015-09-17 02:08:38 +000050// ToAssociateMap constructs a request body from AssociateOpts.
51func (opts AssociateOpts) ToAssociateMap() (map[string]interface{}, error) {
52 if opts.ServerID == "" {
53 return nil, errors.New("Required field missing for floating IP association: ServerID")
54 }
55
56 if opts.FloatingIP == "" {
57 return nil, errors.New("Required field missing for floating IP association: FloatingIP")
58 }
59
60 associateInfo := map[string]interface{}{
61 "serverId": opts.ServerID,
62 "floatingIp": opts.FloatingIP,
63 "fixedIp": opts.FixedIP,
64 }
65
66 return associateInfo, nil
67
68}
69
Joe Topjiandee32222015-02-09 23:56:26 +000070// Create requests the creation of a new floating IP
71func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
72 var res CreateResult
73
74 reqBody, err := opts.ToFloatingIPCreateMap()
75 if err != nil {
76 res.Err = err
77 return res
78 }
79
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010080 _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
81 OkCodes: []int{200},
Joe Topjiandee32222015-02-09 23:56:26 +000082 })
83 return res
84}
85
86// Get returns data about a previously created FloatingIP.
87func Get(client *gophercloud.ServiceClient, id string) GetResult {
88 var res GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010089 _, res.Err = client.Get(getURL(client, id), &res.Body, nil)
Joe Topjiandee32222015-02-09 23:56:26 +000090 return res
91}
92
93// Delete requests the deletion of a previous allocated FloatingIP.
94func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
95 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010096 _, res.Err = client.Delete(deleteURL(client, id), nil)
Joe Topjiandee32222015-02-09 23:56:26 +000097 return res
98}
99
100// association / disassociation
101
102// Associate pairs an allocated floating IP with an instance
Joe Topjian94e4cc52016-01-05 17:01:18 +0000103// Deprecated. Use AssociateInstance.
Joe Topjiandee32222015-02-09 23:56:26 +0000104func Associate(client *gophercloud.ServiceClient, serverId, fip string) AssociateResult {
105 var res AssociateResult
106
107 addFloatingIp := make(map[string]interface{})
108 addFloatingIp["address"] = fip
109 reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp}
110
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100111 _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil)
Joe Topjiandee32222015-02-09 23:56:26 +0000112 return res
113}
114
Joe Topjian94e4cc52016-01-05 17:01:18 +0000115// AssociateInstance pairs an allocated floating IP with an instance.
116func AssociateInstance(client *gophercloud.ServiceClient, opts AssociateOpts) AssociateResult {
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000117 var res AssociateResult
118
119 associateInfo, err := opts.ToAssociateMap()
120 if err != nil {
121 res.Err = err
122 return res
123 }
124
125 addFloatingIp := make(map[string]interface{})
126 addFloatingIp["address"] = associateInfo["floatingIp"].(string)
127
128 // fixedIp is not required
129 if associateInfo["fixedIp"] != "" {
130 addFloatingIp["fixed_address"] = associateInfo["fixedIp"].(string)
131 }
132
133 serverId := associateInfo["serverId"].(string)
134
135 reqBody := map[string]interface{}{"addFloatingIp": addFloatingIp}
136 _, res.Err = client.Post(associateURL(client, serverId), reqBody, nil, nil)
137 return res
138}
139
Joe Topjiandee32222015-02-09 23:56:26 +0000140// Disassociate decouples an allocated floating IP from an instance
Joe Topjian94e4cc52016-01-05 17:01:18 +0000141// Deprecated. Use DisassociateInstance.
Joe Topjiandee32222015-02-09 23:56:26 +0000142func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult {
143 var res DisassociateResult
144
145 removeFloatingIp := make(map[string]interface{})
146 removeFloatingIp["address"] = fip
147 reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp}
148
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100149 _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil)
Joe Topjiandee32222015-02-09 23:56:26 +0000150 return res
151}
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000152
Joe Topjian94e4cc52016-01-05 17:01:18 +0000153// DisassociateInstance decouples an allocated floating IP from an instance
154func DisassociateInstance(client *gophercloud.ServiceClient, opts AssociateOpts) DisassociateResult {
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000155 var res DisassociateResult
156
157 associateInfo, err := opts.ToAssociateMap()
158 if err != nil {
159 res.Err = err
160 return res
161 }
162
163 removeFloatingIp := make(map[string]interface{})
164 removeFloatingIp["address"] = associateInfo["floatingIp"].(string)
165 reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp}
166
167 serverId := associateInfo["serverId"].(string)
168
169 _, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil)
170 return res
171}