Skip to content

Commit af5b54f

Browse files
Two new commands that list conflicting policies
1 parent d423178 commit af5b54f

File tree

4 files changed

+403
-0
lines changed

4 files changed

+403
-0
lines changed

src/cli.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,20 @@ fn policies_subcommands(pre_flight_settings: PreFlightSettings) -> Vec<Command>
21262126
.value_parser(value_parser!(PolicyTarget)),
21272127
);
21282128

2129+
let list_conflicting_cmd = Command::new("list_conflicting")
2130+
.about("Lists policies that have conflicting priorities across all virtual hosts")
2131+
.after_help(color_print::cformat!(
2132+
"<bold>Doc guide</bold>: {}",
2133+
POLICY_GUIDE_URL
2134+
));
2135+
2136+
let list_conflicting_in_cmd = Command::new("list_conflicting_in")
2137+
.about("Lists policies that have conflicting priorities in a specific virtual host")
2138+
.after_help(color_print::cformat!(
2139+
"<bold>Doc guide</bold>: {}",
2140+
POLICY_GUIDE_URL
2141+
));
2142+
21292143
let list_matching_cmd = Command::new("list_matching_object")
21302144
.about("Lists policies that match an object (queue, stream, exchange) name")
21312145
.arg(
@@ -2200,6 +2214,8 @@ fn policies_subcommands(pre_flight_settings: PreFlightSettings) -> Vec<Command>
22002214
delete_definition_keys_cmd,
22012215
delete_definition_keys_from_all_in_cmd,
22022216
list_cmd,
2217+
list_conflicting_cmd,
2218+
list_conflicting_in_cmd,
22032219
list_in_cmd,
22042220
list_matching_cmd,
22052221
patch_cmd,

src/commands.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use rabbitmq_http_client::{password_hashing, requests, responses};
4343
use regex::Regex;
4444
use serde::de::DeserializeOwned;
4545
use serde_json::Value;
46+
use std::collections::HashMap;
4647
use std::fs;
4748
use std::io;
4849
use std::process;
@@ -154,6 +155,48 @@ pub fn list_matching_policies_in(
154155
.collect())
155156
}
156157

158+
pub fn list_policies_with_conflicting_priorities(
159+
client: APIClient,
160+
) -> ClientResult<Vec<responses::Policy>> {
161+
let policies = client.list_policies()?;
162+
Ok(filter_policies_with_conflicting_priorities(policies))
163+
}
164+
165+
pub fn list_policies_with_conflicting_priorities_in(
166+
client: APIClient,
167+
vhost: &str,
168+
) -> ClientResult<Vec<responses::Policy>> {
169+
let policies = client.list_policies_in(vhost)?;
170+
Ok(filter_policies_with_conflicting_priorities(policies))
171+
}
172+
173+
fn filter_policies_with_conflicting_priorities(
174+
policies: Vec<responses::Policy>,
175+
) -> Vec<responses::Policy> {
176+
let mut priority_counts: HashMap<(&str, i16), usize> = HashMap::new();
177+
178+
for pol in &policies {
179+
*priority_counts
180+
.entry((pol.vhost.as_str(), pol.priority))
181+
.or_insert(0) += 1;
182+
}
183+
184+
let dominated: Vec<bool> = policies
185+
.iter()
186+
.map(|pol| {
187+
priority_counts
188+
.get(&(pol.vhost.as_str(), pol.priority))
189+
.is_some_and(|&count| count > 1)
190+
})
191+
.collect();
192+
193+
policies
194+
.into_iter()
195+
.zip(dominated)
196+
.filter_map(|(pol, is_conflicting)| is_conflicting.then_some(pol))
197+
.collect()
198+
}
199+
157200
pub fn list_operator_policies(client: APIClient) -> ClientResult<Vec<responses::Policy>> {
158201
client.list_operator_policies()
159202
}

src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,14 @@ fn dispatch_common_subcommand(
977977
let result = commands::list_policies(client);
978978
res_handler.tabular_result(result)
979979
}
980+
("policies", "list_conflicting") => {
981+
let result = commands::list_policies_with_conflicting_priorities(client);
982+
res_handler.tabular_result(result)
983+
}
984+
("policies", "list_conflicting_in") => {
985+
let result = commands::list_policies_with_conflicting_priorities_in(client, &vhost);
986+
res_handler.tabular_result(result)
987+
}
980988
("policies", "list_in") => {
981989
let typ_opt = second_level_args
982990
.get_one::<PolicyTarget>("apply_to")

0 commit comments

Comments
 (0)