Jamie Hannaford | fba65af | 2014-11-03 10:32:37 +0100 | [diff] [blame] | 1 | package lbs |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 5 | |
| 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 9 | ) |
| 10 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 11 | // Protocol represents the network protocol which the load balancer accepts. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 12 | type Protocol string |
| 13 | |
| 14 | // The constants below represent all the compatible load balancer protocols. |
| 15 | const ( |
| 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. |
| 44 | type Algorithm string |
| 45 | |
| 46 | const ( |
| 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 66 | // Status represents the potential state of a load balancer resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 67 | type Status string |
| 68 | |
| 69 | const ( |
| 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 99 | // Datetime represents the structure of a Created or Updated field. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 100 | type Datetime struct { |
| 101 | Time string |
| 102 | } |
| 103 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 104 | // VIP - temp |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 105 | type VIP struct { |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 106 | 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 Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 112 | // LoadBalancer represents a load balancer API resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 113 | type 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 120 | // Represents the service protocol being load balanced. See Protocol type for |
| 121 | // a list of accepted values. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 122 | Protocol Protocol |
| 123 | |
| 124 | // Defines how traffic should be directed between back-end nodes. The default |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 125 | // algorithm is RANDOM. See Algorithm type for a list of accepted values. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 126 | 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 143 | // Port number for the service you are load balancing. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 144 | Port int |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 145 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 146 | // 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 149 | HalfClosed bool |
| 150 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 151 | // Timeout represents the timeout value between a load balancer and its |
| 152 | // nodes. Defaults to 30 seconds with a maximum of 120 seconds. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 153 | Timeout int |
| 154 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 155 | // TODO |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 156 | Cluster Cluster |
| 157 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 158 | // Nodes shows all the back-end nodes which are associated with the load |
| 159 | // balancer. These are the devices which are delivered traffic. |
Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 160 | Nodes []nodes.Node |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 161 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 162 | // TODO |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 163 | ConnectionLogging ConnectionLogging |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 164 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 165 | // SessionPersistence specifies whether multiple requests from clients are |
| 166 | // directed to the same node. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 167 | SessionPersistence SessionPersistence |
| 168 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 169 | // ConnectionThrottle specifies a limit on the number of connections per IP |
| 170 | // address to help mitigate malicious or abusive traffic to your applications. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 171 | ConnectionThrottle ConnectionThrottle |
| 172 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 173 | // TODO |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 174 | SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"` |
| 175 | } |
| 176 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 177 | // SourceAddrs - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 178 | type 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 185 | // SessionPersistence - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 186 | type SessionPersistence struct { |
| 187 | Type string `json:"persistenceType" mapstructure:"persistenceType"` |
| 188 | } |
| 189 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 190 | // ConnectionThrottle - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 191 | type 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 198 | // ConnectionLogging - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 199 | type ConnectionLogging struct { |
| 200 | Enabled bool |
| 201 | } |
| 202 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 203 | // Cluster - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 204 | type Cluster struct { |
| 205 | Name string |
| 206 | } |
| 207 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 208 | // LBPage is the page returned by a pager when traversing over a collection of |
| 209 | // LBs. |
| 210 | type LBPage struct { |
| 211 | pagination.LinkedPageBase |
| 212 | } |
| 213 | |
| 214 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 215 | func (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. |
| 226 | func 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 235 | |
| 236 | type commonResult struct { |
| 237 | gophercloud.Result |
| 238 | } |
| 239 | |
| 240 | // Extract interprets any commonResult as a LB, if possible. |
| 241 | func (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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 255 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 256 | type CreateResult struct { |
| 257 | commonResult |
| 258 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 259 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 260 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 261 | type DeleteResult struct { |
| 262 | gophercloud.ErrResult |
| 263 | } |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 264 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 265 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 266 | type UpdateResult struct { |
| 267 | gophercloud.ErrResult |
| 268 | } |
| 269 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 270 | // GetResult represents the result of a get operation. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 271 | type GetResult struct { |
| 272 | commonResult |
| 273 | } |