blob: e50ea7851e1cae19db334288642b21e5f0fcf2ba [file] [log] [blame]
Brad Ison53e997c2016-03-26 18:02:05 -04001package policies
2
3import (
4 "github.com/mitchellh/mapstructure"
5
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
10type policyResult struct {
11 gophercloud.Result
12}
13
Brad Isone7d6dfc2016-04-06 14:55:07 -040014// CreateResult represents the result of a create operation.
15type 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.
22func (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 Ison53e997c2016-03-26 18:02:05 -040030// Policy represents a scaling policy.
31type 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.
58type Type string
59
60const (
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.
70type PolicyPage struct {
71 pagination.SinglePageBase
72}
73
74// IsEmpty returns true if a page contains no Policy results.
75func (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.
87func ExtractPolicies(page pagination.Page) ([]Policy, error) {
Brad Isone7d6dfc2016-04-06 14:55:07 -040088 return commonExtractPolicies(page.(PolicyPage).Body)
89}
Brad Ison53e997c2016-03-26 18:02:05 -040090
Brad Isone7d6dfc2016-04-06 14:55:07 -040091func commonExtractPolicies(body interface{}) ([]Policy, error) {
Brad Ison53e997c2016-03-26 18:02:05 -040092 var response struct {
93 Policies []Policy `mapstructure:"policies"`
94 }
95
Brad Isone7d6dfc2016-04-06 14:55:07 -040096 err := mapstructure.Decode(body, &response)
Brad Ison53e997c2016-03-26 18:02:05 -040097
98 if err != nil {
99 return nil, err
100 }
101
102 return response.Policies, err
103}