Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 366a7a0

Browse files
committed
Rackspace Auto Scale: Add webhooks List()
1 parent 282247b commit 366a7a0

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// +build fixtures
2+
3+
package webhooks
4+
5+
import (
6+
"fmt"
7+
"net/http"
8+
"testing"
9+
10+
"github.com/rackspace/gophercloud"
11+
th "github.com/rackspace/gophercloud/testhelper"
12+
"github.com/rackspace/gophercloud/testhelper/client"
13+
)
14+
15+
// WebhookListBody contains the canned body of a webhooks.List response.
16+
const WebhookListBody = `
17+
{
18+
"webhooks": [
19+
{
20+
"id": "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
21+
"name": "first hook",
22+
"links": [
23+
{
24+
"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/",
25+
"rel": "self"
26+
},
27+
{
28+
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
29+
"rel": "capability"
30+
}
31+
],
32+
"metadata": {}
33+
},
34+
{
35+
"id": "76711c36-dfbe-4f5e-bea6-cded99690515",
36+
"name": "second hook",
37+
"links": [
38+
{
39+
"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/",
40+
"rel": "self"
41+
},
42+
{
43+
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
44+
"rel": "capability"
45+
}
46+
],
47+
"metadata": {
48+
"notes": "a note about this webhook"
49+
}
50+
}
51+
],
52+
"webhooks_links": []
53+
}
54+
`
55+
56+
var (
57+
// FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
58+
FirstWebhook = Webhook{
59+
ID: "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
60+
Name: "first hook",
61+
Links: []gophercloud.Link{
62+
{
63+
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/",
64+
Rel: "self",
65+
},
66+
{
67+
Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
68+
Rel: "capability",
69+
},
70+
},
71+
Metadata: map[string]string{},
72+
}
73+
74+
// SecondWebhook is a Webhook corresponding to the second result in WebhookListBody.
75+
SecondWebhook = Webhook{
76+
ID: "76711c36-dfbe-4f5e-bea6-cded99690515",
77+
Name: "second hook",
78+
Links: []gophercloud.Link{
79+
{
80+
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/",
81+
Rel: "self",
82+
},
83+
{
84+
Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
85+
Rel: "capability",
86+
},
87+
},
88+
Metadata: map[string]string{
89+
"notes": "a note about this webhook",
90+
},
91+
}
92+
)
93+
94+
// HandleWebhookListSuccessfully sets up the test server to respond to a webhooks List request.
95+
func HandleWebhookListSuccessfully(t *testing.T) {
96+
path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
97+
98+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
99+
th.TestMethod(t, r, "GET")
100+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
101+
102+
w.Header().Add("Content-Type", "application/json")
103+
104+
fmt.Fprintf(w, WebhookListBody)
105+
})
106+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package webhooks
2+
3+
import (
4+
"github.com/rackspace/gophercloud"
5+
"github.com/rackspace/gophercloud/pagination"
6+
)
7+
8+
// List returns all webhooks for a scaling policy.
9+
func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager {
10+
url := listURL(client, groupID, policyID)
11+
12+
createPageFn := func(r pagination.PageResult) pagination.Page {
13+
return WebhookPage{pagination.SinglePageBase(r)}
14+
}
15+
16+
return pagination.NewPager(client, url, createPageFn)
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package webhooks
2+
3+
import (
4+
"testing"
5+
6+
"github.com/rackspace/gophercloud/pagination"
7+
th "github.com/rackspace/gophercloud/testhelper"
8+
"github.com/rackspace/gophercloud/testhelper/client"
9+
)
10+
11+
func TestList(t *testing.T) {
12+
th.SetupHTTP()
13+
defer th.TeardownHTTP()
14+
HandleWebhookListSuccessfully(t)
15+
16+
groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
17+
policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
18+
19+
pages := 0
20+
pager := List(client.ServiceClient(), groupID, policyID)
21+
22+
err := pager.EachPage(func(page pagination.Page) (bool, error) {
23+
pages++
24+
25+
webhooks, err := ExtractWebhooks(page)
26+
27+
if err != nil {
28+
return false, err
29+
}
30+
31+
if len(webhooks) != 2 {
32+
t.Fatalf("Expected 2 policies, got %d", len(webhooks))
33+
}
34+
35+
th.CheckDeepEquals(t, FirstWebhook, webhooks[0])
36+
th.CheckDeepEquals(t, SecondWebhook, webhooks[1])
37+
38+
return true, nil
39+
})
40+
41+
th.AssertNoErr(t, err)
42+
43+
if pages != 1 {
44+
t.Errorf("Expected 1 page, saw %d", pages)
45+
}
46+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package webhooks
2+
3+
import (
4+
"github.com/mitchellh/mapstructure"
5+
6+
"github.com/rackspace/gophercloud"
7+
"github.com/rackspace/gophercloud/pagination"
8+
)
9+
10+
type webhookResult struct {
11+
gophercloud.Result
12+
}
13+
14+
// Webhook represents a webhook associted with a scaling policy.
15+
type Webhook struct {
16+
// UUID for the webhook.
17+
ID string `mapstructure:"id" json:"id"`
18+
19+
// Name of the webhook.
20+
Name string `mapstructure:"name" json:"name"`
21+
22+
// Links associated with the webhook, including the capability URL.
23+
Links []gophercloud.Link `mapstructure:"links" json:"links"`
24+
25+
// Metadata associated with the webhook.
26+
Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
27+
}
28+
29+
// WebhookPage is the page returned by a pager when traversing over a collection
30+
// of webhooks.
31+
type WebhookPage struct {
32+
pagination.SinglePageBase
33+
}
34+
35+
// IsEmpty returns true if a page contains no Webhook results.
36+
func (page WebhookPage) IsEmpty() (bool, error) {
37+
hooks, err := ExtractWebhooks(page)
38+
39+
if err != nil {
40+
return true, err
41+
}
42+
43+
return len(hooks) == 0, nil
44+
}
45+
46+
// ExtractWebhooks interprets the results of a single page from a List() call,
47+
// producing a slice of Webhooks.
48+
func ExtractWebhooks(page pagination.Page) ([]Webhook, error) {
49+
casted := page.(WebhookPage).Body
50+
51+
var response struct {
52+
Webhooks []Webhook `mapstructure:"webhooks"`
53+
}
54+
55+
err := mapstructure.Decode(casted, &response)
56+
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return response.Webhooks, err
62+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package webhooks
2+
3+
import "github.com/rackspace/gophercloud"
4+
5+
func listURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
6+
return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks")
7+
}

0 commit comments

Comments
 (0)