Skip to content

Commit d4e1ff7

Browse files
committed
feat(permission): add module for interacting with RabbitMQ permissions
+ Implement functions to get, set, and delete permissions for users on specific vhosts + Add tests for permission functions + Include fixture for get_permissions API response
1 parent 3db14c3 commit d4e1ff7

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

lib/ex_rabbitmq_admin/permission.ex

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
defmodule ExRabbitMQAdmin.Permission do
2+
@moduledoc """
3+
This module contains functions for interacting with RabbitMQ permissions.
4+
"""
5+
require Logger
6+
7+
import ExRabbitMQAdmin.Options,
8+
only: [put_vhost_permissions: 0, format_error: 1]
9+
10+
@api_namespace "/api/permissions"
11+
12+
@doc """
13+
Get a list of permissions for all users.
14+
"""
15+
@spec get_permissions(client :: Tesla.Client.t()) :: {:ok, Tesla.Env.t()} | {:error, term()}
16+
def get_permissions(client), do: Tesla.get(client, @api_namespace)
17+
18+
@doc """
19+
Get list of permissions for a user on a specific vhost.
20+
"""
21+
@spec get_vhost_user_permissions(
22+
client :: Tesla.Client.t(),
23+
vhost :: String.t(),
24+
user :: String.t()
25+
) ::
26+
{:ok, Tesla.Env.t()} | {:error, term()}
27+
def get_vhost_user_permissions(client, vhost, user),
28+
do: Tesla.get(client, "#{@api_namespace}/#{vhost}/#{user}")
29+
30+
@doc """
31+
Set permissions for a user on a specific vhost.
32+
"""
33+
@spec put_vhost_user_permissions(
34+
client :: Tesla.Client.t(),
35+
vhost :: String.t(),
36+
user :: String.t(),
37+
opts :: Keyword.t()
38+
) ::
39+
{:ok, Tesla.Env.t()} | no_return()
40+
def put_vhost_user_permissions(client, vhost, user, opts) do
41+
case NimbleOptions.validate(opts, put_vhost_permissions()) do
42+
{:ok, opts} ->
43+
Tesla.put(client, "#{@api_namespace}/#{vhost}/#{user}", Enum.into(opts, %{}))
44+
45+
{:error, error} ->
46+
raise ArgumentError, format_error(error)
47+
end
48+
end
49+
50+
@doc """
51+
Delete permissions for a user on a specific vhost.
52+
"""
53+
@spec delete_vhost_user_permissions(
54+
client :: Tesla.Client.t(),
55+
vhost :: String.t(),
56+
user :: String.t()
57+
) :: {:ok, Tesla.Env.t()} | {:error, term()}
58+
def delete_vhost_user_permissions(client, vhost, user),
59+
do: Tesla.delete(client, "#{@api_namespace}/#{vhost}/#{user}")
60+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule ExRabbitmqAdmin.PermissionTest do
2+
use ExRabbitMQAdmin.TestCase, async: true
3+
alias ExRabbitMQAdmin.Permission
4+
5+
setup do
6+
mock(fn
7+
%{method: :get, url: "https://rabbitmq.example.com:5672/api/permissions"} ->
8+
%Tesla.Env{status: 200, body: read_json("get_permissions.json")}
9+
10+
%{method: :put, url: "https://rabbitmq.example.com:5672/api/permissions/my-vhost/testuser"} ->
11+
%Tesla.Env{status: 201}
12+
13+
%{
14+
method: :delete,
15+
url: "https://rabbitmq.example.com:5672/api/permissions/my-vhost/testuser"
16+
} ->
17+
%Tesla.Env{status: 204, body: ""}
18+
end)
19+
end
20+
21+
test "can get a list of permissions for all users" do
22+
assert {:ok, %Tesla.Env{status: 200, body: _body}} =
23+
Client.client() |> Permission.get_permissions()
24+
end
25+
26+
test "can set permissions for a user on a specific vhost" do
27+
assert {:ok, %Tesla.Env{status: 201}} =
28+
Client.client()
29+
|> Permission.put_vhost_user_permissions("my-vhost", "testuser",
30+
configure: ".*",
31+
write: ".*",
32+
read: ".*"
33+
)
34+
end
35+
36+
test "can delete permissions for a user on a specific vhost" do
37+
assert {:ok, %Tesla.Env{status: 204, body: ""}} =
38+
Client.client() |> Permission.delete_vhost_user_permissions("my-vhost", "testuser")
39+
end
40+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"configure": ".*",
4+
"read": ".*",
5+
"user": "guest",
6+
"vhost": "/",
7+
"write": ".*"
8+
},
9+
{
10+
"configure": ".*",
11+
"read": ".*",
12+
"user": "guest",
13+
"vhost": "my-vhost",
14+
"write": ".*"
15+
},
16+
{
17+
"configure": "(^$)",
18+
"read": "(^amq\\.direct|.*)",
19+
"user": "testuser",
20+
"vhost": "my-vhost",
21+
"write": "(^$)"
22+
},
23+
{
24+
"configure": ".*",
25+
"read": ".*",
26+
"user": "guest",
27+
"vhost": "test",
28+
"write": ".*"
29+
},
30+
{
31+
"configure": "(^amq\\.direct|.*)",
32+
"read": "(^amq\\.direct|.*)",
33+
"user": "testuser",
34+
"vhost": "test",
35+
"write": "(^amq\\.direct|.*)"
36+
}
37+
]

0 commit comments

Comments
 (0)