blob: 0ad998ae697903a883f6fa8729daa3783e7dd882 [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 Hannaford6bc93aa2014-11-06 12:37:52 +01009 "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions"
Jamie Hannaford1c817312014-11-04 10:56:58 +010010 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010011)
12
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010013// Protocol represents the network protocol which the load balancer accepts.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010014type Protocol struct {
15 // The name of the protocol, e.g. HTTP, LDAP, FTP, etc.
16 Name string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010017
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010018 // The port number for the protocol.
19 Port int
20}
Jamie Hannaford186d4e22014-10-31 12:26:11 +010021
22// Algorithm defines how traffic should be directed between back-end nodes.
Jamie Hannaford46336282014-11-04 14:48:20 +010023type Algorithm struct {
24 // The name of the algorithm, e.g RANDOM, ROUND_ROBIN, etc.
25 Name string
26}
Jamie Hannaford186d4e22014-10-31 12:26:11 +010027
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010028// Status represents the potential state of a load balancer resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010029type Status string
30
31const (
32 // ACTIVE indicates that the LB is configured properly and ready to serve
33 // traffic to incoming requests via the configured virtual IPs.
34 ACTIVE = "ACTIVE"
35
36 // BUILD indicates that the LB is being provisioned for the first time and
37 // configuration is being applied to bring the service online. The service
38 // cannot yet serve incoming requests.
39 BUILD = "BUILD"
40
41 // PENDINGUPDATE indicates that the LB is online but configuration changes
42 // are being applied to update the service based on a previous request.
43 PENDINGUPDATE = "PENDING_UPDATE"
44
45 // PENDINGDELETE indicates that the LB is online but configuration changes
46 // are being applied to begin deletion of the service based on a previous
47 // request.
48 PENDINGDELETE = "PENDING_DELETE"
49
50 // SUSPENDED indicates that the LB has been taken offline and disabled.
51 SUSPENDED = "SUSPENDED"
52
53 // ERROR indicates that the system encountered an error when attempting to
54 // configure the load balancer.
55 ERROR = "ERROR"
56
57 // DELETED indicates that the LB has been deleted.
58 DELETED = "DELETED"
59)
60
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010061// Datetime represents the structure of a Created or Updated field.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010062type Datetime struct {
63 Time string
64}
65
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010066// LoadBalancer represents a load balancer API resource.
Jamie Hannaford186d4e22014-10-31 12:26:11 +010067type LoadBalancer struct {
68 // Human-readable name for the load balancer.
69 Name string
70
71 // The unique ID for the load balancer.
72 ID int
73
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010074 // Represents the service protocol being load balanced. See Protocol type for
75 // a list of accepted values.
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +010076 // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html
77 // for a full list of supported protocols.
78 Protocol string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010079
80 // Defines how traffic should be directed between back-end nodes. The default
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010081 // algorithm is RANDOM. See Algorithm type for a list of accepted values.
Jamie Hannaford46336282014-11-04 14:48:20 +010082 Algorithm string
Jamie Hannaford186d4e22014-10-31 12:26:11 +010083
84 // The current status of the load balancer.
85 Status Status
86
87 // The number of load balancer nodes.
88 NodeCount int `mapstructure:"nodeCount"`
89
90 // Slice of virtual IPs associated with this load balancer.
Jamie Hannaford1c817312014-11-04 10:56:58 +010091 VIPs []vips.VIP `mapstructure:"virtualIps"`
Jamie Hannaford186d4e22014-10-31 12:26:11 +010092
93 // Datetime when the LB was created.
94 Created Datetime
95
96 // Datetime when the LB was created.
97 Updated Datetime
98
Jamie Hannafordb2007ee2014-11-03 16:24:43 +010099 // Port number for the service you are load balancing.
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100100 Port int
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100101
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100102 // HalfClosed provides the ability for one end of the connection to
103 // terminate its output while still receiving data from the other end. This
104 // is only available on TCP/TCP_CLIENT_FIRST protocols.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100105 HalfClosed bool
106
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100107 // Timeout represents the timeout value between a load balancer and its
108 // nodes. Defaults to 30 seconds with a maximum of 120 seconds.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100109 Timeout int
110
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100111 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100112 Cluster Cluster
113
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100114 // Nodes shows all the back-end nodes which are associated with the load
115 // balancer. These are the devices which are delivered traffic.
Jamie Hannaford21a3eb12014-11-03 10:34:29 +0100116 Nodes []nodes.Node
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100117
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100118 // TODO
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100119 ConnectionLogging ConnectionLogging
Jamie Hannaford07c06962014-10-31 16:42:03 +0100120
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100121 // SessionPersistence specifies whether multiple requests from clients are
122 // directed to the same node.
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +0100123 SessionPersistence sessions.SessionPersistence
Jamie Hannaford07c06962014-10-31 16:42:03 +0100124
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100125 // ConnectionThrottle specifies a limit on the number of connections per IP
126 // address to help mitigate malicious or abusive traffic to your applications.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100127 ConnectionThrottle ConnectionThrottle
128
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100129 // TODO
Jamie Hannaford07c06962014-10-31 16:42:03 +0100130 SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"`
131}
132
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100133// SourceAddrs - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100134type SourceAddrs struct {
135 IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"`
136 IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"`
137 IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"`
138 IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"`
139}
140
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100141// ConnectionThrottle - temp
Jamie Hannaford07c06962014-10-31 16:42:03 +0100142type ConnectionThrottle struct {
143 MinConns int `json:"minConnections" mapstructure:"minConnections"`
144 MaxConns int `json:"maxConnections" mapstructure:"maxConnections"`
145 MaxConnRate int `json:"maxConnectionRate" mapstructure:"maxConnectionRate"`
146 RateInterval int `json:"rateInterval" mapstructure:"rateInterval"`
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100147}
148
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100149// ConnectionLogging - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100150type ConnectionLogging struct {
151 Enabled bool
152}
153
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100154// Cluster - temp
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100155type Cluster struct {
156 Name string
157}
158
Jamie Hannaford186d4e22014-10-31 12:26:11 +0100159// LBPage is the page returned by a pager when traversing over a collection of
160// LBs.
161type LBPage struct {
162 pagination.LinkedPageBase
163}
164
165// IsEmpty checks whether a NetworkPage struct is empty.
166func (p LBPage) IsEmpty() (bool, error) {
167 is, err := ExtractLBs(p)
168 if err != nil {
169 return true, nil
170 }
171 return len(is) == 0, nil
172}
173
174// ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts
175// the elements into a slice of LoadBalancer structs. In other words, a generic
176// collection is mapped into a relevant slice.
177func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) {
178 var resp struct {
179 LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
180 }
181
182 err := mapstructure.Decode(page.(LBPage).Body, &resp)
183
184 return resp.LBs, err
185}
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100186
187type commonResult struct {
188 gophercloud.Result
189}
190
191// Extract interprets any commonResult as a LB, if possible.
192func (r commonResult) Extract() (*LoadBalancer, error) {
193 if r.Err != nil {
194 return nil, r.Err
195 }
196
197 var response struct {
198 LB LoadBalancer `mapstructure:"loadBalancer"`
199 }
200
201 err := mapstructure.Decode(r.Body, &response)
202
203 return &response.LB, err
204}
205
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100206// CreateResult represents the result of a create operation.
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100207type CreateResult struct {
208 commonResult
209}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100210
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100211// DeleteResult represents the result of a delete operation.
Jamie Hannaford1c260332014-10-31 15:57:22 +0100212type DeleteResult struct {
213 gophercloud.ErrResult
214}
Jamie Hannaford07c06962014-10-31 16:42:03 +0100215
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100216// UpdateResult represents the result of an update operation.
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100217type UpdateResult struct {
218 gophercloud.ErrResult
219}
220
Jamie Hannafordb2007ee2014-11-03 16:24:43 +0100221// GetResult represents the result of a get operation.
Jamie Hannaford07c06962014-10-31 16:42:03 +0100222type GetResult struct {
223 commonResult
224}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100225
226// ProtocolPage is the page returned by a pager when traversing over a
227// collection of LB protocols.
228type ProtocolPage struct {
229 pagination.SinglePageBase
230}
231
232// IsEmpty checks whether a ProtocolPage struct is empty.
233func (p ProtocolPage) IsEmpty() (bool, error) {
234 is, err := ExtractProtocols(p)
235 if err != nil {
236 return true, nil
237 }
238 return len(is) == 0, nil
239}
240
241// ExtractProtocols accepts a Page struct, specifically a ProtocolPage struct,
242// and extracts the elements into a slice of LoadBalancer structs. In other
243// words, a generic collection is mapped into a relevant slice.
244func ExtractProtocols(page pagination.Page) ([]Protocol, error) {
245 var resp struct {
246 Protocols []Protocol `mapstructure:"protocols" json:"protocols"`
247 }
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100248 err := mapstructure.Decode(page.(ProtocolPage).Body, &resp)
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100249 return resp.Protocols, err
250}
Jamie Hannaford46336282014-11-04 14:48:20 +0100251
252// AlgorithmPage is the page returned by a pager when traversing over a
253// collection of LB algorithms.
254type AlgorithmPage struct {
255 pagination.SinglePageBase
256}
257
258// IsEmpty checks whether a ProtocolPage struct is empty.
259func (p AlgorithmPage) IsEmpty() (bool, error) {
260 is, err := ExtractAlgorithms(p)
261 if err != nil {
262 return true, nil
263 }
264 return len(is) == 0, nil
265}
266
267// ExtractAlgorithms accepts a Page struct, specifically a AlgorithmPage struct,
268// and extracts the elements into a slice of Algorithm structs. In other
269// words, a generic collection is mapped into a relevant slice.
270func ExtractAlgorithms(page pagination.Page) ([]Algorithm, error) {
271 var resp struct {
272 Algorithms []Algorithm `mapstructure:"algorithms" json:"algorithms"`
273 }
274 err := mapstructure.Decode(page.(AlgorithmPage).Body, &resp)
275 return resp.Algorithms, err
276}