Skip to content

Commit 1883b3e

Browse files
Two more policies commands
That seek to help troubleshooting policies more efficient. As a drive-by change this makes sure that --apply-to values are fetched correctly and actually used.
1 parent 33db827 commit 1883b3e

File tree

5 files changed

+181
-8
lines changed

5 files changed

+181
-8
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020
# an equivalent of 'delete policy'
2121
rabbitmqadmin policies delete --name "policy-name"
2222
```
23+
* `policies list_in` is a new command that lists policies in a specific virtual host:
24+
25+
```shell
26+
rabbitmqadmin --vhost "a.vhost" policies list_in
27+
```
28+
29+
```shell
30+
rabbitmqadmin --vhost "streams.vhost" policies list_in --apply-to "streams"
31+
```
32+
33+
* `policies list_matching_object` is a new command that lists all policies that
34+
would match an object (a queue, a stream, an exchange)
35+
with a given name:
36+
37+
```shell
38+
rabbitmqadmin --vhost "a.vhost" policies list_matching_object --name 'audit.events' --type queues
39+
```
40+
41+
### Bug Fixes
42+
43+
* `declare policy`'s `--apply-to` argument value was ignored
44+
2345

2446

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

Cargo.lock

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

src/cli.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ fn declare_subcommands() -> [Command; 12] {
734734
Arg::new("apply_to")
735735
.long("apply-to")
736736
.help("entities to apply to (queues, classic_queues, quorum_queues, streams, exchanges, all)")
737+
.value_parser(value_parser!(PolicyTarget))
737738
.required(true),
738739
)
739740
.arg(
@@ -1070,7 +1071,8 @@ fn policies_subcommands() -> [Command; 5] {
10701071
Arg::new("type")
10711072
.long("type")
10721073
.value_parser(value_parser!(PolicyTarget))
1073-
.required(true),
1074+
.required(true)
1075+
.help("target type, one of 'queues', 'streams', 'exchanges'"),
10741076
);
10751077

10761078
[

src/commands.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,10 @@ pub fn declare_policy(
691691
) -> ClientResult<()> {
692692
let name = command_args.get_one::<String>("name").unwrap();
693693
let pattern = command_args.get_one::<String>("pattern").unwrap();
694-
let apply_to = command_args.get_one::<String>("pattern").unwrap();
694+
let apply_to = command_args
695+
.get_one::<PolicyTarget>("apply_to")
696+
.cloned()
697+
.unwrap();
695698
let priority = command_args.get_one::<String>("priority").unwrap();
696699
let definition = command_args.get_one::<String>("definition").unwrap();
697700

@@ -705,7 +708,7 @@ pub fn declare_policy(
705708
vhost,
706709
name,
707710
pattern,
708-
apply_to: commons::PolicyTarget::from(apply_to.as_str()),
711+
apply_to: apply_to.clone(),
709712
priority: priority.parse::<i32>().unwrap(),
710713
definition: parsed_definition,
711714
};
@@ -720,7 +723,10 @@ pub fn declare_operator_policy(
720723
) -> ClientResult<()> {
721724
let name = command_args.get_one::<String>("name").unwrap();
722725
let pattern = command_args.get_one::<String>("pattern").unwrap();
723-
let apply_to = command_args.get_one::<String>("pattern").unwrap();
726+
let apply_to = command_args
727+
.get_one::<PolicyTarget>("apply_to")
728+
.cloned()
729+
.unwrap();
724730
let priority = command_args.get_one::<String>("priority").unwrap();
725731
let definition = command_args.get_one::<String>("definition").unwrap();
726732

@@ -734,7 +740,7 @@ pub fn declare_operator_policy(
734740
vhost,
735741
name,
736742
pattern,
737-
apply_to: commons::PolicyTarget::from(apply_to.as_str()),
743+
apply_to: apply_to.clone(),
738744
priority: priority.parse::<i32>().unwrap(),
739745
definition: parsed_definition,
740746
};

tests/policies_tests.rs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,146 @@ fn test_policies_in_with_entity_type() -> Result<(), Box<dyn std::error::Error>>
197197

198198
Ok(())
199199
}
200+
201+
#[test]
202+
fn test_policies_matching_objects() -> Result<(), Box<dyn std::error::Error>> {
203+
let vh1 = "rabbitmqadmin.vh.policies.11";
204+
let vh2 = "rabbitmqadmin.vh.policies.12";
205+
let vh3 = "rabbitmqadmin.vh.policies.13";
206+
207+
run_succeeds(["delete", "vhost", "--name", vh1, "--idempotently"]);
208+
run_succeeds(["declare", "vhost", "--name", vh1]);
209+
run_succeeds(["delete", "vhost", "--name", vh2, "--idempotently"]);
210+
run_succeeds(["declare", "vhost", "--name", vh2]);
211+
run_succeeds(["delete", "vhost", "--name", vh3, "--idempotently"]);
212+
run_succeeds(["declare", "vhost", "--name", vh3]);
213+
214+
let policy1 = "rabbitmqadmin.policies.11";
215+
run_succeeds([
216+
"--vhost",
217+
vh1,
218+
"policies",
219+
"declare",
220+
"--name",
221+
policy1,
222+
"--pattern",
223+
"^q-.*",
224+
"--apply-to",
225+
"queues",
226+
"--priority",
227+
"47",
228+
"--definition",
229+
"{\"max-length\": 20}",
230+
]);
231+
232+
let policy2 = "rabbitmqadmin.policies.12";
233+
run_succeeds([
234+
"--vhost",
235+
vh2,
236+
"policies",
237+
"declare",
238+
"--name",
239+
policy2,
240+
"--pattern",
241+
"^x-.*",
242+
"--apply-to",
243+
"exchanges",
244+
"--priority",
245+
"17",
246+
"--definition",
247+
"{\"alternate-exchange\": \"amq.fanout\"}",
248+
]);
249+
250+
let policy3 = "rabbitmqadmin.policies.13";
251+
run_succeeds([
252+
"--vhost",
253+
vh3,
254+
"policies",
255+
"declare",
256+
"--name",
257+
policy3,
258+
"--pattern",
259+
"^s-.*",
260+
"--apply-to",
261+
"streams",
262+
"--priority",
263+
"15",
264+
"--definition",
265+
"{\"max-age\": \"1D\"}",
266+
]);
267+
268+
run_succeeds([
269+
"--vhost",
270+
vh1,
271+
"policies",
272+
"list_matching_object",
273+
"--name",
274+
"q-abc",
275+
"--type",
276+
"queues",
277+
])
278+
.stdout(predicate::str::contains(policy1).and(predicate::str::contains("20")));
279+
run_succeeds([
280+
"--vhost",
281+
vh1,
282+
"policies",
283+
"list_matching_object",
284+
"--name",
285+
"q-abc",
286+
"--type",
287+
"exchanges",
288+
])
289+
.stdout(predicate::str::contains(policy1).not());
290+
291+
run_succeeds([
292+
"--vhost",
293+
vh2,
294+
"policies",
295+
"list_matching_object",
296+
"--name",
297+
"x-abc",
298+
"--type",
299+
"exchanges",
300+
])
301+
.stdout(predicate::str::contains(policy2));
302+
run_succeeds([
303+
"--vhost",
304+
vh2,
305+
"policies",
306+
"list_matching_object",
307+
"--name",
308+
"x-abc",
309+
"--type",
310+
"streams",
311+
])
312+
.stdout(predicate::str::contains(policy2).not());
313+
314+
run_succeeds([
315+
"--vhost",
316+
vh3,
317+
"policies",
318+
"list_matching_object",
319+
"--name",
320+
"s-abc",
321+
"--type",
322+
"streams",
323+
])
324+
.stdout(predicate::str::contains(policy3).and(predicate::str::contains("1D")));
325+
run_succeeds([
326+
"--vhost",
327+
vh3,
328+
"policies",
329+
"list_matching_object",
330+
"--name",
331+
"s-abc",
332+
"--type",
333+
"exchanges",
334+
])
335+
.stdout(predicate::str::contains(policy3).not());
336+
337+
run_succeeds(["delete", "vhost", "--name", vh1, "--idempotently"]);
338+
run_succeeds(["delete", "vhost", "--name", vh2, "--idempotently"]);
339+
run_succeeds(["delete", "vhost", "--name", vh3, "--idempotently"]);
340+
341+
Ok(())
342+
}

0 commit comments

Comments
 (0)