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