blob: b304e9d9291bbd513d1be7b45608c238fb552372 [file] [log] [blame]
Brad Ison53e997c2016-03-26 18:02:05 -04001package policies
2
3import (
4 "github.com/mitchellh/mapstructure"
5
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
10type policyResult struct {
11 gophercloud.Result
12}
13
Brad Ison55523e52016-04-06 19:25:20 -040014// Extract interprets any policyResult as a Policy, if possible.
15func (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
Brad Isone7d6dfc2016-04-06 14:55:07 -040029// CreateResult represents the result of a create operation.
30type CreateResult struct {
31 policyResult
32}
33
34// Extract extracts a slice of Policies from a CreateResult. Multiple policies
35// can be created in a single operation, so the result of a create is always a
36// list of policies.
37func (res CreateResult) Extract() ([]Policy, error) {
38 if res.Err != nil {
39 return nil, res.Err
40 }
41
42 return commonExtractPolicies(res.Body)
43}
44
Brad Ison55523e52016-04-06 19:25:20 -040045// GetResult temporarily contains the response from a Get call.
46type GetResult struct {
47 policyResult
48}
49
Brad Isonac037d52016-04-07 19:41:29 -040050// UpdateResult represents the result of an update operation.
51type UpdateResult struct {
52 gophercloud.ErrResult
53}
54
Brad Ison124df8e2016-04-07 19:51:51 -040055// DeleteResult represents the result of a delete operation.
56type DeleteResult struct {
57 gophercloud.ErrResult
58}
59
Brad Ison42f8dfb2016-04-07 20:26:06 -040060// ExecuteResult represents the result of an execute operation.
61type ExecuteResult struct {
62 gophercloud.ErrResult
63}
64
Brad Ison53e997c2016-03-26 18:02:05 -040065// Policy represents a scaling policy.
66type Policy struct {
67 // UUID for the policy.
68 ID string `mapstructure:"id" json:"id"`
69
70 // Name of the policy.
71 Name string `mapstructure:"name" json:"name"`
72
73 // Type of scaling policy.
74 Type Type `mapstructure:"type" json:"type"`
75
76 // Cooldown period, in seconds.
77 Cooldown int `mapstructure:"cooldown" json:"cooldown"`
78
79 // Number of servers added or, if negative, removed.
80 Change interface{} `mapstructure:"change" json:"change"`
81
82 // Percent change to make in the number of servers.
83 ChangePercent interface{} `mapstructure:"changePercent" json:"changePercent"`
84
85 // Desired capacity of the of the associated group.
86 DesiredCapacity interface{} `mapstructure:"desiredCapacity" json:"desiredCapacity"`
87
88 // Additional configuration options for some types of policy.
89 Args map[string]interface{} `mapstructure:"args" json:"args"`
90}
91
92// Type represents a type of scaling policy.
93type Type string
94
95const (
96 // Schedule policies run at given times.
97 Schedule Type = "schedule"
98
99 // Webhook policies are triggered by HTTP requests.
100 Webhook Type = "webhook"
101)
102
103// PolicyPage is the page returned by a pager when traversing over a collection
104// of scaling policies.
105type PolicyPage struct {
106 pagination.SinglePageBase
107}
108
109// IsEmpty returns true if a page contains no Policy results.
110func (page PolicyPage) IsEmpty() (bool, error) {
111 policies, err := ExtractPolicies(page)
112
113 if err != nil {
114 return true, err
115 }
116
117 return len(policies) == 0, nil
118}
119
120// ExtractPolicies interprets the results of a single page from a List() call,
121// producing a slice of Policies.
122func ExtractPolicies(page pagination.Page) ([]Policy, error) {
Brad Isone7d6dfc2016-04-06 14:55:07 -0400123 return commonExtractPolicies(page.(PolicyPage).Body)
124}
Brad Ison53e997c2016-03-26 18:02:05 -0400125
Brad Isone7d6dfc2016-04-06 14:55:07 -0400126func commonExtractPolicies(body interface{}) ([]Policy, error) {
Brad Ison53e997c2016-03-26 18:02:05 -0400127 var response struct {
128 Policies []Policy `mapstructure:"policies"`
129 }
130
Brad Isone7d6dfc2016-04-06 14:55:07 -0400131 err := mapstructure.Decode(body, &response)
Brad Ison53e997c2016-03-26 18:02:05 -0400132
133 if err != nil {
134 return nil, err
135 }
136
137 return response.Policies, err
138}