Embed adjustments in autoscale policy request bodies
diff --git a/rackspace/autoscale/v1/policies/requests.go b/rackspace/autoscale/v1/policies/requests.go
index 0ebdf5f..25747b8 100644
--- a/rackspace/autoscale/v1/policies/requests.go
+++ b/rackspace/autoscale/v1/policies/requests.go
@@ -31,16 +31,6 @@
ToPolicyCreateMap() ([]map[string]interface{}, error)
}
-// Adjustment represents the change in capacity associated with a policy.
-type Adjustment struct {
- // The type for this adjustment.
- Type AdjustmentType
-
- // The value of the adjustment. For adjustments of type Change or
- // DesiredCapacity, this will be converted to an integer.
- Value float64
-}
-
// AdjustmentType represents the way in which a policy will change a group.
type AdjustmentType string
@@ -66,8 +56,14 @@
// Cooldown [required] period in seconds.
Cooldown int
- // Adjustment [requried] type and value for the policy.
- Adjustment Adjustment
+ // AdjustmentType [requried] is the method used to change the capacity of
+ // the group, i.e. one of: Change, ChangePercent, or DesiredCapacity.
+ AdjustmentType AdjustmentType
+
+ // AdjustmentValue [required] is the numeric value of the adjustment. For
+ // adjustments of type Change or DesiredCapacity, this will be converted to
+ // an integer.
+ AdjustmentValue float64
// Additional configuration options for some types of policy.
Args map[string]interface{}
@@ -93,7 +89,9 @@
policy["type"] = o.Type
policy["cooldown"] = o.Cooldown
- if err := setAdjustment(o.Adjustment, policy); err != nil {
+ err := setAdjustment(o.AdjustmentType, o.AdjustmentValue, policy)
+
+ if err != nil {
return nil, err
}
@@ -154,8 +152,14 @@
// it will default to zero, and the policy will be configured as such.
Cooldown int
- // Adjustment [requried] type and value for the policy.
- Adjustment Adjustment
+ // AdjustmentType [requried] is the method used to change the capacity of
+ // the group, i.e. one of: Change, ChangePercent, or DesiredCapacity.
+ AdjustmentType AdjustmentType
+
+ // AdjustmentValue [required] is the numeric value of the adjustment. For
+ // adjustments of type Change or DesiredCapacity, this will be converted to
+ // an integer.
+ AdjustmentValue float64
// Additional configuration options for some types of policy.
Args map[string]interface{}
@@ -178,7 +182,9 @@
policy["type"] = opts.Type
policy["cooldown"] = opts.Cooldown
- if err := setAdjustment(opts.Adjustment, policy); err != nil {
+ err := setAdjustment(opts.AdjustmentType, opts.AdjustmentValue, policy)
+
+ if err != nil {
return nil, err
}
@@ -233,15 +239,15 @@
}
// Validate and set an adjustment on the given request body.
-func setAdjustment(adjustment Adjustment, reqBody map[string]interface{}) error {
- key := string(adjustment.Type)
+func setAdjustment(t AdjustmentType, v float64, body map[string]interface{}) error {
+ key := string(t)
- switch adjustment.Type {
+ switch t {
case ChangePercent:
- reqBody[key] = adjustment.Value
+ body[key] = v
case Change, DesiredCapacity:
- reqBody[key] = int(adjustment.Value)
+ body[key] = int(v)
default:
return ErrInvalidAdjustment
diff --git a/rackspace/autoscale/v1/policies/requests_test.go b/rackspace/autoscale/v1/policies/requests_test.go
index 61b94e9..3af9435 100644
--- a/rackspace/autoscale/v1/policies/requests_test.go
+++ b/rackspace/autoscale/v1/policies/requests_test.go
@@ -56,32 +56,26 @@
client := client.ServiceClient()
opts := CreateOpts{
{
- Name: "webhook policy",
- Type: Webhook,
- Cooldown: 300,
- Adjustment: Adjustment{
- Type: ChangePercent,
- Value: 3.3,
- },
+ Name: "webhook policy",
+ Type: Webhook,
+ Cooldown: 300,
+ AdjustmentType: ChangePercent,
+ AdjustmentValue: 3.3,
},
{
- Name: "one time",
- Type: Schedule,
- Adjustment: Adjustment{
- Type: Change,
- Value: -1,
- },
+ Name: "one time",
+ Type: Schedule,
+ AdjustmentType: Change,
+ AdjustmentValue: -1,
Args: map[string]interface{}{
"at": "2020-04-01T23:00:00.000Z",
},
},
{
- Name: "sunday afternoon",
- Type: Schedule,
- Adjustment: Adjustment{
- Type: DesiredCapacity,
- Value: 2,
- },
+ Name: "sunday afternoon",
+ Type: Schedule,
+ AdjustmentType: DesiredCapacity,
+ AdjustmentValue: 2,
Args: map[string]interface{}{
"cron": "59 15 * * 0",
},
@@ -116,13 +110,11 @@
client := client.ServiceClient()
opts := UpdateOpts{
- Name: "updated webhook policy",
- Type: Webhook,
- Cooldown: 600,
- Adjustment: Adjustment{
- Type: ChangePercent,
- Value: 6.6,
- },
+ Name: "updated webhook policy",
+ Type: Webhook,
+ Cooldown: 600,
+ AdjustmentType: ChangePercent,
+ AdjustmentValue: 6.6,
}
err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()