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

Commit 55523e5

Browse files
committed
Rackspace Auto Scale: Add policies Get()
1 parent e7d6dfc commit 55523e5

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

rackspace/autoscale/v1/policies/fixtures.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ const PolicyCreateRequest = `
9898
]
9999
`
100100

101+
// PolicyGetBody contains the canned body of a policies.Get response.
102+
const PolicyGetBody = `
103+
{
104+
"policy": {
105+
"name": "webhook policy",
106+
"links": [
107+
{
108+
"href": "https://dfw.autoscale.api.rackspacecloud.com/v1.0/123456/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies/2b48d247-0282-4b9d-8775-5c4b67e8e649/",
109+
"rel": "self"
110+
}
111+
],
112+
"changePercent": 3.3,
113+
"cooldown": 300,
114+
"type": "webhook",
115+
"id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
116+
}
117+
}
118+
`
119+
101120
var (
102121
// WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
103122
WebhookPolicy = Policy{
@@ -133,7 +152,7 @@ var (
133152

134153
// HandlePolicyListSuccessfully sets up the test server to respond to a policies List request.
135154
func HandlePolicyListSuccessfully(t *testing.T) {
136-
path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies"
155+
path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
137156

138157
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
139158
th.TestMethod(t, r, "GET")
@@ -147,7 +166,7 @@ func HandlePolicyListSuccessfully(t *testing.T) {
147166

148167
// HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request.
149168
func HandlePolicyCreateSuccessfully(t *testing.T) {
150-
path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies"
169+
path := "/groups/60b15dad-5ea1-43fa-9a12-a1d737b4da07/policies"
151170

152171
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
153172
th.TestMethod(t, r, "POST")
@@ -163,3 +182,20 @@ func HandlePolicyCreateSuccessfully(t *testing.T) {
163182
fmt.Fprintf(w, PolicyCreateBody)
164183
})
165184
}
185+
186+
// HandlePolicyGetSuccessfully sets up the test server to respond to a policies Get request.
187+
func HandlePolicyGetSuccessfully(t *testing.T) {
188+
groupID := "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
189+
policyID := "2b48d247-0282-4b9d-8775-5c4b67e8e649"
190+
191+
path := fmt.Sprintf("/groups/%s/policies/%s", groupID, policyID)
192+
193+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
194+
th.TestMethod(t, r, "GET")
195+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
196+
197+
w.Header().Add("Content-Type", "application/json")
198+
199+
fmt.Fprintf(w, PolicyGetBody)
200+
})
201+
}

rackspace/autoscale/v1/policies/requests.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,12 @@ func Create(client *gophercloud.ServiceClient, groupID string, opts CreateOptsBu
120120

121121
return res
122122
}
123+
124+
// Get requests the details of a single policy with the given ID.
125+
func Get(client *gophercloud.ServiceClient, groupID, policyID string) GetResult {
126+
var result GetResult
127+
128+
_, result.Err = client.Get(getURL(client, groupID, policyID), &result.Body, nil)
129+
130+
return result
131+
}

rackspace/autoscale/v1/policies/requests_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
)
1010

1111
const (
12-
groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
12+
groupID = "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
13+
webhookPolicyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
1314
)
1415

1516
func TestList(t *testing.T) {
@@ -18,7 +19,7 @@ func TestList(t *testing.T) {
1819
HandlePolicyListSuccessfully(t)
1920

2021
pages := 0
21-
pager := List(client.ServiceClient(), "10eb3219-1b12-4b34-b1e4-e10ee4f24c65")
22+
pager := List(client.ServiceClient(), "60b15dad-5ea1-43fa-9a12-a1d737b4da07")
2223

2324
err := pager.EachPage(func(page pagination.Page) (bool, error) {
2425
pages++
@@ -94,3 +95,16 @@ func TestCreate(t *testing.T) {
9495
th.CheckDeepEquals(t, OneTimePolicy, policies[1])
9596
th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
9697
}
98+
99+
func TestGet(t *testing.T) {
100+
th.SetupHTTP()
101+
defer th.TeardownHTTP()
102+
HandlePolicyGetSuccessfully(t)
103+
104+
client := client.ServiceClient()
105+
106+
policy, err := Get(client, groupID, webhookPolicyID).Extract()
107+
108+
th.AssertNoErr(t, err)
109+
th.CheckDeepEquals(t, WebhookPolicy, *policy)
110+
}

rackspace/autoscale/v1/policies/results.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ type policyResult struct {
1111
gophercloud.Result
1212
}
1313

14+
// Extract interprets any policyResult as a Policy, if possible.
15+
func (r policyResult) Extract() (*Policy, error) {
16+
if r.Err != nil {
17+
return nil, r.Err
18+
}
19+
20+
var response struct {
21+
Policy Policy `mapstructure:"policy"`
22+
}
23+
24+
err := mapstructure.Decode(r.Body, &response)
25+
26+
return &response.Policy, err
27+
}
28+
1429
// CreateResult represents the result of a create operation.
1530
type CreateResult struct {
1631
policyResult
@@ -27,6 +42,11 @@ func (res CreateResult) Extract() ([]Policy, error) {
2742
return commonExtractPolicies(res.Body)
2843
}
2944

45+
// GetResult temporarily contains the response from a Get call.
46+
type GetResult struct {
47+
policyResult
48+
}
49+
3050
// Policy represents a scaling policy.
3151
type Policy struct {
3252
// UUID for the policy.

rackspace/autoscale/v1/policies/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ func listURL(c *gophercloud.ServiceClient, groupID string) string {
99
func createURL(c *gophercloud.ServiceClient, groupID string) string {
1010
return c.ServiceURL("groups", groupID, "policies")
1111
}
12+
13+
func getURL(c *gophercloud.ServiceClient, groupID, policyID string) string {
14+
return c.ServiceURL("groups", groupID, "policies", policyID)
15+
}

0 commit comments

Comments
 (0)