Rackspace Auto Scale: Add policies Update()
diff --git a/rackspace/autoscale/v1/policies/fixtures.go b/rackspace/autoscale/v1/policies/fixtures.go
index 587c5e7..a66d6d1 100644
--- a/rackspace/autoscale/v1/policies/fixtures.go
+++ b/rackspace/autoscale/v1/policies/fixtures.go
@@ -117,6 +117,16 @@
}
`
+// PolicyUpdateRequest contains the canned body of a policies.Update request.
+const PolicyUpdateRequest = `
+{
+ "name": "updated webhook policy",
+ "type": "webhook",
+ "cooldown": 600,
+ "changePercent": 6.6
+}
+`
+
var (
// WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
WebhookPolicy = Policy{
@@ -199,3 +209,20 @@
fmt.Fprintf(w, PolicyGetBody)
})
}
+
+// HandlePolicyUpdateSuccessfully sets up the test server to respond to a policies Update request.
+func HandlePolicyUpdateSuccessfully(t *testing.T) {
+ groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
+ policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+
+ path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
+
+ th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PUT")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ th.TestJSONRequest(t, r, PolicyUpdateRequest)
+
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
diff --git a/rackspace/autoscale/v1/policies/requests.go b/rackspace/autoscale/v1/policies/requests.go
index ab3d5eb..9cc6c34 100644
--- a/rackspace/autoscale/v1/policies/requests.go
+++ b/rackspace/autoscale/v1/policies/requests.go
@@ -129,3 +129,78 @@
return result
}
+
+// UpdateOptsBuilder is the interface responsible for generating the map
+// structure for producing JSON for an Update operation.
+type UpdateOptsBuilder interface {
+ ToPolicyUpdateMap() (map[string]interface{}, error)
+}
+
+// UpdateOpts represents the options for updating an existing policy.
+//
+// Update operations completely replace the configuration being updated. Empty
+// values in the update are accepted and overwrite previously specified
+// parameters.
+type UpdateOpts struct {
+ // Name [required] is a name for the policy.
+ Name string
+
+ // Type [required] of policy, i.e. either "webhook" or "schedule".
+ Type Type
+
+ // Cooldown [required] period in seconds. If you don't specify a cooldown,
+ // it will default to zero, and the policy will be configured as such.
+ Cooldown int
+
+ // Adjustment [requried] type and value for the policy.
+ Adjustment Adjustment
+
+ // Additional configuration options for some types of policy.
+ Args map[string]interface{}
+}
+
+// ToPolicyUpdateMap converts an UpdateOpts struct into a map for use as the
+// request body in an Update request.
+func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
+ if opts.Name == "" {
+ return nil, ErrNoName
+ }
+
+ if opts.Type == Schedule && opts.Args == nil {
+ return nil, ErrNoArgs
+ }
+
+ policy := make(map[string]interface{})
+
+ policy["name"] = opts.Name
+ policy["type"] = opts.Type
+ policy["cooldown"] = opts.Cooldown
+
+ // TODO: Function to validate and cast key + value?
+ policy[string(opts.Adjustment.Type)] = opts.Adjustment.Value
+
+ if opts.Args != nil {
+ policy["args"] = opts.Args
+ }
+
+ return policy, nil
+}
+
+// Update requests the configuration of the given policy be updated.
+func Update(client *gophercloud.ServiceClient, groupID, policyID string, opts UpdateOptsBuilder) UpdateResult {
+ var result UpdateResult
+
+ url := updateURL(client, groupID, policyID)
+ reqBody, err := opts.ToPolicyUpdateMap()
+
+ if err != nil {
+ result.Err = err
+ return result
+ }
+
+ _, result.Err = client.Put(url, reqBody, nil, &gophercloud.RequestOpts{
+ OkCodes: []int{204},
+ })
+
+ return result
+}
diff --git a/rackspace/autoscale/v1/policies/requests_test.go b/rackspace/autoscale/v1/policies/requests_test.go
index b600377..8ee90f3 100644
--- a/rackspace/autoscale/v1/policies/requests_test.go
+++ b/rackspace/autoscale/v1/policies/requests_test.go
@@ -108,3 +108,24 @@
th.AssertNoErr(t, err)
th.CheckDeepEquals(t, WebhookPolicy, *policy)
}
+
+func TestUpdate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePolicyUpdateSuccessfully(t)
+
+ client := client.ServiceClient()
+ opts := UpdateOpts{
+ Name: "updated webhook policy",
+ Type: Webhook,
+ Cooldown: 600,
+ Adjustment: Adjustment{
+ Type: ChangePercent,
+ Value: 6.6,
+ },
+ }
+
+ err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()
+
+ th.AssertNoErr(t, err)
+}
diff --git a/rackspace/autoscale/v1/policies/results.go b/rackspace/autoscale/v1/policies/results.go
index e92d636..f63b1a7 100644
--- a/rackspace/autoscale/v1/policies/results.go
+++ b/rackspace/autoscale/v1/policies/results.go
@@ -47,6 +47,11 @@
policyResult
}
+// UpdateResult represents the result of an update operation.
+type UpdateResult struct {
+ gophercloud.ErrResult
+}
+
// Policy represents a scaling policy.
type Policy struct {
// UUID for the policy.
diff --git a/rackspace/autoscale/v1/policies/urls.go b/rackspace/autoscale/v1/policies/urls.go
index ebbd154..7203ccf 100644
--- a/rackspace/autoscale/v1/policies/urls.go
+++ b/rackspace/autoscale/v1/policies/urls.go
@@ -13,3 +13,7 @@
func getURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
return c.ServiceURL("groups", groupID, "policies", policyID)
}
+
+func updateURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
+ return getURL(c, groupID, policyID)
+}