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