Adding create pool operation
diff --git a/openstack/networking/v2/extensions/lbaas/pools/requests.go b/openstack/networking/v2/extensions/lbaas/pools/requests.go
index 7337911..d2a0b6d 100644
--- a/openstack/networking/v2/extensions/lbaas/pools/requests.go
+++ b/openstack/networking/v2/extensions/lbaas/pools/requests.go
@@ -3,6 +3,7 @@
import (
"strconv"
+ "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
"github.com/rackspace/gophercloud/pagination"
@@ -83,3 +84,68 @@
return PoolPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
})
}
+
+const (
+ LBMethodRoundRobin = "ROUND_ROBIN"
+ LBMethodLeastConnections = "LEAST_CONNECTIONS"
+
+ ProtocolTCP = "TCP"
+ ProtocolHTTP = "HTTP"
+ ProtocolHTTPS = "HTTPS"
+)
+
+// CreateOpts contains all the values needed to create a new pool.
+type CreateOpts struct {
+ // Only required if the caller has an admin role and wants to create a pool
+ // for another tenant.
+ TenantID string
+
+ // Required. Name of the pool.
+ Name string
+
+ // Required. The protocol used by the pool members, you can use either
+ // ProtocolTCP, ProtocolHTTP, or ProtocolHTTPS.
+ Protocol string
+
+ // The network on which the members of the pool will be located. Only members
+ // that are on this network can be added to the pool.
+ SubnetID string
+
+ // The algorithm used to distribute load between the members of the pool. The
+ // current specification supports LBMethodRoundRobin and
+ // LBMethodLeastConnections as valid values for this attribute.
+ LBMethod string
+}
+
+// Create accepts a CreateOpts struct and uses the values to create a new
+// load balancer pool.
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
+ type pool struct {
+ Name string `json:"name,"`
+ TenantID string `json:"tenant_id"`
+ Protocol string `json:"protocol"`
+ SubnetID string `json:"subnet_id"`
+ LBMethod string `json:"lb_method"`
+ }
+ type request struct {
+ Pool pool `json:"pool"`
+ }
+
+ reqBody := request{Pool: pool{
+ Name: opts.Name,
+ TenantID: opts.TenantID,
+ Protocol: opts.Protocol,
+ SubnetID: opts.SubnetID,
+ LBMethod: opts.LBMethod,
+ }}
+
+ 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
+}
diff --git a/openstack/networking/v2/extensions/lbaas/pools/requests_test.go b/openstack/networking/v2/extensions/lbaas/pools/requests_test.go
index 6c3b23d..2ddcda8 100644
--- a/openstack/networking/v2/extensions/lbaas/pools/requests_test.go
+++ b/openstack/networking/v2/extensions/lbaas/pools/requests_test.go
@@ -110,3 +110,71 @@
t.Errorf("Expected 1 page, got %d", count)
}
}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/v2.0/lb/pools", 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, `
+{
+ "pool": {
+ "lb_method": "ROUND_ROBIN",
+ "protocol": "HTTP",
+ "name": "Example pool",
+ "subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
+ "tenant_id": "2ffc6e22aae24e4795f87155d24c896f"
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+
+ fmt.Fprintf(w, `
+{
+ "pool": {
+ "status": "PENDING_CREATE",
+ "lb_method": "ROUND_ROBIN",
+ "protocol": "HTTP",
+ "description": "",
+ "health_monitors": [],
+ "members": [],
+ "status_description": null,
+ "id": "69055154-f603-4a28-8951-7cc2d9e54a9a",
+ "vip_id": null,
+ "name": "Example pool",
+ "admin_state_up": true,
+ "subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
+ "tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
+ "health_monitors_status": []
+ }
+}
+ `)
+ })
+
+ options := CreateOpts{
+ LBMethod: LBMethodRoundRobin,
+ Protocol: "HTTP",
+ Name: "Example pool",
+ SubnetID: "1981f108-3c48-48d2-b908-30f7d28532c9",
+ TenantID: "2ffc6e22aae24e4795f87155d24c896f",
+ }
+ p, err := Create(serviceClient(), options).Extract()
+ th.AssertNoErr(t, err)
+
+ th.AssertEquals(t, "PENDING_CREATE", p.Status)
+ th.AssertEquals(t, "ROUND_ROBIN", p.LBMethod)
+ th.AssertEquals(t, "HTTP", p.Protocol)
+ th.AssertEquals(t, "", p.Description)
+ th.AssertDeepEquals(t, []string{}, p.MonitorIDs)
+ th.AssertDeepEquals(t, []string{}, p.MemberIDs)
+ th.AssertEquals(t, "69055154-f603-4a28-8951-7cc2d9e54a9a", p.ID)
+ th.AssertEquals(t, "Example pool", p.Name)
+ th.AssertEquals(t, "1981f108-3c48-48d2-b908-30f7d28532c9", p.SubnetID)
+ th.AssertEquals(t, "2ffc6e22aae24e4795f87155d24c896f", p.TenantID)
+}
diff --git a/openstack/networking/v2/extensions/lbaas/pools/results.go b/openstack/networking/v2/extensions/lbaas/pools/results.go
index a9298b7..b077c75 100644
--- a/openstack/networking/v2/extensions/lbaas/pools/results.go
+++ b/openstack/networking/v2/extensions/lbaas/pools/results.go
@@ -1,7 +1,10 @@
package pools
import (
+ "fmt"
+
"github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -23,7 +26,8 @@
// The IDs of associated monitors which check the health of the pool members.
MonitorIDs []string `json:"health_monitors" mapstructure:"health_monitors"`
- // The ID of the subnet associated with the pool
+ // The network on which the members of the pool will be located. Only members
+ // that are on this network can be added to the pool.
SubnetID string `json:"subnet_id" mapstructure:"subnet_id"`
// Owner of the pool. Only an administrative user can specify a tenant ID
@@ -107,3 +111,43 @@
return resp.Pools, nil
}
+
+type commonResult struct {
+ gophercloud.CommonResult
+}
+
+// Extract is a function that accepts a result and extracts a router.
+func (r commonResult) Extract() (*Pool, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ Pool *Pool `json:"pool"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+ if err != nil {
+ return nil, fmt.Errorf("Error decoding Neutron pool: %v", err)
+ }
+
+ return res.Pool, 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