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

Commit ac037d5

Browse files
committed
Rackspace Auto Scale: Add policies Update()
1 parent 55523e5 commit ac037d5

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

rackspace/autoscale/v1/policies/fixtures.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ const PolicyGetBody = `
117117
}
118118
`
119119

120+
// PolicyUpdateRequest contains the canned body of a policies.Update request.
121+
const PolicyUpdateRequest = `
122+
{
123+
"name": "updated webhook policy",
124+
"type": "webhook",
125+
"cooldown": 600,
126+
"changePercent": 6.6
127+
}
128+
`
129+
120130
var (
121131
// WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
122132
WebhookPolicy = Policy{
@@ -199,3 +209,20 @@ func HandlePolicyGetSuccessfully(t *testing.T) {
199209
fmt.Fprintf(w, PolicyGetBody)
200210
})
201211
}
212+
213+
// HandlePolicyUpdateSuccessfully sets up the test server to respond to a policies Update request.
214+
func HandlePolicyUpdateSuccessfully(t *testing.T) {
215+
groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
216+
policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
217+
218+
path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
219+
220+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
221+
th.TestMethod(t, r, "PUT")
222+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
223+
224+
th.TestJSONRequest(t, r, PolicyUpdateRequest)
225+
226+
w.WriteHeader(http.StatusNoContent)
227+
})
228+
}

rackspace/autoscale/v1/policies/requests.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,78 @@ func Get(client *gophercloud.ServiceClient, groupID, policyID string) GetResult
129129

130130
return result
131131
}
132+
133+
// UpdateOptsBuilder is the interface responsible for generating the map
134+
// structure for producing JSON for an Update operation.
135+
type UpdateOptsBuilder interface {
136+
ToPolicyUpdateMap() (map[string]interface{}, error)
137+
}
138+
139+
// UpdateOpts represents the options for updating an existing policy.
140+
//
141+
// Update operations completely replace the configuration being updated. Empty
142+
// values in the update are accepted and overwrite previously specified
143+
// parameters.
144+
type UpdateOpts struct {
145+
// Name [required] is a name for the policy.
146+
Name string
147+
148+
// Type [required] of policy, i.e. either "webhook" or "schedule".
149+
Type Type
150+
151+
// Cooldown [required] period in seconds. If you don't specify a cooldown,
152+
// it will default to zero, and the policy will be configured as such.
153+
Cooldown int
154+
155+
// Adjustment [requried] type and value for the policy.
156+
Adjustment Adjustment
157+
158+
// Additional configuration options for some types of policy.
159+
Args map[string]interface{}
160+
}
161+
162+
// ToPolicyUpdateMap converts an UpdateOpts struct into a map for use as the
163+
// request body in an Update request.
164+
func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
165+
if opts.Name == "" {
166+
return nil, ErrNoName
167+
}
168+
169+
if opts.Type == Schedule && opts.Args == nil {
170+
return nil, ErrNoArgs
171+
}
172+
173+
policy := make(map[string]interface{})
174+
175+
policy["name"] = opts.Name
176+
policy["type"] = opts.Type
177+
policy["cooldown"] = opts.Cooldown
178+
179+
// TODO: Function to validate and cast key + value?
180+
policy[string(opts.Adjustment.Type)] = opts.Adjustment.Value
181+
182+
if opts.Args != nil {
183+
policy["args"] = opts.Args
184+
}
185+
186+
return policy, nil
187+
}
188+
189+
// Update requests the configuration of the given policy be updated.
190+
func Update(client *gophercloud.ServiceClient, groupID, policyID string, opts UpdateOptsBuilder) UpdateResult {
191+
var result UpdateResult
192+
193+
url := updateURL(client, groupID, policyID)
194+
reqBody, err := opts.ToPolicyUpdateMap()
195+
196+
if err != nil {
197+
result.Err = err
198+
return result
199+
}
200+
201+
_, result.Err = client.Put(url, reqBody, nil, &gophercloud.RequestOpts{
202+
OkCodes: []int{204},
203+
})
204+
205+
return result
206+
}

rackspace/autoscale/v1/policies/requests_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,24 @@ func TestGet(t *testing.T) {
108108
th.AssertNoErr(t, err)
109109
th.CheckDeepEquals(t, WebhookPolicy, *policy)
110110
}
111+
112+
func TestUpdate(t *testing.T) {
113+
th.SetupHTTP()
114+
defer th.TeardownHTTP()
115+
HandlePolicyUpdateSuccessfully(t)
116+
117+
client := client.ServiceClient()
118+
opts := UpdateOpts{
119+
Name: "updated webhook policy",
120+
Type: Webhook,
121+
Cooldown: 600,
122+
Adjustment: Adjustment{
123+
Type: ChangePercent,
124+
Value: 6.6,
125+
},
126+
}
127+
128+
err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()
129+
130+
th.AssertNoErr(t, err)
131+
}

rackspace/autoscale/v1/policies/results.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ type GetResult struct {
4747
policyResult
4848
}
4949

50+
// UpdateResult represents the result of an update operation.
51+
type UpdateResult struct {
52+
gophercloud.ErrResult
53+
}
54+
5055
// Policy represents a scaling policy.
5156
type Policy struct {
5257
// UUID for the policy.

rackspace/autoscale/v1/policies/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 string) string {
1313
func getURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
1414
return c.ServiceURL("groups", groupID, "policies", policyID)
1515
}
16+
17+
func updateURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
18+
return getURL(c, groupID, policyID)
19+
}

0 commit comments

Comments
 (0)