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

Commit d9ebfb9

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

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

rackspace/autoscale/v1/webhooks/fixtures.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ const WebhookGetBody = `
9292
}
9393
`
9494

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+
95105
var (
96106
// FirstWebhook is a Webhook corresponding to the first result in WebhookListBody.
97107
FirstWebhook = Webhook{
@@ -180,3 +190,22 @@ func HandleWebhookGetSuccessfully(t *testing.T) {
180190
fmt.Fprintf(w, WebhookGetBody)
181191
})
182192
}
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.Header().Add("Content-Type", "application/json")
209+
w.WriteHeader(http.StatusNoContent)
210+
})
211+
}

rackspace/autoscale/v1/webhooks/requests.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"github.com/rackspace/gophercloud/pagination"
88
)
99

10+
// ErrNoName represents a validation error in which a create or update operation
11+
// has an empty name field.
12+
var ErrNoName = errors.New("Webhook name cannot by empty.")
13+
1014
// List returns all webhooks for a scaling policy.
1115
func List(client *gophercloud.ServiceClient, groupID, policyID string) pagination.Pager {
1216
url := listURL(client, groupID, policyID)
@@ -45,7 +49,7 @@ func (opts CreateOpts) ToWebhookCreateMap() ([]map[string]interface{}, error) {
4549

4650
for _, o := range opts {
4751
if o.Name == "" {
48-
return nil, errors.New("Cannot create a Webhook without a name.")
52+
return nil, ErrNoName
4953
}
5054

5155
hook := make(map[string]interface{})
@@ -93,3 +97,59 @@ func Get(client *gophercloud.ServiceClient, groupID, policyID, webhookID string)
9397

9498
return result
9599
}
100+
101+
// UpdateOptsBuilder is the interface responsible for generating the map
102+
// structure for producing JSON for an Update operation.
103+
type UpdateOptsBuilder interface {
104+
ToWebhookUpdateMap() (map[string]interface{}, error)
105+
}
106+
107+
// UpdateOpts represents the options for updating an existing webhook.
108+
//
109+
// Update operations completely replace the configuration being updated. Empty
110+
// values in the update are accepted and overwrite previously specified
111+
// parameters.
112+
type UpdateOpts struct {
113+
// Name of the webhook.
114+
Name string `mapstructure:"name" json:"name"`
115+
116+
// Metadata associated with the webhook.
117+
Metadata map[string]string `mapstructure:"metadata" json:"metadata"`
118+
}
119+
120+
// ToWebhookUpdateMap converts an UpdateOpts struct into a map for use as the
121+
// request body in an Update request.
122+
func (opts UpdateOpts) ToWebhookUpdateMap() (map[string]interface{}, error) {
123+
if opts.Name == "" {
124+
return nil, ErrNoName
125+
}
126+
127+
hook := make(map[string]interface{})
128+
129+
hook["name"] = opts.Name
130+
131+
if opts.Metadata != nil {
132+
hook["metadata"] = opts.Metadata
133+
}
134+
135+
return hook, nil
136+
}
137+
138+
// Update requests the configuration of the given webhook be updated.
139+
func Update(client *gophercloud.ServiceClient, groupID, policyID, webhookID string, opts UpdateOptsBuilder) UpdateResult {
140+
var result UpdateResult
141+
142+
url := updateURL(client, groupID, policyID, webhookID)
143+
reqBody, err := opts.ToWebhookUpdateMap()
144+
145+
if err != nil {
146+
result.Err = err
147+
return result
148+
}
149+
150+
_, result.Err = client.Put(url, reqBody, nil, &gophercloud.RequestOpts{
151+
OkCodes: []int{204},
152+
})
153+
154+
return result
155+
}

rackspace/autoscale/v1/webhooks/requests_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,21 @@ func TestGet(t *testing.T) {
8686
th.AssertNoErr(t, err)
8787
th.CheckDeepEquals(t, FirstWebhook, *webhook)
8888
}
89+
90+
func TestUpdate(t *testing.T) {
91+
th.SetupHTTP()
92+
defer th.TeardownHTTP()
93+
HandleWebhookUpdateSuccessfully(t)
94+
95+
client := client.ServiceClient()
96+
opts := UpdateOpts{
97+
Name: "updated hook",
98+
Metadata: map[string]string{
99+
"new-key": "some data",
100+
},
101+
}
102+
103+
err := Update(client, groupID, policyID, firstID, opts).ExtractErr()
104+
105+
th.AssertNoErr(t, err)
106+
}

rackspace/autoscale/v1/webhooks/results.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ type CreateResult struct {
3434
pagination.SinglePageBase
3535
}
3636

37-
// GetResult temporarily contains the response from a Get call.
38-
type GetResult struct {
39-
webhookResult
40-
}
41-
4237
// ExtractWebhooks extracts a slice of Webhooks from a CreateResult.
4338
func (res CreateResult) ExtractWebhooks() ([]Webhook, error) {
4439
if res.Err != nil {
@@ -48,6 +43,16 @@ func (res CreateResult) ExtractWebhooks() ([]Webhook, error) {
4843
return commonExtractWebhooks(res.Body)
4944
}
5045

46+
// GetResult temporarily contains the response from a Get call.
47+
type GetResult struct {
48+
webhookResult
49+
}
50+
51+
// UpdateResult represents the result of an update operation.
52+
type UpdateResult struct {
53+
gophercloud.ErrResult
54+
}
55+
5156
// Webhook represents a webhook associted with a scaling policy.
5257
type Webhook struct {
5358
// UUID for the webhook.

rackspace/autoscale/v1/webhooks/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ func createURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
1313
func getURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
1414
return c.ServiceURL("groups", groupID, "policies", policyID, "webhooks", webhookID)
1515
}
16+
17+
func updateURL(c *gophercloud.ServiceClient, groupID, policyID, webhookID string) string {
18+
return getURL(c, groupID, policyID, webhookID)
19+
}

0 commit comments

Comments
 (0)