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 | |
| 14 | // Policy represents a scaling policy. |
| 15 | type Policy struct { |
| 16 | // UUID for the policy. |
| 17 | ID string `mapstructure:"id" json:"id"` |
| 18 | |
| 19 | // Name of the policy. |
| 20 | Name string `mapstructure:"name" json:"name"` |
| 21 | |
| 22 | // Type of scaling policy. |
| 23 | Type Type `mapstructure:"type" json:"type"` |
| 24 | |
| 25 | // Cooldown period, in seconds. |
| 26 | Cooldown int `mapstructure:"cooldown" json:"cooldown"` |
| 27 | |
| 28 | // Number of servers added or, if negative, removed. |
| 29 | Change interface{} `mapstructure:"change" json:"change"` |
| 30 | |
| 31 | // Percent change to make in the number of servers. |
| 32 | ChangePercent interface{} `mapstructure:"changePercent" json:"changePercent"` |
| 33 | |
| 34 | // Desired capacity of the of the associated group. |
| 35 | DesiredCapacity interface{} `mapstructure:"desiredCapacity" json:"desiredCapacity"` |
| 36 | |
| 37 | // Additional configuration options for some types of policy. |
| 38 | Args map[string]interface{} `mapstructure:"args" json:"args"` |
| 39 | } |
| 40 | |
| 41 | // Type represents a type of scaling policy. |
| 42 | type Type string |
| 43 | |
| 44 | const ( |
| 45 | // Schedule policies run at given times. |
| 46 | Schedule Type = "schedule" |
| 47 | |
| 48 | // Webhook policies are triggered by HTTP requests. |
| 49 | Webhook Type = "webhook" |
| 50 | ) |
| 51 | |
| 52 | // PolicyPage is the page returned by a pager when traversing over a collection |
| 53 | // of scaling policies. |
| 54 | type PolicyPage struct { |
| 55 | pagination.SinglePageBase |
| 56 | } |
| 57 | |
| 58 | // IsEmpty returns true if a page contains no Policy results. |
| 59 | func (page PolicyPage) IsEmpty() (bool, error) { |
| 60 | policies, err := ExtractPolicies(page) |
| 61 | |
| 62 | if err != nil { |
| 63 | return true, err |
| 64 | } |
| 65 | |
| 66 | return len(policies) == 0, nil |
| 67 | } |
| 68 | |
| 69 | // ExtractPolicies interprets the results of a single page from a List() call, |
| 70 | // producing a slice of Policies. |
| 71 | func ExtractPolicies(page pagination.Page) ([]Policy, error) { |
| 72 | casted := page.(PolicyPage).Body |
| 73 | |
| 74 | var response struct { |
| 75 | Policies []Policy `mapstructure:"policies"` |
| 76 | } |
| 77 | |
| 78 | err := mapstructure.Decode(casted, &response) |
| 79 | |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | |
| 84 | return response.Policies, err |
| 85 | } |