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