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 ( |
Jamie Hannaford | 4d398ff | 2014-11-14 11:03:00 +0100 | [diff] [blame] | 4 | "reflect" |
| 5 | "time" |
| 6 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 7 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 8 | |
| 9 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/rackspace/lb/v1/acl" |
Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes" |
Jamie Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle" |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 15 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 18 | // Protocol represents the network protocol which the load balancer accepts. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 19 | type Protocol struct { |
| 20 | // The name of the protocol, e.g. HTTP, LDAP, FTP, etc. |
| 21 | Name string |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 22 | |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 23 | // The port number for the protocol. |
| 24 | Port int |
| 25 | } |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 26 | |
| 27 | // Algorithm defines how traffic should be directed between back-end nodes. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 28 | type Algorithm struct { |
| 29 | // The name of the algorithm, e.g RANDOM, ROUND_ROBIN, etc. |
| 30 | Name string |
| 31 | } |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 32 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 33 | // Status represents the potential state of a load balancer resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 34 | type Status string |
| 35 | |
| 36 | const ( |
| 37 | // ACTIVE indicates that the LB is configured properly and ready to serve |
| 38 | // traffic to incoming requests via the configured virtual IPs. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 39 | ACTIVE Status = "ACTIVE" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 40 | |
| 41 | // BUILD indicates that the LB is being provisioned for the first time and |
| 42 | // configuration is being applied to bring the service online. The service |
| 43 | // cannot yet serve incoming requests. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 44 | BUILD Status = "BUILD" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 45 | |
| 46 | // PENDINGUPDATE indicates that the LB is online but configuration changes |
| 47 | // are being applied to update the service based on a previous request. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 48 | PENDINGUPDATE Status = "PENDING_UPDATE" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 49 | |
| 50 | // PENDINGDELETE indicates that the LB is online but configuration changes |
| 51 | // are being applied to begin deletion of the service based on a previous |
| 52 | // request. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 53 | PENDINGDELETE Status = "PENDING_DELETE" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 54 | |
| 55 | // SUSPENDED indicates that the LB has been taken offline and disabled. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 56 | SUSPENDED Status = "SUSPENDED" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 57 | |
| 58 | // ERROR indicates that the system encountered an error when attempting to |
| 59 | // configure the load balancer. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 60 | ERROR Status = "ERROR" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 61 | |
| 62 | // DELETED indicates that the LB has been deleted. |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 63 | DELETED Status = "DELETED" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 64 | ) |
| 65 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 66 | // Datetime represents the structure of a Created or Updated field. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 67 | type Datetime struct { |
Jamie Hannaford | 4d398ff | 2014-11-14 11:03:00 +0100 | [diff] [blame] | 68 | Time time.Time `mapstructure:"-"` |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 71 | // LoadBalancer represents a load balancer API resource. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 72 | type LoadBalancer struct { |
| 73 | // Human-readable name for the load balancer. |
| 74 | Name string |
| 75 | |
| 76 | // The unique ID for the load balancer. |
| 77 | ID int |
| 78 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 79 | // Represents the service protocol being load balanced. See Protocol type for |
| 80 | // a list of accepted values. |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 81 | // See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/protocols.html |
| 82 | // for a full list of supported protocols. |
| 83 | Protocol string |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 84 | |
| 85 | // Defines how traffic should be directed between back-end nodes. The default |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 86 | // algorithm is RANDOM. See Algorithm type for a list of accepted values. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 87 | Algorithm string |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 88 | |
| 89 | // The current status of the load balancer. |
| 90 | Status Status |
| 91 | |
| 92 | // The number of load balancer nodes. |
| 93 | NodeCount int `mapstructure:"nodeCount"` |
| 94 | |
| 95 | // Slice of virtual IPs associated with this load balancer. |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 96 | VIPs []vips.VIP `mapstructure:"virtualIps"` |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 97 | |
| 98 | // Datetime when the LB was created. |
| 99 | Created Datetime |
| 100 | |
| 101 | // Datetime when the LB was created. |
| 102 | Updated Datetime |
| 103 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 104 | // Port number for the service you are load balancing. |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 105 | Port int |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 106 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 107 | // HalfClosed provides the ability for one end of the connection to |
| 108 | // terminate its output while still receiving data from the other end. This |
| 109 | // is only available on TCP/TCP_CLIENT_FIRST protocols. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 110 | HalfClosed bool |
| 111 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 112 | // Timeout represents the timeout value between a load balancer and its |
| 113 | // nodes. Defaults to 30 seconds with a maximum of 120 seconds. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 114 | Timeout int |
| 115 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 116 | // The cluster name. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 117 | Cluster Cluster |
| 118 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 119 | // Nodes shows all the back-end nodes which are associated with the load |
| 120 | // balancer. These are the devices which are delivered traffic. |
Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 121 | Nodes []nodes.Node |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 122 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 123 | // Current connection logging configuration. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 124 | ConnectionLogging ConnectionLogging |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 125 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 126 | // SessionPersistence specifies whether multiple requests from clients are |
| 127 | // directed to the same node. |
Jamie Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame] | 128 | SessionPersistence sessions.SessionPersistence |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 129 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 130 | // ConnectionThrottle specifies a limit on the number of connections per IP |
| 131 | // address to help mitigate malicious or abusive traffic to your applications. |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 132 | ConnectionThrottle throttle.ConnectionThrottle |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 133 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 134 | // The source public and private IP addresses. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 135 | SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"` |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 136 | |
| 137 | // Represents the access rules for this particular load balancer. IP addresses |
| 138 | // or subnet ranges, depending on their type (ALLOW or DENY), can be permitted |
| 139 | // or blocked. |
| 140 | AccessList acl.AccessList |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 143 | // SourceAddrs represents the source public and private IP addresses. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 144 | type SourceAddrs struct { |
| 145 | IPv4Public string `json:"ipv4Public" mapstructure:"ipv4Public"` |
| 146 | IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"` |
| 147 | IPv6Public string `json:"ipv6Public" mapstructure:"ipv6Public"` |
| 148 | IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"` |
| 149 | } |
| 150 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 151 | // ConnectionLogging - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 152 | type ConnectionLogging struct { |
| 153 | Enabled bool |
| 154 | } |
| 155 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 156 | // Cluster - temp |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 157 | type Cluster struct { |
| 158 | Name string |
| 159 | } |
| 160 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 161 | // LBPage is the page returned by a pager when traversing over a collection of |
| 162 | // LBs. |
| 163 | type LBPage struct { |
| 164 | pagination.LinkedPageBase |
| 165 | } |
| 166 | |
| 167 | // IsEmpty checks whether a NetworkPage struct is empty. |
| 168 | func (p LBPage) IsEmpty() (bool, error) { |
| 169 | is, err := ExtractLBs(p) |
| 170 | if err != nil { |
| 171 | return true, nil |
| 172 | } |
| 173 | return len(is) == 0, nil |
| 174 | } |
| 175 | |
| 176 | // ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts |
| 177 | // the elements into a slice of LoadBalancer structs. In other words, a generic |
| 178 | // collection is mapped into a relevant slice. |
| 179 | func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) { |
| 180 | var resp struct { |
| 181 | LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"` |
| 182 | } |
| 183 | |
Jamie Hannaford | 4d398ff | 2014-11-14 11:03:00 +0100 | [diff] [blame] | 184 | coll := page.(LBPage).Body |
| 185 | err := mapstructure.Decode(coll, &resp) |
| 186 | |
| 187 | s := reflect.ValueOf(coll.(map[string]interface{})["loadBalancers"]) |
| 188 | |
| 189 | for i := 0; i < s.Len(); i++ { |
| 190 | val := (s.Index(i).Interface()).(map[string]interface{}) |
| 191 | |
| 192 | ts, err := extractTS(val, "created") |
| 193 | if err != nil { |
| 194 | return resp.LBs, err |
| 195 | } |
| 196 | resp.LBs[i].Created.Time = ts |
| 197 | |
| 198 | ts, err = extractTS(val, "updated") |
| 199 | if err != nil { |
| 200 | return resp.LBs, err |
| 201 | } |
| 202 | resp.LBs[i].Updated.Time = ts |
| 203 | } |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 204 | |
| 205 | return resp.LBs, err |
| 206 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 207 | |
Jamie Hannaford | 4d398ff | 2014-11-14 11:03:00 +0100 | [diff] [blame] | 208 | func extractTS(body map[string]interface{}, key string) (time.Time, error) { |
| 209 | val := body[key].(map[string]interface{}) |
Jamie Hannaford | a587fb0 | 2014-11-14 11:09:23 +0100 | [diff] [blame] | 210 | return time.Parse(time.RFC3339, val["time"].(string)) |
Jamie Hannaford | 4d398ff | 2014-11-14 11:03:00 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 213 | type commonResult struct { |
| 214 | gophercloud.Result |
| 215 | } |
| 216 | |
| 217 | // Extract interprets any commonResult as a LB, if possible. |
| 218 | func (r commonResult) Extract() (*LoadBalancer, error) { |
| 219 | if r.Err != nil { |
| 220 | return nil, r.Err |
| 221 | } |
| 222 | |
| 223 | var response struct { |
| 224 | LB LoadBalancer `mapstructure:"loadBalancer"` |
| 225 | } |
| 226 | |
| 227 | err := mapstructure.Decode(r.Body, &response) |
| 228 | |
Jamie Hannaford | 4d398ff | 2014-11-14 11:03:00 +0100 | [diff] [blame] | 229 | json := r.Body.(map[string]interface{}) |
| 230 | lb := json["loadBalancer"].(map[string]interface{}) |
| 231 | |
| 232 | ts, err := extractTS(lb, "created") |
| 233 | if err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | response.LB.Created.Time = ts |
| 237 | |
| 238 | ts, err = extractTS(lb, "updated") |
| 239 | if err != nil { |
| 240 | return nil, err |
| 241 | } |
| 242 | response.LB.Updated.Time = ts |
| 243 | |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 244 | return &response.LB, err |
| 245 | } |
| 246 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 247 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 248 | type CreateResult struct { |
| 249 | commonResult |
| 250 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 251 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 252 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 253 | type DeleteResult struct { |
| 254 | gophercloud.ErrResult |
| 255 | } |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 256 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 257 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 258 | type UpdateResult struct { |
| 259 | gophercloud.ErrResult |
| 260 | } |
| 261 | |
Jamie Hannaford | b2007ee | 2014-11-03 16:24:43 +0100 | [diff] [blame] | 262 | // GetResult represents the result of a get operation. |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 263 | type GetResult struct { |
| 264 | commonResult |
| 265 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 266 | |
| 267 | // ProtocolPage is the page returned by a pager when traversing over a |
| 268 | // collection of LB protocols. |
| 269 | type ProtocolPage struct { |
| 270 | pagination.SinglePageBase |
| 271 | } |
| 272 | |
| 273 | // IsEmpty checks whether a ProtocolPage struct is empty. |
| 274 | func (p ProtocolPage) IsEmpty() (bool, error) { |
| 275 | is, err := ExtractProtocols(p) |
| 276 | if err != nil { |
| 277 | return true, nil |
| 278 | } |
| 279 | return len(is) == 0, nil |
| 280 | } |
| 281 | |
| 282 | // ExtractProtocols accepts a Page struct, specifically a ProtocolPage struct, |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 283 | // and extracts the elements into a slice of Protocol structs. In other |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 284 | // words, a generic collection is mapped into a relevant slice. |
| 285 | func ExtractProtocols(page pagination.Page) ([]Protocol, error) { |
| 286 | var resp struct { |
| 287 | Protocols []Protocol `mapstructure:"protocols" json:"protocols"` |
| 288 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 289 | err := mapstructure.Decode(page.(ProtocolPage).Body, &resp) |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 290 | return resp.Protocols, err |
| 291 | } |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 292 | |
| 293 | // AlgorithmPage is the page returned by a pager when traversing over a |
| 294 | // collection of LB algorithms. |
| 295 | type AlgorithmPage struct { |
| 296 | pagination.SinglePageBase |
| 297 | } |
| 298 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 299 | // IsEmpty checks whether an AlgorithmPage struct is empty. |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 300 | func (p AlgorithmPage) IsEmpty() (bool, error) { |
| 301 | is, err := ExtractAlgorithms(p) |
| 302 | if err != nil { |
| 303 | return true, nil |
| 304 | } |
| 305 | return len(is) == 0, nil |
| 306 | } |
| 307 | |
Alex Gaynor | ba18608 | 2014-11-18 07:51:18 -0800 | [diff] [blame^] | 308 | // ExtractAlgorithms accepts a Page struct, specifically an AlgorithmPage struct, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 309 | // and extracts the elements into a slice of Algorithm structs. In other |
| 310 | // words, a generic collection is mapped into a relevant slice. |
| 311 | func ExtractAlgorithms(page pagination.Page) ([]Algorithm, error) { |
| 312 | var resp struct { |
| 313 | Algorithms []Algorithm `mapstructure:"algorithms" json:"algorithms"` |
| 314 | } |
| 315 | err := mapstructure.Decode(page.(AlgorithmPage).Body, &resp) |
| 316 | return resp.Algorithms, err |
| 317 | } |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 318 | |
| 319 | // ErrorPage represents the HTML file that is shown to an end user who is |
| 320 | // attempting to access a load balancer node that is offline/unavailable. |
| 321 | // |
| 322 | // During provisioning, every load balancer is configured with a default error |
| 323 | // page that gets displayed when traffic is requested for an offline node. |
| 324 | // |
| 325 | // You can add a single custom error page with an HTTP-based protocol to a load |
| 326 | // balancer. Page updates override existing content. If a custom error page is |
| 327 | // deleted, or the load balancer is changed to a non-HTTP protocol, the default |
| 328 | // error page is restored. |
| 329 | type ErrorPage struct { |
| 330 | Content string |
| 331 | } |
| 332 | |
| 333 | // ErrorPageResult represents the result of an error page operation - |
| 334 | // specifically getting or creating one. |
| 335 | type ErrorPageResult struct { |
| 336 | gophercloud.Result |
| 337 | } |
| 338 | |
| 339 | // Extract interprets any commonResult as an ErrorPage, if possible. |
| 340 | func (r ErrorPageResult) Extract() (*ErrorPage, error) { |
| 341 | if r.Err != nil { |
| 342 | return nil, r.Err |
| 343 | } |
| 344 | |
| 345 | var response struct { |
| 346 | ErrorPage ErrorPage `mapstructure:"errorpage"` |
| 347 | } |
| 348 | |
| 349 | err := mapstructure.Decode(r.Body, &response) |
| 350 | |
| 351 | return &response.ErrorPage, err |
| 352 | } |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 353 | |
| 354 | // Stats represents all the key information about a load balancer's usage. |
| 355 | type Stats struct { |
| 356 | // The number of connections closed by this load balancer because its |
| 357 | // ConnectTimeout interval was exceeded. |
| 358 | ConnectTimeout int `mapstructure:"connectTimeOut"` |
| 359 | |
| 360 | // The number of transaction or protocol errors for this load balancer. |
| 361 | ConnectError int |
| 362 | |
| 363 | // Number of connection failures for this load balancer. |
| 364 | ConnectFailure int |
| 365 | |
| 366 | // Number of connections closed by this load balancer because its Timeout |
| 367 | // interval was exceeded. |
| 368 | DataTimedOut int |
| 369 | |
| 370 | // Number of connections closed by this load balancer because the |
| 371 | // 'keepalive_timeout' interval was exceeded. |
| 372 | KeepAliveTimedOut int |
| 373 | |
| 374 | // The maximum number of simultaneous TCP connections this load balancer has |
| 375 | // processed at any one time. |
| 376 | MaxConnections int `mapstructure:"maxConn"` |
| 377 | |
| 378 | // Number of simultaneous connections active at the time of the request. |
| 379 | CurrentConnections int `mapstructure:"currentConn"` |
| 380 | |
| 381 | // Number of SSL connections closed by this load balancer because the |
| 382 | // ConnectTimeout interval was exceeded. |
| 383 | SSLConnectTimeout int `mapstructure:"connectTimeOutSsl"` |
| 384 | |
| 385 | // Number of SSL transaction or protocol erros in this load balancer. |
| 386 | SSLConnectError int `mapstructure:"connectErrorSsl"` |
| 387 | |
| 388 | // Number of SSL connection failures in this load balancer. |
| 389 | SSLConnectFailure int `mapstructure:"connectFailureSsl"` |
| 390 | |
| 391 | // Number of SSL connections closed by this load balancer because the |
| 392 | // Timeout interval was exceeded. |
| 393 | SSLDataTimedOut int `mapstructure:"dataTimedOutSsl"` |
| 394 | |
| 395 | // Number of SSL connections closed by this load balancer because the |
| 396 | // 'keepalive_timeout' interval was exceeded. |
| 397 | SSLKeepAliveTimedOut int `mapstructure:"keepAliveTimedOutSsl"` |
| 398 | |
| 399 | // Maximum number of simultaneous SSL connections this load balancer has |
Alex Gaynor | ba18608 | 2014-11-18 07:51:18 -0800 | [diff] [blame^] | 400 | // processed at any one time. |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 401 | SSLMaxConnections int `mapstructure:"maxConnSsl"` |
| 402 | |
| 403 | // Number of simultaneous SSL connections active at the time of the request. |
| 404 | SSLCurrentConnections int `mapstructure:"currentConnSsl"` |
| 405 | } |
| 406 | |
Jamie Hannaford | 227d959 | 2014-11-13 10:32:07 +0100 | [diff] [blame] | 407 | // StatsResult represents the result of a Stats operation. |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 408 | type StatsResult struct { |
| 409 | gophercloud.Result |
| 410 | } |
| 411 | |
| 412 | // Extract interprets any commonResult as a Stats struct, if possible. |
| 413 | func (r StatsResult) Extract() (*Stats, error) { |
| 414 | if r.Err != nil { |
| 415 | return nil, r.Err |
| 416 | } |
| 417 | res := &Stats{} |
| 418 | err := mapstructure.Decode(r.Body, res) |
| 419 | return res, err |
| 420 | } |