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 | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame^] | 9 | "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions" |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 13 | // Protocol represents the network protocol which the load balancer accepts. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 14 | type Protocol struct { |
| 15 | // The name of the protocol, e.g. HTTP, LDAP, FTP, etc. |
| 16 | Name string |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 17 | |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 18 | // The port number for the protocol. |
| 19 | Port int |
| 20 | } |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 21 | |
| 22 | // Algorithm defines how traffic should be directed between back-end nodes. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 23 | type Algorithm struct { |
| 24 | // The name of the algorithm, e.g RANDOM, ROUND_ROBIN, etc. |
| 25 | Name string |
| 26 | } |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 27 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 28 | // Status represents the potential state of a load balancer resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 29 | type Status string |
| 30 | |
| 31 | const ( |
| 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 61 | // Datetime represents the structure of a Created or Updated field. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 62 | type Datetime struct { |
| 63 | Time string |
| 64 | } |
| 65 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 66 | // LoadBalancer represents a load balancer API resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 67 | type 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 74 | // Represents the service protocol being load balanced. See Protocol type for |
| 75 | // a list of accepted values. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 76 | // 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 Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 79 | |
| 80 | // Defines how traffic should be directed between back-end nodes. The default |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 81 | // algorithm is RANDOM. See Algorithm type for a list of accepted values. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 82 | Algorithm string |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 83 | |
| 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 Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 91 | VIPs []vips.VIP `mapstructure:"virtualIps"` |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 92 | |
| 93 | // Datetime when the LB was created. |
| 94 | Created Datetime |
| 95 | |
| 96 | // Datetime when the LB was created. |
| 97 | Updated Datetime |
| 98 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 99 | // Port number for the service you are load balancing. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 100 | Port int |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 101 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 102 | // 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 105 | HalfClosed bool |
| 106 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 107 | // Timeout represents the timeout value between a load balancer and its |
| 108 | // nodes. Defaults to 30 seconds with a maximum of 120 seconds. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 109 | Timeout int |
| 110 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 111 | // TODO |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 112 | Cluster Cluster |
| 113 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 114 | // Nodes shows all the back-end nodes which are associated with the load |
| 115 | // balancer. These are the devices which are delivered traffic. |
Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 116 | Nodes []nodes.Node |
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 | // TODO |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 119 | ConnectionLogging ConnectionLogging |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 120 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 121 | // SessionPersistence specifies whether multiple requests from clients are |
| 122 | // directed to the same node. |
Jamie Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame^] | 123 | SessionPersistence sessions.SessionPersistence |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 124 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 125 | // ConnectionThrottle specifies a limit on the number of connections per IP |
| 126 | // address to help mitigate malicious or abusive traffic to your applications. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 127 | ConnectionThrottle ConnectionThrottle |
| 128 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 129 | // TODO |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 130 | SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"` |
| 131 | } |
| 132 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 133 | // SourceAddrs - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 134 | type 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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 141 | // ConnectionThrottle - temp |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 142 | type 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 149 | // ConnectionLogging - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 150 | type ConnectionLogging struct { |
| 151 | Enabled bool |
| 152 | } |
| 153 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 154 | // Cluster - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 155 | type Cluster struct { |
| 156 | Name string |
| 157 | } |
| 158 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 159 | // LBPage is the page returned by a pager when traversing over a collection of |
| 160 | // LBs. |
| 161 | type LBPage struct { |
| 162 | pagination.LinkedPageBase |
| 163 | } |
| 164 | |
| 165 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 166 | func (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. |
| 177 | func 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 Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 186 | |
| 187 | type commonResult struct { |
| 188 | gophercloud.Result |
| 189 | } |
| 190 | |
| 191 | // Extract interprets any commonResult as a LB, if possible. |
| 192 | func (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 Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 206 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 207 | type CreateResult struct { |
| 208 | commonResult |
| 209 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 210 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 211 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 212 | type DeleteResult struct { |
| 213 | gophercloud.ErrResult |
| 214 | } |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 215 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 216 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 217 | type UpdateResult struct { |
| 218 | gophercloud.ErrResult |
| 219 | } |
| 220 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 221 | // GetResult represents the result of a get operation. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 222 | type GetResult struct { |
| 223 | commonResult |
| 224 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 225 | |
| 226 | // ProtocolPage is the page returned by a pager when traversing over a |
| 227 | // collection of LB protocols. |
| 228 | type ProtocolPage struct { |
| 229 | pagination.SinglePageBase |
| 230 | } |
| 231 | |
| 232 | // IsEmpty checks whether a ProtocolPage struct is empty. |
| 233 | func (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. |
| 244 | func ExtractProtocols(page pagination.Page) ([]Protocol, error) { |
| 245 | var resp struct { |
| 246 | Protocols []Protocol `mapstructure:"protocols" json:"protocols"` |
| 247 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 248 | err := mapstructure.Decode(page.(ProtocolPage).Body, &resp) |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 249 | return resp.Protocols, err |
| 250 | } |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 251 | |
| 252 | // AlgorithmPage is the page returned by a pager when traversing over a |
| 253 | // collection of LB algorithms. |
| 254 | type AlgorithmPage struct { |
| 255 | pagination.SinglePageBase |
| 256 | } |
| 257 | |
| 258 | // IsEmpty checks whether a ProtocolPage struct is empty. |
| 259 | func (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. |
| 270 | func 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 | } |