| 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 | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 9 | ) | 
|  | 10 |  | 
|  | 11 | type Protocol string | 
|  | 12 |  | 
|  | 13 | // The constants below represent all the compatible load balancer protocols. | 
|  | 14 | const ( | 
|  | 15 | // DNSTCP is a protocol that works with IPv6 and allows your DNS server to | 
|  | 16 | // receive traffic using TCP port 53. | 
|  | 17 | DNSTCP = "DNS_TCP" | 
|  | 18 |  | 
|  | 19 | // DNSUDP is a protocol that works with IPv6 and allows your DNS server to | 
|  | 20 | // receive traffic using UDP port 53. | 
|  | 21 | DNSUDP = "DNS_UDP" | 
|  | 22 |  | 
|  | 23 | // TCP is one of the core protocols of the Internet Protocol Suite. It | 
|  | 24 | // provides a reliable, ordered delivery of a stream of bytes from one | 
|  | 25 | // program on a computer to another program on another computer. Applications | 
|  | 26 | // that require an ordered and reliable delivery of packets use this protocol. | 
|  | 27 | TCP = "TCP" | 
|  | 28 |  | 
|  | 29 | // TCPCLIENTFIRST is a protocol similar to TCP, but is more efficient when a | 
|  | 30 | // client is expected to write the data first. | 
|  | 31 | TCPCLIENTFIRST = "TCP_CLIENT_FIRST" | 
|  | 32 |  | 
|  | 33 | // UDP provides a datagram service that emphasizes speed over reliability. It | 
|  | 34 | // works well with applications that provide security through other measures. | 
|  | 35 | UDP = "UDP" | 
|  | 36 |  | 
|  | 37 | // UDPSTREAM is a protocol designed to stream media over networks and is | 
|  | 38 | // built on top of UDP. | 
|  | 39 | UDPSTREAM = "UDP_STREAM" | 
|  | 40 | ) | 
|  | 41 |  | 
|  | 42 | // Algorithm defines how traffic should be directed between back-end nodes. | 
|  | 43 | type Algorithm string | 
|  | 44 |  | 
|  | 45 | const ( | 
|  | 46 | // LC directs traffic to the node with the lowest number of connections. | 
|  | 47 | LC = "LEAST_CONNECTIONS" | 
|  | 48 |  | 
|  | 49 | // RAND directs traffic to nodes at random. | 
|  | 50 | RAND = "RANDOM" | 
|  | 51 |  | 
|  | 52 | // RR directs traffic to each of the nodes in turn. | 
|  | 53 | RR = "ROUND_ROBIN" | 
|  | 54 |  | 
|  | 55 | // WLC directs traffic to a node based on the number of concurrent | 
|  | 56 | // connections and its weight. | 
|  | 57 | WLC = "WEIGHTED_LEAST_CONNECTIONS" | 
|  | 58 |  | 
|  | 59 | // WRR directs traffic to a node according to the RR algorithm, but with | 
|  | 60 | // different proportions of traffic being directed to the back-end nodes. | 
|  | 61 | // Weights must be defined as part of the node configuration. | 
|  | 62 | WRR = "WEIGHTED_ROUND_ROBIN" | 
|  | 63 | ) | 
|  | 64 |  | 
|  | 65 | type Status string | 
|  | 66 |  | 
|  | 67 | const ( | 
|  | 68 | // ACTIVE indicates that the LB is configured properly and ready to serve | 
|  | 69 | // traffic to incoming requests via the configured virtual IPs. | 
|  | 70 | ACTIVE = "ACTIVE" | 
|  | 71 |  | 
|  | 72 | // BUILD indicates that the LB is being provisioned for the first time and | 
|  | 73 | // configuration is being applied to bring the service online. The service | 
|  | 74 | // cannot yet serve incoming requests. | 
|  | 75 | BUILD = "BUILD" | 
|  | 76 |  | 
|  | 77 | // PENDINGUPDATE indicates that the LB is online but configuration changes | 
|  | 78 | // are being applied to update the service based on a previous request. | 
|  | 79 | PENDINGUPDATE = "PENDING_UPDATE" | 
|  | 80 |  | 
|  | 81 | // PENDINGDELETE indicates that the LB is online but configuration changes | 
|  | 82 | // are being applied to begin deletion of the service based on a previous | 
|  | 83 | // request. | 
|  | 84 | PENDINGDELETE = "PENDING_DELETE" | 
|  | 85 |  | 
|  | 86 | // SUSPENDED indicates that the LB has been taken offline and disabled. | 
|  | 87 | SUSPENDED = "SUSPENDED" | 
|  | 88 |  | 
|  | 89 | // ERROR indicates that the system encountered an error when attempting to | 
|  | 90 | // configure the load balancer. | 
|  | 91 | ERROR = "ERROR" | 
|  | 92 |  | 
|  | 93 | // DELETED indicates that the LB has been deleted. | 
|  | 94 | DELETED = "DELETED" | 
|  | 95 | ) | 
|  | 96 |  | 
|  | 97 | type Datetime struct { | 
|  | 98 | Time string | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | type VIP struct { | 
| Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 102 | Address string `json:"address,omitempty"` | 
|  | 103 | ID      int    `json:"id,omitempty"` | 
|  | 104 | Type    string `json:"type,omitempty"` | 
|  | 105 | Version string `json:"ipVersion,omitempty" mapstructure:"ipVersion"` | 
| Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
|  | 108 | type LoadBalancer struct { | 
|  | 109 | // Human-readable name for the load balancer. | 
|  | 110 | Name string | 
|  | 111 |  | 
|  | 112 | // The unique ID for the load balancer. | 
|  | 113 | ID int | 
|  | 114 |  | 
|  | 115 | // Represents the service protocol being load balanced. | 
|  | 116 | Protocol Protocol | 
|  | 117 |  | 
|  | 118 | // Defines how traffic should be directed between back-end nodes. The default | 
|  | 119 | // algorithm is RANDOM. | 
|  | 120 | Algorithm Algorithm | 
|  | 121 |  | 
|  | 122 | // The current status of the load balancer. | 
|  | 123 | Status Status | 
|  | 124 |  | 
|  | 125 | // The number of load balancer nodes. | 
|  | 126 | NodeCount int `mapstructure:"nodeCount"` | 
|  | 127 |  | 
|  | 128 | // Slice of virtual IPs associated with this load balancer. | 
|  | 129 | VIPs []VIP `mapstructure:"virtualIps"` | 
|  | 130 |  | 
|  | 131 | // Datetime when the LB was created. | 
|  | 132 | Created Datetime | 
|  | 133 |  | 
|  | 134 | // Datetime when the LB was created. | 
|  | 135 | Updated Datetime | 
|  | 136 |  | 
|  | 137 | Port int | 
| Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 138 |  | 
|  | 139 | HalfClosed bool | 
|  | 140 |  | 
|  | 141 | Timeout int | 
|  | 142 |  | 
|  | 143 | Cluster Cluster | 
|  | 144 |  | 
| Jamie Hannaford | 21a3eb1 | 2014-11-03 10:34:29 +0100 | [diff] [blame] | 145 | Nodes []nodes.Node | 
| Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 146 |  | 
|  | 147 | ConnectionLogging ConnectionLogging | 
| Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 148 |  | 
|  | 149 | SessionPersistence SessionPersistence | 
|  | 150 |  | 
|  | 151 | ConnectionThrottle ConnectionThrottle | 
|  | 152 |  | 
|  | 153 | SourceAddrs SourceAddrs `mapstructure:"sourceAddresses"` | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | type SourceAddrs struct { | 
|  | 157 | IPv4Public  string `json:"ipv4Public" mapstructure:"ipv4Public"` | 
|  | 158 | IPv4Private string `json:"ipv4Servicenet" mapstructure:"ipv4Servicenet"` | 
|  | 159 | IPv6Public  string `json:"ipv6Public" mapstructure:"ipv6Public"` | 
|  | 160 | IPv6Private string `json:"ipv6Servicenet" mapstructure:"ipv6Servicenet"` | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | type SessionPersistence struct { | 
|  | 164 | Type string `json:"persistenceType" mapstructure:"persistenceType"` | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | type ConnectionThrottle struct { | 
|  | 168 | MinConns     int `json:"minConnections" mapstructure:"minConnections"` | 
|  | 169 | MaxConns     int `json:"maxConnections" mapstructure:"maxConnections"` | 
|  | 170 | MaxConnRate  int `json:"maxConnectionRate" mapstructure:"maxConnectionRate"` | 
|  | 171 | RateInterval int `json:"rateInterval" mapstructure:"rateInterval"` | 
| Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
|  | 174 | type ConnectionLogging struct { | 
|  | 175 | Enabled bool | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | type Cluster struct { | 
|  | 179 | Name string | 
|  | 180 | } | 
|  | 181 |  | 
| Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 182 | // LBPage is the page returned by a pager when traversing over a collection of | 
|  | 183 | // LBs. | 
|  | 184 | type LBPage struct { | 
|  | 185 | pagination.LinkedPageBase | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | // IsEmpty checks whether a NetworkPage struct is empty. | 
|  | 189 | func (p LBPage) IsEmpty() (bool, error) { | 
|  | 190 | is, err := ExtractLBs(p) | 
|  | 191 | if err != nil { | 
|  | 192 | return true, nil | 
|  | 193 | } | 
|  | 194 | return len(is) == 0, nil | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | // ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts | 
|  | 198 | // the elements into a slice of LoadBalancer structs. In other words, a generic | 
|  | 199 | // collection is mapped into a relevant slice. | 
|  | 200 | func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) { | 
|  | 201 | var resp struct { | 
|  | 202 | LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"` | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | err := mapstructure.Decode(page.(LBPage).Body, &resp) | 
|  | 206 |  | 
|  | 207 | return resp.LBs, err | 
|  | 208 | } | 
| Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 209 |  | 
|  | 210 | type commonResult struct { | 
|  | 211 | gophercloud.Result | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | // Extract interprets any commonResult as a LB, if possible. | 
|  | 215 | func (r commonResult) Extract() (*LoadBalancer, error) { | 
|  | 216 | if r.Err != nil { | 
|  | 217 | return nil, r.Err | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | var response struct { | 
|  | 221 | LB LoadBalancer `mapstructure:"loadBalancer"` | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | err := mapstructure.Decode(r.Body, &response) | 
|  | 225 |  | 
|  | 226 | return &response.LB, err | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | type CreateResult struct { | 
|  | 230 | commonResult | 
|  | 231 | } | 
| Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 232 |  | 
|  | 233 | type DeleteResult struct { | 
|  | 234 | gophercloud.ErrResult | 
|  | 235 | } | 
| Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 236 |  | 
| Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 237 | type UpdateResult struct { | 
|  | 238 | gophercloud.ErrResult | 
|  | 239 | } | 
|  | 240 |  | 
| Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 241 | type GetResult struct { | 
|  | 242 | commonResult | 
|  | 243 | } |