blob: 2feb8565bb0c11ce9f4458a8fbc81e29464d7575 [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 Hannaford186d4e22014-10-31 12:26:11 +01009)
10
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010011// Protocol represents the network protocol which the load balancer accepts.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010012type Protocol string
13
14// The constants below represent all the compatible load balancer protocols.
15const (
16 // DNSTCP is a protocol that works with IPv6 and allows your DNS server to
17 // receive traffic using TCP port 53.
18 DNSTCP = "DNS_TCP"
19
20 // DNSUDP is a protocol that works with IPv6 and allows your DNS server to
21 // receive traffic using UDP port 53.
22 DNSUDP = "DNS_UDP"
23
24 // TCP is one of the core protocols of the Internet Protocol Suite. It
25 // provides a reliable, ordered delivery of a stream of bytes from one
26 // program on a computer to another program on another computer. Applications
27 // that require an ordered and reliable delivery of packets use this protocol.
28 TCP = "TCP"
29
30 // TCPCLIENTFIRST is a protocol similar to TCP, but is more efficient when a
31 // client is expected to write the data first.
32 TCPCLIENTFIRST = "TCP_CLIENT_FIRST"
33
34 // UDP provides a datagram service that emphasizes speed over reliability. It
35 // works well with applications that provide security through other measures.
36 UDP = "UDP"
37
38 // UDPSTREAM is a protocol designed to stream media over networks and is
39 // built on top of UDP.
40 UDPSTREAM = "UDP_STREAM"
41)
42
43// Algorithm defines how traffic should be directed between back-end nodes.
44type Algorithm string
45
46const (
47 // LC directs traffic to the node with the lowest number of connections.
48 LC = "LEAST_CONNECTIONS"
49
50 // RAND directs traffic to nodes at random.
51 RAND = "RANDOM"
52
53 // RR directs traffic to each of the nodes in turn.
54 RR = "ROUND_ROBIN"
55
56 // WLC directs traffic to a node based on the number of concurrent
57 // connections and its weight.
58 WLC = "WEIGHTED_LEAST_CONNECTIONS"
59
60 // WRR directs traffic to a node according to the RR algorithm, but with
61 // different proportions of traffic being directed to the back-end nodes.
62 // Weights must be defined as part of the node configuration.
63 WRR = "WEIGHTED_ROUND_ROBIN"
64)
65
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010066// Status represents the potential state of a load balancer resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010067type Status string
68
69const (
70 // ACTIVE indicates that the LB is configured properly and ready to serve
71 // traffic to incoming requests via the configured virtual IPs.
72 ACTIVE = "ACTIVE"
73
74 // BUILD indicates that the LB is being provisioned for the first time and
75 // configuration is being applied to bring the service online. The service
76 // cannot yet serve incoming requests.
77 BUILD = "BUILD"
78
79 // PENDINGUPDATE indicates that the LB is online but configuration changes
80 // are being applied to update the service based on a previous request.
81 PENDINGUPDATE = "PENDING_UPDATE"
82
83 // PENDINGDELETE indicates that the LB is online but configuration changes
84 // are being applied to begin deletion of the service based on a previous
85 // request.
86 PENDINGDELETE = "PENDING_DELETE"
87
88 // SUSPENDED indicates that the LB has been taken offline and disabled.
89 SUSPENDED = "SUSPENDED"
90
91 // ERROR indicates that the system encountered an error when attempting to
92 // configure the load balancer.
93 ERROR = "ERROR"
94
95 // DELETED indicates that the LB has been deleted.
96 DELETED = "DELETED"
97)
98
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010099// Datetime represents the structure of a Created or Updated field.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100100type Datetime struct {
101 Time string
102}
103
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100104// VIP - temp
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100105type VIP struct {
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100106 Address string `json:"address,omitempty"`
107 ID int `json:"id,omitempty"`
108 Type string `json:"type,omitempty"`
109 Version string `json:"ipVersion,omitempty" mapstructure:"ipVersion"`
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100110}
111
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100112// LoadBalancer represents a load balancer API resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100113type LoadBalancer struct {
114 // Human-readable name for the load balancer.
115 Name string
116
117 // The unique ID for the load balancer.
118 ID int
119
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100120 // Represents the service protocol being load balanced. See Protocol type for
121 // a list of accepted values.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100122 Protocol Protocol
123
124 // Defines how traffic should be directed between back-end nodes. The default
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100125 // algorithm is RANDOM. See Algorithm type for a list of accepted values.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100126 Algorithm Algorithm
127
128 // The current status of the load balancer.
129 Status Status
130
131 // The number of load balancer nodes.
132 NodeCount int `mapstructure:"nodeCount"`
133
134 // Slice of virtual IPs associated with this load balancer.
135 VIPs []VIP `mapstructure:"virtualIps"`
136
137 // Datetime when the LB was created.
138 Created Datetime
139
140 // Datetime when the LB was created.
141 Updated Datetime
142
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100143 // Port number for the service you are load balancing.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100144 Port int
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100145
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100146 // HalfClosed provides the ability for one end of the connection to
147 // terminate its output while still receiving data from the other end. This
148 // is only available on TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100149 HalfClosed bool
150
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100151 // Timeout represents the timeout value between a load balancer and its
152 // nodes. Defaults to 30 seconds with a maximum of 120 seconds.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100153 Timeout int
154
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100155 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100156 Cluster Cluster
157
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100158 // Nodes shows all the back-end nodes which are associated with the load
159 // balancer. These are the devices which are delivered traffic.
Jamie Hannaford21a3eb12014-11-03 10:34:29 +0100160 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100161
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100162 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100163 ConnectionLogging ConnectionLogging
Jamie Hannaford07c06962014-10-31 16:42:03 +0100164
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100165 // SessionPersistence specifies whether multiple requests from clients are
166 // directed to the same node.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100167 SessionPersistence SessionPersistence
168
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100169 // ConnectionThrottle specifies a limit on the number of connections per IP
170 // address to help mitigate malicious or abusive traffic to your applications.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100171 ConnectionThrottle ConnectionThrottle
172
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100173 // TODO
Jamie Hannaford07c06962014-10-31 16:42:03 +0100174 SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"`
175}
176
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100177// SourceAddrs - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100178type SourceAddrs struct {
179 IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"`
180 IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"`
181 IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"`
182 IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"`
183}
184
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100185// SessionPersistence - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100186type SessionPersistence struct {
187 Type string `json:"persistenceType" mapstructure:"persistenceType"`
188}
189
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100190// ConnectionThrottle - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100191type ConnectionThrottle struct {
192 MinConns int `json:"minConnections" mapstructure:"minConnections"`
193 MaxConns int `json:"maxConnections" mapstructure:"maxConnections"`
194 MaxConnRate int `json:"maxConnectionRate" mapstructure:"maxConnectionRate"`
195 RateInterval int `json:"rateInterval" mapstructure:"rateInterval"`
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100196}
197
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100198// ConnectionLogging - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100199type ConnectionLogging struct {
200 Enabled bool
201}
202
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100203// Cluster - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100204type Cluster struct {
205 Name string
206}
207
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100208// LBPage is the page returned by a pager when traversing over a collection of
209// LBs.
210type LBPage struct {
211 pagination.LinkedPageBase
212}
213
214// IsEmpty checks whether a NetworkPage struct is empty.
215func (p LBPage) IsEmpty() (bool, error) {
216 is, err := ExtractLBs(p)
217 if err != nil {
218 return true, nil
219 }
220 return len(is) == 0, nil
221}
222
223// ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts
224// the elements into a slice of LoadBalancer structs. In other words, a generic
225// collection is mapped into a relevant slice.
226func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) {
227 var resp struct {
228 LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
229 }
230
231 err := mapstructure.Decode(page.(LBPage).Body, &resp)
232
233 return resp.LBs, err
234}
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100235
236type commonResult struct {
237 gophercloud.Result
238}
239
240// Extract interprets any commonResult as a LB, if possible.
241func (r commonResult) Extract() (*LoadBalancer, error) {
242 if r.Err != nil {
243 return nil, r.Err
244 }
245
246 var response struct {
247 LB LoadBalancer `mapstructure:"loadBalancer"`
248 }
249
250 err := mapstructure.Decode(r.Body, &response)
251
252 return &response.LB, err
253}
254
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100255// CreateResult represents the result of a create operation.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100256type CreateResult struct {
257 commonResult
258}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100259
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100260// DeleteResult represents the result of a delete operation.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100261type DeleteResult struct {
262 gophercloud.ErrResult
263}
Jamie Hannaford07c06962014-10-31 16:42:03 +0100264
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100265// UpdateResult represents the result of an update operation.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100266type UpdateResult struct {
267 gophercloud.ErrResult
268}
269
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100270// GetResult represents the result of a get operation.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100271type GetResult struct {
272 commonResult
273}