Rackspace Auto Scale: Add webhooks Update()
diff --git a/rackspace/autoscale/v1/webhooks/fixtures.go b/rackspace/autoscale/v1/webhooks/fixtures.go
index 46ba6b8..8847f46 100644
--- a/rackspace/autoscale/v1/webhooks/fixtures.go
+++ b/rackspace/autoscale/v1/webhooks/fixtures.go
@@ -92,6 +92,16 @@
}
`
+// WebhookUpdateRequest contains the canned body of a webhooks.Update request.
+const WebhookUpdateRequest = `
+{
+ "name": "updated hook",
+ "metadata": {
+ "new-key": "some data"
+ }
+}
+`
+
var (
// FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
FirstWebhook = Webhook{
@@ -180,3 +190,22 @@
fmt.Fprintf(w, WebhookGetBody)
})
}
+
+// HandleWebhookUpdateSuccessfully sets up the test server to respond to a webhooks Update request.
+func HandleWebhookUpdateSuccessfully(t *testing.T) {
+ groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
+ policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+ webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
+
+ path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
+
+ 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, WebhookUpdateRequest)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
diff --git a/rackspace/autoscale/v1/webhooks/requests.go b/rackspace/autoscale/v1/webhooks/requests.go
index 38aa4a7..893ad57 100644
--- a/rackspace/autoscale/v1/webhooks/requests.go
+++ b/rackspace/autoscale/v1/webhooks/requests.go
@@ -7,6 +7,10 @@
"github.com/rackspace/gophercloud/pagination"
)
+// ErrNoName represents a validation error in which a create or update operation
+// has an empty name field.
+var ErrNoName = errors.New("Webhook name cannot by empty.")
+
// List returns all webhooks for a scaling policy.
func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager {
url := listURL(client, groupID, policyID)
@@ -45,7 +49,7 @@
for _, o := range opts {
if o.Name == "" {
- return nil, errors.New("Cannot create a Webhook without a name.")
+ return nil, ErrNoName
}
hook := make(map[string]interface{})
@@ -93,3 +97,59 @@
return result
}
+
+// UpdateOptsBuilder is the interface responsible for generating the map
+// structure for producing JSON for an Update operation.
+type UpdateOptsBuilder interface {
+ ToWebhookUpdateMap() (map[string]interface{}, error)
+}
+
+// UpdateOpts represents the options for updating an existing webhook.
+//
+// Update operations completely replace the configuration being updated. Empty
+// values in the update are accepted and overwrite previously specified
+// parameters.
+type UpdateOpts struct {
+ // Name of the webhook.
+ Name string `mapstructure:"name" json:"name"`
+
+ // Metadata associated with the webhook.
+ Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
+}
+
+// ToWebhookUpdateMap converts an UpdateOpts struct into a map for use as the
+// request body in an Update request.
+func (opts UpdateOpts) ToWebhookUpdateMap() (map[string]interface{}, error) {
+ if opts.Name == "" {
+ return nil, ErrNoName
+ }
+
+ hook := make(map[string]interface{})
+
+ hook["name"] = opts.Name
+
+ if opts.Metadata != nil {
+ hook["metadata"] = opts.Metadata
+ }
+
+ return hook, nil
+}
+
+// Update requests the configuration of the given webhook be updated.
+func Update(client *gophercloud.ServiceClient, groupID, policyID, webhookID string, opts UpdateOptsBuilder) UpdateResult {
+ var result UpdateResult
+
+ url := updateURL(client, groupID, policyID, webhookID)
+ reqBody, err := opts.ToWebhookUpdateMap()
+
+ 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/webhooks/requests_test.go b/rackspace/autoscale/v1/webhooks/requests_test.go
index 5a46a63..e06080a 100644
--- a/rackspace/autoscale/v1/webhooks/requests_test.go
+++ b/rackspace/autoscale/v1/webhooks/requests_test.go
@@ -86,3 +86,21 @@
th.AssertNoErr(t, err)
th.CheckDeepEquals(t, FirstWebhook, *webhook)
}
+
+func TestUpdate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleWebhookUpdateSuccessfully(t)
+
+ client := client.ServiceClient()
+ opts := UpdateOpts{
+ Name: "updated hook",
+ Metadata: map[string]string{
+ "new-key": "some data",
+ },
+ }
+
+ err := Update(client, groupID, policyID, firstID, opts).ExtractErr()
+
+ th.AssertNoErr(t, err)
+}
diff --git a/rackspace/autoscale/v1/webhooks/results.go b/rackspace/autoscale/v1/webhooks/results.go
index 0fa550e..9628724 100644
--- a/rackspace/autoscale/v1/webhooks/results.go
+++ b/rackspace/autoscale/v1/webhooks/results.go
@@ -34,11 +34,6 @@
pagination.SinglePageBase
}
-// GetResult temporarily contains the response from a Get call.
-type GetResult struct {
- webhookResult
-}
-
// ExtractWebhooks extracts a slice of Webhooks from a CreateResult.
func (res CreateResult) ExtractWebhooks() ([]Webhook, error) {
if res.Err != nil {
@@ -48,6 +43,16 @@
return commonExtractWebhooks(res.Body)
}
+// GetResult temporarily contains the response from a Get call.
+type GetResult struct {
+ webhookResult
+}
+
+// UpdateResult represents the result of an update operation.
+type UpdateResult struct {
+ gophercloud.ErrResult
+}
+
// Webhook represents a webhook associted with a scaling policy.
type Webhook struct {
// UUID for the webhook.
diff --git a/rackspace/autoscale/v1/webhooks/urls.go b/rackspace/autoscale/v1/webhooks/urls.go
index d5b7dcb..0efb90f 100644
--- a/rackspace/autoscale/v1/webhooks/urls.go
+++ b/rackspace/autoscale/v1/webhooks/urls.go
@@ -13,3 +13,7 @@
func getURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks", webhookID)
}
+
+func updateURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
+ return getURL(c, groupID, policyID, webhookID)
+}