Validate Rackspace Auto Scale policy adjustments
diff --git a/rackspace/autoscale/v1/policies/requests.go b/rackspace/autoscale/v1/policies/requests.go
index 8b56c2e..0ebdf5f 100644
--- a/rackspace/autoscale/v1/policies/requests.go
+++ b/rackspace/autoscale/v1/policies/requests.go
@@ -9,8 +9,9 @@
// Validation errors returned by create or update operations.
var (
- ErrNoName = errors.New("Policy name cannot by empty.")
- ErrNoArgs = errors.New("Args cannot be nil for schedule policies.")
+ ErrNoName = errors.New("Policy name cannot by empty.")
+ ErrNoArgs = errors.New("Args cannot be nil for schedule policies.")
+ ErrInvalidAdjustment = errors.New("Invalid adjustment type.")
)
// List returns all scaling policies for a group.
@@ -92,8 +93,9 @@
policy["type"] = o.Type
policy["cooldown"] = o.Cooldown
- // TODO: Function to validate and cast key + value?
- policy[string(o.Adjustment.Type)] = o.Adjustment.Value
+ if err := setAdjustment(o.Adjustment, policy); err != nil {
+ return nil, err
+ }
if o.Args != nil {
policy["args"] = o.Args
@@ -176,8 +178,9 @@
policy["type"] = opts.Type
policy["cooldown"] = opts.Cooldown
- // TODO: Function to validate and cast key + value?
- policy[string(opts.Adjustment.Type)] = opts.Adjustment.Value
+ if err := setAdjustment(opts.Adjustment, policy); err != nil {
+ return nil, err
+ }
if opts.Args != nil {
policy["args"] = opts.Args
@@ -228,3 +231,21 @@
return result
}
+
+// Validate and set an adjustment on the given request body.
+func setAdjustment(adjustment Adjustment, reqBody map[string]interface{}) error {
+ key := string(adjustment.Type)
+
+ switch adjustment.Type {
+ case ChangePercent:
+ reqBody[key] = adjustment.Value
+
+ case Change, DesiredCapacity:
+ reqBody[key] = int(adjustment.Value)
+
+ default:
+ return ErrInvalidAdjustment
+ }
+
+ return nil
+}