blob: 338de80ecba0e450801af3ef5753f1cefba06cb9 [file] [log] [blame]
Jamie Hannafordfba65af2014-11-03 10:32:37 +01001package lbs
Jamie Hannaford186d4e22014-10-31 12:26:11 +01002
3import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannaforde09b6822014-10-31 15:33:57 +01005
6 "github.com/rackspace/gophercloud"
Jamie Hannaford186d4e22014-10-31 12:26:11 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +01008 "github.com/rackspace/gophercloud/rackspace/lb/v1/acl"
Jamie Hannaford21a3eb12014-11-03 10:34:29 +01009 "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +010010 "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010011 "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle"
Jamie Hannaford1c817312014-11-04 10:56:58 +010012 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010013)
14
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010015// Protocol represents the network protocol which the load balancer accepts.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010016type Protocol struct {
17 // The name of the protocol, e.g. HTTP, LDAP, FTP, etc.
18 Name string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010019
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010020 // The port number for the protocol.
21 Port int
22}
Jamie Hannaford186d4e22014-10-31 12:26:11 +010023
24// Algorithm defines how traffic should be directed between back-end nodes.
Jamie Hannaford46336282014-11-04 14:48:20 +010025type Algorithm struct {
26 // The name of the algorithm, e.g RANDOM, ROUND_ROBIN, etc.
27 Name string
28}
Jamie Hannaford186d4e22014-10-31 12:26:11 +010029
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010030// Status represents the potential state of a load balancer resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010031type Status string
32
33const (
34 // ACTIVE indicates that the LB is configured properly and ready to serve
35 // traffic to incoming requests via the configured virtual IPs.
Jamie Hannaford227d9592014-11-13 10:32:07 +010036 ACTIVE Status = "ACTIVE"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010037
38 // BUILD indicates that the LB is being provisioned for the first time and
39 // configuration is being applied to bring the service online. The service
40 // cannot yet serve incoming requests.
Jamie Hannaford227d9592014-11-13 10:32:07 +010041 BUILD Status = "BUILD"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010042
43 // PENDINGUPDATE indicates that the LB is online but configuration changes
44 // are being applied to update the service based on a previous request.
Jamie Hannaford227d9592014-11-13 10:32:07 +010045 PENDINGUPDATE Status = "PENDING_UPDATE"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010046
47 // PENDINGDELETE indicates that the LB is online but configuration changes
48 // are being applied to begin deletion of the service based on a previous
49 // request.
Jamie Hannaford227d9592014-11-13 10:32:07 +010050 PENDINGDELETE Status = "PENDING_DELETE"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010051
52 // SUSPENDED indicates that the LB has been taken offline and disabled.
Jamie Hannaford227d9592014-11-13 10:32:07 +010053 SUSPENDED Status = "SUSPENDED"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010054
55 // ERROR indicates that the system encountered an error when attempting to
56 // configure the load balancer.
Jamie Hannaford227d9592014-11-13 10:32:07 +010057 ERROR Status = "ERROR"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010058
59 // DELETED indicates that the LB has been deleted.
Jamie Hannaford227d9592014-11-13 10:32:07 +010060 DELETED Status = "DELETED"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010061)
62
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010063// Datetime represents the structure of a Created or Updated field.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010064type Datetime struct {
65 Time string
66}
67
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010068// LoadBalancer represents a load balancer API resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010069type LoadBalancer struct {
70 // Human-readable name for the load balancer.
71 Name string
72
73 // The unique ID for the load balancer.
74 ID int
75
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010076 // Represents the service protocol being load balanced. See Protocol type for
77 // a list of accepted values.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010078 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
79 // for a full list of supported protocols.
80 Protocol string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010081
82 // Defines how traffic should be directed between back-end nodes. The default
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010083 // algorithm is RANDOM. See Algorithm type for a list of accepted values.
Jamie Hannaford46336282014-11-04 14:48:20 +010084 Algorithm string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010085
86 // The current status of the load balancer.
87 Status Status
88
89 // The number of load balancer nodes.
90 NodeCount int `mapstructure:"nodeCount"`
91
92 // Slice of virtual IPs associated with this load balancer.
Jamie Hannaford1c817312014-11-04 10:56:58 +010093 VIPs []vips.VIP `mapstructure:"virtualIps"`
Jamie Hannaford186d4e22014-10-31 12:26:11 +010094
95 // Datetime when the LB was created.
96 Created Datetime
97
98 // Datetime when the LB was created.
99 Updated Datetime
100
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100101 // Port number for the service you are load balancing.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100102 Port int
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100103
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100104 // HalfClosed provides the ability for one end of the connection to
105 // terminate its output while still receiving data from the other end. This
106 // is only available on TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100107 HalfClosed bool
108
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100109 // Timeout represents the timeout value between a load balancer and its
110 // nodes. Defaults to 30 seconds with a maximum of 120 seconds.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100111 Timeout int
112
Jamie Hannaford227d9592014-11-13 10:32:07 +0100113 // The cluster name.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100114 Cluster Cluster
115
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100116 // Nodes shows all the back-end nodes which are associated with the load
117 // balancer. These are the devices which are delivered traffic.
Jamie Hannaford21a3eb12014-11-03 10:34:29 +0100118 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100119
Jamie Hannaford227d9592014-11-13 10:32:07 +0100120 // Current connection logging configuration.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100121 ConnectionLogging ConnectionLogging
Jamie Hannaford07c06962014-10-31 16:42:03 +0100122
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100123 // SessionPersistence specifies whether multiple requests from clients are
124 // directed to the same node.
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +0100125 SessionPersistence sessions.SessionPersistence
Jamie Hannaford07c06962014-10-31 16:42:03 +0100126
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100127 // ConnectionThrottle specifies a limit on the number of connections per IP
128 // address to help mitigate malicious or abusive traffic to your applications.
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100129 ConnectionThrottle throttle.ConnectionThrottle
Jamie Hannaford07c06962014-10-31 16:42:03 +0100130
Jamie Hannaford227d9592014-11-13 10:32:07 +0100131 // The source public and private IP addresses.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100132 SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"`
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100133
134 // Represents the access rules for this particular load balancer. IP addresses
135 // or subnet ranges, depending on their type (ALLOW or DENY), can be permitted
136 // or blocked.
137 AccessList acl.AccessList
Jamie Hannaford07c06962014-10-31 16:42:03 +0100138}
139
Jamie Hannaford227d9592014-11-13 10:32:07 +0100140// SourceAddrs represents the source public and private IP addresses.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100141type SourceAddrs struct {
142 IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"`
143 IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"`
144 IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"`
145 IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"`
146}
147
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100148// ConnectionLogging - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100149type ConnectionLogging struct {
150 Enabled bool
151}
152
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100153// Cluster - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100154type Cluster struct {
155 Name string
156}
157
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100158// LBPage is the page returned by a pager when traversing over a collection of
159// LBs.
160type LBPage struct {
161 pagination.LinkedPageBase
162}
163
164// IsEmpty checks whether a NetworkPage struct is empty.
165func (p LBPage) IsEmpty() (bool, error) {
166 is, err := ExtractLBs(p)
167 if err != nil {
168 return true, nil
169 }
170 return len(is) == 0, nil
171}
172
173// ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts
174// the elements into a slice of LoadBalancer structs. In other words, a generic
175// collection is mapped into a relevant slice.
176func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) {
177 var resp struct {
178 LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
179 }
180
181 err := mapstructure.Decode(page.(LBPage).Body, &resp)
182
183 return resp.LBs, err
184}
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100185
186type commonResult struct {
187 gophercloud.Result
188}
189
190// Extract interprets any commonResult as a LB, if possible.
191func (r commonResult) Extract() (*LoadBalancer, error) {
192 if r.Err != nil {
193 return nil, r.Err
194 }
195
196 var response struct {
197 LB LoadBalancer `mapstructure:"loadBalancer"`
198 }
199
200 err := mapstructure.Decode(r.Body, &response)
201
202 return &response.LB, err
203}
204
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100205// CreateResult represents the result of a create operation.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100206type CreateResult struct {
207 commonResult
208}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100209
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100210// DeleteResult represents the result of a delete operation.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100211type DeleteResult struct {
212 gophercloud.ErrResult
213}
Jamie Hannaford07c06962014-10-31 16:42:03 +0100214
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100215// UpdateResult represents the result of an update operation.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100216type UpdateResult struct {
217 gophercloud.ErrResult
218}
219
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100220// GetResult represents the result of a get operation.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100221type GetResult struct {
222 commonResult
223}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100224
225// ProtocolPage is the page returned by a pager when traversing over a
226// collection of LB protocols.
227type ProtocolPage struct {
228 pagination.SinglePageBase
229}
230
231// IsEmpty checks whether a ProtocolPage struct is empty.
232func (p ProtocolPage) IsEmpty() (bool, error) {
233 is, err := ExtractProtocols(p)
234 if err != nil {
235 return true, nil
236 }
237 return len(is) == 0, nil
238}
239
240// ExtractProtocols accepts a Page struct, specifically a ProtocolPage struct,
Jamie Hannaford227d9592014-11-13 10:32:07 +0100241// and extracts the elements into a slice of Protocol structs. In other
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100242// words, a generic collection is mapped into a relevant slice.
243func ExtractProtocols(page pagination.Page) ([]Protocol, error) {
244 var resp struct {
245 Protocols []Protocol `mapstructure:"protocols" json:"protocols"`
246 }
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100247 err := mapstructure.Decode(page.(ProtocolPage).Body, &resp)
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100248 return resp.Protocols, err
249}
Jamie Hannaford46336282014-11-04 14:48:20 +0100250
251// AlgorithmPage is the page returned by a pager when traversing over a
252// collection of LB algorithms.
253type AlgorithmPage struct {
254 pagination.SinglePageBase
255}
256
Jamie Hannaford227d9592014-11-13 10:32:07 +0100257// IsEmpty checks whether an AlgorithmPage struct is empty.
Jamie Hannaford46336282014-11-04 14:48:20 +0100258func (p AlgorithmPage) IsEmpty() (bool, error) {
259 is, err := ExtractAlgorithms(p)
260 if err != nil {
261 return true, nil
262 }
263 return len(is) == 0, nil
264}
265
266// ExtractAlgorithms accepts a Page struct, specifically a AlgorithmPage struct,
267// and extracts the elements into a slice of Algorithm structs. In other
268// words, a generic collection is mapped into a relevant slice.
269func ExtractAlgorithms(page pagination.Page) ([]Algorithm, error) {
270 var resp struct {
271 Algorithms []Algorithm `mapstructure:"algorithms" json:"algorithms"`
272 }
273 err := mapstructure.Decode(page.(AlgorithmPage).Body, &resp)
274 return resp.Algorithms, err
275}
Jamie Hannafordda45b422014-11-10 11:00:38 +0100276
277// ErrorPage represents the HTML file that is shown to an end user who is
278// attempting to access a load balancer node that is offline/unavailable.
279//
280// During provisioning, every load balancer is configured with a default error
281// page that gets displayed when traffic is requested for an offline node.
282//
283// You can add a single custom error page with an HTTP-based protocol to a load
284// balancer. Page updates override existing content. If a custom error page is
285// deleted, or the load balancer is changed to a non-HTTP protocol, the default
286// error page is restored.
287type ErrorPage struct {
288 Content string
289}
290
291// ErrorPageResult represents the result of an error page operation -
292// specifically getting or creating one.
293type ErrorPageResult struct {
294 gophercloud.Result
295}
296
297// Extract interprets any commonResult as an ErrorPage, if possible.
298func (r ErrorPageResult) Extract() (*ErrorPage, error) {
299 if r.Err != nil {
300 return nil, r.Err
301 }
302
303 var response struct {
304 ErrorPage ErrorPage `mapstructure:"errorpage"`
305 }
306
307 err := mapstructure.Decode(r.Body, &response)
308
309 return &response.ErrorPage, err
310}
Jamie Hannaford3da65282014-11-10 11:36:16 +0100311
312// Stats represents all the key information about a load balancer's usage.
313type Stats struct {
314 // The number of connections closed by this load balancer because its
315 // ConnectTimeout interval was exceeded.
316 ConnectTimeout int `mapstructure:"connectTimeOut"`
317
318 // The number of transaction or protocol errors for this load balancer.
319 ConnectError int
320
321 // Number of connection failures for this load balancer.
322 ConnectFailure int
323
324 // Number of connections closed by this load balancer because its Timeout
325 // interval was exceeded.
326 DataTimedOut int
327
328 // Number of connections closed by this load balancer because the
329 // 'keepalive_timeout' interval was exceeded.
330 KeepAliveTimedOut int
331
332 // The maximum number of simultaneous TCP connections this load balancer has
333 // processed at any one time.
334 MaxConnections int `mapstructure:"maxConn"`
335
336 // Number of simultaneous connections active at the time of the request.
337 CurrentConnections int `mapstructure:"currentConn"`
338
339 // Number of SSL connections closed by this load balancer because the
340 // ConnectTimeout interval was exceeded.
341 SSLConnectTimeout int `mapstructure:"connectTimeOutSsl"`
342
343 // Number of SSL transaction or protocol erros in this load balancer.
344 SSLConnectError int `mapstructure:"connectErrorSsl"`
345
346 // Number of SSL connection failures in this load balancer.
347 SSLConnectFailure int `mapstructure:"connectFailureSsl"`
348
349 // Number of SSL connections closed by this load balancer because the
350 // Timeout interval was exceeded.
351 SSLDataTimedOut int `mapstructure:"dataTimedOutSsl"`
352
353 // Number of SSL connections closed by this load balancer because the
354 // 'keepalive_timeout' interval was exceeded.
355 SSLKeepAliveTimedOut int `mapstructure:"keepAliveTimedOutSsl"`
356
357 // Maximum number of simultaneous SSL connections this load balancer has
358 // processed a any one time.
359 SSLMaxConnections int `mapstructure:"maxConnSsl"`
360
361 // Number of simultaneous SSL connections active at the time of the request.
362 SSLCurrentConnections int `mapstructure:"currentConnSsl"`
363}
364
Jamie Hannaford227d9592014-11-13 10:32:07 +0100365// StatsResult represents the result of a Stats operation.
Jamie Hannaford3da65282014-11-10 11:36:16 +0100366type StatsResult struct {
367 gophercloud.Result
368}
369
370// Extract interprets any commonResult as a Stats struct, if possible.
371func (r StatsResult) Extract() (*Stats, error) {
372 if r.Err != nil {
373 return nil, r.Err
374 }
375 res := &Stats{}
376 err := mapstructure.Decode(r.Body, res)
377 return res, err
378}