blob: 342f10790ec2697c6d6345f1c6afc93447ce951d [file] [log] [blame]
Jamie Hannafordfba65af2014-11-03 10:32:37 +01001package lbs
Jamie Hannaford186d4e22014-10-31 12:26:11 +01002
3import (
Jamie Hannaforde09b6822014-10-31 15:33:57 +01004 "errors"
5
Jamie Hannafordd78bb352014-11-07 16:36:09 +01006 "github.com/mitchellh/mapstructure"
Jamie Hannaforde09b6822014-10-31 15:33:57 +01007 "github.com/racker/perigee"
8
Jamie Hannaford186d4e22014-10-31 12:26:11 +01009 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010011 "github.com/rackspace/gophercloud/rackspace/lb/v1/acl"
12 "github.com/rackspace/gophercloud/rackspace/lb/v1/monitors"
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +010013 "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +010014 "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010015 "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle"
Jamie Hannaford1c817312014-11-04 10:56:58 +010016 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010017)
18
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010019var (
20 errNameRequired = errors.New("Name is a required attribute")
21 errTimeoutExceeded = errors.New("Timeout must be less than 120")
22)
23
Jamie Hannaford186d4e22014-10-31 12:26:11 +010024// ListOptsBuilder allows extensions to add additional parameters to the
25// List request.
26type ListOptsBuilder interface {
27 ToLBListQuery() (string, error)
28}
29
30// ListOpts allows the filtering and sorting of paginated collections through
31// the API.
32type ListOpts struct {
33 ChangesSince string `q:"changes-since"`
34 Status Status `q:"status"`
35 NodeAddr string `q:"nodeaddress"`
36 Marker string `q:"marker"`
37 Limit int `q:"limit"`
38}
39
40// ToLBListQuery formats a ListOpts into a query string.
41func (opts ListOpts) ToLBListQuery() (string, error) {
42 q, err := gophercloud.BuildQueryString(opts)
43 if err != nil {
44 return "", err
45 }
46 return q.String(), nil
47}
48
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010049// List is the operation responsible for returning a paginated collection of
50// load balancers. You may pass in a ListOpts struct to filter results.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010051func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
52 url := rootURL(client)
53 if opts != nil {
54 query, err := opts.ToLBListQuery()
55 if err != nil {
56 return pagination.Pager{Err: err}
57 }
58 url += query
59 }
60
61 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
62 return LBPage{pagination.LinkedPageBase{PageResult: r}}
63 })
64}
Jamie Hannaforde09b6822014-10-31 15:33:57 +010065
Jamie Hannaforde09b6822014-10-31 15:33:57 +010066// CreateOptsBuilder is the interface options structs have to satisfy in order
67// to be used in the main Create operation in this package. Since many
68// extensions decorate or modify the common logic, it is useful for them to
69// satisfy a basic interface in order for them to be used.
70type CreateOptsBuilder interface {
71 ToLBCreateMap() (map[string]interface{}, error)
72}
73
74// CreateOpts is the common options struct used in this package's Create
75// operation.
76type CreateOpts struct {
77 // Required - name of the load balancer to create. The name must be 128
78 // characters or fewer in length, and all UTF-8 characters are valid.
79 Name string
80
81 // Optional - nodes to be added.
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +010082 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +010083
84 // Required - protocol of the service that is being load balanced.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010085 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
86 // for a full list of supported protocols.
87 Protocol string
Jamie Hannaforde09b6822014-10-31 15:33:57 +010088
89 // Optional - enables or disables Half-Closed support for the load balancer.
90 // Half-Closed support provides the ability for one end of the connection to
91 // terminate its output, while still receiving data from the other end. Only
92 // available for TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010093 HalfClosed gophercloud.EnabledState
Jamie Hannaforde09b6822014-10-31 15:33:57 +010094
95 // Optional - the type of virtual IPs you want associated with the load
96 // balancer.
Jamie Hannaford1c817312014-11-04 10:56:58 +010097 VIPs []vips.VIP
Jamie Hannaforde09b6822014-10-31 15:33:57 +010098
99 // Optional - the access list management feature allows fine-grained network
100 // access controls to be applied to the load balancer virtual IP address.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100101 AccessList *acl.AccessList
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100102
103 // Optional - algorithm that defines how traffic should be directed between
104 // back-end nodes.
Jamie Hannaford46336282014-11-04 14:48:20 +0100105 Algorithm string
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100106
107 // Optional - current connection logging configuration.
108 ConnectionLogging *ConnectionLogging
109
110 // Optional - specifies a limit on the number of connections per IP address
111 // to help mitigate malicious or abusive traffic to your applications.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100112 ConnThrottle *throttle.ConnectionThrottle
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100113
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100114 // Optional - the type of health monitor check to perform to ensure that the
115 // service is performing properly.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100116 HealthMonitor *monitors.Monitor
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100117
118 // Optional - arbitrary information that can be associated with each LB.
119 Metadata map[string]interface{}
120
121 // Optional - port number for the service you are load balancing.
122 Port int
123
124 // Optional - the timeout value for the load balancer and communications with
125 // its nodes. Defaults to 30 seconds with a maximum of 120 seconds.
126 Timeout int
127
128 // Optional - specifies whether multiple requests from clients are directed
129 // to the same node.
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +0100130 SessionPersistence *sessions.SessionPersistence
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100131
132 // Optional - enables or disables HTTP to HTTPS redirection for the load
133 // balancer. When enabled, any HTTP request returns status code 301 (Moved
134 // Permanently), and the requester is redirected to the requested URL via the
135 // HTTPS protocol on port 443. For example, http://example.com/page.html
136 // would be redirected to https://example.com/page.html. Only available for
137 // HTTPS protocol (port=443), or HTTP protocol with a properly configured SSL
138 // termination (secureTrafficOnly=true, securePort=443).
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100139 HTTPSRedirect gophercloud.EnabledState
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100140}
141
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100142// ToLBCreateMap casts a CreateOpts struct to a map.
143func (opts CreateOpts) ToLBCreateMap() (map[string]interface{}, error) {
144 lb := make(map[string]interface{})
145
146 if opts.Name == "" {
147 return lb, errNameRequired
148 }
149 if opts.Timeout > 120 {
150 return lb, errTimeoutExceeded
151 }
152
153 lb["name"] = opts.Name
154
155 if len(opts.Nodes) > 0 {
156 nodes := []map[string]interface{}{}
157 for _, n := range opts.Nodes {
158 nodes = append(nodes, map[string]interface{}{
159 "address": n.Address,
160 "port": n.Port,
161 "condition": n.Condition,
162 })
163 }
164 lb["nodes"] = nodes
165 }
166
167 if opts.Protocol != "" {
168 lb["protocol"] = opts.Protocol
169 }
170 if opts.HalfClosed != nil {
171 lb["halfClosed"] = opts.HalfClosed
172 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100173 if len(opts.VIPs) > 0 {
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100174 lb["virtualIps"] = opts.VIPs
175 }
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100176 if opts.AccessList != nil {
177 lb["accessList"] = &opts.AccessList
178 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100179 if opts.Algorithm != "" {
180 lb["algorithm"] = opts.Algorithm
181 }
182 if opts.ConnectionLogging != nil {
183 lb["connectionLogging"] = &opts.ConnectionLogging
184 }
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100185 if opts.ConnThrottle != nil {
186 lb["connectionThrottle"] = &opts.ConnThrottle
187 }
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100188 if opts.HealthMonitor != nil {
189 lb["healthMonitor"] = &opts.HealthMonitor
190 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100191 if len(opts.Metadata) != 0 {
192 lb["metadata"] = opts.Metadata
193 }
194 if opts.Port > 0 {
195 lb["port"] = opts.Port
196 }
197 if opts.Timeout > 0 {
198 lb["timeout"] = opts.Timeout
199 }
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100200 if opts.SessionPersistence != nil {
201 lb["sessionPersistence"] = &opts.SessionPersistence
202 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100203 if opts.HTTPSRedirect != nil {
204 lb["httpsRedirect"] = &opts.HTTPSRedirect
205 }
206
207 return map[string]interface{}{"loadBalancer": lb}, nil
208}
209
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100210// Create is the operation responsible for asynchronously provisioning a new
211// load balancer based on the configuration defined in CreateOpts. Once the
212// request is validated and progress has started on the provisioning process, a
213// response struct is returned. When extracted (with Extract()), you have
214// to the load balancer's unique ID and status.
215//
216// Once an ID is attained, you can check on the progress of the operation by
217// calling Get and passing in the ID. If the corresponding request cannot be
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100218// fulfilled due to insufficient or invalid data, an HTTP 400 (Bad Request)
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100219// error response is returned with information regarding the nature of the
220// failure in the body of the response. Failures in the validation process are
221// non-recoverable and require the caller to correct the cause of the failure.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100222func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
223 var res CreateResult
224
225 reqBody, err := opts.ToLBCreateMap()
226 if err != nil {
227 res.Err = err
228 return res
229 }
230
231 _, res.Err = perigee.Request("POST", rootURL(c), perigee.Options{
232 MoreHeaders: c.AuthenticatedHeaders(),
233 ReqBody: &reqBody,
234 Results: &res.Body,
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100235 OkCodes: []int{202},
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100236 })
237
238 return res
239}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100240
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100241// Get is the operation responsible for providing detailed information
242// regarding a specific load balancer which is configured and associated with
243// your account. This operation is not capable of returning details for a load
244// balancer which has been deleted.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100245func Get(c *gophercloud.ServiceClient, id int) GetResult {
246 var res GetResult
247
248 _, res.Err = perigee.Request("GET", resourceURL(c, id), perigee.Options{
249 MoreHeaders: c.AuthenticatedHeaders(),
250 Results: &res.Body,
251 OkCodes: []int{200},
252 })
253
254 return res
255}
256
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100257// BulkDelete removes all the load balancers referenced in the slice of IDs.
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100258// Any and all configuration data associated with these load balancers is
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100259// immediately purged and is not recoverable.
260//
261// If one of the items in the list cannot be removed due to its current status,
262// a 400 Bad Request error is returned along with the IDs of the ones the
263// system identified as potential failures for this request.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100264func BulkDelete(c *gophercloud.ServiceClient, ids []int) DeleteResult {
265 var res DeleteResult
266
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +0100267 if len(ids) > 10 || len(ids) == 0 {
268 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 LB IDs")
269 return res
270 }
271
Jamie Hannaford1c260332014-10-31 15:57:22 +0100272 url := rootURL(c)
Jamie Hannaford950561c2014-11-12 11:12:20 +0100273 url += gophercloud.IDSliceToQueryString("id", ids)
Jamie Hannaford1c260332014-10-31 15:57:22 +0100274
275 _, res.Err = perigee.Request("DELETE", url, perigee.Options{
276 MoreHeaders: c.AuthenticatedHeaders(),
277 OkCodes: []int{202},
278 })
279
280 return res
281}
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100282
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100283// Delete removes a single load balancer.
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100284func Delete(c *gophercloud.ServiceClient, id int) DeleteResult {
285 var res DeleteResult
286
287 _, res.Err = perigee.Request("DELETE", resourceURL(c, id), perigee.Options{
288 MoreHeaders: c.AuthenticatedHeaders(),
289 OkCodes: []int{202},
290 })
291
292 return res
293}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100294
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100295// UpdateOptsBuilder represents a type that can be converted into a JSON-like
296// map structure.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100297type UpdateOptsBuilder interface {
298 ToLBUpdateMap() (map[string]interface{}, error)
299}
300
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100301// UpdateOpts represents the options for updating an existing load balancer.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100302type UpdateOpts struct {
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100303 // Optional - new name of the load balancer.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100304 Name string
305
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100306 // Optional - the new protocol you want your load balancer to have.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100307 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
308 // for a full list of supported protocols.
309 Protocol string
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100310
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100311 // Optional - see the HalfClosed field in CreateOpts for more information.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100312 HalfClosed gophercloud.EnabledState
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100313
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100314 // Optional - see the Algorithm field in CreateOpts for more information.
Jamie Hannaford46336282014-11-04 14:48:20 +0100315 Algorithm string
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100316
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100317 // Optional - see the Port field in CreateOpts for more information.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100318 Port int
319
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100320 // Optional - see the Timeout field in CreateOpts for more information.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100321 Timeout int
322
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100323 // Optional - see the HTTPSRedirect field in CreateOpts for more information.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100324 HTTPSRedirect gophercloud.EnabledState
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100325}
326
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100327// ToLBUpdateMap casts an UpdateOpts struct to a map.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100328func (opts UpdateOpts) ToLBUpdateMap() (map[string]interface{}, error) {
329 lb := make(map[string]interface{})
330
331 if opts.Name != "" {
332 lb["name"] = opts.Name
333 }
334 if opts.Protocol != "" {
335 lb["protocol"] = opts.Protocol
336 }
337 if opts.HalfClosed != nil {
338 lb["halfClosed"] = opts.HalfClosed
339 }
340 if opts.Algorithm != "" {
341 lb["algorithm"] = opts.Algorithm
342 }
343 if opts.Port > 0 {
344 lb["port"] = opts.Port
345 }
346 if opts.Timeout > 0 {
347 lb["timeout"] = opts.Timeout
348 }
349 if opts.HTTPSRedirect != nil {
350 lb["httpsRedirect"] = &opts.HTTPSRedirect
351 }
352
353 return map[string]interface{}{"loadBalancer": lb}, nil
354}
355
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100356// Update is the operation responsible for asynchronously updating the
357// attributes of a specific load balancer. Upon successful validation of the
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100358// request, the service returns a 202 Accepted response, and the load balancer
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100359// enters a PENDING_UPDATE state. A user can poll the load balancer with Get to
360// wait for the changes to be applied. When this happens, the load balancer will
361// return to an ACTIVE state.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100362func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult {
363 var res UpdateResult
364
365 reqBody, err := opts.ToLBUpdateMap()
366 if err != nil {
367 res.Err = err
368 return res
369 }
370
371 _, res.Err = perigee.Request("PUT", resourceURL(c, id), perigee.Options{
372 MoreHeaders: c.AuthenticatedHeaders(),
373 ReqBody: &reqBody,
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100374 OkCodes: []int{202},
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100375 })
376
377 return res
378}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100379
380// ListProtocols is the operation responsible for returning a paginated
381// collection of load balancer protocols.
382func ListProtocols(client *gophercloud.ServiceClient) pagination.Pager {
383 url := protocolsURL(client)
384 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
385 return ProtocolPage{pagination.SinglePageBase(r)}
386 })
387}
Jamie Hannaford46336282014-11-04 14:48:20 +0100388
389// ListAlgorithms is the operation responsible for returning a paginated
390// collection of load balancer algorithms.
391func ListAlgorithms(client *gophercloud.ServiceClient) pagination.Pager {
392 url := algorithmsURL(client)
393 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
394 return AlgorithmPage{pagination.SinglePageBase(r)}
395 })
396}
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100397
398// IsLoggingEnabled returns true if the load balancer has connection logging
399// enabled and false if not.
400func IsLoggingEnabled(client *gophercloud.ServiceClient, id int) (bool, error) {
401 var body interface{}
402
403 _, err := perigee.Request("GET", loggingURL(client, id), perigee.Options{
404 MoreHeaders: client.AuthenticatedHeaders(),
405 Results: &body,
406 OkCodes: []int{200},
407 })
408 if err != nil {
409 return false, err
410 }
411
412 var resp struct {
413 CL struct {
414 Enabled bool `mapstructure:"enabled"`
415 } `mapstructure:"connectionLogging"`
416 }
417
418 err = mapstructure.Decode(body, &resp)
419 return resp.CL.Enabled, err
420}
421
422func toConnLoggingMap(state bool) map[string]map[string]bool {
423 return map[string]map[string]bool{
Jamie Hannaford20b75882014-11-10 13:39:51 +0100424 "connectionLogging": map[string]bool{"enabled": state},
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100425 }
426}
427
428// EnableLogging will enable connection logging for a specified load balancer.
429func EnableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
430 reqBody := toConnLoggingMap(true)
431 var res gophercloud.ErrResult
432
Jamie Hannaford20b75882014-11-10 13:39:51 +0100433 _, res.Err = perigee.Request("PUT", loggingURL(client, id), perigee.Options{
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100434 MoreHeaders: client.AuthenticatedHeaders(),
435 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100436 OkCodes: []int{202},
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100437 })
438
439 return res
440}
441
442// DisableLogging will disable connection logging for a specified load balancer.
443func DisableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
444 reqBody := toConnLoggingMap(false)
445 var res gophercloud.ErrResult
446
Jamie Hannaford20b75882014-11-10 13:39:51 +0100447 _, res.Err = perigee.Request("PUT", loggingURL(client, id), perigee.Options{
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100448 MoreHeaders: client.AuthenticatedHeaders(),
449 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100450 OkCodes: []int{202},
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100451 })
452
453 return res
454}
Jamie Hannafordda45b422014-11-10 11:00:38 +0100455
Jamie Hannaford3da65282014-11-10 11:36:16 +0100456// GetErrorPage will retrieve the current error page for the load balancer.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100457func GetErrorPage(client *gophercloud.ServiceClient, id int) ErrorPageResult {
458 var res ErrorPageResult
459
460 _, res.Err = perigee.Request("GET", errorPageURL(client, id), perigee.Options{
461 MoreHeaders: client.AuthenticatedHeaders(),
462 Results: &res.Body,
463 OkCodes: []int{200},
464 })
465
466 return res
467}
468
Jamie Hannaford3da65282014-11-10 11:36:16 +0100469// SetErrorPage will set the HTML of the load balancer's error page to a
470// specific value.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100471func SetErrorPage(client *gophercloud.ServiceClient, id int, html string) ErrorPageResult {
472 var res ErrorPageResult
473
474 type stringMap map[string]string
475 reqBody := map[string]stringMap{"errorpage": stringMap{"content": html}}
476
477 _, res.Err = perigee.Request("PUT", errorPageURL(client, id), perigee.Options{
478 MoreHeaders: client.AuthenticatedHeaders(),
479 Results: &res.Body,
480 ReqBody: &reqBody,
481 OkCodes: []int{200},
482 })
483
484 return res
485}
486
Jamie Hannaford3da65282014-11-10 11:36:16 +0100487// DeleteErrorPage will delete the current error page for the load balancer.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100488func DeleteErrorPage(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
489 var res gophercloud.ErrResult
490
491 _, res.Err = perigee.Request("DELETE", errorPageURL(client, id), perigee.Options{
492 MoreHeaders: client.AuthenticatedHeaders(),
493 OkCodes: []int{200},
494 })
495
496 return res
497}
Jamie Hannaford3da65282014-11-10 11:36:16 +0100498
499// GetStats will retrieve detailed stats related to the load balancer's usage.
500func GetStats(client *gophercloud.ServiceClient, id int) StatsResult {
501 var res StatsResult
502
503 _, res.Err = perigee.Request("GET", statsURL(client, id), perigee.Options{
504 MoreHeaders: client.AuthenticatedHeaders(),
505 Results: &res.Body,
506 OkCodes: []int{200},
507 })
508
509 return res
510}
Jamie Hannaford20b75882014-11-10 13:39:51 +0100511
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100512// IsContentCached will check to see whether the specified load balancer caches
513// content. When content caching is enabled, recently-accessed files are stored
514// on the load balancer for easy retrieval by web clients. Content caching
515// improves the performance of high traffic web sites by temporarily storing
516// data that was recently accessed. While it's cached, requests for that data
Jamie Hannaford227d9592014-11-13 10:32:07 +0100517// are served by the load balancer, which in turn reduces load off the back-end
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100518// nodes. The result is improved response times for those requests and less
519// load on the web server.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100520func IsContentCached(client *gophercloud.ServiceClient, id int) (bool, error) {
521 var body interface{}
522
523 _, err := perigee.Request("GET", cacheURL(client, id), perigee.Options{
524 MoreHeaders: client.AuthenticatedHeaders(),
525 Results: &body,
526 OkCodes: []int{200},
527 })
528 if err != nil {
529 return false, err
530 }
531
532 var resp struct {
533 CC struct {
534 Enabled bool `mapstructure:"enabled"`
535 } `mapstructure:"contentCaching"`
536 }
537
538 err = mapstructure.Decode(body, &resp)
539 return resp.CC.Enabled, err
540}
541
542func toCachingMap(state bool) map[string]map[string]bool {
543 return map[string]map[string]bool{
544 "contentCaching": map[string]bool{"enabled": state},
545 }
546}
547
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100548// EnableCaching will enable content-caching for the specified load balancer.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100549func EnableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
550 reqBody := toCachingMap(true)
551 var res gophercloud.ErrResult
552
553 _, res.Err = perigee.Request("PUT", cacheURL(client, id), perigee.Options{
554 MoreHeaders: client.AuthenticatedHeaders(),
555 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100556 OkCodes: []int{202},
Jamie Hannaford20b75882014-11-10 13:39:51 +0100557 })
558
559 return res
560}
561
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100562// DisableCaching will disable content-caching for the specified load balancer.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100563func DisableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
564 reqBody := toCachingMap(false)
565 var res gophercloud.ErrResult
566
567 _, res.Err = perigee.Request("PUT", cacheURL(client, id), perigee.Options{
568 MoreHeaders: client.AuthenticatedHeaders(),
569 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100570 OkCodes: []int{202},
Jamie Hannaford20b75882014-11-10 13:39:51 +0100571 })
572
573 return res
574}