Skip to content

Commit bdcd2fd

Browse files
New command: list user_connections
1 parent 45cf7dc commit bdcd2fd

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
### Enhancements
66

7+
* `list user_connections` is a new command that lists connections of a specific user:
8+
9+
```
10+
rabbitmqadmin --vhost="/" list user_connections --username "monitoring.1"
11+
12+
rabbitmqadmin --vhost="production" list user_connections --username "web.45cf7dc28"
13+
```
14+
715
* New general option `--table-style`, can be used to change output table styling.
816

917
By default, the following style is used:

src/cli.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ pub fn parser() -> Command {
310310
])
311311
}
312312

313-
fn list_subcommands() -> [Command; 18] {
313+
fn list_subcommands() -> [Command; 19] {
314314
// duplicate this very common global argument so that
315315
// it can be passed as the end of argument list
316316
let vhost_arg = Arg::new("vhost")
@@ -343,6 +343,20 @@ fn list_subcommands() -> [Command; 18] {
343343
"<bold>Doc guide</bold>: {}",
344344
CONNECTION_GUIDE_URL
345345
)),
346+
Command::new("user_connections")
347+
.arg(vhost_arg.clone())
348+
.arg(
349+
Arg::new("username")
350+
.short('u')
351+
.long("username")
352+
.required(true)
353+
.help("Name of the user whose connections to list"),
354+
)
355+
.long_about("Lists client connections that authenticated with a specific username")
356+
.after_long_help(color_print::cformat!(
357+
"<bold>Doc guide</bold>: {}",
358+
CONNECTION_GUIDE_URL
359+
)),
346360
Command::new("channels")
347361
.arg(vhost_arg.clone())
348362
.long_about("Lists AMQP 0-9-1 channels")

src/commands.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ pub fn list_connections(client: APIClient) -> ClientResult<Vec<responses::Connec
8080
client.list_connections()
8181
}
8282

83+
pub fn list_user_connections(
84+
client: APIClient,
85+
command_args: &ArgMatches,
86+
) -> ClientResult<Vec<responses::UserConnection>> {
87+
let username = command_args.get_one::<String>("username").cloned().unwrap();
88+
client.list_user_connections(&username)
89+
}
90+
8391
pub fn list_channels(client: APIClient) -> ClientResult<Vec<responses::Channel>> {
8492
client.list_channels()
8593
}

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ fn dispatch_common_subcommand(
313313
let result = commands::list_connections(client);
314314
res_handler.tabular_result(result)
315315
}
316+
("list", "user_connections") => {
317+
let result = commands::list_user_connections(client, second_level_args);
318+
res_handler.tabular_result(result)
319+
}
316320
("list", "channels") => {
317321
let result = commands::list_channels(client);
318322
res_handler.tabular_result(result)

tests/connections_tests.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (C) 2023-2025 RabbitMQ Core Team ([email protected])
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
mod test_helpers;
16+
use crate::test_helpers::*;
17+
18+
#[test]
19+
fn test_list_connections() -> Result<(), Box<dyn std::error::Error>> {
20+
run_succeeds(["list", "connections"]);
21+
22+
Ok(())
23+
}
24+
25+
#[test]
26+
fn test_list_connections_table_styles() -> Result<(), Box<dyn std::error::Error>> {
27+
run_succeeds(["--table-style", "markdown", "list", "connections"]);
28+
29+
Ok(())
30+
}
31+
32+
#[test]
33+
fn test_list_user_connections() -> Result<(), Box<dyn std::error::Error>> {
34+
run_succeeds([
35+
"--table-style",
36+
"markdown",
37+
"list",
38+
"user_connections",
39+
"--username",
40+
"monitoring",
41+
]);
42+
43+
Ok(())
44+
}

0 commit comments

Comments
 (0)