Skip to content

Commit 62d3d98

Browse files
New command group: users
1 parent 9b63a8c commit 62d3d98

File tree

4 files changed

+178
-26
lines changed

4 files changed

+178
-26
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.rs

Lines changed: 126 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ pub fn parser(pre_flight_settings: PreFlightSettings) -> Command {
216216
))
217217
.subcommand_value_name("subcommand")
218218
.subcommands(tanzu_subcommands());
219+
let users_group = Command::new("users")
220+
.about("Operations on users")
221+
.infer_subcommands(pre_flight_settings.infer_subcommands)
222+
.infer_long_args(pre_flight_settings.infer_long_options)
223+
.after_help(color_print::cformat!(
224+
"<bold>Doc guide</bold>: {}",
225+
ACCESS_CONTROL_GUIDE_URL
226+
))
227+
.subcommand_value_name("subcommand")
228+
.subcommands(users_subcommands(pre_flight_settings.clone()));
219229
let vhosts_group = Command::new("vhosts")
220230
.about("Virtual host operations")
221231
.infer_subcommands(pre_flight_settings.infer_subcommands)
@@ -243,6 +253,7 @@ pub fn parser(pre_flight_settings: PreFlightSettings) -> Command {
243253
show_group,
244254
shovels_group,
245255
tanzu_group,
256+
users_group,
246257
vhosts_group,
247258
];
248259

