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

Commit 53e997c

Browse files
committed
Rackspace Auto Scale: Add policies List()
1 parent a09b5b4 commit 53e997c

File tree

5 files changed

+266
-0
lines changed

5 files changed

+266
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// +build fixtures
2+
3+
package policies
4+
5+
import (
6+
"fmt"
7+
"net/http"
8+
"testing"
9+
10+
th "github.com/rackspace/gophercloud/testhelper"
11+
"github.com/rackspace/gophercloud/testhelper/client"
12+
)
13+
14+
// PolicyListBody contains the canned body of a policies.List response.
15+
const PolicyListBody = `
16+
{
17+
"policies_links": [],
18+
"policies": [
19+
{
20+
"name": "webhook policy",
21+
"links": [
22+
{
23+
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/",
24+
"rel": "self"
25+
}
26+
],
27+
"changePercent": 3,
28+
"cooldown": 300,
29+
"type": "webhook",
30+
"id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
31+
},
32+
{
33+
"cooldown": 0,
34+
"name": "one time",
35+
"links": [
36+
{
37+
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/c175c31e-65f9-41de-8b15-918420d3253e/",
38+
"rel": "self"
39+
}
40+
],
41+
"args": {
42+
"at": "2020-04-01T23:00:00.000Z"
43+
},
44+
"type": "schedule",
45+
"id": "c175c31e-65f9-41de-8b15-918420d3253e",
46+
"change": -1
47+
},
48+
{
49+
"cooldown": 0,
50+
"name": "sunday afternoon",
51+
"links": [
52+
{
53+
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/e785e3e7-af9e-4f3c-99ae-b80a532e1663/",
54+
"rel": "self"
55+
}
56+
],
57+
"args": {
58+
"cron": "59 15 * * 0"
59+
},
60+
"type": "schedule",
61+
"id": "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
62+
"change": 2
63+
}
64+
]
65+
}
66+
`
67+
68+
var (
69+
// WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
70+
WebhookPolicy = Policy{
71+
ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
72+
Name: "webhook policy",
73+
Type: Webhook,
74+
Cooldown: 300,
75+
ChangePercent: 3,
76+
}
77+
78+
// OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
79+
OneTimePolicy = Policy{
80+
ID: "c175c31e-65f9-41de-8b15-918420d3253e",
81+
Name: "one time",
82+
Type: Schedule,
83+
Change: -1,
84+
Args: map[string]interface{}{
85+
"at": "2020-04-01T23:00:00.000Z",
86+
},
87+
}
88+
89+
// SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
90+
SundayAfternoonPolicy = Policy{
91+
ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
92+
Name: "sunday afternoon",
93+
Type: Schedule,
94+
Change: 2,
95+
Args: map[string]interface{}{
96+
"cron": "59 15 * * 0",
97+
},
98+
}
99+
)
100+
101+
// HandlePolicyListSuccessfully sets up the test server to respond to a policies List request.
102+
func HandlePolicyListSuccessfully(t *testing.T) {
103+
path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies"
104+
105+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
106+
th.TestMethod(t, r, "GET")
107+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
108+
109+
w.Header().Add("Content-Type", "application/json")
110+
111+
fmt.Fprintf(w, PolicyListBody)
112+
})
113+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package policies
2+
3+
import (
4+
"github.com/rackspace/gophercloud"
5+
"github.com/rackspace/gophercloud/pagination"
6+
)
7+
8+
// List returns all scaling policies for a group.
9+
func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager {
10+
url := listURL(client, groupID)
11+
12+
createPageFn := func(r pagination.PageResult) pagination.Page {
13+
return PolicyPage{pagination.SinglePageBase(r)}
14+
}
15+
16+
return pagination.NewPager(client, url, createPageFn)
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package policies
2+
3+
import (
4+
"testing"
5+
6+
"github.com/rackspace/gophercloud/pagination"
7+
th "github.com/rackspace/gophercloud/testhelper"
8+
"github.com/rackspace/gophercloud/testhelper/client"
9+
)
10+
11+
func TestList(t *testing.T) {
12+
th.SetupHTTP()
13+
defer th.TeardownHTTP()
14+
HandlePolicyListSuccessfully(t)
15+
16+
pages := 0
17+
pager := List(client.ServiceClient(), "10eb3219-1b12-4b34-b1e4-e10ee4f24c65")
18+
19+
err := pager.EachPage(func(page pagination.Page) (bool, error) {
20+
pages++
21+
22+
policies, err := ExtractPolicies(page)
23+
24+
if err != nil {
25+
return false, err
26+
}
27+
28+
if len(policies) != 3 {
29+
t.Fatalf("Expected 3 policies, got %d", len(policies))
30+
}
31+
32+
th.CheckDeepEquals(t, WebhookPolicy, policies[0])
33+
th.CheckDeepEquals(t, OneTimePolicy, policies[1])
34+
th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
35+
36+
return true, nil
37+
})
38+
39+
th.AssertNoErr(t, err)
40+
41+
if pages != 1 {
42+
t.Errorf("Expected 1 page, saw %d", pages)
43+
}
44+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package policies
2+
3+
import (
4+
"github.com/mitchellh/mapstructure"
5+
6+
"github.com/rackspace/gophercloud"
7+
"github.com/rackspace/gophercloud/pagination"
8+
)
9+
10+
type policyResult struct {
11+
gophercloud.Result
12+
}
13+
14+
// Policy represents a scaling policy.
15+
type Policy struct {
16+
// UUID for the policy.
17+
ID string `mapstructure:"id" json:"id"`
18+
19+
// Name of the policy.
20+
Name string `mapstructure:"name" json:"name"`
21+
22+
// Type of scaling policy.
23+
Type Type `mapstructure:"type" json:"type"`
24+
25+
// Cooldown period, in seconds.
26+
Cooldown int `mapstructure:"cooldown" json:"cooldown"`
27+
28+
// Number of servers added or, if negative, removed.
29+
Change interface{} `mapstructure:"change" json:"change"`
30+
31+
// Percent change to make in the number of servers.
32+
ChangePercent interface{} `mapstructure:"changePercent" json:"changePercent"`
33+
34+
// Desired capacity of the of the associated group.
35+
DesiredCapacity interface{} `mapstructure:"desiredCapacity" json:"desiredCapacity"`
36+
37+
// Additional configuration options for some types of policy.
38+
Args map[string]interface{} `mapstructure:"args" json:"args"`
39+
}
40+
41+
// Type represents a type of scaling policy.
42+
type Type string
43+
44+
const (
45+
// Schedule policies run at given times.
46+
Schedule Type = "schedule"
47+
48+
// Webhook policies are triggered by HTTP requests.
49+
Webhook Type = "webhook"
50+
)
51+
52+
// PolicyPage is the page returned by a pager when traversing over a collection
53+
// of scaling policies.
54+
type PolicyPage struct {
55+
pagination.SinglePageBase
56+
}
57+
58+
// IsEmpty returns true if a page contains no Policy results.
59+
func (page PolicyPage) IsEmpty() (bool, error) {
60+
policies, err := ExtractPolicies(page)
61+
62+
if err != nil {
63+
return true, err
64+
}
65+
66+
return len(policies) == 0, nil
67+
}
68+
69+
// ExtractPolicies interprets the results of a single page from a List() call,
70+
// producing a slice of Policies.
71+
func ExtractPolicies(page pagination.Page) ([]Policy, error) {
72+
casted := page.(PolicyPage).Body
73+
74+
var response struct {
75+
Policies []Policy `mapstructure:"policies"`
76+
}
77+
78+
err := mapstructure.Decode(casted, &response)
79+
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
return response.Policies, err
85+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package policies
2+
3+
import "github.com/rackspace/gophercloud"
4+
5+
func listURL(c *gophercloud.ServiceClient, groupID string) string {
6+
return c.ServiceURL("groups", groupID, "policies")
7+
}

0 commit comments

Comments
 (0)