Jamie Hannaford | fba65af | 2014-11-03 10:32:37 +0100 | [diff] [blame] | 1 | package lbs |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 7 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/rackspace/lb/v1/acl" |
| 11 | "github.com/rackspace/gophercloud/rackspace/lb/v1/monitors" |
Jamie Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes" |
Jamie Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle" |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 15 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 18 | var ( |
| 19 | errNameRequired = errors.New("Name is a required attribute") |
| 20 | errTimeoutExceeded = errors.New("Timeout must be less than 120") |
| 21 | ) |
| 22 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 23 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 24 | // List request. |
| 25 | type ListOptsBuilder interface { |
| 26 | ToLBListQuery() (string, error) |
| 27 | } |
| 28 | |
| 29 | // ListOpts allows the filtering and sorting of paginated collections through |
| 30 | // the API. |
| 31 | type 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. |
| 40 | func (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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 48 | // 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 Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 50 | func 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 64 | |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 65 | // 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. |
| 69 | type 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. |
| 75 | type 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 Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 81 | Nodes []nodes.Node |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 82 | |
| 83 | // Required - protocol of the service that is being load balanced. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 84 | // 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 87 | |
| 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 92 | HalfClosed gophercloud.EnabledState |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 93 | |
| 94 | // Optional - the type of virtual IPs you want associated with the load |
| 95 | // balancer. |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 96 | VIPs []vips.VIP |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 97 | |
| 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 100 | AccessList *acl.AccessList |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 101 | |
| 102 | // Optional - algorithm that defines how traffic should be directed between |
| 103 | // back-end nodes. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 104 | Algorithm string |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 105 | |
| 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 111 | ConnThrottle *throttle.ConnectionThrottle |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 112 | |
Jamie Hannaford | dfdf0a2 | 2014-11-12 11:06:45 +0100 | [diff] [blame] | 113 | // Optional - the type of health monitor check to perform to ensure that the |
| 114 | // service is performing properly. |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 115 | HealthMonitor *monitors.Monitor |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 116 | |
| 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 Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame] | 129 | SessionPersistence *sessions.SessionPersistence |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 130 | |
| 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 138 | HTTPSRedirect gophercloud.EnabledState |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 141 | // ToLBCreateMap casts a CreateOpts struct to a map. |
| 142 | func (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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 172 | if len(opts.VIPs) > 0 { |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 173 | lb["virtualIps"] = opts.VIPs |
| 174 | } |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 175 | if opts.AccessList != nil { |
| 176 | lb["accessList"] = &opts.AccessList |
| 177 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 178 | if opts.Algorithm != "" { |
| 179 | lb["algorithm"] = opts.Algorithm |
| 180 | } |
| 181 | if opts.ConnectionLogging != nil { |
| 182 | lb["connectionLogging"] = &opts.ConnectionLogging |
| 183 | } |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 184 | if opts.ConnThrottle != nil { |
| 185 | lb["connectionThrottle"] = &opts.ConnThrottle |
| 186 | } |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 187 | if opts.HealthMonitor != nil { |
| 188 | lb["healthMonitor"] = &opts.HealthMonitor |
| 189 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 190 | 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 199 | if opts.SessionPersistence != nil { |
| 200 | lb["sessionPersistence"] = &opts.SessionPersistence |
| 201 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 202 | if opts.HTTPSRedirect != nil { |
| 203 | lb["httpsRedirect"] = &opts.HTTPSRedirect |
| 204 | } |
| 205 | |
| 206 | return map[string]interface{}{"loadBalancer": lb}, nil |
| 207 | } |
| 208 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 209 | // 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 Hannaford | dfdf0a2 | 2014-11-12 11:06:45 +0100 | [diff] [blame] | 217 | // fulfilled due to insufficient or invalid data, an HTTP 400 (Bad Request) |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 218 | // 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 221 | func 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 Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 230 | _, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil) |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 231 | return res |
| 232 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 233 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 234 | // 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 Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 238 | func Get(c *gophercloud.ServiceClient, id int) GetResult { |
| 239 | var res GetResult |
| 240 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 241 | _, res.Err = c.Get(resourceURL(c, id), &res.Body, &gophercloud.RequestOpts{ |
| 242 | OkCodes: []int{200}, |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 243 | }) |
| 244 | |
| 245 | return res |
| 246 | } |
| 247 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 248 | // BulkDelete removes all the load balancers referenced in the slice of IDs. |
Jamie Hannaford | dfdf0a2 | 2014-11-12 11:06:45 +0100 | [diff] [blame] | 249 | // Any and all configuration data associated with these load balancers is |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 250 | // 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 Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 255 | func BulkDelete(c *gophercloud.ServiceClient, ids []int) DeleteResult { |
| 256 | var res DeleteResult |
| 257 | |
Jamie Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 258 | 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 Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 263 | url := rootURL(c) |
Jamie Hannaford | 950561c | 2014-11-12 11:12:20 +0100 | [diff] [blame] | 264 | url += gophercloud.IDSliceToQueryString("id", ids) |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 265 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 266 | _, res.Err = c.Delete(url, nil) |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 267 | return res |
| 268 | } |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 269 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 270 | // Delete removes a single load balancer. |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 271 | func Delete(c *gophercloud.ServiceClient, id int) DeleteResult { |
| 272 | var res DeleteResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 273 | _, res.Err = c.Delete(resourceURL(c, id), nil) |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 274 | return res |
| 275 | } |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 276 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 277 | // UpdateOptsBuilder represents a type that can be converted into a JSON-like |
| 278 | // map structure. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 279 | type UpdateOptsBuilder interface { |
| 280 | ToLBUpdateMap() (map[string]interface{}, error) |
| 281 | } |
| 282 | |
Jamie Hannaford | dfdf0a2 | 2014-11-12 11:06:45 +0100 | [diff] [blame] | 283 | // UpdateOpts represents the options for updating an existing load balancer. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 284 | type UpdateOpts struct { |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 285 | // Optional - new name of the load balancer. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 286 | Name string |
| 287 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 288 | // Optional - the new protocol you want your load balancer to have. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 289 | // 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 Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 292 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 293 | // Optional - see the HalfClosed field in CreateOpts for more information. |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 294 | HalfClosed gophercloud.EnabledState |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 295 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 296 | // Optional - see the Algorithm field in CreateOpts for more information. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 297 | Algorithm string |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 298 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 299 | // Optional - see the Port field in CreateOpts for more information. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 300 | Port int |
| 301 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 302 | // Optional - see the Timeout field in CreateOpts for more information. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 303 | Timeout int |
| 304 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 305 | // Optional - see the HTTPSRedirect field in CreateOpts for more information. |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 306 | HTTPSRedirect gophercloud.EnabledState |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 309 | // ToLBUpdateMap casts an UpdateOpts struct to a map. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 310 | func (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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 338 | // Update is the operation responsible for asynchronously updating the |
| 339 | // attributes of a specific load balancer. Upon successful validation of the |
Jamie Hannaford | dfdf0a2 | 2014-11-12 11:06:45 +0100 | [diff] [blame] | 340 | // request, the service returns a 202 Accepted response, and the load balancer |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 341 | // 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 Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 344 | func 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 Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 353 | _, res.Err = c.Put(resourceURL(c, id), reqBody, nil, nil) |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 354 | return res |
| 355 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 356 | |
| 357 | // ListProtocols is the operation responsible for returning a paginated |
| 358 | // collection of load balancer protocols. |
| 359 | func 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 Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 365 | |
| 366 | // ListAlgorithms is the operation responsible for returning a paginated |
| 367 | // collection of load balancer algorithms. |
| 368 | func 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 Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 374 | |
| 375 | // IsLoggingEnabled returns true if the load balancer has connection logging |
| 376 | // enabled and false if not. |
| 377 | func IsLoggingEnabled(client *gophercloud.ServiceClient, id int) (bool, error) { |
| 378 | var body interface{} |
| 379 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 380 | _, err := client.Get(loggingURL(client, id), &body, nil) |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 381 | 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 | |
| 395 | func toConnLoggingMap(state bool) map[string]map[string]bool { |
| 396 | return map[string]map[string]bool{ |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 397 | "connectionLogging": map[string]bool{"enabled": state}, |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | // EnableLogging will enable connection logging for a specified load balancer. |
| 402 | func EnableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult { |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 403 | var res gophercloud.ErrResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 404 | _, res.Err = client.Put(loggingURL(client, id), toConnLoggingMap(true), nil, nil) |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 405 | return res |
| 406 | } |
| 407 | |
| 408 | // DisableLogging will disable connection logging for a specified load balancer. |
| 409 | func DisableLogging(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult { |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 410 | var res gophercloud.ErrResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 411 | _, res.Err = client.Put(loggingURL(client, id), toConnLoggingMap(false), nil, nil) |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 412 | return res |
| 413 | } |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 414 | |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 415 | // GetErrorPage will retrieve the current error page for the load balancer. |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 416 | func GetErrorPage(client *gophercloud.ServiceClient, id int) ErrorPageResult { |
| 417 | var res ErrorPageResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 418 | _, res.Err = client.Get(errorPageURL(client, id), &res.Body, nil) |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 419 | return res |
| 420 | } |
| 421 | |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 422 | // SetErrorPage will set the HTML of the load balancer's error page to a |
| 423 | // specific value. |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 424 | func 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 Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 430 | _, res.Err = client.Put(errorPageURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 431 | OkCodes: []int{200}, |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 432 | }) |
| 433 | |
| 434 | return res |
| 435 | } |
| 436 | |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 437 | // DeleteErrorPage will delete the current error page for the load balancer. |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 438 | func DeleteErrorPage(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult { |
| 439 | var res gophercloud.ErrResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 440 | _, res.Err = client.Delete(errorPageURL(client, id), &gophercloud.RequestOpts{ |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 441 | OkCodes: []int{200}, |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 442 | }) |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 443 | return res |
| 444 | } |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 445 | |
| 446 | // GetStats will retrieve detailed stats related to the load balancer's usage. |
| 447 | func GetStats(client *gophercloud.ServiceClient, id int) StatsResult { |
| 448 | var res StatsResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 449 | _, res.Err = client.Get(statsURL(client, id), &res.Body, nil) |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 450 | return res |
| 451 | } |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 452 | |
Jamie Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 453 | // 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 Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 458 | // are served by the load balancer, which in turn reduces load off the back-end |
Jamie Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 459 | // nodes. The result is improved response times for those requests and less |
| 460 | // load on the web server. |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 461 | func IsContentCached(client *gophercloud.ServiceClient, id int) (bool, error) { |
| 462 | var body interface{} |
| 463 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 464 | _, err := client.Get(cacheURL(client, id), &body, nil) |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 465 | 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 | |
| 479 | func 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 Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 485 | // EnableCaching will enable content-caching for the specified load balancer. |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 486 | func EnableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult { |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 487 | var res gophercloud.ErrResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 488 | _, res.Err = client.Put(cacheURL(client, id), toCachingMap(true), nil, nil) |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 489 | return res |
| 490 | } |
| 491 | |
Jamie Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 492 | // DisableCaching will disable content-caching for the specified load balancer. |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 493 | func DisableCaching(client *gophercloud.ServiceClient, id int) gophercloud.ErrResult { |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 494 | var res gophercloud.ErrResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 495 | _, res.Err = client.Put(cacheURL(client, id), toCachingMap(false), nil, nil) |
Jamie Hannaford | 20b7588 | 2014-11-10 13:39:51 +0100 | [diff] [blame] | 496 | return res |
| 497 | } |