blob: 2680a892b6806f281bb912de1930f520c60b1b8d [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
Ash Wilson59fb6c42015-02-12 16:21:13 -050058 _, res.Err = c.Request("PUT", rootURL(c, lbID), gophercloud.RequestOpts{
59 JSONBody: &reqBody,
60 JSONResponse: &res.Body,
61 OkCodes: []int{202},
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010062 })
63
64 return res
65}
66
67// Get is the operation responsible for showing the details of the connection
68// throttling configuration for a load balancer.
69func Get(c *gophercloud.ServiceClient, lbID int) GetResult {
70 var res GetResult
71
Ash Wilson59fb6c42015-02-12 16:21:13 -050072 _, res.Err = c.Request("GET", rootURL(c, lbID), gophercloud.RequestOpts{
73 JSONResponse: &res.Body,
74 OkCodes: []int{200},
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010075 })
76
77 return res
78}
79
80// Delete is the operation responsible for deleting the connection throttling
81// configuration for a load balancer.
82func Delete(c *gophercloud.ServiceClient, lbID int) DeleteResult {
83 var res DeleteResult
84
Ash Wilson59fb6c42015-02-12 16:21:13 -050085 _, res.Err = c.Request("DELETE", rootURL(c, lbID), gophercloud.RequestOpts{
86 OkCodes: []int{202},
Jamie Hannaford1ba046f2014-11-06 13:21:30 +010087 })
88
89 return res
90}