blob: 20cd2ee8fc30cfd5b4e67dbc840ca021a043bcdc [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 Ison53e997c2016-03-26 18:02:05 -040060// Policy represents a scaling policy.
61type Policy struct {
62 // UUID for the policy.
63 ID string `mapstructure:"id" json:"id"`
64
65 // Name of the policy.
66 Name string `mapstructure:"name" json:"name"`
67
68 // Type of scaling policy.
69 Type Type `mapstructure:"type" json:"type"`
70
71 // Cooldown period, in seconds.
72 Cooldown int `mapstructure:"cooldown" json:"cooldown"`
73
74 // Number of servers added or, if negative, removed.
75 Change interface{} `mapstructure:"change" json:"change"`
76
77 // Percent change to make in the number of servers.
78 ChangePercent interface{} `mapstructure:"changePercent" json:"changePercent"`
79
80 // Desired capacity of the of the associated group.
81 DesiredCapacity interface{} `mapstructure:"desiredCapacity" json:"desiredCapacity"`
82
83 // Additional configuration options for some types of policy.
84 Args map[string]interface{} `mapstructure:"args" json:"args"`
85}
86
87// Type represents a type of scaling policy.
88type Type string
89
90const (
91 // Schedule policies run at given times.
92 Schedule Type = "schedule"
93
94 // Webhook policies are triggered by HTTP requests.
95 Webhook Type = "webhook"
96)
97
98// PolicyPage is the page returned by a pager when traversing over a collection
99// of scaling policies.
100type PolicyPage struct {
101 pagination.SinglePageBase
102}
103
104// IsEmpty returns true if a page contains no Policy results.
105func (page PolicyPage) IsEmpty() (bool, error) {
106 policies, err := ExtractPolicies(page)
107
108 if err != nil {
109 return true, err
110 }
111
112 return len(policies) == 0, nil
113}
114
115// ExtractPolicies interprets the results of a single page from a List() call,
116// producing a slice of Policies.
117func ExtractPolicies(page pagination.Page) ([]Policy, error) {
Brad Isone7d6dfc2016-04-06 14:55:07 -0400118 return commonExtractPolicies(page.(PolicyPage).Body)
119}
Brad Ison53e997c2016-03-26 18:02:05 -0400120
Brad Isone7d6dfc2016-04-06 14:55:07 -0400121func commonExtractPolicies(body interface{}) ([]Policy, error) {
Brad Ison53e997c2016-03-26 18:02:05 -0400122 var response struct {
123 Policies []Policy `mapstructure:"policies"`
124 }
125
Brad Isone7d6dfc2016-04-06 14:55:07 -0400126 err := mapstructure.Decode(body, &response)
Brad Ison53e997c2016-03-26 18:02:05 -0400127
128 if err != nil {
129 return nil, err
130 }
131
132 return response.Policies, err
133}