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 | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame^] | 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 | |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 30 | // Policy represents a scaling policy. |
| 31 | type Policy struct { |
| 32 | // UUID for the policy. |
| 33 | ID string `mapstructure:"id" json:"id"` |
| 34 | |
| 35 | // Name of the policy. |
| 36 | Name string `mapstructure:"name" json:"name"` |
| 37 | |
| 38 | // Type of scaling policy. |
| 39 | Type Type `mapstructure:"type" json:"type"` |
| 40 | |
| 41 | // Cooldown period, in seconds. |
| 42 | Cooldown int `mapstructure:"cooldown" json:"cooldown"` |
| 43 | |
| 44 | // Number of servers added or, if negative, removed. |
| 45 | Change interface{} `mapstructure:"change" json:"change"` |
| 46 | |
| 47 | // Percent change to make in the number of servers. |
| 48 | ChangePercent interface{} `mapstructure:"changePercent" json:"changePercent"` |
| 49 | |
| 50 | // Desired capacity of the of the associated group. |
| 51 | DesiredCapacity interface{} `mapstructure:"desiredCapacity" json:"desiredCapacity"` |
| 52 | |
| 53 | // Additional configuration options for some types of policy. |
| 54 | Args map[string]interface{} `mapstructure:"args" json:"args"` |
| 55 | } |
| 56 | |
| 57 | // Type represents a type of scaling policy. |
| 58 | type Type string |
| 59 | |
| 60 | const ( |
| 61 | // Schedule policies run at given times. |
| 62 | Schedule Type = "schedule" |
| 63 | |
| 64 | // Webhook policies are triggered by HTTP requests. |
| 65 | Webhook Type = "webhook" |
| 66 | ) |
| 67 | |
| 68 | // PolicyPage is the page returned by a pager when traversing over a collection |
| 69 | // of scaling policies. |
| 70 | type PolicyPage struct { |
| 71 | pagination.SinglePageBase |
| 72 | } |
| 73 | |
| 74 | // IsEmpty returns true if a page contains no Policy results. |
| 75 | func (page PolicyPage) IsEmpty() (bool, error) { |
| 76 | policies, err := ExtractPolicies(page) |
| 77 | |
| 78 | if err != nil { |
| 79 | return true, err |
| 80 | } |
| 81 | |
| 82 | return len(policies) == 0, nil |
| 83 | } |
| 84 | |
| 85 | // ExtractPolicies interprets the results of a single page from a List() call, |
| 86 | // producing a slice of Policies. |
| 87 | func ExtractPolicies(page pagination.Page) ([]Policy, error) { |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame^] | 88 | return commonExtractPolicies(page.(PolicyPage).Body) |
| 89 | } |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 90 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame^] | 91 | func commonExtractPolicies(body interface{}) ([]Policy, error) { |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 92 | var response struct { |
| 93 | Policies []Policy `mapstructure:"policies"` |
| 94 | } |
| 95 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame^] | 96 | err := mapstructure.Decode(body, &response) |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 97 | |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | return response.Policies, err |
| 103 | } |