blob: 46f5f02a43a6a480a2acb68f3afb882fc8e3ed7d [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
Jamie Hannaford186d4e22014-10-31 12:26:11 +01008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010010 "github.com/rackspace/gophercloud/rackspace/lb/v1/acl"
11 "github.com/rackspace/gophercloud/rackspace/lb/v1/monitors"
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +010012 "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +010013 "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010014 "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle"
Jamie Hannaford1c817312014-11-04 10:56:58 +010015 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010016)
17
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010018var (
19 errNameRequired = errors.New("Name is a required attribute")
20 errTimeoutExceeded = errors.New("Timeout must be less than 120")
21)
22
Jamie Hannaford186d4e22014-10-31 12:26:11 +010023// ListOptsBuilder allows extensions to add additional parameters to the
24// List request.
25type ListOptsBuilder interface {
26 ToLBListQuery() (string, error)
27}
28
29// ListOpts allows the filtering and sorting of paginated collections through
30// the API.
31type ListOpts struct {
32 ChangesSince string `q:"changes-since"`
33 Status Status `q:"status"`
34 NodeAddr string `q:"nodeaddress"`
35 Marker string `q:"marker"`
36 Limit int `q:"limit"`
37}
38
39// ToLBListQuery formats a ListOpts into a query string.
40func (opts ListOpts) ToLBListQuery() (string, error) {
41 q, err := gophercloud.BuildQueryString(opts)
42 if err != nil {
43 return "", err
44 }
45 return q.String(), nil
46}
47
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010048// List is the operation responsible for returning a paginated collection of
49// load balancers. You may pass in a ListOpts struct to filter results.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010050func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
51 url := rootURL(client)
52 if opts != nil {
53 query, err := opts.ToLBListQuery()
54 if err != nil {
55 return pagination.Pager{Err: err}
56 }
57 url += query
58 }
59
60 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
61 return LBPage{pagination.LinkedPageBase{PageResult: r}}
62 })
63}
Jamie Hannaforde09b6822014-10-31 15:33:57 +010064
Jamie Hannaforde09b6822014-10-31 15:33:57 +010065// CreateOptsBuilder is the interface options structs have to satisfy in order
66// to be used in the main Create operation in this package. Since many
67// extensions decorate or modify the common logic, it is useful for them to
68// satisfy a basic interface in order for them to be used.
69type CreateOptsBuilder interface {
70 ToLBCreateMap() (map[string]interface{}, error)
71}
72
73// CreateOpts is the common options struct used in this package's Create
74// operation.
75type CreateOpts struct {
76 // Required - name of the load balancer to create. The name must be 128
77 // characters or fewer in length, and all UTF-8 characters are valid.
78 Name string
79
80 // Optional - nodes to be added.
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +010081 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +010082
83 // Required - protocol of the service that is being load balanced.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010084 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
85 // for a full list of supported protocols.
86 Protocol string
Jamie Hannaforde09b6822014-10-31 15:33:57 +010087
88 // Optional - enables or disables Half-Closed support for the load balancer.
89 // Half-Closed support provides the ability for one end of the connection to
90 // terminate its output, while still receiving data from the other end. Only
91 // available for TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010092 HalfClosed gophercloud.EnabledState
Jamie Hannaforde09b6822014-10-31 15:33:57 +010093
94 // Optional - the type of virtual IPs you want associated with the load
95 // balancer.
Jamie Hannaford1c817312014-11-04 10:56:58 +010096 VIPs []vips.VIP
Jamie Hannaforde09b6822014-10-31 15:33:57 +010097
98 // Optional - the access list management feature allows fine-grained network
99 // access controls to be applied to the load balancer virtual IP address.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100100 AccessList *acl.AccessList
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100101
102 // Optional - algorithm that defines how traffic should be directed between
103 // back-end nodes.
Jamie Hannaford46336282014-11-04 14:48:20 +0100104 Algorithm string
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100105
106 // Optional - current connection logging configuration.
107 ConnectionLogging *ConnectionLogging
108
109 // Optional - specifies a limit on the number of connections per IP address
110 // to help mitigate malicious or abusive traffic to your applications.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100111 ConnThrottle *throttle.ConnectionThrottle
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100112
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100113 // Optional - the type of health monitor check to perform to ensure that the
114 // service is performing properly.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100115 HealthMonitor *monitors.Monitor
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100116
117 // Optional - arbitrary information that can be associated with each LB.
118 Metadata map[string]interface{}
119
120 // Optional - port number for the service you are load balancing.
121 Port int
122
123 // Optional - the timeout value for the load balancer and communications with
124 // its nodes. Defaults to 30 seconds with a maximum of 120 seconds.
125 Timeout int
126
127 // Optional - specifies whether multiple requests from clients are directed
128 // to the same node.
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +0100129 SessionPersistence *sessions.SessionPersistence
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100130
131 // Optional - enables or disables HTTP to HTTPS redirection for the load
132 // balancer. When enabled, any HTTP request returns status code 301 (Moved
133 // Permanently), and the requester is redirected to the requested URL via the
134 // HTTPS protocol on port 443. For example, http://example.com/page.html
135 // would be redirected to https://example.com/page.html. Only available for
136 // HTTPS protocol (port=443), or HTTP protocol with a properly configured SSL
137 // termination (secureTrafficOnly=true, securePort=443).
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100138 HTTPSRedirect gophercloud.EnabledState
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100139}
140
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100141// ToLBCreateMap casts a CreateOpts struct to a map.
142func (opts CreateOpts) ToLBCreateMap() (map[string]interface{}, error) {
143 lb := make(map[string]interface{})
144
145 if opts.Name == "" {
146 return lb, errNameRequired
147 }
148 if opts.Timeout > 120 {
149 return lb, errTimeoutExceeded
150 }
151
152 lb["name"] = opts.Name
153
154 if len(opts.Nodes) > 0 {
155 nodes := []map[string]interface{}{}
156 for _, n := range opts.Nodes {
157 nodes = append(nodes, map[string]interface{}{
158 "address": n.Address,
159 "port": n.Port,
160 "condition": n.Condition,
161 })
162 }
163 lb["nodes"] = nodes
164 }
165
166 if opts.Protocol != "" {
167 lb["protocol"] = opts.Protocol
168 }
169 if opts.HalfClosed != nil {
170 lb["halfClosed"] = opts.HalfClosed
171 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100172 if len(opts.VIPs) > 0 {
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100173 lb["virtualIps"] = opts.VIPs
174 }
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100175 if opts.AccessList != nil {
176 lb["accessList"] = &opts.AccessList
177 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100178 if opts.Algorithm != "" {
179 lb["algorithm"] = opts.Algorithm
180 }
181 if opts.ConnectionLogging != nil {
182 lb["connectionLogging"] = &opts.ConnectionLogging
183 }
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100184 if opts.ConnThrottle != nil {
185 lb["connectionThrottle"] = &opts.ConnThrottle
186 }
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100187 if opts.HealthMonitor != nil {
188 lb["healthMonitor"] = &opts.HealthMonitor
189 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100190 if len(opts.Metadata) != 0 {
191 lb["metadata"] = opts.Metadata
192 }
193 if opts.Port > 0 {
194 lb["port"] = opts.Port
195 }
196 if opts.Timeout > 0 {
197 lb["timeout"] = opts.Timeout
198 }
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100199 if opts.SessionPersistence != nil {
200 lb["sessionPersistence"] = &opts.SessionPersistence
201 }
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100202 if opts.HTTPSRedirect != nil {
203 lb["httpsRedirect"] = &opts.HTTPSRedirect
204 }
205
206 return map[string]interface{}{"loadBalancer": lb}, nil
207}
208
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100209// Create is the operation responsible for asynchronously provisioning a new
210// load balancer based on the configuration defined in CreateOpts. Once the
211// request is validated and progress has started on the provisioning process, a
212// response struct is returned. When extracted (with Extract()), you have
213// to the load balancer's unique ID and status.
214//
215// Once an ID is attained, you can check on the progress of the operation by
216// calling Get and passing in the ID. If the corresponding request cannot be
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100217// fulfilled due to insufficient or invalid data, an HTTP 400 (Bad Request)
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100218// error response is returned with information regarding the nature of the
219// failure in the body of the response. Failures in the validation process are
220// non-recoverable and require the caller to correct the cause of the failure.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100221func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
222 var res CreateResult
223
224 reqBody, err := opts.ToLBCreateMap()
225 if err != nil {
226 res.Err = err
227 return res
228 }
229
Jamie Hannaford5497f942015-03-25 11:55:51 +0100230 _, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100231 return res
232}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100233
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100234// Get is the operation responsible for providing detailed information
235// regarding a specific load balancer which is configured and associated with
236// your account. This operation is not capable of returning details for a load
237// balancer which has been deleted.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100238func Get(c *gophercloud.ServiceClient, id int) GetResult {
239 var res GetResult
240
Jamie Hannaford5497f942015-03-25 11:55:51 +0100241 _, res.Err = c.Get(resourceURL(c, id), &res.Body, &gophercloud.RequestOpts{
242 OkCodes: []int{200},
Jamie Hannaford07c06962014-10-31 16:42:03 +0100243 })
244
245 return res
246}
247
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100248// BulkDelete removes all the load balancers referenced in the slice of IDs.
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100249// Any and all configuration data associated with these load balancers is
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100250// immediately purged and is not recoverable.
251//
252// If one of the items in the list cannot be removed due to its current status,
253// a 400 Bad Request error is returned along with the IDs of the ones the
254// system identified as potential failures for this request.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100255func BulkDelete(c *gophercloud.ServiceClient, ids []int) DeleteResult {
256 var res DeleteResult
257
Jamie Hannaford0a9a6be2014-11-03 12:55:38 +0100258 if len(ids) > 10 || len(ids) == 0 {
259 res.Err = errors.New("You must provide a minimum of 1 and a maximum of 10 LB IDs")
260 return res
261 }
262
Jamie Hannaford1c260332014-10-31 15:57:22 +0100263 url := rootURL(c)
Jamie Hannaford950561c2014-11-12 11:12:20 +0100264 url += gophercloud.IDSliceToQueryString("id", ids)
Jamie Hannaford1c260332014-10-31 15:57:22 +0100265
Jamie Hannaford5497f942015-03-25 11:55:51 +0100266 _, res.Err = c.Delete(url, nil)
Jamie Hannaford1c260332014-10-31 15:57:22 +0100267 return res
268}
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100269
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100270// Delete removes a single load balancer.
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100271func Delete(c *gophercloud.ServiceClient, id int) DeleteResult {
272 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100273 _, res.Err = c.Delete(resourceURL(c, id), nil)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100274 return res
275}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100276
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100277// UpdateOptsBuilder represents a type that can be converted into a JSON-like
278// map structure.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100279type UpdateOptsBuilder interface {
280 ToLBUpdateMap() (map[string]interface{}, error)
281}
282
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100283// UpdateOpts represents the options for updating an existing load balancer.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100284type UpdateOpts struct {
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100285 // Optional - new name of the load balancer.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100286 Name string
287
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100288 // Optional - the new protocol you want your load balancer to have.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100289 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
290 // for a full list of supported protocols.
291 Protocol string
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100292
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100293 // Optional - see the HalfClosed field in CreateOpts for more information.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100294 HalfClosed gophercloud.EnabledState
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100295
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100296 // Optional - see the Algorithm field in CreateOpts for more information.
Jamie Hannaford46336282014-11-04 14:48:20 +0100297 Algorithm string
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100298
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100299 // Optional - see the Port field in CreateOpts for more information.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100300 Port int
301
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100302 // Optional - see the Timeout field in CreateOpts for more information.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100303 Timeout int
304
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100305 // Optional - see the HTTPSRedirect field in CreateOpts for more information.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100306 HTTPSRedirect gophercloud.EnabledState
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100307}
308
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100309// ToLBUpdateMap casts an UpdateOpts struct to a map.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100310func (opts UpdateOpts) ToLBUpdateMap() (map[string]interface{}, error) {
311 lb := make(map[string]interface{})
312
313 if opts.Name != "" {
314 lb["name"] = opts.Name
315 }
316 if opts.Protocol != "" {
317 lb["protocol"] = opts.Protocol
318 }
319 if opts.HalfClosed != nil {
320 lb["halfClosed"] = opts.HalfClosed
321 }
322 if opts.Algorithm != "" {
323 lb["algorithm"] = opts.Algorithm
324 }
325 if opts.Port > 0 {
326 lb["port"] = opts.Port
327 }
328 if opts.Timeout > 0 {
329 lb["timeout"] = opts.Timeout
330 }
331 if opts.HTTPSRedirect != nil {
332 lb["httpsRedirect"] = &opts.HTTPSRedirect
333 }
334
335 return map[string]interface{}{"loadBalancer": lb}, nil
336}
337
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100338// Update is the operation responsible for asynchronously updating the
339// attributes of a specific load balancer. Upon successful validation of the
Jamie Hannaforddfdf0a22014-11-12 11:06:45 +0100340// request, the service returns a 202 Accepted response, and the load balancer
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100341// enters a PENDING_UPDATE state. A user can poll the load balancer with Get to
342// wait for the changes to be applied. When this happens, the load balancer will
343// return to an ACTIVE state.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100344func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult {
345 var res UpdateResult
346
347 reqBody, err := opts.ToLBUpdateMap()
348 if err != nil {
349 res.Err = err
350 return res
351 }
352
Jamie Hannaford5497f942015-03-25 11:55:51 +0100353 _, res.Err = c.Put(resourceURL(c, id), reqBody, nil, nil)
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100354 return res
355}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100356
357// ListProtocols is the operation responsible for returning a paginated
358// collection of load balancer protocols.
359func ListProtocols(client *gophercloud.ServiceClient) pagination.Pager {
360 url := protocolsURL(client)
361 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
362 return ProtocolPage{pagination.SinglePageBase(r)}
363 })
364}
Jamie Hannaford46336282014-11-04 14:48:20 +0100365
366// ListAlgorithms is the operation responsible for returning a paginated
367// collection of load balancer algorithms.
368func ListAlgorithms(client *gophercloud.ServiceClient) pagination.Pager {
369 url := algorithmsURL(client)
370 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
371 return AlgorithmPage{pagination.SinglePageBase(r)}
372 })
373}
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100374
375// IsLoggingEnabled returns true if the load balancer has connection logging
376// enabled and false if not.
377func IsLoggingEnabled(client *gophercloud.ServiceClient, id int) (bool, error) {
378 var body interface{}
379
Jamie Hannaford5497f942015-03-25 11:55:51 +0100380 _, err := client.Get(loggingURL(client, id), &body, nil)
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100381 if err != nil {
382 return false, err
383 }
384
385 var resp struct {
386 CL struct {
387 Enabled bool `mapstructure:"enabled"`
388 } `mapstructure:"connectionLogging"`
389 }
390
391 err = mapstructure.Decode(body, &resp)
392 return resp.CL.Enabled, err
393}
394
395func toConnLoggingMap(state bool) map[string]map[string]bool {
396 return map[string]map[string]bool{
Jamie Hannaford20b75882014-11-10 13:39:51 +0100397 "connectionLogging": map[string]bool{"enabled": state},
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100398 }
399}
400
401// EnableLogging will enable connection logging for a specified load balancer.
402func EnableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100403 var res gophercloud.ErrResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100404 _, res.Err = client.Put(loggingURL(client, id), toConnLoggingMap(true), nil, nil)
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100405 return res
406}
407
408// DisableLogging will disable connection logging for a specified load balancer.
409func DisableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100410 var res gophercloud.ErrResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100411 _, res.Err = client.Put(loggingURL(client, id), toConnLoggingMap(false), nil, nil)
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100412 return res
413}
Jamie Hannafordda45b422014-11-10 11:00:38 +0100414
Jamie Hannaford3da65282014-11-10 11:36:16 +0100415// GetErrorPage will retrieve the current error page for the load balancer.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100416func GetErrorPage(client *gophercloud.ServiceClient, id int) ErrorPageResult {
417 var res ErrorPageResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100418 _, res.Err = client.Get(errorPageURL(client, id), &res.Body, nil)
Jamie Hannafordda45b422014-11-10 11:00:38 +0100419 return res
420}
421
Jamie Hannaford3da65282014-11-10 11:36:16 +0100422// SetErrorPage will set the HTML of the load balancer's error page to a
423// specific value.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100424func SetErrorPage(client *gophercloud.ServiceClient, id int, html string) ErrorPageResult {
425 var res ErrorPageResult
426
427 type stringMap map[string]string
428 reqBody := map[string]stringMap{"errorpage": stringMap{"content": html}}
429
Jamie Hannaford5497f942015-03-25 11:55:51 +0100430 _, res.Err = client.Put(errorPageURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
431 OkCodes: []int{200},
Jamie Hannafordda45b422014-11-10 11:00:38 +0100432 })
433
434 return res
435}
436
Jamie Hannaford3da65282014-11-10 11:36:16 +0100437// DeleteErrorPage will delete the current error page for the load balancer.
Jamie Hannafordda45b422014-11-10 11:00:38 +0100438func DeleteErrorPage(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
439 var res gophercloud.ErrResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100440 _, res.Err = client.Delete(errorPageURL(client, id), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500441 OkCodes: []int{200},
Jamie Hannafordda45b422014-11-10 11:00:38 +0100442 })
Jamie Hannafordda45b422014-11-10 11:00:38 +0100443 return res
444}
Jamie Hannaford3da65282014-11-10 11:36:16 +0100445
446// GetStats will retrieve detailed stats related to the load balancer's usage.
447func GetStats(client *gophercloud.ServiceClient, id int) StatsResult {
448 var res StatsResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100449 _, res.Err = client.Get(statsURL(client, id), &res.Body, nil)
Jamie Hannaford3da65282014-11-10 11:36:16 +0100450 return res
451}
Jamie Hannaford20b75882014-11-10 13:39:51 +0100452
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100453// IsContentCached will check to see whether the specified load balancer caches
454// content. When content caching is enabled, recently-accessed files are stored
455// on the load balancer for easy retrieval by web clients. Content caching
456// improves the performance of high traffic web sites by temporarily storing
457// data that was recently accessed. While it's cached, requests for that data
Jamie Hannaford227d9592014-11-13 10:32:07 +0100458// are served by the load balancer, which in turn reduces load off the back-end
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100459// nodes. The result is improved response times for those requests and less
460// load on the web server.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100461func IsContentCached(client *gophercloud.ServiceClient, id int) (bool, error) {
462 var body interface{}
463
Jamie Hannaford5497f942015-03-25 11:55:51 +0100464 _, err := client.Get(cacheURL(client, id), &body, nil)
Jamie Hannaford20b75882014-11-10 13:39:51 +0100465 if err != nil {
466 return false, err
467 }
468
469 var resp struct {
470 CC struct {
471 Enabled bool `mapstructure:"enabled"`
472 } `mapstructure:"contentCaching"`
473 }
474
475 err = mapstructure.Decode(body, &resp)
476 return resp.CC.Enabled, err
477}
478
479func toCachingMap(state bool) map[string]map[string]bool {
480 return map[string]map[string]bool{
481 "contentCaching": map[string]bool{"enabled": state},
482 }
483}
484
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100485// EnableCaching will enable content-caching for the specified load balancer.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100486func EnableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
Jamie Hannaford20b75882014-11-10 13:39:51 +0100487 var res gophercloud.ErrResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100488 _, res.Err = client.Put(cacheURL(client, id), toCachingMap(true), nil, nil)
Jamie Hannaford20b75882014-11-10 13:39:51 +0100489 return res
490}
491
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100492// DisableCaching will disable content-caching for the specified load balancer.
Jamie Hannaford20b75882014-11-10 13:39:51 +0100493func DisableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult {
Jamie Hannaford20b75882014-11-10 13:39:51 +0100494 var res gophercloud.ErrResult
Jamie Hannaford5497f942015-03-25 11:55:51 +0100495 _, res.Err = client.Put(cacheURL(client, id), toCachingMap(false), nil, nil)
Jamie Hannaford20b75882014-11-10 13:39:51 +0100496 return res
497}