blob: a556f215a72fbdf79a3d3768602d6aa26de541dc [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 Hannaford21a3eb12014-11-03 10:34:29 +01008 "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
Jamie Hannaford1c817312014-11-04 10:56:58 +01009 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010010)
11
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010012// Protocol represents the network protocol which the load balancer accepts.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010013type Protocol struct {
14 // The name of the protocol, e.g. HTTP, LDAP, FTP, etc.
15 Name string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010016
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010017 // The port number for the protocol.
18 Port int
19}
Jamie Hannaford186d4e22014-10-31 12:26:11 +010020
21// Algorithm defines how traffic should be directed between back-end nodes.
Jamie Hannaford46336282014-11-04 14:48:20 +010022type Algorithm struct {
23 // The name of the algorithm, e.g RANDOM, ROUND_ROBIN, etc.
24 Name string
25}
Jamie Hannaford186d4e22014-10-31 12:26:11 +010026
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010027// Status represents the potential state of a load balancer resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010028type Status string
29
30const (
31 // ACTIVE indicates that the LB is configured properly and ready to serve
32 // traffic to incoming requests via the configured virtual IPs.
33 ACTIVE = "ACTIVE"
34
35 // BUILD indicates that the LB is being provisioned for the first time and
36 // configuration is being applied to bring the service online. The service
37 // cannot yet serve incoming requests.
38 BUILD = "BUILD"
39
40 // PENDINGUPDATE indicates that the LB is online but configuration changes
41 // are being applied to update the service based on a previous request.
42 PENDINGUPDATE = "PENDING_UPDATE"
43
44 // PENDINGDELETE indicates that the LB is online but configuration changes
45 // are being applied to begin deletion of the service based on a previous
46 // request.
47 PENDINGDELETE = "PENDING_DELETE"
48
49 // SUSPENDED indicates that the LB has been taken offline and disabled.
50 SUSPENDED = "SUSPENDED"
51
52 // ERROR indicates that the system encountered an error when attempting to
53 // configure the load balancer.
54 ERROR = "ERROR"
55
56 // DELETED indicates that the LB has been deleted.
57 DELETED = "DELETED"
58)
59
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010060// Datetime represents the structure of a Created or Updated field.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010061type Datetime struct {
62 Time string
63}
64
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010065// LoadBalancer represents a load balancer API resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010066type LoadBalancer struct {
67 // Human-readable name for the load balancer.
68 Name string
69
70 // The unique ID for the load balancer.
71 ID int
72
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010073 // Represents the service protocol being load balanced. See Protocol type for
74 // a list of accepted values.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010075 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
76 // for a full list of supported protocols.
77 Protocol string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010078
79 // Defines how traffic should be directed between back-end nodes. The default
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010080 // algorithm is RANDOM. See Algorithm type for a list of accepted values.
Jamie Hannaford46336282014-11-04 14:48:20 +010081 Algorithm string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010082
83 // The current status of the load balancer.
84 Status Status
85
86 // The number of load balancer nodes.
87 NodeCount int `mapstructure:"nodeCount"`
88
89 // Slice of virtual IPs associated with this load balancer.
Jamie Hannaford1c817312014-11-04 10:56:58 +010090 VIPs []vips.VIP `mapstructure:"virtualIps"`
Jamie Hannaford186d4e22014-10-31 12:26:11 +010091
92 // Datetime when the LB was created.
93 Created Datetime
94
95 // Datetime when the LB was created.
96 Updated Datetime
97
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010098 // Port number for the service you are load balancing.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010099 Port int
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100100
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100101 // HalfClosed provides the ability for one end of the connection to
102 // terminate its output while still receiving data from the other end. This
103 // is only available on TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100104 HalfClosed bool
105
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100106 // Timeout represents the timeout value between a load balancer and its
107 // nodes. Defaults to 30 seconds with a maximum of 120 seconds.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100108 Timeout int
109
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100110 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100111 Cluster Cluster
112
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100113 // Nodes shows all the back-end nodes which are associated with the load
114 // balancer. These are the devices which are delivered traffic.
Jamie Hannaford21a3eb12014-11-03 10:34:29 +0100115 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100116
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100117 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100118 ConnectionLogging ConnectionLogging
Jamie Hannaford07c06962014-10-31 16:42:03 +0100119
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100120 // SessionPersistence specifies whether multiple requests from clients are
121 // directed to the same node.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100122 SessionPersistence SessionPersistence
123
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100124 // ConnectionThrottle specifies a limit on the number of connections per IP
125 // address to help mitigate malicious or abusive traffic to your applications.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100126 ConnectionThrottle ConnectionThrottle
127
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100128 // TODO
Jamie Hannaford07c06962014-10-31 16:42:03 +0100129 SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"`
130}
131
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100132// SourceAddrs - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100133type SourceAddrs struct {
134 IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"`
135 IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"`
136 IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"`
137 IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"`
138}
139
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100140// SessionPersistence - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100141type SessionPersistence struct {
142 Type string `json:"persistenceType" mapstructure:"persistenceType"`
143}
144
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100145// ConnectionThrottle - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100146type ConnectionThrottle struct {
147 MinConns int `json:"minConnections" mapstructure:"minConnections"`
148 MaxConns int `json:"maxConnections" mapstructure:"maxConnections"`
149 MaxConnRate int `json:"maxConnectionRate" mapstructure:"maxConnectionRate"`
150 RateInterval int `json:"rateInterval" mapstructure:"rateInterval"`
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100151}
152
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100153// ConnectionLogging - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100154type ConnectionLogging struct {
155 Enabled bool
156}
157
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100158// Cluster - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100159type Cluster struct {
160 Name string
161}
162
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100163// LBPage is the page returned by a pager when traversing over a collection of
164// LBs.
165type LBPage struct {
166 pagination.LinkedPageBase
167}
168
169// IsEmpty checks whether a NetworkPage struct is empty.
170func (p LBPage) IsEmpty() (bool, error) {
171 is, err := ExtractLBs(p)
172 if err != nil {
173 return true, nil
174 }
175 return len(is) == 0, nil
176}
177
178// ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts
179// the elements into a slice of LoadBalancer structs. In other words, a generic
180// collection is mapped into a relevant slice.
181func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) {
182 var resp struct {
183 LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
184 }
185
186 err := mapstructure.Decode(page.(LBPage).Body, &resp)
187
188 return resp.LBs, err
189}
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100190
191type commonResult struct {
192 gophercloud.Result
193}
194
195// Extract interprets any commonResult as a LB, if possible.
196func (r commonResult) Extract() (*LoadBalancer, error) {
197 if r.Err != nil {
198 return nil, r.Err
199 }
200
201 var response struct {
202 LB LoadBalancer `mapstructure:"loadBalancer"`
203 }
204
205 err := mapstructure.Decode(r.Body, &response)
206
207 return &response.LB, err
208}
209
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100210// CreateResult represents the result of a create operation.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100211type CreateResult struct {
212 commonResult
213}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100214
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100215// DeleteResult represents the result of a delete operation.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100216type DeleteResult struct {
217 gophercloud.ErrResult
218}
Jamie Hannaford07c06962014-10-31 16:42:03 +0100219
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100220// UpdateResult represents the result of an update operation.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100221type UpdateResult struct {
222 gophercloud.ErrResult
223}
224
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100225// GetResult represents the result of a get operation.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100226type GetResult struct {
227 commonResult
228}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100229
230// ProtocolPage is the page returned by a pager when traversing over a
231// collection of LB protocols.
232type ProtocolPage struct {
233 pagination.SinglePageBase
234}
235
236// IsEmpty checks whether a ProtocolPage struct is empty.
237func (p ProtocolPage) IsEmpty() (bool, error) {
238 is, err := ExtractProtocols(p)
239 if err != nil {
240 return true, nil
241 }
242 return len(is) == 0, nil
243}
244
245// ExtractProtocols accepts a Page struct, specifically a ProtocolPage struct,
246// and extracts the elements into a slice of LoadBalancer structs. In other
247// words, a generic collection is mapped into a relevant slice.
248func ExtractProtocols(page pagination.Page) ([]Protocol, error) {
249 var resp struct {
250 Protocols []Protocol `mapstructure:"protocols" json:"protocols"`
251 }
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100252 err := mapstructure.Decode(page.(ProtocolPage).Body, &resp)
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100253 return resp.Protocols, err
254}
Jamie Hannaford46336282014-11-04 14:48:20 +0100255
256// AlgorithmPage is the page returned by a pager when traversing over a
257// collection of LB algorithms.
258type AlgorithmPage struct {
259 pagination.SinglePageBase
260}
261
262// IsEmpty checks whether a ProtocolPage struct is empty.
263func (p AlgorithmPage) IsEmpty() (bool, error) {
264 is, err := ExtractAlgorithms(p)
265 if err != nil {
266 return true, nil
267 }
268 return len(is) == 0, nil
269}
270
271// ExtractAlgorithms accepts a Page struct, specifically a AlgorithmPage struct,
272// and extracts the elements into a slice of Algorithm structs. In other
273// words, a generic collection is mapped into a relevant slice.
274func ExtractAlgorithms(page pagination.Page) ([]Algorithm, error) {
275 var resp struct {
276 Algorithms []Algorithm `mapstructure:"algorithms" json:"algorithms"`
277 }
278 err := mapstructure.Decode(page.(AlgorithmPage).Body, &resp)
279 return resp.Algorithms, err
280}