Create VIP operation
diff --git a/rackspace/lb/v1/vips/fixtures.go b/rackspace/lb/v1/vips/fixtures.go
index 1b820a6..4c0a00d 100644
--- a/rackspace/lb/v1/vips/fixtures.go
+++ b/rackspace/lb/v1/vips/fixtures.go
@@ -49,7 +49,7 @@
`)
w.Header().Add("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
+ w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, `
{
diff --git a/rackspace/lb/v1/vips/requests.go b/rackspace/lb/v1/vips/requests.go
index d4bdb98..92bd0b2 100644
--- a/rackspace/lb/v1/vips/requests.go
+++ b/rackspace/lb/v1/vips/requests.go
@@ -1,6 +1,8 @@
package vips
import (
+ "github.com/racker/perigee"
+
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -13,3 +15,57 @@
return VIPPage{pagination.SinglePageBase(r)}
})
}
+
+// CreateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Create operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type CreateOptsBuilder interface {
+ ToVIPCreateMap() (map[string]interface{}, error)
+}
+
+// CreateOpts is the common options struct used in this package's Create
+// operation.
+type CreateOpts struct {
+ ID string
+
+ Type string
+
+ Version string
+}
+
+// ToVIPCreateMap casts a CreateOpts struct to a map.
+func (opts CreateOpts) ToVIPCreateMap() (map[string]interface{}, error) {
+ lb := make(map[string]interface{})
+
+ if opts.ID != "" {
+ lb["id"] = opts.ID
+ }
+ if opts.Type != "" {
+ lb["type"] = opts.Type
+ }
+ if opts.Version != "" {
+ lb["ipVersion"] = opts.Version
+ }
+
+ return lb, nil
+}
+
+func Create(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) CreateResult {
+ var res CreateResult
+
+ reqBody, err := opts.ToVIPCreateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = perigee.Request("POST", rootURL(c, lbID), perigee.Options{
+ MoreHeaders: c.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ Results: &res.Body,
+ OkCodes: []int{202},
+ })
+
+ return res
+}
diff --git a/rackspace/lb/v1/vips/requests_test.go b/rackspace/lb/v1/vips/requests_test.go
index a50fbae..762b139 100644
--- a/rackspace/lb/v1/vips/requests_test.go
+++ b/rackspace/lb/v1/vips/requests_test.go
@@ -38,3 +38,27 @@
th.AssertNoErr(t, err)
th.AssertEquals(t, 1, count)
}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockCreateResponse(t, lbID)
+
+ opts := CreateOpts{
+ Type: "PUBLIC",
+ Version: "IPV6",
+ }
+
+ vip, err := Create(client.ServiceClient(), lbID, opts).Extract()
+ th.AssertNoErr(t, err)
+
+ expected := &VIP{
+ Address: "fd24:f480:ce44:91bc:1af2:15ff:0000:0002",
+ ID: 9000134,
+ Type: "PUBLIC",
+ Version: "IPV6",
+ }
+
+ th.CheckDeepEquals(t, expected, vip)
+}
diff --git a/rackspace/lb/v1/vips/results.go b/rackspace/lb/v1/vips/results.go
index a3bfed7..aa5ef37 100644
--- a/rackspace/lb/v1/vips/results.go
+++ b/rackspace/lb/v1/vips/results.go
@@ -2,6 +2,8 @@
import (
"github.com/mitchellh/mapstructure"
+
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -40,3 +42,22 @@
return resp.VIPs, err
}
+
+type commonResult struct {
+ gophercloud.Result
+}
+
+func (r commonResult) Extract() (*VIP, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ resp := &VIP{}
+ err := mapstructure.Decode(r.Body, resp)
+
+ return resp, err
+}
+
+type CreateResult struct {
+ commonResult
+}