Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 1 | package policies |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
| 10 | type policyResult struct { |
| 11 | gophercloud.Result |
| 12 | } |
| 13 | |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 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 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 29 | // CreateResult represents the result of a create operation. |
| 30 | type 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. |
| 37 | func (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 Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 45 | // GetResult temporarily contains the response from a Get call. |
| 46 | type GetResult struct { |
| 47 | policyResult |
| 48 | } |
| 49 | |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame^] | 50 | // UpdateResult represents the result of an update operation. |
| 51 | type UpdateResult struct { |
| 52 | gophercloud.ErrResult |
| 53 | } |
| 54 | |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 55 | // Policy represents a scaling policy. |
| 56 | type 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. |
| 83 | type Type string |
| 84 | |
| 85 | const ( |
| 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. |
| 95 | type PolicyPage struct { |
| 96 | pagination.SinglePageBase |
| 97 | } |
| 98 | |
| 99 | // IsEmpty returns true if a page contains no Policy results. |
| 100 | func (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. |
| 112 | func ExtractPolicies(page pagination.Page) ([]Policy, error) { |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 113 | return commonExtractPolicies(page.(PolicyPage).Body) |
| 114 | } |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 115 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 116 | func commonExtractPolicies(body interface{}) ([]Policy, error) { |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 117 | var response struct { |
| 118 | Policies []Policy `mapstructure:"policies"` |
| 119 | } |
| 120 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 121 | err := mapstructure.Decode(body, &response) |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 122 | |
| 123 | if err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | |
| 127 | return response.Policies, err |
| 128 | } |