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

Commit e7d6dfc

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

File tree

5 files changed

+241
-10
lines changed

5 files changed

+241
-10
lines changed

rackspace/autoscale/v1/policies/fixtures.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const PolicyListBody = `
2424
"rel": "self"
2525
}
2626
],
27-
"changePercent": 3,
27+
"changePercent": 3.3,
2828
"cooldown": 300,
2929
"type": "webhook",
3030
"id": "2b48d247-0282-4b9d-8775-5c4b67e8e649"
@@ -59,39 +59,72 @@ const PolicyListBody = `
5959
},
6060
"type": "schedule",
6161
"id": "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
62-
"change": 2
62+
"desiredCapacity": 2
6363
}
6464
]
6565
}
6666
`
6767

68+
// PolicyCreateBody contains the canned body of a policies.Create response.
69+
const PolicyCreateBody = PolicyListBody
70+
71+
// PolicyCreateRequest contains the canned body of a policies.Create request.
72+
const PolicyCreateRequest = `
73+
[
74+
{
75+
"name": "webhook policy",
76+
"changePercent": 3.3,
77+
"cooldown": 300,
78+
"type": "webhook"
79+
},
80+
{
81+
"cooldown": 0,
82+
"name": "one time",
83+
"args": {
84+
"at": "2020-04-01T23:00:00.000Z"
85+
},
86+
"type": "schedule",
87+
"change": -1
88+
},
89+
{
90+
"cooldown": 0,
91+
"name": "sunday afternoon",
92+
"args": {
93+
"cron": "59 15 * * 0"
94+
},
95+
"type": "schedule",
96+
"desiredCapacity": 2
97+
}
98+
]
99+
`
100+
68101
var (
69102
// WebhookPolicy is a Policy corresponding to the first result in PolicyListBody.
70103
WebhookPolicy = Policy{
71104
ID: "2b48d247-0282-4b9d-8775-5c4b67e8e649",
72105
Name: "webhook policy",
73106
Type: Webhook,
74107
Cooldown: 300,
75-
ChangePercent: 3,
108+
ChangePercent: 3.3,
76109
}
77110

78111
// OneTimePolicy is a Policy corresponding to the second result in PolicyListBody.
79112
OneTimePolicy = Policy{
80113
ID: "c175c31e-65f9-41de-8b15-918420d3253e",
81114
Name: "one time",
82115
Type: Schedule,
83-
Change: -1,
116+
Change: float64(-1),
84117
Args: map[string]interface{}{
85118
"at": "2020-04-01T23:00:00.000Z",
86119
},
87120
}
88121

89122
// SundayAfternoonPolicy is a Policy corresponding to the third result in PolicyListBody.
90123
SundayAfternoonPolicy = Policy{
91-
ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
92-
Name: "sunday afternoon",
93-
Type: Schedule,
94-
Change: 2,
124+
ID: "e785e3e7-af9e-4f3c-99ae-b80a532e1663",
125+
Name: "sunday afternoon",
126+
Type: Schedule,
127+
DesiredCapacity: float64(2),
95128
Args: map[string]interface{}{
96129
"cron": "59 15 * * 0",
97130
},
@@ -111,3 +144,22 @@ func HandlePolicyListSuccessfully(t *testing.T) {
111144
fmt.Fprintf(w, PolicyListBody)
112145
})
113146
}
147+
148+
// HandlePolicyCreateSuccessfully sets up the test server to respond to a policies Create request.
149+
func HandlePolicyCreateSuccessfully(t *testing.T) {
150+
path := "/groups/10eb3219-1b12-4b34-b1e4-e10ee4f24c65/policies"
151+
152+
th.Mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
153+
th.TestMethod(t, r, "POST")
154+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
155+
th.TestHeader(t, r, "Content-Type", "application/json")
156+
th.TestHeader(t, r, "Accept", "application/json")
157+
158+
th.TestJSONRequest(t, r, PolicyCreateRequest)
159+
160+
w.Header().Add("Content-Type", "application/json")
161+
w.WriteHeader(http.StatusCreated)
162+
163+
fmt.Fprintf(w, PolicyCreateBody)
164+
})
165+
}

rackspace/autoscale/v1/policies/requests.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package policies
22

33
import (
4+
"errors"
5+
46
"github.com/rackspace/gophercloud"
57
"github.com/rackspace/gophercloud/pagination"
68
)
79

10+
// Validation errors returned by create or update operations.
11+
var (
12+
ErrNoName = errors.New("Policy name cannot by empty.")
13+
ErrNoArgs = errors.New("Args cannot be nil for schedule policies.")
14+
)
15+
816
// List returns all scaling policies for a group.
917
func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager {
1018
url := listURL(client, groupID)
@@ -15,3 +23,100 @@ func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager {
1523

1624
return pagination.NewPager(client, url, createPageFn)
1725
}
26+
27+
// CreateOptsBuilder is the interface responsible for generating the map that
28+
// will be marshalled to JSON for a Create operation.
29+
type CreateOptsBuilder interface {
30+
ToPolicyCreateMap() ([]map[string]interface{}, error)
31+
}
32+
33+
// Adjustment represents the change in capacity associated with a policy.
34+
type Adjustment struct {
35+
// The type for this adjustment.
36+
Type AdjustmentType
37+
38+
// The value of the adjustment. For adjustments of type Change or
39+
// DesiredCapacity, this will be converted to an integer.
40+
Value float64
41+
}
42+
43+
// AdjustmentType represents the way in which a policy will change a group.
44+
type AdjustmentType string
45+
46+
// Valid types of adjustments for a policy.
47+
const (
48+
Change AdjustmentType = "change"
49+
ChangePercent AdjustmentType = "changePercent"
50+
DesiredCapacity AdjustmentType = "desiredCapacity"
51+
)
52+
53+
// CreateOpts is a slice of CreateOpt structs that allow the user to create
54+
// multiple policies in a single operation.
55+
type CreateOpts []CreateOpt
56+
57+
// CreateOpt represents the options to create a policy.
58+
type CreateOpt struct {
59+
// Name [required] is a name for the policy.
60+
Name string
61+
62+
// Type [required] of policy, i.e. either "webhook" or "schedule".
63+
Type Type
64+
65+
// Cooldown [required] period in seconds.
66+
Cooldown int
67+
68+
// Adjustment [requried] type and value for the policy.
69+
Adjustment Adjustment
70+
71+
// Additional configuration options for some types of policy.
72+
Args map[string]interface{}
73+
}
74+
75+
// ToPolicyCreateMap converts a slice of CreateOpt structs into a map for use
76+
// in the request body of a Create operation.
77+
func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) {
78+
var policies []map[string]interface{}
79+
80+
for _, o := range opts {
81+
if o.Name == "" {
82+
return nil, ErrNoName
83+
}
84+
85+
if o.Type == Schedule && o.Args == nil {
86+
return nil, ErrNoArgs
87+
}
88+
89+
policy := make(map[string]interface{})
90+
91+
policy["name"] = o.Name
92+
policy["type"] = o.Type
93+
policy["cooldown"] = o.Cooldown
94+
95+
// TODO: Function to validate and cast key + value?
96+
policy[string(o.Adjustment.Type)] = o.Adjustment.Value
97+
98+
if o.Args != nil {
99+
policy["args"] = o.Args
100+
}
101+
102+
policies = append(policies, policy)
103+
}
104+
105+
return policies, nil
106+
}
107+
108+
// Create requests a new policy be created and associated with the given group.
109+
func Create(client *gophercloud.ServiceClient, groupID string, opts CreateOptsBuilder) CreateResult {
110+
var res CreateResult
111+
112+
reqBody, err := opts.ToPolicyCreateMap()
113+
114+
if err != nil {
115+
res.Err = err
116+
return res
117+
}
118+
119+
_, res.Err = client.Post(createURL(client, groupID), reqBody, &res.Body, nil)
120+
121+
return res
122+
}

rackspace/autoscale/v1/policies/requests_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/rackspace/gophercloud/testhelper/client"
99
)
1010

11+
const (
12+
groupID = "10eb3219-1b12-4b34-b1e4-e10ee4f24c65"
13+
)
14+
1115
func TestList(t *testing.T) {
1216
th.SetupHTTP()
1317
defer th.TeardownHTTP()
@@ -42,3 +46,51 @@ func TestList(t *testing.T) {
4246
t.Errorf("Expected 1 page, saw %d", pages)
4347
}
4448
}
49+
50+
func TestCreate(t *testing.T) {
51+
th.SetupHTTP()
52+
defer th.TeardownHTTP()
53+
HandlePolicyCreateSuccessfully(t)
54+
55+
client := client.ServiceClient()
56+
opts := CreateOpts{
57+
{
58+
Name: "webhook policy",
59+
Type: Webhook,
60+
Cooldown: 300,
61+
Adjustment: Adjustment{
62+
Type: ChangePercent,
63+
Value: 3.3,
64+
},
65+
},
66+
{
67+
Name: "one time",
68+
Type: Schedule,
69+
Adjustment: Adjustment{
70+
Type: Change,
71+
Value: -1,
72+
},
73+
Args: map[string]interface{}{
74+
"at": "2020-04-01T23:00:00.000Z",
75+
},
76+
},
77+
{
78+
Name: "sunday afternoon",
79+
Type: Schedule,
80+
Adjustment: Adjustment{
81+
Type: DesiredCapacity,
82+
Value: 2,
83+
},
84+
Args: map[string]interface{}{
85+
"cron": "59 15 * * 0",
86+
},
87+
},
88+
}
89+
90+
policies, err := Create(client, groupID, opts).Extract()
91+
92+
th.AssertNoErr(t, err)
93+
th.CheckDeepEquals(t, WebhookPolicy, policies[0])
94+
th.CheckDeepEquals(t, OneTimePolicy, policies[1])
95+
th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
96+
}

rackspace/autoscale/v1/policies/results.go

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

14+
// CreateResult represents the result of a create operation.
15+
type CreateResult struct {
16+
policyResult
17+
}
18+
19+
// Extract extracts a slice of Policies from a CreateResult. Multiple policies
20+
// can be created in a single operation, so the result of a create is always a
21+
// list of policies.
22+
func (res CreateResult) Extract() ([]Policy, error) {
23+
if res.Err != nil {
24+
return nil, res.Err
25+
}
26+
27+
return commonExtractPolicies(res.Body)
28+
}
29+
1430
// Policy represents a scaling policy.
1531
type Policy struct {
1632
// UUID for the policy.
@@ -69,13 +85,15 @@ func (page PolicyPage) IsEmpty() (bool, error) {
6985
// ExtractPolicies interprets the results of a single page from a List() call,
7086
// producing a slice of Policies.
7187
func ExtractPolicies(page pagination.Page) ([]Policy, error) {
72-
casted := page.(PolicyPage).Body
88+
return commonExtractPolicies(page.(PolicyPage).Body)
89+
}
7390

91+
func commonExtractPolicies(body interface{}) ([]Policy, error) {
7492
var response struct {
7593
Policies []Policy `mapstructure:"policies"`
7694
}
7795

78-
err := mapstructure.Decode(casted, &response)
96+
err := mapstructure.Decode(body, &response)
7997

8098
if err != nil {
8199
return nil, err

rackspace/autoscale/v1/policies/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ import "github.com/rackspace/gophercloud"
55
func listURL(c *gophercloud.ServiceClient, groupID string) string {
66
return c.ServiceURL("groups", groupID, "policies")
77
}
8+
9+
func createURL(c *gophercloud.ServiceClient, groupID string) string {
10+
return c.ServiceURL("groups", groupID, "policies")
11+
}

0 commit comments

Comments
 (0)