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 | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame^] | 9 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 10 | ) |
| 11 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 12 | // Protocol represents the network protocol which the load balancer accepts. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 13 | type Protocol string |
| 14 | |
| 15 | // The constants below represent all the compatible load balancer protocols. |
| 16 | const ( |
| 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. |
| 45 | type Algorithm string |
| 46 | |
| 47 | const ( |
| 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 67 | // Status represents the potential state of a load balancer resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 68 | type Status string |
| 69 | |
| 70 | const ( |
| 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 100 | // Datetime represents the structure of a Created or Updated field. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 101 | type Datetime struct { |
| 102 | Time string |
| 103 | } |
| 104 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 105 | // LoadBalancer represents a load balancer API resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 106 | type 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 113 | // Represents the service protocol being load balanced. See Protocol type for |
| 114 | // a list of accepted values. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 115 | Protocol Protocol |
| 116 | |
| 117 | // Defines how traffic should be directed between back-end nodes. The default |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 118 | // algorithm is RANDOM. See Algorithm type for a list of accepted values. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 119 | 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 Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame^] | 128 | VIPs []vips.VIP `mapstructure:"virtualIps"` |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 129 | |
| 130 | // Datetime when the LB was created. |
| 131 | Created Datetime |
| 132 | |
| 133 | // Datetime when the LB was created. |
| 134 | Updated Datetime |
| 135 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 136 | // Port number for the service you are load balancing. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 137 | Port int |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 138 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 139 | // 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 142 | HalfClosed bool |
| 143 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 144 | // Timeout represents the timeout value between a load balancer and its |
| 145 | // nodes. Defaults to 30 seconds with a maximum of 120 seconds. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 146 | Timeout int |
| 147 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 148 | // TODO |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 149 | Cluster Cluster |
| 150 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 151 | // Nodes shows all the back-end nodes which are associated with the load |
| 152 | // balancer. These are the devices which are delivered traffic. |
Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 153 | Nodes []nodes.Node |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 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 | ConnectionLogging ConnectionLogging |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 157 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 158 | // SessionPersistence specifies whether multiple requests from clients are |
| 159 | // directed to the same node. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 160 | SessionPersistence SessionPersistence |
| 161 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 162 | // ConnectionThrottle specifies a limit on the number of connections per IP |
| 163 | // address to help mitigate malicious or abusive traffic to your applications. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 164 | ConnectionThrottle ConnectionThrottle |
| 165 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 166 | // TODO |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 167 | SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"` |
| 168 | } |
| 169 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 170 | // SourceAddrs - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 171 | type 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 178 | // SessionPersistence - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 179 | type SessionPersistence struct { |
| 180 | Type string `json:"persistenceType" mapstructure:"persistenceType"` |
| 181 | } |
| 182 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 183 | // ConnectionThrottle - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 184 | type 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 191 | // ConnectionLogging - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 192 | type ConnectionLogging struct { |
| 193 | Enabled bool |
| 194 | } |
| 195 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 196 | // Cluster - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 197 | type Cluster struct { |
| 198 | Name string |
| 199 | } |
| 200 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 201 | // LBPage is the page returned by a pager when traversing over a collection of |
| 202 | // LBs. |
| 203 | type LBPage struct { |
| 204 | pagination.LinkedPageBase |
| 205 | } |
| 206 | |
| 207 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 208 | func (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. |
| 219 | func 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 228 | |
| 229 | type commonResult struct { |
| 230 | gophercloud.Result |
| 231 | } |
| 232 | |
| 233 | // Extract interprets any commonResult as a LB, if possible. |
| 234 | func (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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 248 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 249 | type CreateResult struct { |
| 250 | commonResult |
| 251 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 252 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 253 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 254 | type DeleteResult struct { |
| 255 | gophercloud.ErrResult |
| 256 | } |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 257 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 258 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 259 | type UpdateResult struct { |
| 260 | gophercloud.ErrResult |
| 261 | } |
| 262 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 263 | // GetResult represents the result of a get operation. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 264 | type GetResult struct { |
| 265 | commonResult |
| 266 | } |