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

Commit eedc8d9

Browse files
committed
Add additional validation for autoscale polices
1 parent 2b73916 commit eedc8d9

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

rackspace/autoscale/v1/policies/requests.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
var (
1212
ErrNoName = errors.New("Policy name cannot by empty.")
1313
ErrNoArgs = errors.New("Args cannot be nil for schedule policies.")
14-
ErrInvalidAdjustment = errors.New("Invalid adjustment type.")
14+
ErrCooldownRange = errors.New("Cooldown is out of range (0, 86400).")
15+
ErrUnknownType = errors.New("Unknown policy type.")
16+
ErrUnknownAdjustment = errors.New("Unknown adjustment type.")
1517
)
1618

1719
// List returns all scaling policies for a group.
@@ -73,6 +75,14 @@ func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) {
7375
return nil, ErrNoArgs
7476
}
7577

78+
if ok := validateType(o.Type); !ok {
79+
return nil, ErrUnknownType
80+
}
81+
82+
if ok := validateCooldown(o.Cooldown); !ok {
83+
return nil, ErrCooldownRange
84+
}
85+
7686
policy := make(map[string]interface{})
7787

7888
policy["name"] = o.Name
@@ -166,6 +176,14 @@ func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
166176
return nil, ErrNoArgs
167177
}
168178

179+
if ok := validateType(opts.Type); !ok {
180+
return nil, ErrUnknownType
181+
}
182+
183+
if ok := validateCooldown(opts.Cooldown); !ok {
184+
return nil, ErrCooldownRange
185+
}
186+
169187
policy := make(map[string]interface{})
170188

171189
policy["name"] = opts.Name
@@ -240,8 +258,30 @@ func setAdjustment(t AdjustmentType, v float64, body map[string]interface{}) err
240258
body[key] = int(v)
241259

242260
default:
243-
return ErrInvalidAdjustment
261+
return ErrUnknownAdjustment
244262
}
245263

246264
return nil
247265
}
266+
267+
func validateType(t Type) (ok bool) {
268+
switch t {
269+
case Schedule, Webhook:
270+
ok = true
271+
return
272+
273+
default:
274+
ok = false
275+
return
276+
}
277+
}
278+
279+
func validateCooldown(cooldown int) (ok bool) {
280+
if cooldown < 0 || cooldown > 86400 {
281+
ok = false
282+
return
283+
}
284+
285+
ok = true
286+
return
287+
}

rackspace/autoscale/v1/policies/requests_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,28 @@ func TestExecute(t *testing.T) {
143143

144144
th.AssertNoErr(t, err)
145145
}
146+
147+
func TestValidateType(t *testing.T) {
148+
ok := validateType(Schedule)
149+
th.AssertEquals(t, true, ok)
150+
151+
ok = validateType(Webhook)
152+
th.AssertEquals(t, true, ok)
153+
154+
ok = validateType("BAD")
155+
th.AssertEquals(t, false, ok)
156+
}
157+
158+
func TestValidateCooldown(t *testing.T) {
159+
ok := validateCooldown(0)
160+
th.AssertEquals(t, true, ok)
161+
162+
ok = validateCooldown(86400)
163+
th.AssertEquals(t, true, ok)
164+
165+
ok = validateCooldown(-1)
166+
th.AssertEquals(t, false, ok)
167+
168+
ok = validateCooldown(172800)
169+
th.AssertEquals(t, false, ok)
170+
}

0 commit comments

Comments
 (0)