Jamie Hannaford | 1ba046f | 2014-11-06 13:21:30 +0100 | [diff] [blame] | 1 | package throttle |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Jamie Hannaford | 1ba046f | 2014-11-06 13:21:30 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | ) |
| 8 | |
| 9 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 10 | // to be used in the main Create operation in this package. |
| 11 | type CreateOptsBuilder interface { |
| 12 | ToCTCreateMap() (map[string]interface{}, error) |
| 13 | } |
| 14 | |
| 15 | // CreateOpts is the common options struct used in this package's Create |
| 16 | // operation. |
| 17 | type CreateOpts struct { |
| 18 | // Required - the maximum amount of connections per IP address to allow per LB. |
| 19 | MaxConnections int |
Jamie Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 20 | |
| 21 | // Deprecated as of v1.22. |
| 22 | MaxConnectionRate int |
| 23 | |
| 24 | // Deprecated as of v1.22. |
| 25 | MinConnections int |
| 26 | |
| 27 | // Deprecated as of v1.22. |
| 28 | RateInterval int |
Jamie Hannaford | 1ba046f | 2014-11-06 13:21:30 +0100 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // ToCTCreateMap casts a CreateOpts struct to a map. |
| 32 | func (opts CreateOpts) ToCTCreateMap() (map[string]interface{}, error) { |
| 33 | ct := make(map[string]interface{}) |
| 34 | |
| 35 | if opts.MaxConnections < 0 || opts.MaxConnections > 100000 { |
| 36 | return ct, errors.New("MaxConnections must be an int between 0 and 100000") |
| 37 | } |
| 38 | |
| 39 | ct["maxConnections"] = opts.MaxConnections |
Jamie Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 40 | ct["maxConnectionRate"] = opts.MaxConnectionRate |
| 41 | ct["minConnections"] = opts.MinConnections |
| 42 | ct["rateInterval"] = opts.RateInterval |
| 43 | |
Jamie Hannaford | 1ba046f | 2014-11-06 13:21:30 +0100 | [diff] [blame] | 44 | return map[string]interface{}{"connectionThrottle": ct}, nil |
| 45 | } |
| 46 | |
| 47 | // Create is the operation responsible for creating or updating the connection |
| 48 | // throttling configuration for a load balancer. |
| 49 | func Create(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) CreateResult { |
| 50 | var res CreateResult |
| 51 | |
| 52 | reqBody, err := opts.ToCTCreateMap() |
| 53 | if err != nil { |
| 54 | res.Err = err |
| 55 | return res |
| 56 | } |
| 57 | |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 58 | _, res.Err = c.Put(rootURL(c, lbID), reqBody, &res.Body, nil) |
Jamie Hannaford | 1ba046f | 2014-11-06 13:21:30 +0100 | [diff] [blame] | 59 | return res |
| 60 | } |
| 61 | |
| 62 | // Get is the operation responsible for showing the details of the connection |
| 63 | // throttling configuration for a load balancer. |
| 64 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 65 | var res GetResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 66 | _, res.Err = c.Get(rootURL(c, lbID), &res.Body, nil) |
Jamie Hannaford | 1ba046f | 2014-11-06 13:21:30 +0100 | [diff] [blame] | 67 | return res |
| 68 | } |
| 69 | |
| 70 | // Delete is the operation responsible for deleting the connection throttling |
| 71 | // configuration for a load balancer. |
| 72 | func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult { |
| 73 | var res DeleteResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 74 | _, res.Err = c.Delete(rootURL(c, lbID), nil) |
Jamie Hannaford | 1ba046f | 2014-11-06 13:21:30 +0100 | [diff] [blame] | 75 | return res |
| 76 | } |