@@ -434,39 +445,24 @@ pub fn parser(pre_flight_settings: PreFlightSettings) -> Command {
434445

435446
fn list_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 19] {
436447
let nodes_cmd = Command::new("nodes").long_about("Lists cluster members");
437-
let users_cmd = Command::new("users").long_about("Lists users in the internal database");
438448
let vhosts_cmd = Command::new("vhosts")
439449
.long_about("Lists virtual hosts")
440450
.after_help(color_print::cformat!(
441451
"<bold>Doc guide</bold>: {}",
442452
VIRTUAL_HOST_GUIDE_URL
443453
));
444-
445-
let permissions_cmd = Command::new("permissions")
446-
.long_about("Lists user permissions")
454+
let vhost_limits_cmd = Command::new("vhost_limits")
455+
.long_about("Lists virtual host (resource) limits")
447456
.after_help(color_print::cformat!(
448457
"<bold>Doc guide</bold>: {}",
449-
ACCESS_CONTROL_GUIDE_URL
458+
VIRTUAL_HOST_GUIDE_URL
450459
));
451460
let connections_cmd = Command::new("connections")
452461
.long_about("Lists client connections")
453462
.after_help(color_print::cformat!(
454463
"<bold>Doc guide</bold>: {}",
455464
CONNECTION_GUIDE_URL
456465
));
457-
let user_connections_cmd = Command::new("user_connections")
458-
.arg(
459-
Arg::new("username")
460-
.short('u')
461-
.long("username")
462-
.required(true)
463-
.help("Name of the user whose connections to list"),
464-
)
465-
.long_about("Lists client connections that authenticated with a specific username")
466-
.after_help(color_print::cformat!(
467-
"<bold>Doc guide</bold>: {}",
468-
CONNECTION_GUIDE_URL
469-
));
470466
let channels_cmd = Command::new("channels")
471467
.long_about("Lists AMQP 0-9-1 channels")
472468
.after_help(color_print::cformat!(
@@ -511,11 +507,25 @@ fn list_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 19] {
511507
"<bold>Doc guide</bold>: {}",
512508
OPERATOR_POLICY_GUIDE_URL
513509
));
514-
let vhost_limits_cmd = Command::new("vhost_limits")
515-
.long_about("Lists virtual host (resource) limits")
510+
let users_cmd = Command::new("users").long_about("Lists users in the internal database");
511+
let permissions_cmd = Command::new("permissions")
512+
.long_about("Lists user permissions")
516513
.after_help(color_print::cformat!(
517514
"<bold>Doc guide</bold>: {}",
518-
VIRTUAL_HOST_GUIDE_URL
515+
ACCESS_CONTROL_GUIDE_URL
516+
));
517+
let user_connections_cmd = Command::new("user_connections")
518+
.arg(
519+
Arg::new("username")
520+
.short('u')
521+
.long("username")
522+
.required(true)
523+
.help("Name of the user whose connections to list"),
524+
)
525+
.long_about("Lists client connections that authenticated with a specific username")
526+
.after_help(color_print::cformat!(
527+
"<bold>Doc guide</bold>: {}",
528+
CONNECTION_GUIDE_URL
519529
));
520530
let user_limits_cmd = Command::new("user_limits")
521531
.arg(
@@ -1719,6 +1729,101 @@ pub fn vhosts_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 3
17191729
.map(|cmd| cmd.infer_long_args(pre_flight_settings.infer_long_options))
17201730
}
17211731

1732+
pub fn users_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 6] {
1733+
let declare_cmd = Command::new("declare")
1734+
.about("Creates a user")
1735+
.arg(
1736+
Arg::new("name")
1737+
.long("name")
1738+
.help("username")
1739+
.required(true),
1740+
)
1741+
.arg(
1742+
Arg::new("password_hash")
1743+
.help(color_print::cformat!(
1744+
"salted password hash, see {}",
1745+
PASSWORD_GUIDE_URL
1746+
))
1747+
.long("password-hash")
1748+
.required(false)
1749+
.default_value(""),
1750+
)
1751+
.arg(
1752+
Arg::new("password")
1753+
.long("password")
1754+
.help(color_print::cformat!(
1755+
"prefer providing a hash, see {}",
1756+
PASSWORD_GUIDE_URL
1757+
))
1758+
.required(false)
1759+
.default_value(""),
1760+
)
1761+
.arg(
1762+
Arg::new("tags")
1763+
.long("tags")
1764+
.help("a list of comma-separated tags")
1765+
.default_value(""),
1766+
);
1767+
let list_cmd = Command::new("list").long_about("Lists users in the internal database");
1768+
let permissions_cmd = Command::new("permissions")
1769+
.long_about("Lists user permissions")
1770+
.after_help(color_print::cformat!(
1771+
"<bold>Doc guide</bold>: {}",
1772+
ACCESS_CONTROL_GUIDE_URL
1773+
));
1774+
let connections_cmd = Command::new("connections")
1775+
.arg(
1776+
Arg::new("username")
1777+
.short('u')
1778+
.long("username")
1779+
.required(true)
1780+
.help("Name of the user whose connections to list"),
1781+
)
1782+
.long_about("Lists client connections that authenticated with a specific username")
1783+
.after_help(color_print::cformat!(
1784+
"<bold>Doc guide</bold>: {}",
1785+
CONNECTION_GUIDE_URL
1786+
));
1787+
let limits_cmd = Command::new("limits")
1788+
.arg(
1789+
Arg::new("user")
1790+
.long("user")
1791+
.help("username")
1792+
.required(false),
1793+
)
1794+
.long_about("Lists per-user (resource) limits")
1795+
.after_help(color_print::cformat!(
1796+
"<bold>Doc guide</bold>: {}",
1797+
USER_LIMIT_GUIDE_URL
1798+
));
1799+
1800+
let idempotently_arg = Arg::new("idempotently")
1801+
.long("idempotently")
1802+
.value_parser(value_parser!(bool))
1803+
.action(ArgAction::SetTrue)
1804+
.help("do not consider 404 Not Found API responses to be errors")
1805+
.required(false);
1806+
let delete_cmd = Command::new("delete")
1807+
.about("Deletes a user")
1808+
.arg(
1809+
Arg::new("name")
1810+
.long("name")
1811+
.help("username")
1812+
.required(true),
1813+
)
1814+
.arg(idempotently_arg.clone());
1815+
1816+
[
1817+
connections_cmd,
1818+
declare_cmd,
1819+
delete_cmd,
1820+
limits_cmd,
1821+
list_cmd,
1822+
permissions_cmd,
1823+
]
1824+
.map(|cmd| cmd.infer_long_args(pre_flight_settings.infer_long_options))
1825+
}
1826+
17221827
pub fn publish_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 1] {
17231828
[Command::new("message")
17241829
.about(color_print::cstr!("Publishes (<red>inefficiently</red>) message(s) to a queue or a stream. <bold><red>Only suitable for development and test environments</red></bold>. Prefer messaging or streaming protocol clients!"))

src/main.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ fn dispatch_common_subcommand(
299299
let result = commands::show_memory_breakdown(client, second_level_args);
300300
res_handler.memory_breakdown_in_bytes_result(result)
301301
}
302-
303302
("list", "nodes") => {
304303
let result = commands::list_nodes(client);
305304
res_handler.tabular_result(result)
@@ -488,6 +487,30 @@ fn dispatch_common_subcommand(
488487
let result = commands::delete_vhost(client, second_level_args);
489488
res_handler.delete_operation_result(result);
490489
}
490+
("users", "declare") => {
491+
let result = commands::declare_user(client, second_level_args);
492+
res_handler.no_output_on_success(result);
493+
}
494+
("users", "list") => {
495+
let result = commands::list_users(client);
496+
res_handler.tabular_result(result)
497+
}
498+
("users", "delete") => {
499+
let result = commands::delete_user(client, second_level_args);
500+
res_handler.delete_operation_result(result);
501+
}
502+
("users", "permissions") => {
503+
let result = commands::list_permissions(client);
504+
res_handler.tabular_result(result)
505+
}
506+
("users", "connections") => {
507+
let result = commands::list_user_connections(client, second_level_args);
508+
res_handler.tabular_result(result)
509+
}
510+
("users", "limits") => {
511+
let result = commands::list_user_limits(client, second_level_args);
512+
res_handler.tabular_result(result)
513+
}
491514
("nodes", "list") => {
492515
let result = commands::list_nodes(client);
493516
res_handler.tabular_result(result)

tests/users_tests.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::test_helpers::*;
1919

2020
#[test]
2121
fn test_list_users() -> Result<(), Box<dyn std::error::Error>> {
22-
let username = "new_user";
22+
let username = "test_list_users";
2323
let password = "pa$$w0rd";
2424
run_succeeds([
2525
"declare",
@@ -32,15 +32,38 @@ fn test_list_users() -> Result<(), Box<dyn std::error::Error>> {
3232

3333
run_succeeds(["list", "users"]).stdout(predicate::str::contains(username));
3434
run_succeeds(["delete", "user", "--name", username]);
35+
run_succeeds(["delete", "user", "--name", username, "--idempotently"]);
3536

3637
run_succeeds(["list", "users"]).stdout(predicate::str::contains(username).not());
3738

3839
Ok(())
3940
}
4041

42+
#[test]
43+
fn test_users_list() -> Result<(), Box<dyn std::error::Error>> {
44+
let username = "test_users_list.2";
45+
let password = "pa$$w0rd";
46+
run_succeeds([
47+
"users",
48+
"declare",
49+
"--name",
50+
username,
51+
"--password",
52+
password,
53+
]);
54+
55+
run_succeeds(["users", "list"]).stdout(predicate::str::contains(username));
56+
run_succeeds(["users", "delete", "--name", username]);
57+
run_succeeds(["users", "delete", "--name", username, "--idempotently"]);
58+
59+
run_succeeds(["users", "list"]).stdout(predicate::str::contains(username).not());
60+
61+
Ok(())
62+
}
63+
4164
#[test]
4265
fn test_list_users_with_table_styles() -> Result<(), Box<dyn std::error::Error>> {
43-
let username = "new_user";
66+
let username = "test_list_users_with_table_styles";
4467
let password = "pa$$w0rd";
4568
run_succeeds([
4669
"declare",
@@ -53,6 +76,7 @@ fn test_list_users_with_table_styles() -> Result<(), Box<dyn std::error::Error>>
5376

5477
run_succeeds(["--table-style", "markdown", "list", "users"])
5578
.stdout(predicate::str::contains(username));
79+
run_succeeds(["delete", "user", "--name", username]);
5680
run_succeeds(["delete", "user", "--name", username, "--idempotently"]);
5781

5882
run_succeeds(["--table-style", "borderless", "list", "users"])

0 commit comments

Comments
 (0)