| package policies |
| |
| import ( |
| "github.com/mitchellh/mapstructure" |
| |
| "github.com/rackspace/gophercloud" |
| "github.com/rackspace/gophercloud/pagination" |
| ) |
| |
| type policyResult struct { |
| gophercloud.Result |
| } |
| |
| // Extract interprets any policyResult as a Policy, if possible. |
| func (r policyResult) Extract() (*Policy, error) { |
| if r.Err != nil { |
| return nil, r.Err |
| } |
| |
| var response struct { |
| Policy Policy `mapstructure:"policy"` |
| } |
| |
| err := mapstructure.Decode(r.Body, &response) |
| |
| return &response.Policy, err |
| } |
| |
| // CreateResult represents the result of a create operation. |
| type CreateResult struct { |
| policyResult |
| } |
| |
| // Extract extracts a slice of Policies from a CreateResult. Multiple policies |
| // can be created in a single operation, so the result of a create is always a |
| // list of policies. |
| func (res CreateResult) Extract() ([]Policy, error) { |
| if res.Err != nil { |
| return nil, res.Err |
| } |
| |
| return commonExtractPolicies(res.Body) |
| } |
| |
| // GetResult temporarily contains the response from a Get call. |
| type GetResult struct { |
| policyResult |
| } |
| |
| // UpdateResult represents the result of an update operation. |
| type UpdateResult struct { |
| gophercloud.ErrResult |
| } |
| |
| // DeleteResult represents the result of a delete operation. |
| type DeleteResult struct { |
| gophercloud.ErrResult |
| } |
| |
| // ExecuteResult represents the result of an execute operation. |
| type ExecuteResult struct { |
| gophercloud.ErrResult |
| } |
| |
| // Policy represents a scaling policy. |
| type Policy struct { |
| // UUID for the policy. |
| ID string `mapstructure:"id" json:"id"` |
| |
| // Name of the policy. |
| Name string `mapstructure:"name" json:"name"` |
| |
| // Type of scaling policy. |
| Type Type `mapstructure:"type" json:"type"` |
| |
| // Cooldown period, in seconds. |
| Cooldown int `mapstructure:"cooldown" json:"cooldown"` |
| |
| // Number of servers added or, if negative, removed. |
| Change interface{} `mapstructure:"change" json:"change"` |
| |
| // Percent change to make in the number of servers. |
| ChangePercent interface{} `mapstructure:"changePercent" json:"changePercent"` |
| |
| // Desired capacity of the of the associated group. |
| DesiredCapacity interface{} `mapstructure:"desiredCapacity" json:"desiredCapacity"` |
| |
| // Additional configuration options for some types of policy. |
| Args map[string]interface{} `mapstructure:"args" json:"args"` |
| } |
| |
| // Type represents a type of scaling policy. |
| type Type string |
| |
| const ( |
| // Schedule policies run at given times. |
| Schedule Type = "schedule" |
| |
| // Webhook policies are triggered by HTTP requests. |
| Webhook Type = "webhook" |
| ) |
| |
| // PolicyPage is the page returned by a pager when traversing over a collection |
| // of scaling policies. |
| type PolicyPage struct { |
| pagination.SinglePageBase |
| } |
| |
| // IsEmpty returns true if a page contains no Policy results. |
| func (page PolicyPage) IsEmpty() (bool, error) { |
| policies, err := ExtractPolicies(page) |
| |
| if err != nil { |
| return true, err |
| } |
| |
| return len(policies) == 0, nil |
| } |
| |
| // ExtractPolicies interprets the results of a single page from a List() call, |
| // producing a slice of Policies. |
| func ExtractPolicies(page pagination.Page) ([]Policy, error) { |
| return commonExtractPolicies(page.(PolicyPage).Body) |
| } |
| |
| func commonExtractPolicies(body interface{}) ([]Policy, error) { |
| var response struct { |
| Policies []Policy `mapstructure:"policies"` |
| } |
| |
| err := mapstructure.Decode(body, &response) |
| |
| if err != nil { |
| return nil, err |
| } |
| |
| return response.Policies, err |
| } |