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

Commit 4d1a9a6

Browse files
committed
Validate Rackspace Auto Scale policy adjustments
1 parent 40dd634 commit 4d1a9a6

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

rackspace/autoscale/v1/policies/requests.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99

1010
// Validation errors returned by create or update operations.
1111
var (
12-
ErrNoName = errors.New("Policy name cannot by empty.")
13-
ErrNoArgs = errors.New("Args cannot be nil for schedule policies.")
12+
ErrNoName = errors.New("Policy name cannot by empty.")
13+
ErrNoArgs = errors.New("Args cannot be nil for schedule policies.")
14+
ErrInvalidAdjustment = errors.New("Invalid adjustment type.")
1415
)
1516

1617
// List returns all scaling policies for a group.
@@ -92,8 +93,9 @@ func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) {
9293
policy["type"] = o.Type
9394
policy["cooldown"] = o.Cooldown
9495

95-
// TODO: Function to validate and cast key + value?
96-
policy[string(o.Adjustment.Type)] = o.Adjustment.Value
96+
if err := setAdjustment(o.Adjustment, policy); err != nil {
97+
return nil, err
98+
}
9799

98100
if o.Args != nil {
99101
policy["args"] = o.Args
@@ -176,8 +178,9 @@ func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
176178
policy["type"] = opts.Type
177179
policy["cooldown"] = opts.Cooldown
178180

179-
// TODO: Function to validate and cast key + value?
180-
policy[string(opts.Adjustment.Type)] = opts.Adjustment.Value
181+
if err := setAdjustment(opts.Adjustment, policy); err != nil {
182+
return nil, err
183+
}
181184

182185
if opts.Args != nil {
183186
policy["args"] = opts.Args
@@ -228,3 +231,21 @@ func Execute(client *gophercloud.ServiceClient, groupID, policyID string) Execut
228231

229232
return result
230233
}
234+
235+
// Validate and set an adjustment on the given request body.
236+
func setAdjustment(adjustment Adjustment, reqBody map[string]interface{}) error {
237+
key := string(adjustment.Type)
238+
239+
switch adjustment.Type {
240+
case ChangePercent:
241+
reqBody[key] = adjustment.Value
242+
243+
case Change, DesiredCapacity:
244+
reqBody[key] = int(adjustment.Value)
245+
246+
default:
247+
return ErrInvalidAdjustment
248+
}
249+
250+
return nil
251+
}

0 commit comments

Comments
 (0)