Beginning work on Load Balancer resource
diff --git a/rackspace/lb/lb/doc.go b/rackspace/lb/lb/doc.go
new file mode 100644
index 0000000..3e8a357
--- /dev/null
+++ b/rackspace/lb/lb/doc.go
@@ -0,0 +1,6 @@
+/*
+A load balancer is a logical device which belongs to a cloud account. It is
+used to distribute workloads between multiple back-end systems or services,
+based on the criteria defined as part of its configuration.
+*/
+package lb
diff --git a/rackspace/lb/lb/fixtures.go b/rackspace/lb/lb/fixtures.go
new file mode 100644
index 0000000..331d06e
--- /dev/null
+++ b/rackspace/lb/lb/fixtures.go
@@ -0,0 +1,50 @@
+package lb
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func mockListLBResponse(t *testing.T) {
+ th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "loadBalancers": [
+ {
+ "name": "lb-site1",
+ "id": 71,
+ "protocol": "HTTP",
+ "port": 80,
+ "algorithm": "RANDOM",
+ "status": "ACTIVE",
+ "nodeCount": 3,
+ "virtualIps": [
+ {
+ "id": 403,
+ "address": "206.55.130.1",
+ "type": "PUBLIC",
+ "ipVersion": "IPV4"
+ }
+ ],
+ "created": {
+ "time": "2010-11-30T03:23:42Z"
+ },
+ "updated": {
+ "time": "2010-11-30T03:23:44Z"
+ }
+ }
+ ]
+}
+ `)
+ })
+}
diff --git a/rackspace/lb/lb/requests.go b/rackspace/lb/lb/requests.go
new file mode 100644
index 0000000..91262ac
--- /dev/null
+++ b/rackspace/lb/lb/requests.go
@@ -0,0 +1,46 @@
+package lb
+
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// ListOptsBuilder allows extensions to add additional parameters to the
+// List request.
+type ListOptsBuilder interface {
+ ToLBListQuery() (string, error)
+}
+
+// ListOpts allows the filtering and sorting of paginated collections through
+// the API.
+type ListOpts struct {
+ ChangesSince string `q:"changes-since"`
+ Status Status `q:"status"`
+ NodeAddr string `q:"nodeaddress"`
+ Marker string `q:"marker"`
+ Limit int `q:"limit"`
+}
+
+// ToLBListQuery formats a ListOpts into a query string.
+func (opts ListOpts) ToLBListQuery() (string, error) {
+ q, err := gophercloud.BuildQueryString(opts)
+ if err != nil {
+ return "", err
+ }
+ return q.String(), nil
+}
+
+func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
+ url := rootURL(client)
+ if opts != nil {
+ query, err := opts.ToLBListQuery()
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query
+ }
+
+ return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
+ return LBPage{pagination.LinkedPageBase{PageResult: r}}
+ })
+}
diff --git a/rackspace/lb/lb/requests_test.go b/rackspace/lb/lb/requests_test.go
new file mode 100644
index 0000000..922d102
--- /dev/null
+++ b/rackspace/lb/lb/requests_test.go
@@ -0,0 +1,53 @@
+package lb
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockListLBResponse(t)
+
+ count := 0
+
+ err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractLBs(page)
+ th.AssertNoErr(t, err)
+
+ expected := []LoadBalancer{
+ LoadBalancer{
+ Name: "lb-site1",
+ ID: 71,
+ Protocol: "HTTP",
+ Port: 80,
+ Algorithm: RAND,
+ Status: ACTIVE,
+ NodeCount: 3,
+ VIPs: []VIP{
+ VIP{
+ ID: 403,
+ Address: "206.55.130.1",
+ Type: "PUBLIC",
+ Version: "IPV4",
+ },
+ },
+ Created: Datetime{Time: "2010-11-30T03:23:42Z"},
+ Updated: Datetime{Time: "2010-11-30T03:23:44Z"},
+ },
+ }
+
+ th.CheckDeepEquals(t, expected, actual)
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, 1, count)
+}
diff --git a/rackspace/lb/lb/results.go b/rackspace/lb/lb/results.go
new file mode 100644
index 0000000..b1e9792
--- /dev/null
+++ b/rackspace/lb/lb/results.go
@@ -0,0 +1,163 @@
+package lb
+
+import (
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+type Protocol string
+
+// The constants below represent all the compatible load balancer protocols.
+const (
+ // DNSTCP is a protocol that works with IPv6 and allows your DNS server to
+ // receive traffic using TCP port 53.
+ DNSTCP = "DNS_TCP"
+
+ // DNSUDP is a protocol that works with IPv6 and allows your DNS server to
+ // receive traffic using UDP port 53.
+ DNSUDP = "DNS_UDP"
+
+ // TCP is one of the core protocols of the Internet Protocol Suite. It
+ // provides a reliable, ordered delivery of a stream of bytes from one
+ // program on a computer to another program on another computer. Applications
+ // that require an ordered and reliable delivery of packets use this protocol.
+ TCP = "TCP"
+
+ // TCPCLIENTFIRST is a protocol similar to TCP, but is more efficient when a
+ // client is expected to write the data first.
+ TCPCLIENTFIRST = "TCP_CLIENT_FIRST"
+
+ // UDP provides a datagram service that emphasizes speed over reliability. It
+ // works well with applications that provide security through other measures.
+ UDP = "UDP"
+
+ // UDPSTREAM is a protocol designed to stream media over networks and is
+ // built on top of UDP.
+ UDPSTREAM = "UDP_STREAM"
+)
+
+// Algorithm defines how traffic should be directed between back-end nodes.
+type Algorithm string
+
+const (
+ // LC directs traffic to the node with the lowest number of connections.
+ LC = "LEAST_CONNECTIONS"
+
+ // RAND directs traffic to nodes at random.
+ RAND = "RANDOM"
+
+ // RR directs traffic to each of the nodes in turn.
+ RR = "ROUND_ROBIN"
+
+ // WLC directs traffic to a node based on the number of concurrent
+ // connections and its weight.
+ WLC = "WEIGHTED_LEAST_CONNECTIONS"
+
+ // WRR directs traffic to a node according to the RR algorithm, but with
+ // different proportions of traffic being directed to the back-end nodes.
+ // Weights must be defined as part of the node configuration.
+ WRR = "WEIGHTED_ROUND_ROBIN"
+)
+
+type Status string
+
+const (
+ // ACTIVE indicates that the LB is configured properly and ready to serve
+ // traffic to incoming requests via the configured virtual IPs.
+ ACTIVE = "ACTIVE"
+
+ // BUILD indicates that the LB is being provisioned for the first time and
+ // configuration is being applied to bring the service online. The service
+ // cannot yet serve incoming requests.
+ BUILD = "BUILD"
+
+ // PENDINGUPDATE indicates that the LB is online but configuration changes
+ // are being applied to update the service based on a previous request.
+ PENDINGUPDATE = "PENDING_UPDATE"
+
+ // PENDINGDELETE indicates that the LB is online but configuration changes
+ // are being applied to begin deletion of the service based on a previous
+ // request.
+ PENDINGDELETE = "PENDING_DELETE"
+
+ // SUSPENDED indicates that the LB has been taken offline and disabled.
+ SUSPENDED = "SUSPENDED"
+
+ // ERROR indicates that the system encountered an error when attempting to
+ // configure the load balancer.
+ ERROR = "ERROR"
+
+ // DELETED indicates that the LB has been deleted.
+ DELETED = "DELETED"
+)
+
+type Datetime struct {
+ Time string
+}
+
+type VIP struct {
+ Address string
+ ID int
+ Type string
+ Version string `mapstructure:"ipVersion"`
+}
+
+type LoadBalancer struct {
+ // Human-readable name for the load balancer.
+ Name string
+
+ // The unique ID for the load balancer.
+ ID int
+
+ // Represents the service protocol being load balanced.
+ Protocol Protocol
+
+ // Defines how traffic should be directed between back-end nodes. The default
+ // algorithm is RANDOM.
+ Algorithm Algorithm
+
+ // The current status of the load balancer.
+ Status Status
+
+ // The number of load balancer nodes.
+ NodeCount int `mapstructure:"nodeCount"`
+
+ // Slice of virtual IPs associated with this load balancer.
+ VIPs []VIP `mapstructure:"virtualIps"`
+
+ // Datetime when the LB was created.
+ Created Datetime
+
+ // Datetime when the LB was created.
+ Updated Datetime
+
+ Port int
+}
+
+// LBPage is the page returned by a pager when traversing over a collection of
+// LBs.
+type LBPage struct {
+ pagination.LinkedPageBase
+}
+
+// IsEmpty checks whether a NetworkPage struct is empty.
+func (p LBPage) IsEmpty() (bool, error) {
+ is, err := ExtractLBs(p)
+ if err != nil {
+ return true, nil
+ }
+ return len(is) == 0, nil
+}
+
+// ExtractLBs accepts a Page struct, specifically a LBPage struct, and extracts
+// the elements into a slice of LoadBalancer structs. In other words, a generic
+// collection is mapped into a relevant slice.
+func ExtractLBs(page pagination.Page) ([]LoadBalancer, error) {
+ var resp struct {
+ LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
+ }
+
+ err := mapstructure.Decode(page.(LBPage).Body, &resp)
+
+ return resp.LBs, err
+}
diff --git a/rackspace/lb/lb/urls.go b/rackspace/lb/lb/urls.go
new file mode 100644
index 0000000..a11d6b9
--- /dev/null
+++ b/rackspace/lb/lb/urls.go
@@ -0,0 +1,13 @@
+package lb
+
+import "github.com/rackspace/gophercloud"
+
+const path = "loadbalancers"
+
+func resourceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(path, id)
+}
+
+func rootURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL(path)
+}