Add update LB functionality
diff --git a/rackspace/lb/lb/fixtures.go b/rackspace/lb/lb/fixtures.go
index 19a605d..d27aae2 100644
--- a/rackspace/lb/lb/fixtures.go
+++ b/rackspace/lb/lb/fixtures.go
@@ -233,3 +233,26 @@
`)
})
}
+
+func mockUpdateLBResponse(t *testing.T, id int) {
+ th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PUT")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ th.TestJSONRequest(t, r, `
+{
+ "loadBalancer": {
+ "name": "a-new-loadbalancer",
+ "protocol": "TCP",
+ "halfClosed": true,
+ "algorithm": "RANDOM",
+ "port": 8080,
+ "timeout": 100,
+ "httpsRedirect": false
+ }
+}
+ `)
+
+ w.WriteHeader(http.StatusOK)
+ })
+}
diff --git a/rackspace/lb/lb/requests.go b/rackspace/lb/lb/requests.go
index 81a8642..158c9be 100644
--- a/rackspace/lb/lb/requests.go
+++ b/rackspace/lb/lb/requests.go
@@ -270,3 +270,70 @@
return res
}
+
+type UpdateOptsBuilder interface {
+ ToLBUpdateMap() (map[string]interface{}, error)
+}
+
+type UpdateOpts struct {
+ Name string
+
+ Protocol Protocol
+
+ HalfClosed enabledState
+
+ Algorithm Algorithm
+
+ Port int
+
+ Timeout int
+
+ HTTPSRedirect enabledState
+}
+
+// ToLBUpdateMap casts a CreateOpts struct to a map.
+func (opts UpdateOpts) ToLBUpdateMap() (map[string]interface{}, error) {
+ lb := make(map[string]interface{})
+
+ if opts.Name != "" {
+ lb["name"] = opts.Name
+ }
+ if opts.Protocol != "" {
+ lb["protocol"] = opts.Protocol
+ }
+ if opts.HalfClosed != nil {
+ lb["halfClosed"] = opts.HalfClosed
+ }
+ if opts.Algorithm != "" {
+ lb["algorithm"] = opts.Algorithm
+ }
+ if opts.Port > 0 {
+ lb["port"] = opts.Port
+ }
+ if opts.Timeout > 0 {
+ lb["timeout"] = opts.Timeout
+ }
+ if opts.HTTPSRedirect != nil {
+ lb["httpsRedirect"] = &opts.HTTPSRedirect
+ }
+
+ return map[string]interface{}{"loadBalancer": lb}, nil
+}
+
+func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult {
+ var res UpdateResult
+
+ reqBody, err := opts.ToLBUpdateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = perigee.Request("PUT", resourceURL(c, id), perigee.Options{
+ MoreHeaders: c.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ OkCodes: []int{200},
+ })
+
+ return res
+}
diff --git a/rackspace/lb/lb/requests_test.go b/rackspace/lb/lb/requests_test.go
index 6f9417f..5c0fd57 100644
--- a/rackspace/lb/lb/requests_test.go
+++ b/rackspace/lb/lb/requests_test.go
@@ -204,3 +204,23 @@
th.AssertDeepEquals(t, expected, lb)
th.AssertNoErr(t, err)
}
+
+func TestUpdate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ mockUpdateLBResponse(t, id1)
+
+ opts := UpdateOpts{
+ Name: "a-new-loadbalancer",
+ Protocol: "TCP",
+ HalfClosed: Enabled,
+ Algorithm: RAND,
+ Port: 8080,
+ Timeout: 100,
+ HTTPSRedirect: Disabled,
+ }
+
+ err := Update(client.ServiceClient(), id1, opts).ExtractErr()
+ th.AssertNoErr(t, err)
+}
diff --git a/rackspace/lb/lb/results.go b/rackspace/lb/lb/results.go
index d0ce197..70b0272 100644
--- a/rackspace/lb/lb/results.go
+++ b/rackspace/lb/lb/results.go
@@ -242,6 +242,10 @@
gophercloud.ErrResult
}
+type UpdateResult struct {
+ gophercloud.ErrResult
+}
+
type GetResult struct {
commonResult
}