Skip to content

Commit ca60f13

Browse files
New command category: 'policies'
1 parent 0e02525 commit ca60f13

File tree

4 files changed

+129
-13
lines changed

4 files changed

+129
-13
lines changed

CHANGELOG.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,24 @@
22

33
## v0.26.0 (in development)
44

5-
No changes yet.
5+
### Enhancements
6+
7+
* `policies` is a new command group for policy operations:
8+
9+
```shell
10+
rabbitmqadmin help policies
11+
12+
# an equivalent of 'declare policy'
13+
rabbitmqadmin policies declare --name "policy-name" --pattern '^matching\..+' --apply-to "quorum_queues" \
14+
--priority 10 \
15+
--definition '{"max-length": 10000}'
16+
17+
# an equivalent of 'list policies'
18+
rabbitmqadmin policies list
19+
20+
# an equivalent of 'delete policy'
21+
rabbitmqadmin policies delete --name "policy-name"
22+
```
623

724

825
## v0.25.0 (Mar 2, 2025)

src/cli.rs

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use super::tanzu_cli::tanzu_subcommands;
1919
use crate::output::TableStyle;
2020
use clap::{Arg, ArgAction, ArgGroup, Command, value_parser};
2121
use rabbitmq_http_client::commons::{
22-
BindingDestinationType, ExchangeType, QueueType, ShovelAcknowledgementMode, SupportedProtocol,
22+
BindingDestinationType, ExchangeType, PolicyTarget, QueueType, ShovelAcknowledgementMode,
23+
SupportedProtocol,
2324
};
2425

