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 | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame^] | 13 | type Protocol struct { |
| 14 | // The name of the protocol, e.g. HTTP, LDAP, FTP, etc. |
| 15 | Name string |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 16 | |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame^] | 17 | // The port number for the protocol. |
| 18 | Port int |
| 19 | } |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 20 | |
| 21 | // Algorithm defines how traffic should be directed between back-end nodes. |
| 22 | type Algorithm string |
| 23 | |
| 24 | const ( |
| 25 | // LC directs traffic to the node with the lowest number of connections. |
| 26 | LC = "LEAST_CONNECTIONS" |
| 27 | |
| 28 | // RAND directs traffic to nodes at random. |
| 29 | RAND = "RANDOM" |
| 30 | |
| 31 | // RR directs traffic to each of the nodes in turn. |
| 32 | RR = "ROUND_ROBIN" |
| 33 | |
| 34 | // WLC directs traffic to a node based on the number of concurrent |
| 35 | // connections and its weight. |
| 36 | WLC = "WEIGHTED_LEAST_CONNECTIONS" |
| 37 | |
| 38 | // WRR directs traffic to a node according to the RR algorithm, but with |
| 39 | // different proportions of traffic being directed to the back-end nodes. |
| 40 | // Weights must be defined as part of the node configuration. |
| 41 | WRR = "WEIGHTED_ROUND_ROBIN" |
| 42 | ) |
| 43 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 44 | // Status represents the potential state of a load balancer resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 45 | type Status string |
| 46 | |
| 47 | const ( |
| 48 | // ACTIVE indicates that the LB is configured properly and ready to serve |
| 49 | // traffic to incoming requests via the configured virtual IPs. |
| 50 | ACTIVE = "ACTIVE" |
| 51 | |
| 52 | // BUILD indicates that the LB is being provisioned for the first time and |
| 53 | // configuration is being applied to bring the service online. The service |
| 54 | // cannot yet serve incoming requests. |
| 55 | BUILD = "BUILD" |
| 56 | |
| 57 | // PENDINGUPDATE indicates that the LB is online but configuration changes |
| 58 | // are being applied to update the service based on a previous request. |
| 59 | PENDINGUPDATE = "PENDING_UPDATE" |
| 60 | |
| 61 | // PENDINGDELETE indicates that the LB is online but configuration changes |
| 62 | // are being applied to begin deletion of the service based on a previous |
| 63 | // request. |
| 64 | PENDINGDELETE = "PENDING_DELETE" |
| 65 | |
| 66 | // SUSPENDED indicates that the LB has been taken offline and disabled. |
| 67 | SUSPENDED = "SUSPENDED" |
| 68 | |
| 69 | // ERROR indicates that the system encountered an error when attempting to |
| 70 | // configure the load balancer. |
| 71 | ERROR = "ERROR" |
| 72 | |
| 73 | // DELETED indicates that the LB has been deleted. |
| 74 | DELETED = "DELETED" |
| 75 | ) |
| 76 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 77 | // Datetime represents the structure of a Created or Updated field. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 78 | type Datetime struct { |
| 79 | Time string |
| 80 | } |
| 81 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 82 | // LoadBalancer represents a load balancer API resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 83 | type LoadBalancer struct { |
| 84 | // Human-readable name for the load balancer. |
| 85 | Name string |
| 86 | |
| 87 | // The unique ID for the load balancer. |
| 88 | ID int |
| 89 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 90 | // Represents the service protocol being load balanced. See Protocol type for |
| 91 | // a list of accepted values. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame^] | 92 | // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html |
| 93 | // for a full list of supported protocols. |
| 94 | Protocol string |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 95 | |
| 96 | // Defines how traffic should be directed between back-end nodes. The default |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 97 | // algorithm is RANDOM. See Algorithm type for a list of accepted values. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 98 | Algorithm Algorithm |
| 99 | |
| 100 | // The current status of the load balancer. |
| 101 | Status Status |
| 102 | |
| 103 | // The number of load balancer nodes. |
| 104 | NodeCount int `mapstructure:"nodeCount"` |
| 105 | |
| 106 | // Slice of virtual IPs associated with this load balancer. |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 107 | VIPs []vips.VIP `mapstructure:"virtualIps"` |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 108 | |
| 109 | // Datetime when the LB was created. |
| 110 | Created Datetime |
| 111 | |
| 112 | // Datetime when the LB was created. |
| 113 | Updated Datetime |
| 114 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 115 | // Port number for the service you are load balancing. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 116 | Port int |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 117 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 118 | // HalfClosed provides the ability for one end of the connection to |
| 119 | // terminate its output while still receiving data from the other end. This |
| 120 | // is only available on TCP/TCP_CLIENT_FIRST protocols. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 121 | HalfClosed bool |
| 122 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 123 | // Timeout represents the timeout value between a load balancer and its |
| 124 | // nodes. Defaults to 30 seconds with a maximum of 120 seconds. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 125 | Timeout int |
| 126 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 127 | // TODO |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 128 | Cluster Cluster |
| 129 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 130 | // Nodes shows all the back-end nodes which are associated with the load |
| 131 | // balancer. These are the devices which are delivered traffic. |
Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 132 | Nodes []nodes.Node |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 133 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 134 | // TODO |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 135 | ConnectionLogging ConnectionLogging |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 136 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 137 | // SessionPersistence specifies whether multiple requests from clients are |
| 138 | // directed to the same node. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 139 | SessionPersistence SessionPersistence |
| 140 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 141 | // ConnectionThrottle specifies a limit on the number of connections per IP |
| 142 | // address to help mitigate malicious or abusive traffic to your applications. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 143 | ConnectionThrottle ConnectionThrottle |
| 144 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 145 | // TODO |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 146 | SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"` |
| 147 | } |
| 148 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 149 | // SourceAddrs - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 150 | type SourceAddrs struct { |
| 151 | IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"` |
| 152 | IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"` |
| 153 | IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"` |
| 154 | IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"` |
| 155 | } |
| 156 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 157 | // SessionPersistence - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 158 | type SessionPersistence struct { |
| 159 | Type string `json:"persistenceType" mapstructure:"persistenceType"` |
| 160 | } |
| 161 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 162 | // ConnectionThrottle - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 163 | type ConnectionThrottle struct { |
| 164 | MinConns int `json:"minConnections" mapstructure:"minConnections"` |
| 165 | MaxConns int `json:"maxConnections" mapstructure:"maxConnections"` |
| 166 | MaxConnRate int `json:"maxConnectionRate" mapstructure:"maxConnectionRate"` |
| 167 | RateInterval int `json:"rateInterval" mapstructure:"rateInterval"` |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 170 | // ConnectionLogging - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 171 | type ConnectionLogging struct { |
| 172 | Enabled bool |
| 173 | } |
| 174 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 175 | // Cluster - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 176 | type Cluster struct { |
| 177 | Name string |
| 178 | } |
| 179 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 180 | // LBPage is the page returned by a pager when traversing over a collection of |
| 181 | // LBs. |
| 182 | type LBPage struct { |
| 183 | pagination.LinkedPageBase |
| 184 | } |
| 185 | |
| 186 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 187 | func (p LBPage) IsEmpty() (bool, error) { |
| 188 | is, err := ExtractLBs(p) |
| 189 | if err != nil { |
| 190 | return true, nil |
| 191 | } |
| 192 | return len(is) == 0, nil |
| 193 | } |
| 194 | |
| 195 | // ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts |
| 196 | // the elements into a slice of LoadBalancer structs. In other words, a generic |
| 197 | // collection is mapped into a relevant slice. |
| 198 | func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) { |
| 199 | var resp struct { |
| 200 | LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"` |
| 201 | } |
| 202 | |
| 203 | err := mapstructure.Decode(page.(LBPage).Body, &resp) |
| 204 | |
| 205 | return resp.LBs, err |
| 206 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 207 | |
| 208 | type commonResult struct { |
| 209 | gophercloud.Result |
| 210 | } |
| 211 | |
| 212 | // Extract interprets any commonResult as a LB, if possible. |
| 213 | func (r commonResult) Extract() (*LoadBalancer, error) { |
| 214 | if r.Err != nil { |
| 215 | return nil, r.Err |
| 216 | } |
| 217 | |
| 218 | var response struct { |
| 219 | LB LoadBalancer `mapstructure:"loadBalancer"` |
| 220 | } |
| 221 | |
| 222 | err := mapstructure.Decode(r.Body, &response) |
| 223 | |
| 224 | return &response.LB, err |
| 225 | } |
| 226 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 227 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 228 | type CreateResult struct { |
| 229 | commonResult |
| 230 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 231 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 232 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 233 | type DeleteResult struct { |
| 234 | gophercloud.ErrResult |
| 235 | } |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 236 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 237 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 238 | type UpdateResult struct { |
| 239 | gophercloud.ErrResult |
| 240 | } |
| 241 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 242 | // GetResult represents the result of a get operation. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 243 | type GetResult struct { |
| 244 | commonResult |
| 245 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame^] | 246 | |
| 247 | // ProtocolPage is the page returned by a pager when traversing over a |
| 248 | // collection of LB protocols. |
| 249 | type ProtocolPage struct { |
| 250 | pagination.SinglePageBase |
| 251 | } |
| 252 | |
| 253 | // IsEmpty checks whether a ProtocolPage struct is empty. |
| 254 | func (p ProtocolPage) IsEmpty() (bool, error) { |
| 255 | is, err := ExtractProtocols(p) |
| 256 | if err != nil { |
| 257 | return true, nil |
| 258 | } |
| 259 | return len(is) == 0, nil |
| 260 | } |
| 261 | |
| 262 | // ExtractProtocols accepts a Page struct, specifically a ProtocolPage struct, |
| 263 | // and extracts the elements into a slice of LoadBalancer structs. In other |
| 264 | // words, a generic collection is mapped into a relevant slice. |
| 265 | func ExtractProtocols(page pagination.Page) ([]Protocol, error) { |
| 266 | var resp struct { |
| 267 | Protocols []Protocol `mapstructure:"protocols" json:"protocols"` |
| 268 | } |
| 269 | |
| 270 | err := mapstructure.Decode(page.(ProtocolPage).Body, &resp) |
| 271 | |
| 272 | return resp.Protocols, err |
| 273 | } |