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