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 |
| 22 | } |
| 23 | |
| 24 | // ToCTCreateMap casts a CreateOpts struct to a map. |
| 25 | func (opts CreateOpts) ToCTCreateMap() (map[string]interface{}, error) { |
| 26 | ct := make(map[string]interface{}) |
| 27 | |
| 28 | if opts.MaxConnections < 0 || opts.MaxConnections > 100000 { |
| 29 | return ct, errors.New("MaxConnections must be an int between 0 and 100000") |
| 30 | } |
| 31 | |
| 32 | ct["maxConnections"] = opts.MaxConnections |
| 33 | return map[string]interface{}{"connectionThrottle": ct}, nil |
| 34 | } |
| 35 | |
| 36 | // Create is the operation responsible for creating or updating the connection |
| 37 | // throttling configuration for a load balancer. |
| 38 | func Create(c *gophercloud.ServiceClient, lbID int, opts CreateOptsBuilder) CreateResult { |
| 39 | var res CreateResult |
| 40 | |
| 41 | reqBody, err := opts.ToCTCreateMap() |
| 42 | if err != nil { |
| 43 | res.Err = err |
| 44 | return res |
| 45 | } |
| 46 | |
| 47 | _, res.Err = perigee.Request("PUT", rootURL(c, lbID), perigee.Options{ |
| 48 | MoreHeaders: c.AuthenticatedHeaders(), |
| 49 | ReqBody: &reqBody, |
| 50 | Results: &res.Body, |
| 51 | OkCodes: []int{200}, |
| 52 | }) |
| 53 | |
| 54 | return res |
| 55 | } |
| 56 | |
| 57 | // Get is the operation responsible for showing the details of the connection |
| 58 | // throttling configuration for a load balancer. |
| 59 | func Get(c *gophercloud.ServiceClient, lbID int) GetResult { |
| 60 | var res GetResult |
| 61 | |
| 62 | _, res.Err = perigee.Request("GET", rootURL(c, lbID), perigee.Options{ |
| 63 | MoreHeaders: c.AuthenticatedHeaders(), |
| 64 | Results: &res.Body, |
| 65 | OkCodes: []int{200}, |
| 66 | }) |
| 67 | |
| 68 | return res |
| 69 | } |
| 70 | |
| 71 | // Delete is the operation responsible for deleting the connection throttling |
| 72 | // configuration for a load balancer. |
| 73 | func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult { |
| 74 | var res DeleteResult |
| 75 | |
| 76 | _, res.Err = perigee.Request("DELETE", rootURL(c, lbID), perigee.Options{ |
| 77 | MoreHeaders: c.AuthenticatedHeaders(), |
| 78 | OkCodes: []int{200}, |
| 79 | }) |
| 80 | |
| 81 | return res |
| 82 | } |