-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for user limits #217
Merged
michaelklishin
merged 2 commits into
michaelklishin:master
from
aitorpazos:Add_support_for_user_limits
Nov 22, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1116,6 +1116,14 @@ var _ = Describe("RabbitMQ HTTP API client", func() { | |
}) | ||
Ω(err).Should(BeNil()) | ||
}) | ||
It("returns all the limits", func() { | ||
xs, err := rmqc.GetAllVhostLimits() | ||
Ω(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("returns the limits", func() { | ||
xs, err := rmqc.GetVhostLimits("rabbit/hole") | ||
Ω(err).Should(BeNil()) | ||
|
@@ -1135,6 +1143,48 @@ var _ = Describe("RabbitMQ HTTP API client", func() { | |
}) | ||
}) | ||
|
||
Context("user-limits", func() { | ||
maxConnections := 1 | ||
maxChannels := 2 | ||
It("returns an empty list of limits", func() { | ||
xs, err := rmqc.GetUserLimits("guest") | ||
Ω(err).Should(BeNil()) | ||
Ω(xs).Should(HaveLen(0)) | ||
}) | ||
It("sets the limits", func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates an example inter-dependency (one cannot pass without another being run first). This is not the case with other examples. If it would makes things easier, several examples can be combined. |
||
_, err := rmqc.PutUserLimits("guest", UserLimitsValues{ | ||
"max-connections": maxConnections, | ||
"max-channels": maxChannels, | ||
}) | ||
Ω(err).Should(BeNil()) | ||
}) | ||
It("returns the limits", func() { | ||
xs, err := rmqc.GetUserLimits("guest") | ||
Ω(err).Should(BeNil()) | ||
Ω(xs).Should(HaveLen(1)) | ||
Ω(xs[0].User).Should(Equal("guest")) | ||
Ω(xs[0].Value["max-connections"]).Should(Equal(maxConnections)) | ||
Ω(xs[0].Value["max-channels"]).Should(Equal(maxChannels)) | ||
}) | ||
It("returns all the limits", func() { | ||
xs, err := rmqc.GetAllUserLimits() | ||
Ω(err).Should(BeNil()) | ||
Ω(xs).Should(HaveLen(1)) | ||
Ω(xs[0].User).Should(Equal("guest")) | ||
Ω(xs[0].Value["max-connections"]).Should(Equal(maxConnections)) | ||
Ω(xs[0].Value["max-channels"]).Should(Equal(maxChannels)) | ||
}) | ||
It("deletes the limits", func() { | ||
_, err := rmqc.DeleteUserLimits("guest", UserLimits{"max-connections", "max-channels"}) | ||
Ω(err).Should(BeNil()) | ||
}) | ||
It("returns an empty list of limits", func() { | ||
xs, err := rmqc.GetUserLimits("guest") | ||
Ω(err).Should(BeNil()) | ||
Ω(xs).Should(HaveLen(0)) | ||
}) | ||
}) | ||
|
||
Context("GET /bindings", func() { | ||
It("returns decoded response", func() { | ||
conn := openConnection("/") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package rabbithole | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// UserLimitsValues are properties used to modify virtual host limits (max-connections, max-channels) | ||
type UserLimitsValues map[string]int | ||
|
||
// UserLimits are properties used to delete virtual host limits (max-connections, max-channels) | ||
type UserLimits []string | ||
|
||
// UserLimitsInfo holds information about the current user limits | ||
type UserLimitsInfo struct { | ||
User string `json:"user"` | ||
Value UserLimitsValues `json:"value"` | ||
} | ||
|
||
// GetAllUserLimits gets all users limits. | ||
func (c *Client) GetAllUserLimits() (rec []UserLimitsInfo, err error) { | ||
req, err := newGETRequest(c, "user-limits") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = executeAndParseRequest(c, req, &rec); err != nil { | ||
return nil, err | ||
} | ||
|
||
return rec, nil | ||
} | ||
|
||
// GetUserLimits gets a user limits. | ||
func (c *Client) GetUserLimits(username string) (rec []UserLimitsInfo, err error) { | ||
req, err := newGETRequest(c, "user-limits/"+url.PathEscape(username)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = executeAndParseRequest(c, req, &rec); err != nil { | ||
return nil, err | ||
} | ||
|
||
return rec, nil | ||
} | ||
|
||
// PutUserLimits puts limits of a user. | ||
func (c *Client) PutUserLimits(username string, limits UserLimitsValues) (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", "user-limits/"+url.PathEscape(username)+"/"+limitName, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if res, err = executeRequest(c, req); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
// DeleteUserLimits deletes limits of a user. | ||
func (c *Client) DeleteUserLimits(username string, limits UserLimits) (res *http.Response, err error) { | ||
for _, limit := range limits { | ||
req, err := newRequestWithBody(c, "DELETE", "user-limits/"+url.PathEscape(username)+"/"+limit, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if res, err = executeRequest(c, req); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not follow the formatting convention used in earlier releases. When in doubt, don't touch the change log at all.