Skip to content

Commit

Permalink
Add vhost-limits API
Browse files Browse the repository at this point in the history
  • Loading branch information
lmbsog0 committed Jul 11, 2021
1 parent 9061c40 commit ff52d0a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
29 changes: 29 additions & 0 deletions rabbithole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand Down
68 changes: 68 additions & 0 deletions vhost_limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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"`
// Maximum number of queues
MaxQueues int `json:"max-queues"`
}

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
}

0 comments on commit ff52d0a

Please sign in to comment.