11package policies
22
33import (
4+ "errors"
5+
46 "github.com/rackspace/gophercloud"
57 "github.com/rackspace/gophercloud/pagination"
68)
79
10+ // Validation errors returned by create or update operations.
11+ var (
12+ ErrNoName = errors .New ("Policy name cannot by empty." )
13+ ErrNoArgs = errors .New ("Args cannot be nil for schedule policies." )
14+ )
15+
816// List returns all scaling policies for a group.
917func List (client * gophercloud.ServiceClient , groupID string ) pagination.Pager {
1018 url := listURL (client , groupID )
@@ -15,3 +23,100 @@ func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager {
1523
1624 return pagination .NewPager (client , url , createPageFn )
1725}
26+
27+ // CreateOptsBuilder is the interface responsible for generating the map that
28+ // will be marshalled to JSON for a Create operation.
29+ type CreateOptsBuilder interface {
30+ ToPolicyCreateMap () ([]map [string ]interface {}, error )
31+ }
32+
33+ // Adjustment represents the change in capacity associated with a policy.
34+ type 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.
44+ type AdjustmentType string
45+
46+ // Valid types of adjustments for a policy.
47+ const (
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.
55+ type CreateOpts []CreateOpt
56+
57+ // CreateOpt represents the options to create a policy.
58+ type 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.
77+ func (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.
109+ func 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+ }
0 commit comments