blob: 359d8e5b01cefee6c283d33095b6d2d3497e7c9b [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 Hannaford186d4e22014-10-31 12:26:11 +010013type Protocol string
14
15// The constants below represent all the compatible load balancer protocols.
16const (
17 // DNSTCP is a protocol that works with IPv6 and allows your DNS server to
18 // receive traffic using TCP port 53.
19 DNSTCP = "DNS_TCP"
20
21 // DNSUDP is a protocol that works with IPv6 and allows your DNS server to
22 // receive traffic using UDP port 53.
23 DNSUDP = "DNS_UDP"
24
25 // TCP is one of the core protocols of the Internet Protocol Suite. It
26 // provides a reliable, ordered delivery of a stream of bytes from one
27 // program on a computer to another program on another computer. Applications
28 // that require an ordered and reliable delivery of packets use this protocol.
29 TCP = "TCP"
30
31 // TCPCLIENTFIRST is a protocol similar to TCP, but is more efficient when a
32 // client is expected to write the data first.
33 TCPCLIENTFIRST = "TCP_CLIENT_FIRST"
34
35 // UDP provides a datagram service that emphasizes speed over reliability. It
36 // works well with applications that provide security through other measures.
37 UDP = "UDP"
38
39 // UDPSTREAM is a protocol designed to stream media over networks and is
40 // built on top of UDP.
41 UDPSTREAM = "UDP_STREAM"
42)
43
44// Algorithm defines how traffic should be directed between back-end nodes.
45type Algorithm string
46
47const (
48 // LC directs traffic to the node with the lowest number of connections.
49 LC = "LEAST_CONNECTIONS"
50
51 // RAND directs traffic to nodes at random.
52 RAND = "RANDOM"
53
54 // RR directs traffic to each of the nodes in turn.
55 RR = "ROUND_ROBIN"
56
57 // WLC directs traffic to a node based on the number of concurrent
58 // connections and its weight.
59 WLC = "WEIGHTED_LEAST_CONNECTIONS"
60
61 // WRR directs traffic to a node according to the RR algorithm, but with
62 // different proportions of traffic being directed to the back-end nodes.
63 // Weights must be defined as part of the node configuration.
64 WRR = "WEIGHTED_ROUND_ROBIN"
65)
66
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010067// Status represents the potential state of a load balancer resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010068type Status string
69
70const (
71 // ACTIVE indicates that the LB is configured properly and ready to serve
72 // traffic to incoming requests via the configured virtual IPs.
73 ACTIVE = "ACTIVE"
74
75 // BUILD indicates that the LB is being provisioned for the first time and
76 // configuration is being applied to bring the service online. The service
77 // cannot yet serve incoming requests.
78 BUILD = "BUILD"
79
80 // PENDINGUPDATE indicates that the LB is online but configuration changes
81 // are being applied to update the service based on a previous request.
82 PENDINGUPDATE = "PENDING_UPDATE"
83
84 // PENDINGDELETE indicates that the LB is online but configuration changes
85 // are being applied to begin deletion of the service based on a previous
86 // request.
87 PENDINGDELETE = "PENDING_DELETE"
88
89 // SUSPENDED indicates that the LB has been taken offline and disabled.
90 SUSPENDED = "SUSPENDED"
91
92 // ERROR indicates that the system encountered an error when attempting to
93 // configure the load balancer.
94 ERROR = "ERROR"
95
96 // DELETED indicates that the LB has been deleted.
97 DELETED = "DELETED"
98)
99
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100100// Datetime represents the structure of a Created or Updated field.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100101type Datetime struct {
102 Time string
103}
104
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100105// LoadBalancer represents a load balancer API resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100106type LoadBalancer struct {
107 // Human-readable name for the load balancer.
108 Name string
109
110 // The unique ID for the load balancer.
111 ID int
112
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100113 // Represents the service protocol being load balanced. See Protocol type for
114 // a list of accepted values.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100115 Protocol Protocol
116
117 // Defines how traffic should be directed between back-end nodes. The default
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100118 // algorithm is RANDOM. See Algorithm type for a list of accepted values.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100119 Algorithm Algorithm
120
121 // The current status of the load balancer.
122 Status Status
123
124 // The number of load balancer nodes.
125 NodeCount int `mapstructure:"nodeCount"`
126
127 // Slice of virtual IPs associated with this load balancer.
Jamie Hannaford1c817312014-11-04 10:56:58 +0100128 VIPs []vips.VIP `mapstructure:"virtualIps"`
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100129
130 // Datetime when the LB was created.
131 Created Datetime
132
133 // Datetime when the LB was created.
134 Updated Datetime
135
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100136 // Port number for the service you are load balancing.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100137 Port int
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100138
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100139 // HalfClosed provides the ability for one end of the connection to
140 // terminate its output while still receiving data from the other end. This
141 // is only available on TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100142 HalfClosed bool
143
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100144 // Timeout represents the timeout value between a load balancer and its
145 // nodes. Defaults to 30 seconds with a maximum of 120 seconds.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100146 Timeout int
147
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100148 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100149 Cluster Cluster
150
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100151 // Nodes shows all the back-end nodes which are associated with the load
152 // balancer. These are the devices which are delivered traffic.
Jamie Hannaford21a3eb12014-11-03 10:34:29 +0100153 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100154
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100155 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100156 ConnectionLogging ConnectionLogging
Jamie Hannaford07c06962014-10-31 16:42:03 +0100157
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100158 // SessionPersistence specifies whether multiple requests from clients are
159 // directed to the same node.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100160 SessionPersistence SessionPersistence
161
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100162 // ConnectionThrottle specifies a limit on the number of connections per IP
163 // address to help mitigate malicious or abusive traffic to your applications.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100164 ConnectionThrottle ConnectionThrottle
165
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100166 // TODO
Jamie Hannaford07c06962014-10-31 16:42:03 +0100167 SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"`
168}
169
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100170// SourceAddrs - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100171type SourceAddrs struct {
172 IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"`
173 IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"`
174 IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"`
175 IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"`
176}
177
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100178// SessionPersistence - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100179type SessionPersistence struct {
180 Type string `json:"persistenceType" mapstructure:"persistenceType"`
181}
182
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100183// ConnectionThrottle - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100184type ConnectionThrottle struct {
185 MinConns int `json:"minConnections" mapstructure:"minConnections"`
186 MaxConns int `json:"maxConnections" mapstructure:"maxConnections"`
187 MaxConnRate int `json:"maxConnectionRate" mapstructure:"maxConnectionRate"`
188 RateInterval int `json:"rateInterval" mapstructure:"rateInterval"`
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100189}
190
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100191// ConnectionLogging - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100192type ConnectionLogging struct {
193 Enabled bool
194}
195
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100196// Cluster - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100197type Cluster struct {
198 Name string
199}
200
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100201// LBPage is the page returned by a pager when traversing over a collection of
202// LBs.
203type LBPage struct {
204 pagination.LinkedPageBase
205}
206
207// IsEmpty checks whether a NetworkPage struct is empty.
208func (p LBPage) IsEmpty() (bool, error) {
209 is, err := ExtractLBs(p)
210 if err != nil {
211 return true, nil
212 }
213 return len(is) == 0, nil
214}
215
216// ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts
217// the elements into a slice of LoadBalancer structs. In other words, a generic
218// collection is mapped into a relevant slice.
219func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) {
220 var resp struct {
221 LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
222 }
223
224 err := mapstructure.Decode(page.(LBPage).Body, &resp)
225
226 return resp.LBs, err
227}
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100228
229type commonResult struct {
230 gophercloud.Result
231}
232
233// Extract interprets any commonResult as a LB, if possible.
234func (r commonResult) Extract() (*LoadBalancer, error) {
235 if r.Err != nil {
236 return nil, r.Err
237 }
238
239 var response struct {
240 LB LoadBalancer `mapstructure:"loadBalancer"`
241 }
242
243 err := mapstructure.Decode(r.Body, &response)
244
245 return &response.LB, err
246}
247
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100248// CreateResult represents the result of a create operation.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100249type CreateResult struct {
250 commonResult
251}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100252
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100253// DeleteResult represents the result of a delete operation.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100254type DeleteResult struct {
255 gophercloud.ErrResult
256}
Jamie Hannaford07c06962014-10-31 16:42:03 +0100257
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100258// UpdateResult represents the result of an update operation.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100259type UpdateResult struct {
260 gophercloud.ErrResult
261}
262
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100263// GetResult represents the result of a get operation.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100264type GetResult struct {
265 commonResult
266}