From 68b409e823ec9f63c1f267cf5c658b518f64f6a7 Mon Sep 17 00:00:00 2001 From: lmbsog0 Date: Sun, 11 Jul 2021 10:32:22 +0200 Subject: [PATCH 1/3] Add vhost-limits API --- rabbithole_test.go | 29 ++++++++++++++++++++ vhost_limits.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 vhost_limits.go diff --git a/rabbithole_test.go b/rabbithole_test.go index db2f0c0..ab94a75 100644 --- a/rabbithole_test.go +++ b/rabbithole_test.go @@ -989,6 +989,35 @@ var _ = Describe("RabbitMQ HTTP API client", func() { }) }) + Context("vhost-limits", func() { + maxConnections := 1 + It("returns an empty list of limits", func() { + xs, err := rmqc.GetVhostLimits("rabbit/hole") + Ω(err).Should(BeNil()) + Ω(xs).Should(HaveLen(0)) + }) + It("sets the max-connections limit", func() { + _, err := rmqc.PutVhostLimitsMaxConnections("rabbit/hole", maxConnections) + Ω(err).Should(BeNil()) + }) + It("returns the max-connections limit", func() { + xs, err := rmqc.GetVhostLimits("rabbit/hole") + Ω(err).Should(BeNil()) + Ω(xs).Should(HaveLen(1)) + Ω(xs[0].Vhost).Should(Equal("rabbit/hole")) + Ω(xs[0].Value.MaxConnections).Should(Equal(maxConnections)) + }) + It("deletes the max-connections limit", func() { + _, err := rmqc.DeleteVhostLimitsMaxConnections("rabbit/hole") + Ω(err).Should(BeNil()) + }) + It("returns the max-connections limit", func() { + xs, err := rmqc.GetVhostLimits("rabbit/hole") + Ω(err).Should(BeNil()) + Ω(xs).Should(HaveLen(0)) + }) + }) + Context("GET /bindings", func() { It("returns decoded response", func() { conn := openConnection("/") diff --git a/vhost_limits.go b/vhost_limits.go new file mode 100644 index 0000000..78cce2d --- /dev/null +++ b/vhost_limits.go @@ -0,0 +1,66 @@ +package rabbithole + +import ( + "encoding/json" + "net/http" + "net/url" +) + +// VhostLimitsValue are properties used to modify virtual hosts limits. +type VhostLimitsValue struct { + // Maximum number of connections + MaxConnections int `json:"max-connections"` +} + +type VhostLimitsInfo struct { + Vhost string `json:"vhost"` + Value VhostLimitsValue `json:"value"` +} + +type VhostLimitsMaxConnections struct { + Value int `json:"value"` +} + +func (c *Client) GetVhostLimits(vhostname string) (rec []VhostLimitsInfo, err error) { + req, err := newGETRequest(c, "vhost-limits/"+url.PathEscape(vhostname)) + if err != nil { + return nil, err + } + + if err = executeAndParseRequest(c, req, &rec); err != nil { + return nil, err + } + + return rec, nil +} + +func (c *Client) PutVhostLimitsMaxConnections(vhostname string, maxConnections int) (res *http.Response, err error) { + body, err := json.Marshal(VhostLimitsMaxConnections{Value: maxConnections}) + if err != nil { + return nil, err + } + + req, err := newRequestWithBody(c, "PUT", "vhost-limits/"+url.PathEscape(vhostname)+"/max-connections", body) + if err != nil { + return nil, err + } + + if res, err = executeRequest(c, req); err != nil { + return nil, err + } + + return res, nil +} + +func (c *Client) DeleteVhostLimitsMaxConnections(vhostname string) (res *http.Response, err error) { + req, err := newRequestWithBody(c, "DELETE", "vhost-limits/"+url.PathEscape(vhostname)+"/max-connections", nil) + if err != nil { + return nil, err + } + + if res, err = executeRequest(c, req); err != nil { + return nil, err + } + + return res, nil +} From f6962f2957b1d6997bbd44cf3c0780b42b54faf6 Mon Sep 17 00:00:00 2001 From: lmbsog0 Date: Mon, 12 Jul 2021 08:09:25 +0200 Subject: [PATCH 2/3] use map-like parameter to pass the limits --- rabbithole_test.go | 19 ++++++++------ vhost_limits.go | 62 ++++++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/rabbithole_test.go b/rabbithole_test.go index ab94a75..c5151a0 100644 --- a/rabbithole_test.go +++ b/rabbithole_test.go @@ -991,27 +991,32 @@ var _ = Describe("RabbitMQ HTTP API client", func() { Context("vhost-limits", func() { maxConnections := 1 + maxQueues := 2 It("returns an empty list of limits", func() { xs, err := rmqc.GetVhostLimits("rabbit/hole") Ω(err).Should(BeNil()) Ω(xs).Should(HaveLen(0)) }) - It("sets the max-connections limit", func() { - _, err := rmqc.PutVhostLimitsMaxConnections("rabbit/hole", maxConnections) + It("sets the limits", func() { + _, err := rmqc.PutVhostLimits("rabbit/hole", VhostLimitsValues{ + "max-connections":maxConnections, + "max-queues":maxQueues, + }) Ω(err).Should(BeNil()) }) - It("returns the max-connections limit", func() { + It("returns the limits", func() { xs, err := rmqc.GetVhostLimits("rabbit/hole") Ω(err).Should(BeNil()) Ω(xs).Should(HaveLen(1)) Ω(xs[0].Vhost).Should(Equal("rabbit/hole")) - Ω(xs[0].Value.MaxConnections).Should(Equal(maxConnections)) + Ω(xs[0].Value["max-connections"]).Should(Equal(maxConnections)) + Ω(xs[0].Value["max-queues"]).Should(Equal(maxQueues)) }) - It("deletes the max-connections limit", func() { - _, err := rmqc.DeleteVhostLimitsMaxConnections("rabbit/hole") + It("deletes the limits", func() { + _, err := rmqc.DeleteVhostLimits("rabbit/hole", VhostLimits{"max-connections", "max-queues"}) Ω(err).Should(BeNil()) }) - It("returns the max-connections limit", func() { + It("returns an empty list of limits", func() { xs, err := rmqc.GetVhostLimits("rabbit/hole") Ω(err).Should(BeNil()) Ω(xs).Should(HaveLen(0)) diff --git a/vhost_limits.go b/vhost_limits.go index 78cce2d..126798e 100644 --- a/vhost_limits.go +++ b/vhost_limits.go @@ -6,21 +6,19 @@ import ( "net/url" ) -// VhostLimitsValue are properties used to modify virtual hosts limits. -type VhostLimitsValue struct { - // Maximum number of connections - MaxConnections int `json:"max-connections"` -} +// VhostLimitsValues are properties used to modify virtual host limits (max-connections, max-queues) +type VhostLimitsValues map[string]int -type VhostLimitsInfo struct { - Vhost string `json:"vhost"` - Value VhostLimitsValue `json:"value"` -} +// VhostLimits are properties used to delete virtual host limits (max-connections, max-queues) +type VhostLimits []string -type VhostLimitsMaxConnections struct { - Value int `json:"value"` +// VhostLimitsInfo holds information about the current virtual host limits +type VhostLimitsInfo struct { + Vhost string `json:"vhost"` + Value VhostLimitsValues `json:"value"` } +// GetVhostLimits gets a virtual host limits. func (c *Client) GetVhostLimits(vhostname string) (rec []VhostLimitsInfo, err error) { req, err := newGETRequest(c, "vhost-limits/"+url.PathEscape(vhostname)) if err != nil { @@ -34,32 +32,38 @@ func (c *Client) GetVhostLimits(vhostname string) (rec []VhostLimitsInfo, err er return rec, nil } -func (c *Client) PutVhostLimitsMaxConnections(vhostname string, maxConnections int) (res *http.Response, err error) { - body, err := json.Marshal(VhostLimitsMaxConnections{Value: maxConnections}) - if err != nil { - return nil, err - } +// PutVhostLimits puts limits of a virtual host. +func (c *Client) PutVhostLimits(vhostname string, limits VhostLimitsValues) (res *http.Response, err error) { + for limitName, limitValue := range limits { + body, err := json.Marshal(struct{Value int `json:"value"`}{Value: limitValue}) + if err != nil { + return nil, err + } - req, err := newRequestWithBody(c, "PUT", "vhost-limits/"+url.PathEscape(vhostname)+"/max-connections", body) - if err != nil { - return nil, err - } + req, err := newRequestWithBody(c, "PUT", "vhost-limits/"+url.PathEscape(vhostname)+"/"+limitName, body) + if err != nil { + return nil, err + } - if res, err = executeRequest(c, req); err != nil { - return nil, err + if res, err = executeRequest(c, req); err != nil { + return nil, err + } } return res, nil } -func (c *Client) DeleteVhostLimitsMaxConnections(vhostname string) (res *http.Response, err error) { - req, err := newRequestWithBody(c, "DELETE", "vhost-limits/"+url.PathEscape(vhostname)+"/max-connections", nil) - if err != nil { - return nil, err - } +// DeleteVhostLimits deletes limits of a virtual host. +func (c *Client) DeleteVhostLimits(vhostname string, limits VhostLimits) (res *http.Response, err error) { + for _, limit := range limits { + req, err := newRequestWithBody(c, "DELETE", "vhost-limits/"+url.PathEscape(vhostname)+"/"+limit, nil) + if err != nil { + return nil, err + } - if res, err = executeRequest(c, req); err != nil { - return nil, err + if res, err = executeRequest(c, req); err != nil { + return nil, err + } } return res, nil From e2536b772e033800d05792dcda6ccffe5f62740d Mon Sep 17 00:00:00 2001 From: lmbsog0 Date: Mon, 12 Jul 2021 12:50:18 +0200 Subject: [PATCH 3/3] format code --- rabbithole_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rabbithole_test.go b/rabbithole_test.go index c5151a0..d6cf840 100644 --- a/rabbithole_test.go +++ b/rabbithole_test.go @@ -999,8 +999,8 @@ var _ = Describe("RabbitMQ HTTP API client", func() { }) It("sets the limits", func() { _, err := rmqc.PutVhostLimits("rabbit/hole", VhostLimitsValues{ - "max-connections":maxConnections, - "max-queues":maxQueues, + "max-connections": maxConnections, + "max-queues": maxQueues, }) Ω(err).Should(BeNil()) })