Skip to content

Commit 5680a64

Browse files
Adapt new 'policies' commands based on core team's feedback
1 parent a39c0b6 commit 5680a64

File tree

6 files changed

+118
-85
lines changed

6 files changed

+118
-85
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
used by `rabbitmqctl` and the latter reflects the fact that the command can be used to update an existing policy,
1313
in particular, to override its definition
1414
* `policies patch` is a new command that updates a policy definition by merging the provided definition with the existing one
15+
* `policies delete_definition_keys` is a new command that removes keys from a policy definition
16+
* `policies delete_definition_keys_from_all_in` is a new command that removes definition keys from all policies in a virtual host
17+
* `policies update_definition` is a new command that updates a policy definition key; for multi-key updates, see `policies patch
18+
* `policies update_definitions_of_all_in` is a new command that updates a definition key for all policies in a virtual host
1519
* `policies declare_override` is a new command that declares a policy that overrides another policy
1620
* `policies declare_blanket` is a new command that declares a low priority policy that matches all objects not matched
1721
by any other policies

src/cli.rs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,26 +1663,32 @@ fn operator_policies_subcommands(pre_flight_settings: PreFlightSettings) -> [Com
16631663
.required(true),
16641664
);
16651665

1666-
let delete_definition_key_cmd = Command::new("delete_definition_key")
1667-
.about("Deletes a definition key from an operator policy, unless it is the only key")
1666+
let delete_definition_key_cmd = Command::new("delete_definition_keys")
1667+
.about("Deletes definition keys from an operator policy, unless it is the only key")
16681668
.arg(
16691669
Arg::new("name")
16701670
.long("name")
16711671
.help("operator policy name")
16721672
.required(true),
16731673
)
16741674
.arg(
1675-
Arg::new("definition_key")
1676-
.long("definition-key")
1677-
.help("definition key"),
1675+
Arg::new("definition_keys")
1676+
.long("definition-keys")
1677+
.num_args(1..)
1678+
.value_delimiter(',')
1679+
.action(ArgAction::Append)
1680+
.help("comma-separated definition keys"),
16781681
);
16791682

1680-
let delete_definition_key_from_all_in_cmd = Command::new("delete_definition_key_from_all_in")
1683+
let delete_definition_key_from_all_in_cmd = Command::new("delete_definition_keys_from_all_in")
16811684
.about("Deletes a definition key from all operator policies in a virtual host, unless it is the only key")
16821685
.arg(
1683-
Arg::new("definition_key")
1684-
.long("definition-key")
1685-
.help("definition key")
1686+
Arg::new("definition_keys")
1687+
.long("definition-keys")
1688+
.num_args(1..)
1689+
.value_delimiter(',')
1690+
.action(ArgAction::Append)
1691+
.help("comma-separated definition keys")
16861692
);
16871693

16881694
let list_in_cmd = Command::new("list_in")
@@ -1872,7 +1878,7 @@ fn policies_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 12]
18721878
.required(true),
18731879
);
18741880

1875-
let delete_definition_key_cmd = Command::new("delete_definition_key")
1881+
let delete_definition_keys_cmd = Command::new("delete_definition_keys")
18761882
.about("Deletes a definition key from a policy, unless it is the only key")
18771883
.arg(
18781884
Arg::new("name")
@@ -1881,17 +1887,24 @@ fn policies_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 12]
18811887
.required(true),
18821888
)
18831889
.arg(
1884-
Arg::new("definition_key")
1885-
.long("definition-key")
1886-
.help("definition key"),
1890+
Arg::new("definition_keys")
1891+
.long("definition-keys")
1892+
.num_args(1..)
1893+
.value_delimiter(',')
1894+
.action(ArgAction::Append)
1895+
.help("comma-separated definition keys"),
18871896
);
18881897

1889-
let delete_definition_key_from_all_in_cmd = Command::new("delete_definition_key_from_all_in")
1890-
.about("Deletes a definition key from all policies in a virtual host, unless it is the only key")
1898+
let delete_definition_keys_from_all_in_cmd = Command::new("delete_definition_keys_from_all_in")
1899+
.about("Deletes definition keys from all policies in a virtual host, unless it is the only policy key")
18911900
.arg(
1892-
Arg::new("definition_key")
1893-
.long("definition-key")
1894-
.help("definition key")
1901+
Arg::new("definition_keys")
1902+
.long("definition-keys")
1903+
.help("comma-separated definition keys")
1904+
.num_args(1..)
1905+
.value_delimiter(',')
1906+
.action(ArgAction::Append)
1907+
.required(true)
18951908
);
18961909

18971910
let list_in_cmd = Command::new("list_in")
@@ -1974,8 +1987,8 @@ fn policies_subcommands(pre_flight_settings: PreFlightSettings) -> [Command; 12]
19741987
declare_override_cmd,
19751988
declare_blanket_cmd,
19761989
delete_cmd,
1977-
delete_definition_key_cmd,
1978-
delete_definition_key_from_all_in_cmd,
1990+
delete_definition_keys_cmd,
1991+
delete_definition_keys_from_all_in_cmd,
19791992
list_cmd,
19801993
list_in_cmd,
19811994
list_matching_cmd,

src/commands.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,37 +1286,43 @@ pub fn update_all_operator_policy_definitions_in(
12861286
Ok(())
12871287
}
12881288

1289-
pub fn delete_policy_definition(
1289+
pub fn delete_policy_definition_keys(
12901290
client: APIClient,
12911291
vhost: &str,
12921292
command_args: &ArgMatches,
12931293
) -> ClientResult<()> {
12941294
let name = command_args.get_one::<String>("name").cloned().unwrap();
1295-
let key = command_args
1296-
.get_one::<String>("definition_key")
1297-
.cloned()
1298-
.unwrap();
1295+
let keys = command_args
1296+
.get_many::<String>("definition_keys")
1297+
.unwrap()
1298+
.into_iter()
1299+
.map(String::from)
1300+
.collect::<Vec<_>>();
1301+
let str_keys: Vec<&str> = keys.iter().map(AsRef::as_ref).collect::<Vec<_>>();
12991302

13001303
let pol = client.get_policy(vhost, &name)?;
1301-
let updated_pol = pol.without_keys(vec![&key]);
1304+
let updated_pol = pol.without_keys(str_keys);
13021305

13031306
let params = PolicyParams::from(&updated_pol);
13041307
client.declare_policy(&params)
13051308
}
13061309

1307-
pub fn delete_policy_definition_key_in(
1310+
pub fn delete_policy_definition_keys_in(
13081311
client: APIClient,
13091312
vhost: &str,
13101313
command_args: &ArgMatches,
13111314
) -> ClientResult<()> {
13121315
let pols = client.list_policies_in(vhost)?;
1313-
let key = command_args
1314-
.get_one::<String>("definition_key")
1315-
.cloned()
1316-
.unwrap();
1316+
let keys = command_args
1317+
.get_many::<String>("definition_keys")
1318+
.unwrap()
1319+
.into_iter()
1320+
.map(String::from)
1321+
.collect::<Vec<_>>();
1322+
let str_keys: Vec<&str> = keys.iter().map(AsRef::as_ref).collect::<Vec<_>>();
13171323

13181324
for pol in pols {
1319-
let updated_pol = pol.without_keys(vec![&key]);
1325+
let updated_pol = pol.without_keys(str_keys.clone());
13201326

13211327
let params = PolicyParams::from(&updated_pol);
13221328
client.declare_policy(&params)?
@@ -1325,37 +1331,43 @@ pub fn delete_policy_definition_key_in(
13251331
Ok(())
13261332
}
13271333

1328-
pub fn delete_operator_policy_definition(
1334+
pub fn delete_operator_policy_definition_keys(
13291335
client: APIClient,
13301336
vhost: &str,
13311337
command_args: &ArgMatches,
13321338
) -> ClientResult<()> {
13331339
let name = command_args.get_one::<String>("name").cloned().unwrap();
1334-
let key = command_args
1335-
.get_one::<String>("definition_key")
1336-
.cloned()
1337-
.unwrap();
1340+
let keys = command_args
1341+
.get_many::<String>("definition_keys")
1342+
.unwrap()
1343+
.into_iter()
1344+
.map(String::from)
1345+
.collect::<Vec<_>>();
1346+
let str_keys: Vec<&str> = keys.iter().map(AsRef::as_ref).collect::<Vec<_>>();
13381347

13391348
let pol = client.get_operator_policy(vhost, &name)?;
1340-
let updated_pol = pol.without_keys(vec![&key]);
1349+
let updated_pol = pol.without_keys(str_keys);
13411350

13421351
let params = PolicyParams::from(&updated_pol);
13431352
client.declare_operator_policy(&params)
13441353
}
13451354

1346-
pub fn delete_operator_policy_definition_key_in(
1355+
pub fn delete_operator_policy_definition_keys_in(
13471356
client: APIClient,
13481357
vhost: &str,
13491358
command_args: &ArgMatches,
13501359
) -> ClientResult<()> {
13511360
let pols = client.list_operator_policies_in(vhost)?;
1352-
let key = command_args
1353-
.get_one::<String>("definition_key")
1354-
.cloned()
1355-
.unwrap();
1361+
let keys = command_args
1362+
.get_many::<String>("definition_keys")
1363+
.unwrap()
1364+
.into_iter()
1365+
.map(String::from)
1366+
.collect::<Vec<_>>();
1367+
let str_keys: Vec<&str> = keys.iter().map(AsRef::as_ref).collect::<Vec<_>>();
13561368

13571369
for pol in pols {
1358-
let updated_pol = pol.without_keys(vec![&key]);
1370+
let updated_pol = pol.without_keys(str_keys.clone());
13591371

13601372
let params = PolicyParams::from(&updated_pol);
13611373
client.declare_operator_policy(&params)?

src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,13 @@ fn dispatch_common_subcommand(
649649
let result = commands::delete_operator_policy(client, &vhost, second_level_args);
650650
res_handler.no_output_on_success(result);
651651
}
652-
("operator_policies", "delete_definition_key") => {
652+
("operator_policies", "delete_definition_keys") => {
653653
let result =
654-
commands::delete_operator_policy_definition(client, &vhost, second_level_args);
654+
commands::delete_operator_policy_definition_keys(client, &vhost, second_level_args);
655655
res_handler.no_output_on_success(result);
656656
}
657-
("operator_policies", "delete_definition_key_from_all_in") => {
658-
let result = commands::delete_operator_policy_definition_key_in(
657+
("operator_policies", "delete_definition_keys_from_all_in") => {
658+
let result = commands::delete_operator_policy_definition_keys_in(
659659
client,
660660
&vhost,
661661
second_level_args,
@@ -745,13 +745,13 @@ fn dispatch_common_subcommand(
745745
let result = commands::delete_policy(client, &vhost, second_level_args);
746746
res_handler.no_output_on_success(result);
747747
}
748-
("policies", "delete_definition_key") => {
749-
let result = commands::delete_policy_definition(client, &vhost, second_level_args);
748+
("policies", "delete_definition_keys") => {
749+
let result = commands::delete_policy_definition_keys(client, &vhost, second_level_args);
750750
res_handler.no_output_on_success(result);
751751
}
752-
("policies", "delete_definition_key_from_all_in") => {
752+
("policies", "delete_definition_keys_from_all_in") => {
753753
let result =
754-
commands::delete_policy_definition_key_in(client, &vhost, second_level_args);
754+
commands::delete_policy_definition_keys_in(client, &vhost, second_level_args);
755755
res_handler.no_output_on_success(result);
756756
}
757757
("policies", "list") => {

0 commit comments

Comments
 (0)