Skip to content

Commit

Permalink
Merge pull request #200 from Sauci/vhost-limits
Browse files Browse the repository at this point in the history
Add vhost-limits API
  • Loading branch information
michaelklishin authored Jul 14, 2021
2 parents 9061c40 + e2536b7 commit a2acdb0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
34 changes: 34 additions & 0 deletions rabbithole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,40 @@ 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 limits", func() {
_, err := rmqc.PutVhostLimits("rabbit/hole", VhostLimitsValues{
"max-connections": maxConnections,
"max-queues": maxQueues,
})
Ω(err).Should(BeNil())
})
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["max-connections"]).Should(Equal(maxConnections))
Ω(xs[0].Value["max-queues"]).Should(Equal(maxQueues))
})
It("deletes the limits", func() {
_, err := rmqc.DeleteVhostLimits("rabbit/hole", VhostLimits{"max-connections", "max-queues"})
Ω(err).Should(BeNil())
})
It("returns an empty list of limits", 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
70 changes: 70 additions & 0 deletions vhost_limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package rabbithole

import (
"encoding/json"
"net/http"
"net/url"
)

// VhostLimitsValues are properties used to modify virtual host limits (max-connections, max-queues)
type VhostLimitsValues map[string]int

// VhostLimits are properties used to delete virtual host limits (max-connections, max-queues)
type VhostLimits []string

// 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 {
return nil, err
}

if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}

return rec, nil
}

// 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)+"/"+limitName, body)
if err != nil {
return nil, err
}

if res, err = executeRequest(c, req); err != nil {
return nil, err
}
}

return res, nil
}

// 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
}
}

return res, nil
}

0 comments on commit a2acdb0

Please sign in to comment.