2526
pub fn parser() -> Command {
@@ -228,6 +229,10 @@ pub fn parser() -> Command {
228229
.about("purges queues")
229230
.subcommand_value_name("queue")
230231
.subcommands(purge_subcommands()),
232+
Command::new("policies")
233+
.about("operations on policies")
234+
.subcommand_value_name("policy")
235+
.subcommands(policies_subcommands()),
231236
Command::new("health_check")
232237
.about("runs health checks")
233238
.subcommand_value_name("check")
@@ -597,7 +602,7 @@ fn declare_subcommands() -> [Command; 12] {
597602
Arg::new("type")
598603
.long("type")
599604
.help("exchange type")
600-
.value_parser(clap::value_parser!(ExchangeType))
605+
.value_parser(clap::value_parser!(ExchangeType))
601606
.required(false),
602607
)
603608
.arg(
@@ -666,10 +671,10 @@ fn declare_subcommands() -> [Command; 12] {
666671
.help("parameter's name")
667672
.required(true)
668673
).arg(
669-
Arg::new("component")
670-
.long("component")
671-
.help("component (eg. federation)")
672-
.required(true))
674+
Arg::new("component")
675+
.long("component")
676+
.help("component (eg. federation)")
677+
.required(true))
673678
.arg(
674679
Arg::new("value")
675680
.long("value")
@@ -687,13 +692,14 @@ fn declare_subcommands() -> [Command; 12] {
687692
.arg(
688693
Arg::new("pattern")
689694
.long("pattern")
690-
.help("queue/exchange name pattern")
695+
.help("the pattern that is used to match entity (queue, stream, exchange) names")
691696
.required(true),
692697
)
693698
.arg(
694699
Arg::new("apply_to")
695700
.long("apply-to")
696701
.help("entities to apply to (queues, classic_queues, quorum_queues, streams, exchanges, all)")
702+
.value_parser(clap::value_parser!(PolicyTarget))
697703
.required(true),
698704
)
699705
.arg(
@@ -993,6 +999,60 @@ fn purge_subcommands() -> [Command; 1] {
993999
)]
9941000
}
9951001

1002+
fn policies_subcommands() -> [Command; 3] {
1003+
let declare_cmd = Command::new("declare")
1004+
.about("creates or updates a policy")
1005+
.after_long_help(color_print::cformat!("<bold>Doc guide:</bold>: {}", POLICY_GUIDE_URL))
1006+
.arg(
1007+
Arg::new("name")
1008+
.long("name")
1009+
.help("policy name")
1010+
.required(true),
1011+
)
1012+
.arg(
1013+
Arg::new("pattern")
1014+
.long("pattern")
1015+
.help("the pattern that is used to match entity (queue, stream, exchange) names")
1016+
.required(true),
1017+
)
1018+
.arg(
1019+
Arg::new("apply_to")
1020+
.long("apply-to")
1021+
.help("entities to apply to (queues, classic_queues, quorum_queues, streams, exchanges, all)")
1022+
.value_parser(clap::value_parser!(PolicyTarget))
1023+
.required(true),
1024+
)
1025+
.arg(
1026+
Arg::new("priority")
1027+
.long("priority")
1028+
.help("policy priority (only the policy with the highest priority is effective)")
1029+
.required(false)
1030+
.default_value("0"),
1031+
)
1032+
.arg(
1033+
Arg::new("definition")
1034+
.long("definition")
1035+
.help("policy definition")
1036+
.required(true),
1037+
);
1038+
1039+
let list_cmd = Command::new("list")
1040+
.long_about("lists policies")
1041+
.after_long_help(color_print::cformat!(
1042+
"<bold>Doc guide</bold>: {}",
1043+
POLICY_GUIDE_URL
1044+
));
1045+
1046+
let delete_cmd = Command::new("delete").about("deletes a policy").arg(
1047+
Arg::new("name")
1048+
.long("name")
1049+
.help("policy name")
1050+
.required(true),
1051+
);
1052+
1053+
[declare_cmd, list_cmd, delete_cmd]
1054+
}
1055+
9961056
fn health_check_subcommands() -> [Command; 6] {
9971057
let node_is_quorum_critical_after_help = color_print::cformat!(
9981058
r#"
@@ -1178,14 +1238,14 @@ fn import_subcommands() -> [Command; 1] {
11781238

11791239
pub fn feature_flags_subcommands() -> [Command; 3] {
11801240
let list_cmd = Command::new("list")
1181-
.long_about("Lists feature flags and their cluster state")
1241+
.long_about("lists feature flags and their cluster state")
11821242
.after_long_help(color_print::cformat!(
11831243
"<bold>Doc guide</bold>: {}",
11841244
FEATURE_FLAG_GUIDE_URL
11851245
));
11861246

11871247
let enable_cmd = Command::new("enable")
1188-
.long_about("Enables a feature flag")
1248+
.long_about("enables a feature flag")
11891249
.after_long_help(color_print::cformat!(
11901250
"<bold>Doc guide</bold>: {}",
11911251
FEATURE_FLAG_GUIDE_URL
@@ -1198,7 +1258,7 @@ pub fn feature_flags_subcommands() -> [Command; 3] {
11981258
);
11991259

12001260
let enable_all_cmd = Command::new("enable_all")
1201-
.long_about("Enables all stable feature flags")
1261+
.long_about("enables all stable feature flags")
12021262
.after_long_help(color_print::cformat!(
12031263
"<bold>Doc guide</bold>: {}",
12041264
FEATURE_FLAG_GUIDE_URL
@@ -1209,14 +1269,14 @@ pub fn feature_flags_subcommands() -> [Command; 3] {
12091269

12101270
pub fn deprecated_features_subcommands() -> [Command; 2] {
12111271
let list_cmd = Command::new("list")
1212-
.long_about("Lists deprecated features")
1272+
.long_about("lists deprecated features")
12131273
.after_long_help(color_print::cformat!(
12141274
"<bold>Doc guide</bold>: {}",
12151275
DEPRECATED_FEATURE_GUIDE_URL
12161276
));
12171277

12181278
let list_in_use_cmd = Command::new("list_used")
1219-
.long_about("Lists the deprecated features that are found to be in use in the cluster")
1279+
.long_about("lists the deprecated features that are found to be in use in the cluster")
12201280
.after_long_help(color_print::cformat!(
12211281
"<bold>Doc guide</bold>: {}",
12221282
DEPRECATED_FEATURE_GUIDE_URL

src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,18 @@ fn dispatch_common_subcommand(
470470
let result = commands::purge_queue(client, &vhost, second_level_args);
471471
res_handler.no_output_on_success(result);
472472
}
473+
("policies", "declare") => {
474+
let result = commands::declare_policy(client, &vhost, second_level_args);
475+
res_handler.no_output_on_success(result);
476+
}
477+
("policies", "list") => {
478+
let result = commands::list_policies(client);
479+
res_handler.tabular_result(result)
480+
}
481+
("policies", "delete") => {
482+
let result = commands::delete_policy(client, &vhost, second_level_args);
483+
res_handler.no_output_on_success(result);
484+
}
473485
("health_check", "local_alarms") => {
474486
let result = commands::health_check_local_alarms(client);
475487
res_handler.health_check_result(result);

tests/policies_tests.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,30 @@ fn test_operator_policies() -> Result<(), Box<dyn std::error::Error>> {
7171

7272
Ok(())
7373
}
74+
75+
#[test]
76+
fn test_policies_declare_list_and_delete() -> Result<(), Box<dyn std::error::Error>> {
77+
let policy_name = "test_policies_declare_list_and_delete";
78+
79+
run_succeeds([
80+
"policies",
81+
"declare",
82+
"--name",
83+
policy_name,
84+
"--pattern",
85+
"foo-.*",
86+
"--apply-to",
87+
"queues",
88+
"--priority",
89+
"123",
90+
"--definition",
91+
"{\"max-length\": 20}",
92+
]);
93+
94+
run_succeeds(["policies", "list"])
95+
.stdout(predicate::str::contains(policy_name).and(predicate::str::contains("20")));
96+
run_succeeds(["policies", "delete", "--name", policy_name]);
97+
run_succeeds(["policies", "list"]).stdout(predicate::str::contains(policy_name).not());
98+
99+
Ok(())
100+
}

0 commit comments

Comments
 (0)