blob: ab3d5ebfa44be851089a589b91229ce01571cbba [file] [log] [blame]
Brad Ison53e997c2016-03-26 18:02:05 -04001package policies
2
3import (
Brad Isone7d6dfc2016-04-06 14:55:07 -04004 "errors"
5
Brad Ison53e997c2016-03-26 18:02:05 -04006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
Brad Isone7d6dfc2016-04-06 14:55:07 -040010// Validation errors returned by create or update operations.
11var (
12 ErrNoName = errors.New("Policy name cannot by empty.")
13 ErrNoArgs = errors.New("Args cannot be nil for schedule policies.")
14)
15
Brad Ison53e997c2016-03-26 18:02:05 -040016// List returns all scaling policies for a group.
17func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager {
18 url := listURL(client, groupID)
19
20 createPageFn := func(r pagination.PageResult) pagination.Page {
21 return PolicyPage{pagination.SinglePageBase(r)}
22 }
23
24 return pagination.NewPager(client, url, createPageFn)
25}
Brad Isone7d6dfc2016-04-06 14:55:07 -040026
27// CreateOptsBuilder is the interface responsible for generating the map that
28// will be marshalled to JSON for a Create operation.
29type CreateOptsBuilder interface {
30 ToPolicyCreateMap() ([]map[string]interface{}, error)
31}
32
33// Adjustment represents the change in capacity associated with a policy.
34type Adjustment struct {
35 // The type for this adjustment.
36 Type AdjustmentType
37
38 // The value of the adjustment. For adjustments of type Change or
39 // DesiredCapacity, this will be converted to an integer.
40 Value float64
41}
42
43// AdjustmentType represents the way in which a policy will change a group.
44type AdjustmentType string
45
46// Valid types of adjustments for a policy.
47const (
48 Change AdjustmentType = "change"
49 ChangePercent AdjustmentType = "changePercent"
50 DesiredCapacity AdjustmentType = "desiredCapacity"
51)
52
53// CreateOpts is a slice of CreateOpt structs that allow the user to create
54// multiple policies in a single operation.
55type CreateOpts []CreateOpt
56
57// CreateOpt represents the options to create a policy.
58type CreateOpt struct {
59 // Name [required] is a name for the policy.
60 Name string
61
62 // Type [required] of policy, i.e. either "webhook" or "schedule".
63 Type Type
64
65 // Cooldown [required] period in seconds.
66 Cooldown int
67
68 // Adjustment [requried] type and value for the policy.
69 Adjustment Adjustment
70
71 // Additional configuration options for some types of policy.
72 Args map[string]interface{}
73}
74
75// ToPolicyCreateMap converts a slice of CreateOpt structs into a map for use
76// in the request body of a Create operation.
77func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) {
78 var policies []map[string]interface{}
79
80 for _, o := range opts {
81 if o.Name == "" {
82 return nil, ErrNoName
83 }
84
85 if o.Type == Schedule && o.Args == nil {
86 return nil, ErrNoArgs
87 }
88
89 policy := make(map[string]interface{})
90
91 policy["name"] = o.Name
92 policy["type"] = o.Type
93 policy["cooldown"] = o.Cooldown
94
95 // TODO: Function to validate and cast key + value?
96 policy[string(o.Adjustment.Type)] = o.Adjustment.Value
97
98 if o.Args != nil {
99 policy["args"] = o.Args
100 }
101
102 policies = append(policies, policy)
103 }
104
105 return policies, nil
106}
107
108// Create requests a new policy be created and associated with the given group.
109func Create(client *gophercloud.ServiceClient, groupID string, opts CreateOptsBuilder) CreateResult {
110 var res CreateResult
111
112 reqBody, err := opts.ToPolicyCreateMap()
113
114 if err != nil {
115 res.Err = err
116 return res
117 }
118
119 _, res.Err = client.Post(createURL(client, groupID), reqBody, &res.Body, nil)
120
121 return res
122}
Brad Ison55523e52016-04-06 19:25:20 -0400123
124// Get requests the details of a single policy with the given ID.
125func Get(client *gophercloud.ServiceClient, groupID, policyID string) GetResult {
126 var result GetResult
127
128 _, result.Err = client.Get(getURL(client, groupID, policyID), &result.Body, nil)
129
130 return result
131}