Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 1 | package policies |
| 2 | |
| 3 | import ( |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 4 | "errors" |
| 5 | |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 10 | // Validation errors returned by create or update operations. |
| 11 | var ( |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 12 | ErrNoName = errors.New("Policy name cannot by empty.") |
| 13 | ErrNoArgs = errors.New("Args cannot be nil for schedule policies.") |
Brad Ison | eedc8d9 | 2016-04-11 13:15:16 -0400 | [diff] [blame^] | 14 | ErrCooldownRange = errors.New("Cooldown is out of range (0, 86400).") |
| 15 | ErrUnknownType = errors.New("Unknown policy type.") |
| 16 | ErrUnknownAdjustment = errors.New("Unknown adjustment type.") |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 17 | ) |
| 18 | |
Brad Ison | 53e997c | 2016-03-26 18:02:05 -0400 | [diff] [blame] | 19 | // List returns all scaling policies for a group. |
| 20 | func List(client *gophercloud.ServiceClient, groupID string) pagination.Pager { |
| 21 | url := listURL(client, groupID) |
| 22 | |
| 23 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 24 | return PolicyPage{pagination.SinglePageBase(r)} |
| 25 | } |
| 26 | |
| 27 | return pagination.NewPager(client, url, createPageFn) |
| 28 | } |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 29 | |
| 30 | // CreateOptsBuilder is the interface responsible for generating the map that |
| 31 | // will be marshalled to JSON for a Create operation. |
| 32 | type CreateOptsBuilder interface { |
| 33 | ToPolicyCreateMap() ([]map[string]interface{}, error) |
| 34 | } |
| 35 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 36 | // CreateOpts is a slice of CreateOpt structs that allow the user to create |
| 37 | // multiple policies in a single operation. |
| 38 | type CreateOpts []CreateOpt |
| 39 | |
| 40 | // CreateOpt represents the options to create a policy. |
| 41 | type CreateOpt struct { |
| 42 | // Name [required] is a name for the policy. |
| 43 | Name string |
| 44 | |
| 45 | // Type [required] of policy, i.e. either "webhook" or "schedule". |
| 46 | Type Type |
| 47 | |
| 48 | // Cooldown [required] period in seconds. |
| 49 | Cooldown int |
| 50 | |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 51 | // AdjustmentType [requried] is the method used to change the capacity of |
| 52 | // the group, i.e. one of: Change, ChangePercent, or DesiredCapacity. |
| 53 | AdjustmentType AdjustmentType |
| 54 | |
| 55 | // AdjustmentValue [required] is the numeric value of the adjustment. For |
| 56 | // adjustments of type Change or DesiredCapacity, this will be converted to |
| 57 | // an integer. |
| 58 | AdjustmentValue float64 |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 59 | |
| 60 | // Additional configuration options for some types of policy. |
| 61 | Args map[string]interface{} |
| 62 | } |
| 63 | |
| 64 | // ToPolicyCreateMap converts a slice of CreateOpt structs into a map for use |
| 65 | // in the request body of a Create operation. |
| 66 | func (opts CreateOpts) ToPolicyCreateMap() ([]map[string]interface{}, error) { |
| 67 | var policies []map[string]interface{} |
| 68 | |
| 69 | for _, o := range opts { |
| 70 | if o.Name == "" { |
| 71 | return nil, ErrNoName |
| 72 | } |
| 73 | |
| 74 | if o.Type == Schedule && o.Args == nil { |
| 75 | return nil, ErrNoArgs |
| 76 | } |
| 77 | |
Brad Ison | eedc8d9 | 2016-04-11 13:15:16 -0400 | [diff] [blame^] | 78 | if ok := validateType(o.Type); !ok { |
| 79 | return nil, ErrUnknownType |
| 80 | } |
| 81 | |
| 82 | if ok := validateCooldown(o.Cooldown); !ok { |
| 83 | return nil, ErrCooldownRange |
| 84 | } |
| 85 | |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 86 | policy := make(map[string]interface{}) |
| 87 | |
| 88 | policy["name"] = o.Name |
| 89 | policy["type"] = o.Type |
| 90 | policy["cooldown"] = o.Cooldown |
| 91 | |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 92 | err := setAdjustment(o.AdjustmentType, o.AdjustmentValue, policy) |
| 93 | |
| 94 | if err != nil { |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 95 | return nil, err |
| 96 | } |
Brad Ison | e7d6dfc | 2016-04-06 14:55:07 -0400 | [diff] [blame] | 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 | } |
Brad Ison | 55523e5 | 2016-04-06 19:25:20 -0400 | [diff] [blame] | 123 | |
| 124 | // Get requests the details of a single policy with the given ID. |
| 125 | func 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 | } |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 132 | |
| 133 | // UpdateOptsBuilder is the interface responsible for generating the map |
| 134 | // structure for producing JSON for an Update operation. |
| 135 | type UpdateOptsBuilder interface { |
| 136 | ToPolicyUpdateMap() (map[string]interface{}, error) |
| 137 | } |
| 138 | |
| 139 | // UpdateOpts represents the options for updating an existing policy. |
| 140 | // |
| 141 | // Update operations completely replace the configuration being updated. Empty |
| 142 | // values in the update are accepted and overwrite previously specified |
| 143 | // parameters. |
| 144 | type UpdateOpts struct { |
| 145 | // Name [required] is a name for the policy. |
| 146 | Name string |
| 147 | |
| 148 | // Type [required] of policy, i.e. either "webhook" or "schedule". |
| 149 | Type Type |
| 150 | |
| 151 | // Cooldown [required] period in seconds. If you don't specify a cooldown, |
| 152 | // it will default to zero, and the policy will be configured as such. |
| 153 | Cooldown int |
| 154 | |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 155 | // AdjustmentType [requried] is the method used to change the capacity of |
| 156 | // the group, i.e. one of: Change, ChangePercent, or DesiredCapacity. |
| 157 | AdjustmentType AdjustmentType |
| 158 | |
| 159 | // AdjustmentValue [required] is the numeric value of the adjustment. For |
| 160 | // adjustments of type Change or DesiredCapacity, this will be converted to |
| 161 | // an integer. |
| 162 | AdjustmentValue float64 |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 163 | |
| 164 | // Additional configuration options for some types of policy. |
| 165 | Args map[string]interface{} |
| 166 | } |
| 167 | |
| 168 | // ToPolicyUpdateMap converts an UpdateOpts struct into a map for use as the |
| 169 | // request body in an Update request. |
| 170 | func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) { |
| 171 | if opts.Name == "" { |
| 172 | return nil, ErrNoName |
| 173 | } |
| 174 | |
| 175 | if opts.Type == Schedule && opts.Args == nil { |
| 176 | return nil, ErrNoArgs |
| 177 | } |
| 178 | |
Brad Ison | eedc8d9 | 2016-04-11 13:15:16 -0400 | [diff] [blame^] | 179 | if ok := validateType(opts.Type); !ok { |
| 180 | return nil, ErrUnknownType |
| 181 | } |
| 182 | |
| 183 | if ok := validateCooldown(opts.Cooldown); !ok { |
| 184 | return nil, ErrCooldownRange |
| 185 | } |
| 186 | |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 187 | policy := make(map[string]interface{}) |
| 188 | |
| 189 | policy["name"] = opts.Name |
| 190 | policy["type"] = opts.Type |
| 191 | policy["cooldown"] = opts.Cooldown |
| 192 | |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 193 | err := setAdjustment(opts.AdjustmentType, opts.AdjustmentValue, policy) |
| 194 | |
| 195 | if err != nil { |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 196 | return nil, err |
| 197 | } |
Brad Ison | ac037d5 | 2016-04-07 19:41:29 -0400 | [diff] [blame] | 198 | |
| 199 | if opts.Args != nil { |
| 200 | policy["args"] = opts.Args |
| 201 | } |
| 202 | |
| 203 | return policy, nil |
| 204 | } |
| 205 | |
| 206 | // Update requests the configuration of the given policy be updated. |
| 207 | func Update(client *gophercloud.ServiceClient, groupID, policyID string, opts UpdateOptsBuilder) UpdateResult { |
| 208 | var result UpdateResult |
| 209 | |
| 210 | url := updateURL(client, groupID, policyID) |
| 211 | reqBody, err := opts.ToPolicyUpdateMap() |
| 212 | |
| 213 | if err != nil { |
| 214 | result.Err = err |
| 215 | return result |
| 216 | } |
| 217 | |
| 218 | _, result.Err = client.Put(url, reqBody, nil, &gophercloud.RequestOpts{ |
| 219 | OkCodes: []int{204}, |
| 220 | }) |
| 221 | |
| 222 | return result |
| 223 | } |
Brad Ison | 124df8e | 2016-04-07 19:51:51 -0400 | [diff] [blame] | 224 | |
| 225 | // Delete requests the given policy be permanently deleted. |
| 226 | func Delete(client *gophercloud.ServiceClient, groupID, policyID string) DeleteResult { |
| 227 | var result DeleteResult |
| 228 | |
| 229 | url := deleteURL(client, groupID, policyID) |
| 230 | _, result.Err = client.Delete(url, &gophercloud.RequestOpts{ |
| 231 | OkCodes: []int{204}, |
| 232 | }) |
| 233 | |
| 234 | return result |
| 235 | } |
Brad Ison | 42f8dfb | 2016-04-07 20:26:06 -0400 | [diff] [blame] | 236 | |
| 237 | // Execute requests the given policy be executed immediately. |
| 238 | func Execute(client *gophercloud.ServiceClient, groupID, policyID string) ExecuteResult { |
| 239 | var result ExecuteResult |
| 240 | |
| 241 | url := executeURL(client, groupID, policyID) |
| 242 | _, result.Err = client.Post(url, nil, &result.Body, &gophercloud.RequestOpts{ |
| 243 | OkCodes: []int{202}, |
| 244 | }) |
| 245 | |
| 246 | return result |
| 247 | } |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 248 | |
| 249 | // Validate and set an adjustment on the given request body. |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 250 | func setAdjustment(t AdjustmentType, v float64, body map[string]interface{}) error { |
| 251 | key := string(t) |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 252 | |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 253 | switch t { |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 254 | case ChangePercent: |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 255 | body[key] = v |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 256 | |
| 257 | case Change, DesiredCapacity: |
Brad Ison | b35ef6d | 2016-04-09 16:54:57 -0400 | [diff] [blame] | 258 | body[key] = int(v) |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 259 | |
| 260 | default: |
Brad Ison | eedc8d9 | 2016-04-11 13:15:16 -0400 | [diff] [blame^] | 261 | return ErrUnknownAdjustment |
Brad Ison | 4d1a9a6 | 2016-04-07 21:31:52 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return nil |
| 265 | } |
Brad Ison | eedc8d9 | 2016-04-11 13:15:16 -0400 | [diff] [blame^] | 266 | |
| 267 | func validateType(t Type) (ok bool) { |
| 268 | switch t { |
| 269 | case Schedule, Webhook: |
| 270 | ok = true |
| 271 | return |
| 272 | |
| 273 | default: |
| 274 | ok = false |
| 275 | return |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | func validateCooldown(cooldown int) (ok bool) { |
| 280 | if cooldown < 0 || cooldown > 86400 { |
| 281 | ok = false |
| 282 | return |
| 283 | } |
| 284 | |
| 285 | ok = true |
| 286 | return |
| 287 | } |