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

Commit b35ef6d

Browse files
committed
Embed adjustments in autoscale policy request bodies
1 parent 4d1a9a6 commit b35ef6d

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

rackspace/autoscale/v1/policies/requests.go

+27-21
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ type CreateOptsBuilder interface {
3131
ToPolicyCreateMap() ([]map[string]interface{}, error)
3232
}
3333

34-
// Adjustment represents the change in capacity associated with a policy.
35-
type Adjustment struct {
36-
// The type for this adjustment.
37-
Type AdjustmentType
38-
39-
// The value of the adjustment. For adjustments of type Change or
40-
// DesiredCapacity, this will be converted to an integer.
41-
Value float64
42-
}
43-
4434
// AdjustmentType represents the way in which a policy will change a group.
4535
type AdjustmentType string
4636

@@ -66,8 +56,14 @@ type CreateOpt struct {
6656
// Cooldown [required] period in seconds.
6757
Cooldown int
6858

69-
// Adjustment [requried] type and value for the policy.
70-
Adjustment Adjustment
59+
// AdjustmentType [requried] is the method used to change the capacity of
60+
// the group, i.e. one of: Change, ChangePercent, or DesiredCapacity.
61+
AdjustmentType AdjustmentType
62+
63+
// AdjustmentValue [required] is the numeric value of the adjustment. For
64+
// adjustments of type Change or DesiredCapacity, this will be converted to
65+
// an integer.
66+
AdjustmentValue float64
7167

7268
// Additional configuration options for some types of policy.
7369
Args map[string]interface{}
@@ -93,7 +89,9 @@ func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) {
9389
policy["type"] = o.Type
9490
policy["cooldown"] = o.Cooldown
9591

96-
if err := setAdjustment(o.Adjustment, policy); err != nil {
92+
err := setAdjustment(o.AdjustmentType, o.AdjustmentValue, policy)
93+
94+
if err != nil {
9795
return nil, err
9896
}
9997

@@ -154,8 +152,14 @@ type UpdateOpts struct {
154152
// it will default to zero, and the policy will be configured as such.
155153
Cooldown int
156154

157-
// Adjustment [requried] type and value for the policy.
158-
Adjustment Adjustment
155+
// AdjustmentType [requried] is the method used to change the capacity of
156+
// the group, i.e. one of: Change, ChangePercent, or DesiredCapacity.
157+
AdjustmentType AdjustmentType
158+
159+
// AdjustmentValue [required] is the numeric value of the adjustment. For
160+
// adjustments of type Change or DesiredCapacity, this will be converted to
161+
// an integer.
162+
AdjustmentValue float64
159163

160164
// Additional configuration options for some types of policy.
161165
Args map[string]interface{}
@@ -178,7 +182,9 @@ func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
178182
policy["type"] = opts.Type
179183
policy["cooldown"] = opts.Cooldown
180184

181-
if err := setAdjustment(opts.Adjustment, policy); err != nil {
185+
err := setAdjustment(opts.AdjustmentType, opts.AdjustmentValue, policy)
186+
187+
if err != nil {
182188
return nil, err
183189
}
184190

@@ -233,15 +239,15 @@ func Execute(client *gophercloud.ServiceClient, groupID, policyID string) Execut
233239
}
234240

235241
// 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)
242+
func setAdjustment(t AdjustmentType, v float64, body map[string]interface{}) error {
243+
key := string(t)
238244

239-
switch adjustment.Type {
245+
switch t {
240246
case ChangePercent:
241-
reqBody[key] = adjustment.Value
247+
body[key] = v
242248

243249
case Change, DesiredCapacity:
244-
reqBody[key] = int(adjustment.Value)
250+
body[key] = int(v)
245251

246252
default:
247253
return ErrInvalidAdjustment

rackspace/autoscale/v1/policies/requests_test.go

+18-26
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,26 @@ func TestCreate(t *testing.T) {
5656
client := client.ServiceClient()
5757
opts := CreateOpts{
5858
{
59-
Name: "webhook policy",
60-
Type: Webhook,
61-
Cooldown: 300,
62-
Adjustment: Adjustment{
63-
Type: ChangePercent,
64-
Value: 3.3,
65-
},
59+
Name: "webhook policy",
60+
Type: Webhook,
61+
Cooldown: 300,
62+
AdjustmentType: ChangePercent,
63+
AdjustmentValue: 3.3,
6664
},
6765
{
68-
Name: "one time",
69-
Type: Schedule,
70-
Adjustment: Adjustment{
71-
Type: Change,
72-
Value: -1,
73-
},
66+
Name: "one time",
67+
Type: Schedule,
68+
AdjustmentType: Change,
69+
AdjustmentValue: -1,
7470
Args: map[string]interface{}{
7571
"at": "2020-04-01T23:00:00.000Z",
7672
},
7773
},
7874
{
79-
Name: "sunday afternoon",
80-
Type: Schedule,
81-
Adjustment: Adjustment{
82-
Type: DesiredCapacity,
83-
Value: 2,
84-
},
75+
Name: "sunday afternoon",
76+
Type: Schedule,
77+
AdjustmentType: DesiredCapacity,
78+
AdjustmentValue: 2,
8579
Args: map[string]interface{}{
8680
"cron": "59 15 * * 0",
8781
},
@@ -116,13 +110,11 @@ func TestUpdate(t *testing.T) {
116110

117111
client := client.ServiceClient()
118112
opts := UpdateOpts{
119-
Name: "updated webhook policy",
120-
Type: Webhook,
121-
Cooldown: 600,
122-
Adjustment: Adjustment{
123-
Type: ChangePercent,
124-
Value: 6.6,
125-
},
113+
Name: "updated webhook policy",
114+
Type: Webhook,
115+
Cooldown: 600,
116+
AdjustmentType: ChangePercent,
117+
AdjustmentValue: 6.6,
126118
}
127119

128120
err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()

0 commit comments

Comments
 (0)