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

Commit 20644be

Browse files
committed
Rackspace Auto Scale: Add webhooks Get()
1 parent e6e0ec1 commit 20644be

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

rackspace/autoscale/v1/webhooks/fixtures.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ const WebhookCreateRequest = `
7171
]
7272
`
7373

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+
7495
var (
7596
// FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
7697
FirstWebhook = Webhook{
@@ -141,3 +162,21 @@ func HandleWebhookCreateSuccessfully(t *testing.T) {
141162
fmt.Fprintf(w, WebhookCreateBody)
142163
})
143164
}
165+
166+
// HandleWebhookGetSuccessfully sets up the test server to respond to a webhooks Get request.
167+
func HandleWebhookGetSuccessfully(t *testing.T) {
168+
groupID := "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
169+
policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
170+
webhookID := "2bd1822c-58c5-49fd-8b3d-ed44781a58d1"
171+
172+
path := fmt.Sprintf("/groups/%s/policies/%s/webhooks/%s", groupID, policyID, webhookID)
173+
174+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
175+
th.TestMethod(t, r, "GET")
176+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
177+
178+
w.Header().Add("Content-Type", "application/json")
179+
180+
fmt.Fprintf(w, WebhookGetBody)
181+
})
182+
}

rackspace/autoscale/v1/webhooks/requests.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,12 @@ func Create(client *gophercloud.ServiceClient, groupID, policyID string, opts Cr
8484
pr := pagination.PageResultFromParsed(resp, res.Body)
8585
return CreateResult{pagination.SinglePageBase(pr)}
8686
}
87+
88+
// Get requests the details of a single webhook with the given ID.
89+
func Get(client *gophercloud.ServiceClient, groupID, policyID, webhookID string) GetResult {
90+
var result GetResult
91+
92+
_, result.Err = client.Get(getURL(client, groupID, policyID, webhookID), &result.Body, nil)
93+
94+
return result
95+
}

rackspace/autoscale/v1/webhooks/requests_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
const (
1212
groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
1313
policyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
14+
firstID = "2bd1822c-58c5-49fd-8b3d-ed44781a58d1" // FirstWebhook
15+
secondID = "76711c36-dfbe-4f5e-bea6-cded99690515" // SecondWebhook
1416
)
1517

1618
func TestList(t *testing.T) {
@@ -71,3 +73,16 @@ func TestCreate(t *testing.T) {
7173
th.CheckDeepEquals(t, FirstWebhook, webhooks[0])
7274
th.CheckDeepEquals(t, SecondWebhook, webhooks[1])
7375
}
76+
77+
func TestGet(t *testing.T) {
78+
th.SetupHTTP()
79+
defer th.TeardownHTTP()
80+
HandleWebhookGetSuccessfully(t)
81+
82+
client := client.ServiceClient()
83+
84+
webhook, err := Get(client, groupID, policyID, firstID).Extract()
85+
86+
th.AssertNoErr(t, err)
87+
th.CheckDeepEquals(t, FirstWebhook, *webhook)
88+
}

rackspace/autoscale/v1/webhooks/results.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ type webhookResult struct {
1111
gophercloud.Result
1212
}
1313

14+
// Extract interprets any webhookResult as a Webhook, if possible.
15+
func (r webhookResult) Extract() (*Webhook, error) {
16+
if r.Err != nil {
17+
return nil, r.Err
18+
}
19+
20+
var response struct {
21+
Webhook Webhook `mapstructure:"webhook"`
22+
}
23+
24+
err := mapstructure.Decode(r.Body, &response)
25+
26+
return &response.Webhook, err
27+
}
28+
1429
// CreateResult represents the result of a create operation. Multiple webhooks
1530
// can be created in a single call, so the result should be treated as a typical
1631
// pagination Page. ExtractWebhooks() can be used to extract a slice of
@@ -19,6 +34,11 @@ type CreateResult struct {
1934
pagination.SinglePageBase
2035
}
2136

37+
// GetResult temporarily contains the response from a Get call.
38+
type GetResult struct {
39+
webhookResult
40+
}
41+
2242
// ExtractWebhooks extracts a slice of Webhooks from a CreateResult.
2343
func (res CreateResult) ExtractWebhooks() ([]Webhook, error) {
2444
if res.Err != nil {

rackspace/autoscale/v1/webhooks/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ func listURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
99
func createURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
1010
return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks")
1111
}
12+
13+
func getURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
14+
return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks", webhookID)
15+
}

0 commit comments

Comments
 (0)