Rackspace Auto Scale: Add policies List()
diff --git a/rackspace/autoscale/v1/policies/fixtures.go b/rackspace/autoscale/v1/policies/fixtures.go
new file mode 100644
index 0000000..bd4a609
--- /dev/null
+++ b/rackspace/autoscale/v1/policies/fixtures.go
@@ -0,0 +1,113 @@
+// +build fixtures
+
+package policies
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// PolicyListBody contains the canned body of a policies.List response.
+const PolicyListBody = `
+{
+ "policies_links": [],
+ "policies": [
+ {
+ "name": "webhook policy",
+ "links": [
+ {
+ "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/",
+ "rel": "self"
+ }
+ ],
+ "changePercent": 3,
+ "cooldown": 300,
+ "type": "webhook",
+ "id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
+ },
+ {
+ "cooldown": 0,
+ "name": "one time",
+ "links": [
+ {
+ "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/c175c31e-65f9-41de-8b15-918420d3253e/",
+ "rel": "self"
+ }
+ ],
+ "args": {
+ "at": "2020-04-01T23:00:00.000Z"
+ },
+ "type": "schedule",
+ "id": "c175c31e-65f9-41de-8b15-918420d3253e",
+ "change": -1
+ },
+ {
+ "cooldown": 0,
+ "name": "sunday afternoon",
+ "links": [
+ {
+ "href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/e785e3e7-af9e-4f3c-99ae-b80a532e1663/",
+ "rel": "self"
+ }
+ ],
+ "args": {
+ "cron": "59 15 * * 0"
+ },
+ "type": "schedule",
+ "id": "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
+ "change": 2
+ }
+ ]
+}
+`
+
+var (
+ // WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
+ WebhookPolicy = Policy{
+ ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
+ Name: "webhook policy",
+ Type: Webhook,
+ Cooldown: 300,
+ ChangePercent: 3,
+ }
+
+ // OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
+ OneTimePolicy = Policy{
+ ID: "c175c31e-65f9-41de-8b15-918420d3253e",
+ Name: "one time",
+ Type: Schedule,
+ Change: -1,
+ Args: map[string]interface{}{
+ "at": "2020-04-01T23:00:00.000Z",
+ },
+ }
+
+ // 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,
+ Args: map[string]interface{}{
+ "cron": "59 15 * * 0",
+ },
+ }
+)
+
+// HandlePolicyListSuccessfully sets up the test server to respond to a policies List request.
+func HandlePolicyListSuccessfully(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, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+
+ fmt.Fprintf(w, PolicyListBody)
+ })
+}
diff --git a/rackspace/autoscale/v1/policies/requests.go b/rackspace/autoscale/v1/policies/requests.go
new file mode 100644
index 0000000..d948d8f
--- /dev/null
+++ b/rackspace/autoscale/v1/policies/requests.go
@@ -0,0 +1,17 @@
+package policies
+
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// List returns all scaling policies for a group.
+func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager {
+ url := listURL(client, groupID)
+
+ createPageFn := func(r pagination.PageResult) pagination.Page {
+ return PolicyPage{pagination.SinglePageBase(r)}
+ }
+
+ return pagination.NewPager(client, url, createPageFn)
+}
diff --git a/rackspace/autoscale/v1/policies/requests_test.go b/rackspace/autoscale/v1/policies/requests_test.go
new file mode 100644
index 0000000..ef2a285
--- /dev/null
+++ b/rackspace/autoscale/v1/policies/requests_test.go
@@ -0,0 +1,44 @@
+package policies
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePolicyListSuccessfully(t)
+
+ pages := 0
+ pager := List(client.ServiceClient(), "10eb3219-1b12-4b34-b1e4-e10ee4f24c65")
+
+ err := pager.EachPage(func(page pagination.Page) (bool, error) {
+ pages++
+
+ policies, err := ExtractPolicies(page)
+
+ if err != nil {
+ return false, err
+ }
+
+ if len(policies) != 3 {
+ t.Fatalf("Expected 3 policies, got %d", len(policies))
+ }
+
+ th.CheckDeepEquals(t, WebhookPolicy, policies[0])
+ th.CheckDeepEquals(t, OneTimePolicy, policies[1])
+ th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+
+ if pages != 1 {
+ t.Errorf("Expected 1 page, saw %d", pages)
+ }
+}
diff --git a/rackspace/autoscale/v1/policies/results.go b/rackspace/autoscale/v1/policies/results.go
new file mode 100644
index 0000000..4a6c2ba
--- /dev/null
+++ b/rackspace/autoscale/v1/policies/results.go
@@ -0,0 +1,85 @@
+package policies
+
+import (
+ "github.com/mitchellh/mapstructure"
+
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+type policyResult struct {
+ gophercloud.Result
+}
+
+// Policy represents a scaling policy.
+type Policy struct {
+ // UUID for the policy.
+ ID string `mapstructure:"id" json:"id"`
+
+ // Name of the policy.
+ Name string `mapstructure:"name" json:"name"`
+
+ // Type of scaling policy.
+ Type Type `mapstructure:"type" json:"type"`
+
+ // Cooldown period, in seconds.
+ Cooldown int `mapstructure:"cooldown" json:"cooldown"`
+
+ // Number of servers added or, if negative, removed.
+ Change interface{} `mapstructure:"change" json:"change"`
+
+ // Percent change to make in the number of servers.
+ ChangePercent interface{} `mapstructure:"changePercent" json:"changePercent"`
+
+ // Desired capacity of the of the associated group.
+ DesiredCapacity interface{} `mapstructure:"desiredCapacity" json:"desiredCapacity"`
+
+ // Additional configuration options for some types of policy.
+ Args map[string]interface{} `mapstructure:"args" json:"args"`
+}
+
+// Type represents a type of scaling policy.
+type Type string
+
+const (
+ // Schedule policies run at given times.
+ Schedule Type = "schedule"
+
+ // Webhook policies are triggered by HTTP requests.
+ Webhook Type = "webhook"
+)
+
+// PolicyPage is the page returned by a pager when traversing over a collection
+// of scaling policies.
+type PolicyPage struct {
+ pagination.SinglePageBase
+}
+
+// IsEmpty returns true if a page contains no Policy results.
+func (page PolicyPage) IsEmpty() (bool, error) {
+ policies, err := ExtractPolicies(page)
+
+ if err != nil {
+ return true, err
+ }
+
+ return len(policies) == 0, nil
+}
+
+// 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
+
+ var response struct {
+ Policies []Policy `mapstructure:"policies"`
+ }
+
+ err := mapstructure.Decode(casted, &response)
+
+ if err != nil {
+ return nil, err
+ }
+
+ return response.Policies, err
+}
diff --git a/rackspace/autoscale/v1/policies/urls.go b/rackspace/autoscale/v1/policies/urls.go
new file mode 100644
index 0000000..adea6cc
--- /dev/null
+++ b/rackspace/autoscale/v1/policies/urls.go
@@ -0,0 +1,7 @@
+package policies
+
+import "github.com/rackspace/gophercloud"
+
+func listURL(c *gophercloud.ServiceClient, groupID string) string {
+ return c.ServiceURL("groups", groupID, "policies")
+}