Rackspace Auto Scale: Add policies Create()
diff --git a/rackspace/autoscale/v1/policies/fixtures.go b/rackspace/autoscale/v1/policies/fixtures.go
index bd4a609..442a891 100644
--- a/rackspace/autoscale/v1/policies/fixtures.go
+++ b/rackspace/autoscale/v1/policies/fixtures.go
@@ -24,7 +24,7 @@
"rel": "self"
}
],
- "changePercent": 3,
+ "changePercent": 3.3,
"cooldown": 300,
"type": "webhook",
"id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
@@ -59,12 +59,45 @@
},
"type": "schedule",
"id": "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
- "change": 2
+ "desiredCapacity": 2
}
]
}
`
+// PolicyCreateBody contains the canned body of a policies.Create response.
+const PolicyCreateBody = PolicyListBody
+
+// PolicyCreateRequest contains the canned body of a policies.Create request.
+const PolicyCreateRequest = `
+[
+ {
+ "name": "webhook policy",
+ "changePercent": 3.3,
+ "cooldown": 300,
+ "type": "webhook"
+ },
+ {
+ "cooldown": 0,
+ "name": "one time",
+ "args": {
+ "at": "2020-04-01T23:00:00.000Z"
+ },
+ "type": "schedule",
+ "change": -1
+ },
+ {
+ "cooldown": 0,
+ "name": "sunday afternoon",
+ "args": {
+ "cron": "59 15 * * 0"
+ },
+ "type": "schedule",
+ "desiredCapacity": 2
+ }
+]
+`
+
var (
// WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
WebhookPolicy = Policy{
@@ -72,7 +105,7 @@
Name: "webhook policy",
Type: Webhook,
Cooldown: 300,
- ChangePercent: 3,
+ ChangePercent: 3.3,
}
// OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
@@ -80,7 +113,7 @@
ID: "c175c31e-65f9-41de-8b15-918420d3253e",
Name: "one time",
Type: Schedule,
- Change: -1,
+ Change: float64(-1),
Args: map[string]interface{}{
"at": "2020-04-01T23:00:00.000Z",
},
@@ -88,10 +121,10 @@
// SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
SundayAfternoonPolicy = Policy{
- ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
- Name: "sunday afternoon",
- Type: Schedule,
- Change: 2,
+ ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
+ Name: "sunday afternoon",
+ Type: Schedule,
+ DesiredCapacity: float64(2),
Args: map[string]interface{}{
"cron": "59 15 * * 0",
},
@@ -111,3 +144,22 @@
fmt.Fprintf(w, PolicyListBody)
})
}
+
+// HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request.
+func HandlePolicyCreateSuccessfully(t *testing.T) {
+ path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies"
+
+ th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+
+ th.TestJSONRequest(t, r, PolicyCreateRequest)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+
+ fmt.Fprintf(w, PolicyCreateBody)
+ })
+}
diff --git a/rackspace/autoscale/v1/policies/requests.go b/rackspace/autoscale/v1/policies/requests.go
index d948d8f..69ad7cd 100644
--- a/rackspace/autoscale/v1/policies/requests.go
+++ b/rackspace/autoscale/v1/policies/requests.go
@@ -1,10 +1,18 @@
package policies
import (
+ "errors"
+
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
+// 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.")
+)
+
// List returns all scaling policies for a group.
func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager {
url := listURL(client, groupID)
@@ -15,3 +23,100 @@
return pagination.NewPager(client, url, createPageFn)
}
+
+// CreateOptsBuilder is the interface responsible for generating the map that
+// will be marshalled to JSON for a Create operation.
+type CreateOptsBuilder interface {
+ 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
+
+// Valid types of adjustments for a policy.
+const (
+ Change AdjustmentType = "change"
+ ChangePercent AdjustmentType = "changePercent"
+ DesiredCapacity AdjustmentType = "desiredCapacity"
+)
+
+// CreateOpts is a slice of CreateOpt structs that allow the user to create
+// multiple policies in a single operation.
+type CreateOpts []CreateOpt
+
+// CreateOpt represents the options to create a policy.
+type CreateOpt struct {
+ // Name [required] is a name for the policy.
+ Name string
+
+ // Type [required] of policy, i.e. either "webhook" or "schedule".
+ Type Type
+
+ // Cooldown [required] period in seconds.
+ Cooldown int
+
+ // Adjustment [requried] type and value for the policy.
+ Adjustment Adjustment
+
+ // Additional configuration options for some types of policy.
+ Args map[string]interface{}
+}
+
+// ToPolicyCreateMap converts a slice of CreateOpt structs into a map for use
+// in the request body of a Create operation.
+func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) {
+ var policies []map[string]interface{}
+
+ for _, o := range opts {
+ if o.Name == "" {
+ return nil, ErrNoName
+ }
+
+ if o.Type == Schedule && o.Args == nil {
+ return nil, ErrNoArgs
+ }
+
+ policy := make(map[string]interface{})
+
+ policy["name"] = o.Name
+ 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 o.Args != nil {
+ policy["args"] = o.Args
+ }
+
+ policies = append(policies, policy)
+ }
+
+ return policies, nil
+}
+
+// Create requests a new policy be created and associated with the given group.
+func Create(client *gophercloud.ServiceClient, groupID string, opts CreateOptsBuilder) CreateResult {
+ var res CreateResult
+
+ reqBody, err := opts.ToPolicyCreateMap()
+
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = client.Post(createURL(client, groupID), reqBody, &res.Body, nil)
+
+ return res
+}
diff --git a/rackspace/autoscale/v1/policies/requests_test.go b/rackspace/autoscale/v1/policies/requests_test.go
index ef2a285..bf49816 100644
--- a/rackspace/autoscale/v1/policies/requests_test.go
+++ b/rackspace/autoscale/v1/policies/requests_test.go
@@ -8,6 +8,10 @@
"github.com/rackspace/gophercloud/testhelper/client"
)
+const (
+ groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
+)
+
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
@@ -42,3 +46,51 @@
t.Errorf("Expected 1 page, saw %d", pages)
}
}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePolicyCreateSuccessfully(t)
+
+ client := client.ServiceClient()
+ opts := CreateOpts{
+ {
+ Name: "webhook policy",
+ Type: Webhook,
+ Cooldown: 300,
+ Adjustment: Adjustment{
+ Type: ChangePercent,
+ Value: 3.3,
+ },
+ },
+ {
+ Name: "one time",
+ Type: Schedule,
+ Adjustment: Adjustment{
+ Type: Change,
+ Value: -1,
+ },
+ Args: map[string]interface{}{
+ "at": "2020-04-01T23:00:00.000Z",
+ },
+ },
+ {
+ Name: "sunday afternoon",
+ Type: Schedule,
+ Adjustment: Adjustment{
+ Type: DesiredCapacity,
+ Value: 2,
+ },
+ Args: map[string]interface{}{
+ "cron": "59 15 * * 0",
+ },
+ },
+ }
+
+ policies, err := Create(client, groupID, opts).Extract()
+
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, WebhookPolicy, policies[0])
+ th.CheckDeepEquals(t, OneTimePolicy, policies[1])
+ th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
+}
diff --git a/rackspace/autoscale/v1/policies/results.go b/rackspace/autoscale/v1/policies/results.go
index 4a6c2ba..e50ea78 100644
--- a/rackspace/autoscale/v1/policies/results.go
+++ b/rackspace/autoscale/v1/policies/results.go
@@ -11,6 +11,22 @@
gophercloud.Result
}
+// CreateResult represents the result of a create operation.
+type CreateResult struct {
+ policyResult
+}
+
+// Extract extracts a slice of Policies from a CreateResult. Multiple policies
+// can be created in a single operation, so the result of a create is always a
+// list of policies.
+func (res CreateResult) Extract() ([]Policy, error) {
+ if res.Err != nil {
+ return nil, res.Err
+ }
+
+ return commonExtractPolicies(res.Body)
+}
+
// Policy represents a scaling policy.
type Policy struct {
// UUID for the policy.
@@ -69,13 +85,15 @@
// ExtractPolicies interprets the results of a single page from a List() call,
// producing a slice of Policies.
func ExtractPolicies(page pagination.Page) ([]Policy, error) {
- casted := page.(PolicyPage).Body
+ return commonExtractPolicies(page.(PolicyPage).Body)
+}
+func commonExtractPolicies(body interface{}) ([]Policy, error) {
var response struct {
Policies []Policy `mapstructure:"policies"`
}
- err := mapstructure.Decode(casted, &response)
+ err := mapstructure.Decode(body, &response)
if err != nil {
return nil, err
diff --git a/rackspace/autoscale/v1/policies/urls.go b/rackspace/autoscale/v1/policies/urls.go
index adea6cc..e837c7f 100644
--- a/rackspace/autoscale/v1/policies/urls.go
+++ b/rackspace/autoscale/v1/policies/urls.go
@@ -5,3 +5,7 @@
func listURL(c *gophercloud.ServiceClient, groupID string) string {
return c.ServiceURL("groups", groupID, "policies")
}
+
+func createURL(c *gophercloud.ServiceClient, groupID string) string {
+ return c.ServiceURL("groups", groupID, "policies")
+}