|
9 | 9 |
|
10 | 10 | // Validation errors returned by create or update operations.
|
11 | 11 | 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.") |
14 | 15 | )
|
15 | 16 |
|
16 | 17 | // List returns all scaling policies for a group.
|
@@ -92,8 +93,9 @@ func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) {
|
92 | 93 | policy["type"] = o.Type
|
93 | 94 | policy["cooldown"] = o.Cooldown
|
94 | 95 |
|
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 | + } |
97 | 99 |
|
98 | 100 | if o.Args != nil {
|
99 | 101 | policy["args"] = o.Args
|
@@ -176,8 +178,9 @@ func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
|
176 | 178 | policy["type"] = opts.Type
|
177 | 179 | policy["cooldown"] = opts.Cooldown
|
178 | 180 |
|
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 | + } |
181 | 184 |
|
182 | 185 | if opts.Args != nil {
|
183 | 186 | policy["args"] = opts.Args
|
@@ -228,3 +231,21 @@ func Execute(client *gophercloud.ServiceClient, groupID, policyID string) Execut
|
228 | 231 |
|
229 | 232 | return result
|
230 | 233 | }
|
| 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