blob: 8c2e4be415ad6264c525fbd9193ed11ad5509c41 [file] [log] [blame]
Jamie Hannaford1ba046f2014-11-06 13:21:30 +01001package throttle
2
3import (
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.
13type 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.
19type CreateOpts struct {
20 // Required - the maximum amount of connections per IP address to allow per LB.
21 MaxConnections int
Jamie Hannafordb514bfd2014-11-10 15:39:15 +010022
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 Hannaford1ba046f2014-11-06 13:21:30 +010031}
32
33// ToCTCreateMap casts a CreateOpts struct to a map.
34func (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 Hannafordb514bfd2014-11-10 15:39:15 +010042 ct["maxConnectionRate"] = opts.MaxConnectionRate
43 ct["minConnections"] = opts.MinConnections
44 ct["rateInterval"] = opts.RateInterval
45
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010046 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.
51func 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 Hannafordb514bfd2014-11-10 15:39:15 +010064 OkCodes: []int{202},
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010065 })
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.
72func 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.
86func 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 Hannafordb514bfd2014-11-10 15:39:15 +010091 OkCodes: []int{202},
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010092 })
93
94 return res
95}