Rackspace Auto Scale: Add webhooks List()
diff --git a/rackspace/autoscale/v1/webhooks/fixtures.go b/rackspace/autoscale/v1/webhooks/fixtures.go
new file mode 100644
index 0000000..18f6b63
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/fixtures.go
@@ -0,0 +1,106 @@
+// +build fixtures
+
+package webhooks
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ "github.com/rackspace/gophercloud"
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// WebhookListBody contains the canned body of a webhooks.List response.
+const WebhookListBody = `
+{
+ "webhooks": [
+ {
+ "id": "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
+ "name": "first hook",
+ "links": [
+ {
+ "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/",
+ "rel": "self"
+ },
+ {
+ "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
+ "rel": "capability"
+ }
+ ],
+ "metadata": {}
+ },
+ {
+ "id": "76711c36-dfbe-4f5e-bea6-cded99690515",
+ "name": "second hook",
+ "links": [
+ {
+ "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/76711c36-dfbe-4f5e-bea6-cded99690515/",
+ "rel": "self"
+ },
+ {
+ "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
+ "rel": "capability"
+ }
+ ],
+ "metadata": {
+ "notes": "a note about this webhook"
+ }
+ }
+ ],
+ "webhooks_links": []
+}
+`
+
+var (
+ // FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
+ FirstWebhook = Webhook{
+ ID: "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
+ Name: "first hook",
+ Links: []gophercloud.Link{
+ {
+ Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/2bd1822c-58c5-49fd-8b3d-ed44781a58d1/",
+ Rel: "self",
+ },
+ {
+ Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
+ Rel: "capability",
+ },
+ },
+ Metadata: map[string]string{},
+ }
+
+ // SecondWebhook is a Webhook corresponding to the second result in WebhookListBody.
+ SecondWebhook = Webhook{
+ ID: "76711c36-dfbe-4f5e-bea6-cded99690515",
+ Name: "second hook",
+ Links: []gophercloud.Link{
+ {
+ Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks/76711c36-dfbe-4f5e-bea6-cded99690515/",
+ Rel: "self",
+ },
+ {
+ Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
+ Rel: "capability",
+ },
+ },
+ Metadata: map[string]string{
+ "notes": "a note about this webhook",
+ },
+ }
+)
+
+// HandleWebhookListSuccessfully sets up the test server to respond to a webhooks List request.
+func HandleWebhookListSuccessfully(t *testing.T) {
+ path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
+
+ th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+
+ fmt.Fprintf(w, WebhookListBody)
+ })
+}
diff --git a/rackspace/autoscale/v1/webhooks/requests.go b/rackspace/autoscale/v1/webhooks/requests.go
new file mode 100644
index 0000000..1d56cef
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/requests.go
@@ -0,0 +1,17 @@
+package webhooks
+
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// List returns all webhooks for a scaling policy.
+func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager {
+ url := listURL(client, groupID, policyID)
+
+ createPageFn := func(r pagination.PageResult) pagination.Page {
+ return WebhookPage{pagination.SinglePageBase(r)}
+ }
+
+ return pagination.NewPager(client, url, createPageFn)
+}
diff --git a/rackspace/autoscale/v1/webhooks/requests_test.go b/rackspace/autoscale/v1/webhooks/requests_test.go
new file mode 100644
index 0000000..c0c1258
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/requests_test.go
@@ -0,0 +1,46 @@
+package webhooks
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleWebhookListSuccessfully(t)
+
+ groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
+ policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+
+ pages := 0
+ pager := List(client.ServiceClient(), groupID, policyID)
+
+ err := pager.EachPage(func(page pagination.Page) (bool, error) {
+ pages++
+
+ webhooks, err := ExtractWebhooks(page)
+
+ if err != nil {
+ return false, err
+ }
+
+ if len(webhooks) != 2 {
+ t.Fatalf("Expected 2 policies, got %d", len(webhooks))
+ }
+
+ th.CheckDeepEquals(t, FirstWebhook, webhooks[0])
+ th.CheckDeepEquals(t, SecondWebhook, webhooks[1])
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+
+ if pages != 1 {
+ t.Errorf("Expected 1 page, saw %d", pages)
+ }
+}
diff --git a/rackspace/autoscale/v1/webhooks/results.go b/rackspace/autoscale/v1/webhooks/results.go
new file mode 100644
index 0000000..a15e20e
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/results.go
@@ -0,0 +1,62 @@
+package webhooks
+
+import (
+ "github.com/mitchellh/mapstructure"
+
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+type webhookResult struct {
+ gophercloud.Result
+}
+
+// Webhook represents a webhook associted with a scaling policy.
+type Webhook struct {
+ // UUID for the webhook.
+ ID string `mapstructure:"id" json:"id"`
+
+ // Name of the webhook.
+ Name string `mapstructure:"name" json:"name"`
+
+ // Links associated with the webhook, including the capability URL.
+ Links []gophercloud.Link `mapstructure:"links" json:"links"`
+
+ // Metadata associated with the webhook.
+ Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
+}
+
+// WebhookPage is the page returned by a pager when traversing over a collection
+// of webhooks.
+type WebhookPage struct {
+ pagination.SinglePageBase
+}
+
+// IsEmpty returns true if a page contains no Webhook results.
+func (page WebhookPage) IsEmpty() (bool, error) {
+ hooks, err := ExtractWebhooks(page)
+
+ if err != nil {
+ return true, err
+ }
+
+ return len(hooks) == 0, nil
+}
+
+// ExtractWebhooks interprets the results of a single page from a List() call,
+// producing a slice of Webhooks.
+func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
+ casted := page.(WebhookPage).Body
+
+ var response struct {
+ Webhooks []Webhook `mapstructure:"webhooks"`
+ }
+
+ err := mapstructure.Decode(casted, &response)
+
+ if err != nil {
+ return nil, err
+ }
+
+ return response.Webhooks, err
+}
diff --git a/rackspace/autoscale/v1/webhooks/urls.go b/rackspace/autoscale/v1/webhooks/urls.go
new file mode 100644
index 0000000..802db89
--- /dev/null
+++ b/rackspace/autoscale/v1/webhooks/urls.go
@@ -0,0 +1,7 @@
+package webhooks
+
+import "github.com/rackspace/gophercloud"
+
+func listURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
+ return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks")
+}