Adding members
diff --git a/openstack/networking/v2/extensions/lbaas/members/requests.go b/openstack/networking/v2/extensions/lbaas/members/requests.go
index acb4272..6899c60 100644
--- a/openstack/networking/v2/extensions/lbaas/members/requests.go
+++ b/openstack/networking/v2/extensions/lbaas/members/requests.go
@@ -1 +1,182 @@
package members
+
+import (
+ "strconv"
+
+ "github.com/racker/perigee"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// ListOpts allows the filtering and sorting of paginated collections through
+// the API. Filtering is achieved by passing in struct field values that map to
+// the floating IP attributes you want to see returned. SortKey allows you to
+// sort by a particular network attribute. SortDir sets the direction, and is
+// either `asc' or `desc'. Marker and Limit are used for pagination.
+type ListOpts struct {
+ Status string
+ Weight int
+ AdminStateUp *bool
+ TenantID string
+ PoolID string
+ Address string
+ ProtocolPort int
+ ID string
+ Limit int
+ Marker string
+ SortKey string
+ SortDir string
+}
+
+// List returns a Pager which allows you to iterate over a collection of
+// pools. It accepts a ListOpts struct, which allows you to filter and sort
+// the returned collection for greater efficiency.
+//
+// Default policy settings return only those pools that are owned by the
+// tenant who submits the request, unless an admin user submits the request.
+func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
+ q := make(map[string]string)
+
+ if opts.Status != "" {
+ q["status"] = opts.Status
+ }
+ if opts.Weight != 0 {
+ q["weight"] = strconv.Itoa(opts.Weight)
+ }
+ if opts.PoolID != "" {
+ q["pool_id"] = opts.PoolID
+ }
+ if opts.Address != "" {
+ q["address"] = opts.Address
+ }
+ if opts.TenantID != "" {
+ q["tenant_id"] = opts.TenantID
+ }
+ if opts.AdminStateUp != nil {
+ q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp)
+ }
+ if opts.ProtocolPort != 0 {
+ q["protocol_port"] = strconv.Itoa(opts.ProtocolPort)
+ }
+ if opts.ID != "" {
+ q["id"] = opts.ID
+ }
+ if opts.Marker != "" {
+ q["marker"] = opts.Marker
+ }
+ if opts.Limit != 0 {
+ q["limit"] = strconv.Itoa(opts.Limit)
+ }
+ if opts.SortKey != "" {
+ q["sort_key"] = opts.SortKey
+ }
+ if opts.SortDir != "" {
+ q["sort_dir"] = opts.SortDir
+ }
+
+ u := rootURL(c) + utils.BuildQuery(q)
+
+ return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
+ return MemberPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
+ })
+}
+
+// CreateOpts contains all the values needed to create a new pool member.
+type CreateOpts struct {
+ // Only required if the caller has an admin role and wants to create a pool
+ // for another tenant.
+ TenantID string
+
+ // Required. The IP address of the member.
+ Address string
+
+ // Required. The port on which the application is hosted.
+ ProtocolPort int
+
+ // Required. Subnet in which to access this member.
+ SubnetID string
+}
+
+// Create accepts a CreateOpts struct and uses the values to create a new
+// load balancer pool member.
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
+ type member struct {
+ TenantID string `json:"tenant_id"`
+ ProtocolPort int `json:"protocol_port"`
+ SubnetID string `json:"subnet_id"`
+ Address string `json:"address"`
+ }
+ type request struct {
+ Member member `json:"member"`
+ }
+
+ reqBody := request{Member: member{
+ Address: opts.Address,
+ TenantID: opts.TenantID,
+ ProtocolPort: opts.ProtocolPort,
+ SubnetID: opts.SubnetID,
+ }}
+
+ var res CreateResult
+ _, err := perigee.Request("POST", rootURL(c), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ Results: &res.Resp,
+ OkCodes: []int{201},
+ })
+ res.Err = err
+ return res
+}
+
+// Get retrieves a particular pool member based on its unique ID.
+func Get(c *gophercloud.ServiceClient, id string) GetResult {
+ var res GetResult
+ _, err := perigee.Request("GET", resourceURL(c, id), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ Results: &res.Resp,
+ OkCodes: []int{200},
+ })
+ res.Err = err
+ return res
+}
+
+// UpdateOpts contains the values used when updating a pool member.
+type UpdateOpts struct {
+ // The administrative state of the member, which is up (true) or down (false).
+ AdminStateUp bool
+}
+
+// Update allows members to be updated.
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
+ type member struct {
+ AdminStateUp bool `json:"admin_state_up"`
+ }
+ type request struct {
+ Member member `json:"member"`
+ }
+
+ reqBody := request{Member: member{AdminStateUp: opts.AdminStateUp}}
+
+ // Send request to API
+ var res UpdateResult
+ _, err := perigee.Request("PUT", resourceURL(c, id), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ Results: &res.Resp,
+ OkCodes: []int{200},
+ })
+ res.Err = err
+ return res
+}
+
+// Delete will permanently delete a particular member based on its unique ID.
+func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
+ var res DeleteResult
+ _, err := perigee.Request("DELETE", resourceURL(c, id), perigee.Options{
+ MoreHeaders: c.Provider.AuthenticatedHeaders(),
+ OkCodes: []int{204},
+ })
+ res.Err = err
+ return res
+}
diff --git a/openstack/networking/v2/extensions/lbaas/members/requests_test.go b/openstack/networking/v2/extensions/lbaas/members/requests_test.go
index acb4272..6f4258b 100644
--- a/openstack/networking/v2/extensions/lbaas/members/requests_test.go
+++ b/openstack/networking/v2/extensions/lbaas/members/requests_test.go
@@ -1 +1,255 @@
package members
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+)
+
+const tokenID = "123"
+
+func serviceClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{
+ Provider: &gophercloud.ProviderClient{TokenID: tokenID},
+ Endpoint: th.Endpoint(),
+ }
+}
+
+func TestURLs(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.AssertEquals(t, th.Endpoint()+"v2.0/lb/members", rootURL(serviceClient()))
+}
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/lb/members", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "members":[
+ {
+ "status":"ACTIVE",
+ "weight":1,
+ "admin_state_up":true,
+ "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
+ "pool_id":"72741b06-df4d-4715-b142-276b6bce75ab",
+ "address":"10.0.0.4",
+ "protocol_port":80,
+ "id":"701b531b-111a-4f21-ad85-4795b7b12af6"
+ },
+ {
+ "status":"ACTIVE",
+ "weight":1,
+ "admin_state_up":true,
+ "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
+ "pool_id":"72741b06-df4d-4715-b142-276b6bce75ab",
+ "address":"10.0.0.3",
+ "protocol_port":80,
+ "id":"beb53b4d-230b-4abd-8118-575b8fa006ef"
+ }
+ ]
+}
+ `)
+ })
+
+ count := 0
+
+ List(serviceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractMembers(page)
+ if err != nil {
+ t.Errorf("Failed to extract members: %v", err)
+ return false, err
+ }
+
+ expected := []Member{
+ Member{
+ Status: "ACTIVE",
+ Weight: 1,
+ AdminStateUp: true,
+ TenantID: "83657cfcdfe44cd5920adaf26c48ceea",
+ PoolID: "72741b06-df4d-4715-b142-276b6bce75ab",
+ Address: "10.0.0.4",
+ ProtocolPort: 80,
+ ID: "701b531b-111a-4f21-ad85-4795b7b12af6",
+ },
+ Member{
+ Status: "ACTIVE",
+ Weight: 1,
+ AdminStateUp: true,
+ TenantID: "83657cfcdfe44cd5920adaf26c48ceea",
+ PoolID: "72741b06-df4d-4715-b142-276b6bce75ab",
+ Address: "10.0.0.3",
+ ProtocolPort: 80,
+ ID: "beb53b4d-230b-4abd-8118-575b8fa006ef",
+ },
+ }
+
+ th.CheckDeepEquals(t, expected, actual)
+
+ return true, nil
+ })
+
+ if count != 1 {
+ t.Errorf("Expected 1 page, got %d", count)
+ }
+}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/lb/members", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "member": {
+ "tenant_id": "453105b9-1754-413f-aab1-55f1af620750",
+ "address": "192.0.2.14",
+ "protocol_port":8080,
+ "subnet_id": "SUBNET_ID"
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+
+ fmt.Fprintf(w, `
+{
+ "member": {
+ "id": "975592ca-e308-48ad-8298-731935ee9f45",
+ "address": "192.0.2.14",
+ "protocol_port": 8080,
+ "tenant_id": "453105b9-1754-413f-aab1-55f1af620750",
+ "admin_state_up":true,
+ "weight": 1,
+ "subnet_id": "SUBNET_ID",
+ "status": "DOWN"
+ }
+}
+ `)
+ })
+
+ options := CreateOpts{
+ TenantID: "453105b9-1754-413f-aab1-55f1af620750",
+ Address: "192.0.2.14",
+ ProtocolPort: 8080,
+ SubnetID: "SUBNET_ID",
+ }
+ _, err := Create(serviceClient(), options).Extract()
+ th.AssertNoErr(t, err)
+}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/lb/members/975592ca-e308-48ad-8298-731935ee9f45", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "member":{
+ "id":"975592ca-e308-48ad-8298-731935ee9f45",
+ "address":"192.0.2.14",
+ "protocol_port":8080,
+ "tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
+ "admin_state_up":true,
+ "weight":1,
+ "subnet_id":"SUBNET_ID",
+ "status":"DOWN"
+ }
+}
+ `)
+ })
+
+ m, err := Get(serviceClient(), "975592ca-e308-48ad-8298-731935ee9f45").Extract()
+ th.AssertNoErr(t, err)
+
+ th.AssertEquals(t, "975592ca-e308-48ad-8298-731935ee9f45", m.ID)
+ th.AssertEquals(t, "192.0.2.14", m.Address)
+ th.AssertEquals(t, 8080, m.ProtocolPort)
+ th.AssertEquals(t, "453105b9-1754-413f-aab1-55f1af620750", m.TenantID)
+ th.AssertEquals(t, true, m.AdminStateUp)
+ th.AssertEquals(t, 1, m.Weight)
+ th.AssertEquals(t, "SUBNET_ID", m.SubnetID)
+ th.AssertEquals(t, "DOWN", m.Status)
+}
+
+func TestUpdate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/lb/members/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PUT")
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "member":{
+ "admin_state_up":false
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+{
+ "member":{
+ "status":"PENDING_UPDATE",
+ "protocol_port":8080,
+ "weight":1,
+ "admin_state_up":false,
+ "tenant_id":"4fd44f30292945e481c7b8a0c8908869",
+ "pool_id":"7803631d-f181-4500-b3a2-1b68ba2a75fd",
+ "address":"10.0.0.5",
+ "status_description":null,
+ "id":"48a471ea-64f1-4eb6-9be7-dae6bbe40a0f"
+ }
+}
+ `)
+ })
+
+ options := UpdateOpts{AdminStateUp: false}
+
+ _, err := Update(serviceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", options).Extract()
+ th.AssertNoErr(t, err)
+}
+
+func TestDelete(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/lb/members/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "DELETE")
+ th.TestHeader(t, r, "X-Auth-Token", tokenID)
+ w.WriteHeader(http.StatusNoContent)
+ })
+
+ res := Delete(serviceClient(), "332abe93-f488-41ba-870b-2ac66be7f853")
+ th.AssertNoErr(t, res.Err)
+}
diff --git a/openstack/networking/v2/extensions/lbaas/members/results.go b/openstack/networking/v2/extensions/lbaas/members/results.go
index acb4272..18605a0 100644
--- a/openstack/networking/v2/extensions/lbaas/members/results.go
+++ b/openstack/networking/v2/extensions/lbaas/members/results.go
@@ -1 +1,142 @@
package members
+
+import (
+ "fmt"
+
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// Member represents the application running on a backend server.
+type Member struct {
+ // The status of the member. Indicates whether the member is operational.
+ Status string
+
+ // Weight of member.
+ Weight int
+
+ // The administrative state of the member, which is up (true) or down (false).
+ AdminStateUp bool `json:"admin_state_up" mapstructure:"admin_state_up"`
+
+ // Owner of the member. Only an administrative user can specify a tenant ID
+ // other than its own.
+ TenantID string `json:"tenant_id" mapstructure:"tenant_id"`
+
+ // The pool to which the member belongs.
+ PoolID string `json:"pool_id" mapstructure:"pool_id"`
+
+ // The IP address of the member.
+ Address string
+
+ // The port on which the application is hosted.
+ ProtocolPort int `json:"protocol_port" mapstructure:"protocol_port"`
+
+ // The unique ID for the member.
+ ID string
+
+ // The ID of the associated subnet for this member.
+ SubnetID string `json:"subnet_id" mapstructure:"subnet_id"`
+}
+
+// MemberPage is the page returned by a pager when traversing over a
+// collection of pool members.
+type MemberPage struct {
+ pagination.LinkedPageBase
+}
+
+// NextPageURL is invoked when a paginated collection of members has reached
+// the end of a page and the pager seeks to traverse over a new one. In order
+// to do this, it needs to construct the next page's URL.
+func (p MemberPage) NextPageURL() (string, error) {
+ type link struct {
+ Href string `mapstructure:"href"`
+ Rel string `mapstructure:"rel"`
+ }
+ type resp struct {
+ Links []link `mapstructure:"members_links"`
+ }
+
+ var r resp
+ err := mapstructure.Decode(p.Body, &r)
+ if err != nil {
+ return "", err
+ }
+
+ var url string
+ for _, l := range r.Links {
+ if l.Rel == "next" {
+ url = l.Href
+ }
+ }
+ if url == "" {
+ return "", nil
+ }
+
+ return url, nil
+}
+
+// IsEmpty checks whether a MemberPage struct is empty.
+func (p MemberPage) IsEmpty() (bool, error) {
+ is, err := ExtractMembers(p)
+ if err != nil {
+ return true, nil
+ }
+ return len(is) == 0, nil
+}
+
+// ExtractMembers accepts a Page struct, specifically a MemberPage struct,
+// and extracts the elements into a slice of Member structs. In other words,
+// a generic collection is mapped into a relevant slice.
+func ExtractMembers(page pagination.Page) ([]Member, error) {
+ var resp struct {
+ Members []Member `mapstructure:"members" json:"members"`
+ }
+
+ err := mapstructure.Decode(page.(MemberPage).Body, &resp)
+ if err != nil {
+ return nil, err
+ }
+
+ return resp.Members, nil
+}
+
+type commonResult struct {
+ gophercloud.CommonResult
+}
+
+// Extract is a function that accepts a result and extracts a router.
+func (r commonResult) Extract() (*Member, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ Member *Member `json:"member"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+ if err != nil {
+ return nil, fmt.Errorf("Error decoding Neutron member: %v", err)
+ }
+
+ return res.Member, nil
+}
+
+// CreateResult represents the result of a create operation.
+type CreateResult struct {
+ commonResult
+}
+
+// GetResult represents the result of a get operation.
+type GetResult struct {
+ commonResult
+}
+
+// UpdateResult represents the result of an update operation.
+type UpdateResult struct {
+ commonResult
+}
+
+// DeleteResult represents the result of a delete operation.
+type DeleteResult commonResult
diff --git a/openstack/networking/v2/extensions/lbaas/members/urls.go b/openstack/networking/v2/extensions/lbaas/members/urls.go
index acb4272..9d5ecec 100644
--- a/openstack/networking/v2/extensions/lbaas/members/urls.go
+++ b/openstack/networking/v2/extensions/lbaas/members/urls.go
@@ -1 +1,17 @@
package members
+
+import "github.com/rackspace/gophercloud"
+
+const (
+ version = "v2.0"
+ rootPath = "lb"
+ resourcePath = "members"
+)
+
+func rootURL(c *gophercloud.ServiceClient) string {
+ return c.ServiceURL(version, rootPath, resourcePath)
+}
+
+func resourceURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL(version, rootPath, resourcePath, id)
+}