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