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 | |
| 6 | "github.com/racker/perigee" |
| 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 | 940159d | 2014-11-03 13:04:08 +0100 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/rackspace/lb/v1" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 11 | "github.com/rackspace/gophercloud/rackspace/lb/v1/acl" |
| 12 | "github.com/rackspace/gophercloud/rackspace/lb/v1/monitors" |
Jamie Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes" |
Jamie Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 15 | "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle" |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 16 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 17 | ) |
| 18 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 19 | var ( |
| 20 | errNameRequired = errors.New("Name is a required attribute") |
| 21 | errTimeoutExceeded = errors.New("Timeout must be less than 120") |
| 22 | ) |
| 23 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 24 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 25 | // List request. |
| 26 | type ListOptsBuilder interface { |
| 27 | ToLBListQuery() (string, error) |
| 28 | } |
| 29 | |
| 30 | // ListOpts allows the filtering and sorting of paginated collections through |
| 31 | // the API. |
| 32 | type 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. |
| 41 | func (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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 49 | // 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 Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 51 | func 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 65 | |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 66 | // 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. |
| 70 | type 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. |
| 76 | type 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 Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 82 | Nodes []nodes.Node |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 83 | |
| 84 | // Required - protocol of the service that is being load balanced. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 85 | // 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 88 | |
| 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 93 | HalfClosed gophercloud.EnabledState |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 94 | |
| 95 | // Optional - the type of virtual IPs you want associated with the load |
| 96 | // balancer. |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 97 | VIPs []vips.VIP |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 98 | |
| 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 101 | AccessList *acl.AccessList |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 102 | |
| 103 | // Optional - algorithm that defines how traffic should be directed between |
| 104 | // back-end nodes. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 105 | Algorithm string |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 106 | |
| 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 Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 112 | ConnThrottle *throttle.ConnectionThrottle |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 113 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 114 | // Optional |
| 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 |
| 217 | // fulfilled due to insufficient or invalid data, a HTTP 400 (Bad Request) |
| 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 | |
| 230 | _, res.Err = perigee.Request("POST", rootURL(c), perigee.Options{ |
| 231 | MoreHeaders: c.AuthenticatedHeaders(), |
| 232 | ReqBody: &reqBody, |
| 233 | Results: &res.Body, |
Jamie Hannaford | d56375d | 2014-11-05 12:38:04 +0100 | [diff] [blame] | 234 | OkCodes: []int{202}, |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 235 | }) |
| 236 | |
| 237 | return res |
| 238 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 239 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 240 | // 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 Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 244 | func 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 256 | // BulkDelete removes all the load balancers referenced in the slice of IDs. |
| 257 | // Any and all configuration data associated with these load balancers are |
| 258 | // 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 Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 263 | func BulkDelete(c *gophercloud.ServiceClient, ids []int) DeleteResult { |
| 264 | var res DeleteResult |
| 265 | |
Jamie Hannaford | 0a9a6be | 2014-11-03 12:55:38 +0100 | [diff] [blame] | 266 | 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 Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 271 | url := rootURL(c) |
Jamie Hannaford | 940159d | 2014-11-03 13:04:08 +0100 | [diff] [blame] | 272 | url += v1.IDSliceToQueryString("id", ids) |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 273 | |
| 274 | _, res.Err = perigee.Request("DELETE", url, perigee.Options{ |
| 275 | MoreHeaders: c.AuthenticatedHeaders(), |
| 276 | OkCodes: []int{202}, |
| 277 | }) |
| 278 | |
| 279 | return res |
| 280 | } |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 281 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 282 | // Delete removes a single load balancer. |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 283 | func Delete(c *gophercloud.ServiceClient, id int) DeleteResult { |
| 284 | var res DeleteResult |
| 285 | |
| 286 | _, res.Err = perigee.Request("DELETE", resourceURL(c, id), perigee.Options{ |
| 287 | MoreHeaders: c.AuthenticatedHeaders(), |
| 288 | OkCodes: []int{202}, |
| 289 | }) |
| 290 | |
| 291 | return res |
| 292 | } |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 293 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 294 | // UpdateOptsBuilder represents a type that can be converted into a JSON-like |
| 295 | // map structure. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 296 | type UpdateOptsBuilder interface { |
| 297 | ToLBUpdateMap() (map[string]interface{}, error) |
| 298 | } |
| 299 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 300 | // UpdateOpts represent the options for updating an existing load balancer. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 301 | type UpdateOpts struct { |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 302 | // Optional - new name of the load balancer. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 303 | Name string |
| 304 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 305 | // Optional - the new protocol you want your load balancer to have. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 306 | // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html |
| 307 | // for a full list of supported protocols. |
| 308 | Protocol string |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 309 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 310 | // Optional - see the HalfClosed field in CreateOpts for more information. |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 311 | HalfClosed gophercloud.EnabledState |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 312 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 313 | // Optional - see the Algorithm field in CreateOpts for more information. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 314 | Algorithm string |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 315 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 316 | // Optional - see the Port field in CreateOpts for more information. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 317 | Port int |
| 318 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 319 | // Optional - see the Timeout field in CreateOpts for more information. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 320 | Timeout int |
| 321 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 322 | // Optional - see the HTTPSRedirect field in CreateOpts for more information. |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 323 | HTTPSRedirect gophercloud.EnabledState |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 326 | // ToLBUpdateMap casts an UpdateOpts struct to a map. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 327 | func (opts UpdateOpts) ToLBUpdateMap() (map[string]interface{}, error) { |
| 328 | lb := make(map[string]interface{}) |
| 329 | |
| 330 | if opts.Name != "" { |
| 331 | lb["name"] = opts.Name |
| 332 | } |
| 333 | if opts.Protocol != "" { |
| 334 | lb["protocol"] = opts.Protocol |
| 335 | } |
| 336 | if opts.HalfClosed != nil { |
| 337 | lb["halfClosed"] = opts.HalfClosed |
| 338 | } |
| 339 | if opts.Algorithm != "" { |
| 340 | lb["algorithm"] = opts.Algorithm |
| 341 | } |
| 342 | if opts.Port > 0 { |
| 343 | lb["port"] = opts.Port |
| 344 | } |
| 345 | if opts.Timeout > 0 { |
| 346 | lb["timeout"] = opts.Timeout |
| 347 | } |
| 348 | if opts.HTTPSRedirect != nil { |
| 349 | lb["httpsRedirect"] = &opts.HTTPSRedirect |
| 350 | } |
| 351 | |
| 352 | return map[string]interface{}{"loadBalancer": lb}, nil |
| 353 | } |
| 354 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 355 | // Update is the operation responsible for asynchronously updating the |
| 356 | // attributes of a specific load balancer. Upon successful validation of the |
| 357 | // request, the service returns a 202 Accepted response and the load balancer |
| 358 | // enters a PENDING_UPDATE state. A user can poll the load balancer with Get to |
| 359 | // wait for the changes to be applied. When this happens, the load balancer will |
| 360 | // return to an ACTIVE state. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 361 | func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult { |
| 362 | var res UpdateResult |
| 363 | |
| 364 | reqBody, err := opts.ToLBUpdateMap() |
| 365 | if err != nil { |
| 366 | res.Err = err |
| 367 | return res |
| 368 | } |
| 369 | |
| 370 | _, res.Err = perigee.Request("PUT", resourceURL(c, id), perigee.Options{ |
| 371 | MoreHeaders: c.AuthenticatedHeaders(), |
| 372 | ReqBody: &reqBody, |
Jamie Hannaford | d56375d | 2014-11-05 12:38:04 +0100 | [diff] [blame] | 373 | OkCodes: []int{202}, |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 374 | }) |
| 375 | |
| 376 | return res |
| 377 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 378 | |
| 379 | // ListProtocols is the operation responsible for returning a paginated |
| 380 | // collection of load balancer protocols. |
| 381 | func ListProtocols(client *gophercloud.ServiceClient) pagination.Pager { |
| 382 | url := protocolsURL(client) |
| 383 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 384 | return ProtocolPage{pagination.SinglePageBase(r)} |
| 385 | }) |
| 386 | } |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 387 | |
| 388 | // ListAlgorithms is the operation responsible for returning a paginated |
| 389 | // collection of load balancer algorithms. |
| 390 | func ListAlgorithms(client *gophercloud.ServiceClient) pagination.Pager { |
| 391 | url := algorithmsURL(client) |
| 392 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 393 | return AlgorithmPage{pagination.SinglePageBase(r)} |
| 394 | }) |
| 395 | } |