blob: 0b677f27fb16342dac10e5aa51861536c66984f6 [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
Ash Wilson2199f102015-02-12 16:16:09 -0500231 _, res.Err = c.Request("POST", rootURL(c), gophercloud.RequestOpts{
232 JSONBody: &reqBody,
233 JSONResponse: &res.Body,
234 OkCodes: []int{202},
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100235 })
236
237 return res
238}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100239
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100240// Get is the operation responsible for providing detailed information
241// regarding a specific load balancer which is configured and associated with
242// your account. This operation is not capable of returning details for a load
243// balancer which has been deleted.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100244func Get(c *gophercloud.ServiceClient, id int) GetResult {
245 var res GetResult
246
247 _, res.Err = perigee.Request("GET", resourceURL(c, id), perigee.Options{
248 MoreHeaders: c.AuthenticatedHeaders(),
249 Results: &res.Body,
250 OkCodes: []int{200},
251 })
252
253 return res
254}
255
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100256// BulkDelete removes all the load balancers referenced in the slice of IDs.
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100257// Any and all configuration data associated with these load balancers is
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100258// immediately purged and is not recoverable.
259//
260// If one of the items in the list cannot be removed due to its current status,
261// a 400 Bad Request error is returned along with the IDs of the ones the
262// system identified as potential failures for this request.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100263func BulkDelete(c *gophercloud.ServiceClient, ids []int) DeleteResult {
264 var res DeleteResult
265
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +0100266 if len(ids) > 10 || len(ids) == 0 {
267 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 LB IDs")
268 return res
269 }
270
Jamie Hannaford1c260332014-10-31 15:57:22 +0100271 url := rootURL(c)
Jamie Hannaford950561c2014-11-12 11:12:20 +0100272 url += gophercloud.IDSliceToQueryString("id", ids)
Jamie Hannaford1c260332014-10-31 15:57:22 +0100273
Ash Wilson2199f102015-02-12 16:16:09 -0500274 _, res.Err = c.Request("DELETE", url, gophercloud.RequestOpts{
275 OkCodes: []int{202},
Jamie Hannaford1c260332014-10-31 15:57:22 +0100276 })
277
278 return res
279}
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100280
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100281// Delete removes a single load balancer.
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100282func Delete(c *gophercloud.ServiceClient, id int) DeleteResult {
283 var res DeleteResult
284
285 _, res.Err = perigee.Request("DELETE", resourceURL(c, id), perigee.Options{
286 MoreHeaders: c.AuthenticatedHeaders(),
287 OkCodes: []int{202},
288 })
289
290 return res
291}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100292
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100293// UpdateOptsBuilder represents a type that can be converted into a JSON-like
294// map structure.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100295type UpdateOptsBuilder interface {
296 ToLBUpdateMap() (map[string]interface{}, error)
297}
298
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100299// UpdateOpts represents the options for updating an existing load balancer.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100300type UpdateOpts struct {
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100301 // Optional - new name of the load balancer.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100302 Name string
303
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100304 // Optional - the new protocol you want your load balancer to have.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100305 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
306 // for a full list of supported protocols.
307 Protocol string
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100308
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100309 // Optional - see the HalfClosed field in CreateOpts for more information.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100310 HalfClosed gophercloud.EnabledState
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100311
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100312 // Optional - see the Algorithm field in CreateOpts for more information.
Jamie Hannaford46336282014-11-04 14:48:20 +0100313 Algorithm string
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100314
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100315 // Optional - see the Port field in CreateOpts for more information.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100316 Port int
317
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100318 // Optional - see the Timeout field in CreateOpts for more information.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100319 Timeout int
320
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100321 // Optional - see the HTTPSRedirect field in CreateOpts for more information.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100322 HTTPSRedirect gophercloud.EnabledState
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100323}
324
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100325// ToLBUpdateMap casts an UpdateOpts struct to a map.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100326func (opts UpdateOpts) ToLBUpdateMap() (map[string]interface{}, error) {
327 lb := make(map[string]interface{})
328
329 if opts.Name != "" {
330 lb["name"] = opts.Name
331 }
332 if opts.Protocol != "" {
333 lb["protocol"] = opts.Protocol
334 }
335 if opts.HalfClosed != nil {
336 lb["halfClosed"] = opts.HalfClosed
337 }
338 if opts.Algorithm != "" {
339 lb["algorithm"] = opts.Algorithm
340 }
341 if opts.Port > 0 {
342 lb["port"] = opts.Port
343 }
344 if opts.Timeout > 0 {
345 lb["timeout"] = opts.Timeout
346 }
347 if opts.HTTPSRedirect != nil {
348 lb["httpsRedirect"] = &opts.HTTPSRedirect
349 }
350
351 return map[string]interface{}{"loadBalancer": lb}, nil
352}
353
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100354// Update is the operation responsible for asynchronously updating the
355// attributes of a specific load balancer. Upon successful validation of the
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100356// request, the service returns a 202 Accepted response, and the load balancer
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100357// enters a PENDING_UPDATE state. A user can poll the load balancer with Get to
358// wait for the changes to be applied. When this happens, the load balancer will
359// return to an ACTIVE state.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100360func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult {
361 var res UpdateResult
362
363 reqBody, err := opts.ToLBUpdateMap()
364 if err != nil {
365 res.Err = err
366 return res
367 }
368
369 _, res.Err = perigee.Request("PUT", resourceURL(c, id), perigee.Options{
370 MoreHeaders: c.AuthenticatedHeaders(),
371 ReqBody: &reqBody,
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100372 OkCodes: []int{202},
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100373 })
374
375 return res
376}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100377
378// ListProtocols is the operation responsible for returning a paginated
379// collection of load balancer protocols.
380func ListProtocols(client *gophercloud.ServiceClient) pagination.Pager {
381 url := protocolsURL(client)
382 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
383 return ProtocolPage{pagination.SinglePageBase(r)}
384 })
385}
Jamie Hannaford46336282014-11-04 14:48:20 +0100386
387// ListAlgorithms is the operation responsible for returning a paginated
388// collection of load balancer algorithms.
389func ListAlgorithms(client *gophercloud.ServiceClient) pagination.Pager {
390 url := algorithmsURL(client)
391 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
392 return AlgorithmPage{pagination.SinglePageBase(r)}
393 })
394}
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100395
396// IsLoggingEnabled returns true if the load balancer has connection logging
397// enabled and false if not.
398func IsLoggingEnabled(client *gophercloud.ServiceClient, id int) (bool, error) {
399 var body interface{}
400
401 _, err := perigee.Request("GET", loggingURL(client, id), perigee.Options{
402 MoreHeaders: client.AuthenticatedHeaders(),
403 Results: &body,
404 OkCodes: []int{200},
405 })
406 if err != nil {
407 return false, err
408 }
409
410 var resp struct {
411 CL struct {
412 Enabled bool `mapstructure:"enabled"`
413 } `mapstructure:"connectionLogging"`
414 }
415
416 err = mapstructure.Decode(body, &resp)
417 return resp.CL.Enabled, err
418}
419
420func toConnLoggingMap(state bool) map[string]map[string]bool {
421 return map[string]map[string]bool{
Jamie Hannaford20b75882014-11-10 13:39:51 +0100422 "connectionLogging": map[string]bool{"enabled": state},
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100423 }
424}
425
426// EnableLogging will enable connection logging for a specified load balancer.
427func EnableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
428 reqBody := toConnLoggingMap(true)
429 var res gophercloud.ErrResult
430
Jamie Hannaford20b75882014-11-10 13:39:51 +0100431 _, res.Err = perigee.Request("PUT", loggingURL(client, id), perigee.Options{
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100432 MoreHeaders: client.AuthenticatedHeaders(),
433 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100434 OkCodes: []int{202},
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100435 })
436
437 return res
438}
439
440// DisableLogging will disable connection logging for a specified load balancer.
441func DisableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
442 reqBody := toConnLoggingMap(false)
443 var res gophercloud.ErrResult
444
Jamie Hannaford20b75882014-11-10 13:39:51 +0100445 _, res.Err = perigee.Request("PUT", loggingURL(client, id), perigee.Options{
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100446 MoreHeaders: client.AuthenticatedHeaders(),
447 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100448 OkCodes: []int{202},
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100449 })
450
451 return res
452}
Jamie Hannafordda45b422014-11-10 11:00:38 +0100453
Jamie Hannaford3da65282014-11-10 11:36:16 +0100454// GetErrorPage will retrieve the current error page for the load balancer.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100455func GetErrorPage(client *gophercloud.ServiceClient, id int) ErrorPageResult {
456 var res ErrorPageResult
457
458 _, res.Err = perigee.Request("GET", errorPageURL(client, id), perigee.Options{
459 MoreHeaders: client.AuthenticatedHeaders(),
460 Results: &res.Body,
461 OkCodes: []int{200},
462 })
463
464 return res
465}
466
Jamie Hannaford3da65282014-11-10 11:36:16 +0100467// SetErrorPage will set the HTML of the load balancer's error page to a
468// specific value.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100469func SetErrorPage(client *gophercloud.ServiceClient, id int, html string) ErrorPageResult {
470 var res ErrorPageResult
471
472 type stringMap map[string]string
473 reqBody := map[string]stringMap{"errorpage": stringMap{"content": html}}
474
475 _, res.Err = perigee.Request("PUT", errorPageURL(client, id), perigee.Options{
476 MoreHeaders: client.AuthenticatedHeaders(),
477 Results: &res.Body,
478 ReqBody: &reqBody,
479 OkCodes: []int{200},
480 })
481
482 return res
483}
484
Jamie Hannaford3da65282014-11-10 11:36:16 +0100485// DeleteErrorPage will delete the current error page for the load balancer.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100486func DeleteErrorPage(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
487 var res gophercloud.ErrResult
488
489 _, res.Err = perigee.Request("DELETE", errorPageURL(client, id), perigee.Options{
490 MoreHeaders: client.AuthenticatedHeaders(),
491 OkCodes: []int{200},
492 })
493
494 return res
495}
Jamie Hannaford3da65282014-11-10 11:36:16 +0100496
497// GetStats will retrieve detailed stats related to the load balancer's usage.
498func GetStats(client *gophercloud.ServiceClient, id int) StatsResult {
499 var res StatsResult
500
501 _, res.Err = perigee.Request("GET", statsURL(client, id), perigee.Options{
502 MoreHeaders: client.AuthenticatedHeaders(),
503 Results: &res.Body,
504 OkCodes: []int{200},
505 })
506
507 return res
508}
Jamie Hannaford20b75882014-11-10 13:39:51 +0100509
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100510// IsContentCached will check to see whether the specified load balancer caches
511// content. When content caching is enabled, recently-accessed files are stored
512// on the load balancer for easy retrieval by web clients. Content caching
513// improves the performance of high traffic web sites by temporarily storing
514// data that was recently accessed. While it's cached, requests for that data
Jamie Hannaford227d9592014-11-13 10:32:07 +0100515// are served by the load balancer, which in turn reduces load off the back-end
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100516// nodes. The result is improved response times for those requests and less
517// load on the web server.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100518func IsContentCached(client *gophercloud.ServiceClient, id int) (bool, error) {
519 var body interface{}
520
521 _, err := perigee.Request("GET", cacheURL(client, id), perigee.Options{
522 MoreHeaders: client.AuthenticatedHeaders(),
523 Results: &body,
524 OkCodes: []int{200},
525 })
526 if err != nil {
527 return false, err
528 }
529
530 var resp struct {
531 CC struct {
532 Enabled bool `mapstructure:"enabled"`
533 } `mapstructure:"contentCaching"`
534 }
535
536 err = mapstructure.Decode(body, &resp)
537 return resp.CC.Enabled, err
538}
539
540func toCachingMap(state bool) map[string]map[string]bool {
541 return map[string]map[string]bool{
542 "contentCaching": map[string]bool{"enabled": state},
543 }
544}
545
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100546// EnableCaching will enable content-caching for the specified load balancer.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100547func EnableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
548 reqBody := toCachingMap(true)
549 var res gophercloud.ErrResult
550
551 _, res.Err = perigee.Request("PUT", cacheURL(client, id), perigee.Options{
552 MoreHeaders: client.AuthenticatedHeaders(),
553 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100554 OkCodes: []int{202},
Jamie Hannaford20b75882014-11-10 13:39:51 +0100555 })
556
557 return res
558}
559
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100560// DisableCaching will disable content-caching for the specified load balancer.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100561func DisableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
562 reqBody := toCachingMap(false)
563 var res gophercloud.ErrResult
564
565 _, res.Err = perigee.Request("PUT", cacheURL(client, id), perigee.Options{
566 MoreHeaders: client.AuthenticatedHeaders(),
567 ReqBody: &reqBody,
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100568 OkCodes: []int{202},
Jamie Hannaford20b75882014-11-10 13:39:51 +0100569 })
570
571 return res
572}