blob: 0446b97a14f5e8f6260f884046268549387da75a [file] [log] [blame]
Jamie Hannaford1ba046f2014-11-06 13:21:30 +01001package throttle
2
3import (
4 "errors"
5
Jamie Hannaford1ba046f2014-11-06 13:21:30 +01006 "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.
11type 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.
17type CreateOpts struct {
18 // Required - the maximum amount of connections per IP address to allow per LB.
19 MaxConnections int
Jamie Hannafordb514bfd2014-11-10 15:39:15 +010020
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 Hannaford1ba046f2014-11-06 13:21:30 +010029}
30
31// ToCTCreateMap casts a CreateOpts struct to a map.
32func (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 Hannafordb514bfd2014-11-10 15:39:15 +010040 ct["maxConnectionRate"] = opts.MaxConnectionRate
41 ct["minConnections"] = opts.MinConnections
42 ct["rateInterval"] = opts.RateInterval
43
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010044 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.
49func 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 Hannaford5497f942015-03-25 11:55:51 +010058 _, res.Err = c.Put(rootURL(c, lbID), reqBody, &res.Body, nil)
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010059 return res
60}
61
62// Get is the operation responsible for showing the details of the connection
63// throttling configuration for a load balancer.
64func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
65 var res GetResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010066 _, res.Err = c.Get(rootURL(c, lbID), &res.Body, nil)
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010067 return res
68}
69
70// Delete is the operation responsible for deleting the connection throttling
71// configuration for a load balancer.
72func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult {
73 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010074 _, res.Err = c.Delete(rootURL(c, lbID), nil)
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010075 return res
76}