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

Commit 1270499

Browse files
committed
Merge pull request #548 from bison/autoscale-webhooks
[RFR] Rackspace Auto Scale: webhooks
2 parents 37123d6 + 5ed9e9f commit 1270499

File tree

8 files changed

+720
-0
lines changed

8 files changed

+720
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Package webhooks provides information and interaction with the webhook API resource
3+
in the Rackspace Auto Scale service.
4+
5+
Auto Scale uses webhooks to initiate scaling events. Webhooks are associated
6+
with scaling policies and provide capability URLs which can be accessed
7+
anonymously to trigger execution of those policies. The Auto Scale webhook
8+
architecture allows Auto Scale to be integrated with other systems, for example,
9+
monitoring systems.
10+
*/
11+
package webhooks
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
// WebhookCreateBody contains the canned body of a webhooks.Create response.
57+
const WebhookCreateBody = WebhookListBody
58+
59+
// WebhookCreateRequest contains the canned body of a webhooks.Create request.
60+
const WebhookCreateRequest = `
61+
[
62+
{
63+
"name": "first hook"
64+
},
65+
{
66+
"name": "second hook",
67+
"metadata": {
68+
"notes": "a note about this webhook"
69+
}
70+
}
71+
]
72+
`
73+
74+
// WebhookGetBody contains the canned body of a webhooks.Get response.
75+
const WebhookGetBody = `
76+
{
77+
"webhook": {
78+
"id": "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
79+
"name": "first hook",
80+
"links": [
81+
{
82+
"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/",
83+
"rel": "self"
84+
},
85+
{
86+
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
87+
"rel": "capability"
88+
}
89+
],
90+
"metadata": {}
91+
}
92+
}
93+
`
94+
95+
// WebhookUpdateRequest contains the canned body of a webhooks.Update request.
96+
const WebhookUpdateRequest = `
97+
{
98+
"name": "updated hook",
99+
"metadata": {
100+
"new-key": "some data"
101+
}
102+
}
103+
`
104+
105+
var (
106+
// FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
107+
FirstWebhook = Webhook{
108+
ID: "2bd1822c-58c5-49fd-8b3d-ed44781a58d1",
109+
Name: "first hook",
110+
Links: []gophercloud.Link{
111+
{
112+
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/",
113+
Rel: "self",
114+
},
115+
{
116+
Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/714c1c17c5e6ea5ef1e710d5ccc62e492575bab5216184d4c27dc0164db1bc06/",
117+
Rel: "capability",
118+
},
119+
},
120+
Metadata: map[string]string{},
121+
}
122+
123+
// SecondWebhook is a Webhook corresponding to the second result in WebhookListBody.
124+
SecondWebhook = Webhook{
125+
ID: "76711c36-dfbe-4f5e-bea6-cded99690515",
126+
Name: "second hook",
127+
Links: []gophercloud.Link{
128+
{
129+
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/",
130+
Rel: "self",
131+
},
132+
{
133+
Href: "https://dfw.autoscale.api.rackspacecloud.com/v1.0/execute/1/982e24858723f9e8bc2afea42a73a3c357c8f518857735400a7f7d8b3f14ccdb/",
134+
Rel: "capability",
135+
},
136+
},
137+
Metadata: map[string]string{
138+
"notes": "a note about this webhook",
139+
},
140+
}
141+
)
142+
143+
// HandleWebhookListSuccessfully sets up the test server to respond to a webhooks List request.
144+
func HandleWebhookListSuccessfully(t *testing.T) {
145+
path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
146+
147+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
148+
th.TestMethod(t, r, "GET")
149+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
150+
151+
w.Header().Add("Content-Type", "application/json")
152+
153+
fmt.Fprintf(w, WebhookListBody)
154+
})
155+
}
156+
157+
// HandleWebhookCreateSuccessfully sets up the test server to respond to a webhooks Create request.
158+
func HandleWebhookCreateSuccessfully(t *testing.T) {
159+
path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/webhooks"
160+
161+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
162+
th.TestMethod(t, r, "POST")
163+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
164+
th.TestHeader(t, r, "Content-Type", "application/json")
165+
th.TestHeader(t, r, "Accept", "application/json")
166+
167+
th.TestJSONRequest(t, r, WebhookCreateRequest)
168+
169+
w.Header().Add("Content-Type", "application/json")
170+
w.WriteHeader(http.StatusCreated)
171+
172+
fmt.Fprintf(w, WebhookCreateBody)
173+
})
174+
}
175+
176+
// HandleWebhookGetSuccessfully sets up the test server to respond to a webhooks Get request.
177+
func HandleWebhookGetSuccessfully(t *testing.T) {
178+
groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
179+
policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
180+
webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
181+
182+
path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
183+
184+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
185+
th.TestMethod(t, r, "GET")
186+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
187+
188+
w.Header().Add("Content-Type", "application/json")
189+
190+
fmt.Fprintf(w, WebhookGetBody)
191+
})
192+
}
193+
194+
// HandleWebhookUpdateSuccessfully sets up the test server to respond to a webhooks Update request.
195+
func HandleWebhookUpdateSuccessfully(t *testing.T) {
196+
groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
197+
policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
198+
webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
199+
200+
path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
201+
202+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
203+
th.TestMethod(t, r, "PUT")
204+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
205+
206+
th.TestJSONRequest(t, r, WebhookUpdateRequest)
207+
208+
w.WriteHeader(http.StatusNoContent)
209+
})
210+
}
211+
212+
// HandleWebhookDeleteSuccessfully sets up the test server to respond to a webhooks Delete request.
213+
func HandleWebhookDeleteSuccessfully(t *testing.T) {
214+
groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
215+
policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
216+
webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
217+
218+
path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
219+
220+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
221+
th.TestMethod(t, r, "DELETE")
222+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
223+
224+
w.WriteHeader(http.StatusNoContent)
225+
})
226+
}

0 commit comments

Comments
 (0)