blob: d73d82389dac7fb31f8aff0031e3e80682b0f96e [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.
22type Algorithm string
23
24const (
25 // LC directs traffic to the node with the lowest number of connections.
26 LC = "LEAST_CONNECTIONS"
27
28 // RAND directs traffic to nodes at random.
29 RAND = "RANDOM"
30
31 // RR directs traffic to each of the nodes in turn.
32 RR = "ROUND_ROBIN"
33
34 // WLC directs traffic to a node based on the number of concurrent
35 // connections and its weight.
36 WLC = "WEIGHTED_LEAST_CONNECTIONS"
37
38 // WRR directs traffic to a node according to the RR algorithm, but with
39 // different proportions of traffic being directed to the back-end nodes.
40 // Weights must be defined as part of the node configuration.
41 WRR = "WEIGHTED_ROUND_ROBIN"
42)
43
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010044// Status represents the potential state of a load balancer resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010045type Status string
46
47const (
48 // ACTIVE indicates that the LB is configured properly and ready to serve
49 // traffic to incoming requests via the configured virtual IPs.
50 ACTIVE = "ACTIVE"
51
52 // BUILD indicates that the LB is being provisioned for the first time and
53 // configuration is being applied to bring the service online. The service
54 // cannot yet serve incoming requests.
55 BUILD = "BUILD"
56
57 // PENDINGUPDATE indicates that the LB is online but configuration changes
58 // are being applied to update the service based on a previous request.
59 PENDINGUPDATE = "PENDING_UPDATE"
60
61 // PENDINGDELETE indicates that the LB is online but configuration changes
62 // are being applied to begin deletion of the service based on a previous
63 // request.
64 PENDINGDELETE = "PENDING_DELETE"
65
66 // SUSPENDED indicates that the LB has been taken offline and disabled.
67 SUSPENDED = "SUSPENDED"
68
69 // ERROR indicates that the system encountered an error when attempting to
70 // configure the load balancer.
71 ERROR = "ERROR"
72
73 // DELETED indicates that the LB has been deleted.
74 DELETED = "DELETED"
75)
76
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010077// Datetime represents the structure of a Created or Updated field.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010078type Datetime struct {
79 Time string
80}
81
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010082// LoadBalancer represents a load balancer API resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010083type LoadBalancer struct {
84 // Human-readable name for the load balancer.
85 Name string
86
87 // The unique ID for the load balancer.
88 ID int
89
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010090 // Represents the service protocol being load balanced. See Protocol type for
91 // a list of accepted values.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010092 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
93 // for a full list of supported protocols.
94 Protocol string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010095
96 // Defines how traffic should be directed between back-end nodes. The default
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010097 // algorithm is RANDOM. See Algorithm type for a list of accepted values.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010098 Algorithm Algorithm
99
100 // The current status of the load balancer.
101 Status Status
102
103 // The number of load balancer nodes.
104 NodeCount int `mapstructure:"nodeCount"`
105
106 // Slice of virtual IPs associated with this load balancer.
Jamie Hannaford1c817312014-11-04 10:56:58 +0100107 VIPs []vips.VIP `mapstructure:"virtualIps"`
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100108
109 // Datetime when the LB was created.
110 Created Datetime
111
112 // Datetime when the LB was created.
113 Updated Datetime
114
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100115 // Port number for the service you are load balancing.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100116 Port int
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100117
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100118 // HalfClosed provides the ability for one end of the connection to
119 // terminate its output while still receiving data from the other end. This
120 // is only available on TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100121 HalfClosed bool
122
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100123 // Timeout represents the timeout value between a load balancer and its
124 // nodes. Defaults to 30 seconds with a maximum of 120 seconds.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100125 Timeout int
126
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100127 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100128 Cluster Cluster
129
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100130 // Nodes shows all the back-end nodes which are associated with the load
131 // balancer. These are the devices which are delivered traffic.
Jamie Hannaford21a3eb12014-11-03 10:34:29 +0100132 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100133
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100134 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100135 ConnectionLogging ConnectionLogging
Jamie Hannaford07c06962014-10-31 16:42:03 +0100136
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100137 // SessionPersistence specifies whether multiple requests from clients are
138 // directed to the same node.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100139 SessionPersistence SessionPersistence
140
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100141 // ConnectionThrottle specifies a limit on the number of connections per IP
142 // address to help mitigate malicious or abusive traffic to your applications.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100143 ConnectionThrottle ConnectionThrottle
144
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100145 // TODO
Jamie Hannaford07c06962014-10-31 16:42:03 +0100146 SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"`
147}
148
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100149// SourceAddrs - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100150type SourceAddrs struct {
151 IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"`
152 IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"`
153 IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"`
154 IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"`
155}
156
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100157// SessionPersistence - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100158type SessionPersistence struct {
159 Type string `json:"persistenceType" mapstructure:"persistenceType"`
160}
161
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100162// ConnectionThrottle - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100163type ConnectionThrottle struct {
164 MinConns int `json:"minConnections" mapstructure:"minConnections"`
165 MaxConns int `json:"maxConnections" mapstructure:"maxConnections"`
166 MaxConnRate int `json:"maxConnectionRate" mapstructure:"maxConnectionRate"`
167 RateInterval int `json:"rateInterval" mapstructure:"rateInterval"`
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100168}
169
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100170// ConnectionLogging - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100171type ConnectionLogging struct {
172 Enabled bool
173}
174
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100175// Cluster - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100176type Cluster struct {
177 Name string
178}
179
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100180// LBPage is the page returned by a pager when traversing over a collection of
181// LBs.
182type LBPage struct {
183 pagination.LinkedPageBase
184}
185
186// IsEmpty checks whether a NetworkPage struct is empty.
187func (p LBPage) IsEmpty() (bool, error) {
188 is, err := ExtractLBs(p)
189 if err != nil {
190 return true, nil
191 }
192 return len(is) == 0, nil
193}
194
195// ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts
196// the elements into a slice of LoadBalancer structs. In other words, a generic
197// collection is mapped into a relevant slice.
198func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) {
199 var resp struct {
200 LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
201 }
202
203 err := mapstructure.Decode(page.(LBPage).Body, &resp)
204
205 return resp.LBs, err
206}
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100207
208type commonResult struct {
209 gophercloud.Result
210}
211
212// Extract interprets any commonResult as a LB, if possible.
213func (r commonResult) Extract() (*LoadBalancer, error) {
214 if r.Err != nil {
215 return nil, r.Err
216 }
217
218 var response struct {
219 LB LoadBalancer `mapstructure:"loadBalancer"`
220 }
221
222 err := mapstructure.Decode(r.Body, &response)
223
224 return &response.LB, err
225}
226
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100227// CreateResult represents the result of a create operation.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100228type CreateResult struct {
229 commonResult
230}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100231
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100232// DeleteResult represents the result of a delete operation.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100233type DeleteResult struct {
234 gophercloud.ErrResult
235}
Jamie Hannaford07c06962014-10-31 16:42:03 +0100236
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100237// UpdateResult represents the result of an update operation.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100238type UpdateResult struct {
239 gophercloud.ErrResult
240}
241
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100242// GetResult represents the result of a get operation.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100243type GetResult struct {
244 commonResult
245}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100246
247// ProtocolPage is the page returned by a pager when traversing over a
248// collection of LB protocols.
249type ProtocolPage struct {
250 pagination.SinglePageBase
251}
252
253// IsEmpty checks whether a ProtocolPage struct is empty.
254func (p ProtocolPage) IsEmpty() (bool, error) {
255 is, err := ExtractProtocols(p)
256 if err != nil {
257 return true, nil
258 }
259 return len(is) == 0, nil
260}
261
262// ExtractProtocols accepts a Page struct, specifically a ProtocolPage struct,
263// and extracts the elements into a slice of LoadBalancer structs. In other
264// words, a generic collection is mapped into a relevant slice.
265func ExtractProtocols(page pagination.Page) ([]Protocol, error) {
266 var resp struct {
267 Protocols []Protocol `mapstructure:"protocols" json:"protocols"`
268 }
269
270 err := mapstructure.Decode(page.(ProtocolPage).Body, &resp)
271
272 return resp.Protocols, err
273}