Skip to content

Commit d634584

Browse files
'definitions export': introduce --transformations
these named functions can help a variety of RabbitMQ operation tasks, from obfuscating values to stripping off deprecated or removed keys from policies, to enforcing a default queue type during upgrades from older series that do not support DQT.
1 parent ffa34b4 commit d634584

File tree

4 files changed

+93
-60
lines changed

4 files changed

+93
-60
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ reqwest = { version = "0.12.12", features = [
1717
"__rustls",
1818
"rustls-tls-native-roots"
1919
]}
20-
rabbitmq_http_client = { git = "https://github.com/michaelklishin/rabbitmq-http-api-rs.git", features = [
20+
rabbitmq_http_client = { path = "/Users/antares/Development/RabbitMQ/rabbitmq-http-api-client-rs.git", features = [
2121
"core",
2222
"blocking",
2323
"tabled"

src/cli.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::constants::*;
1717
use super::static_urls::*;
1818
use super::tanzu_cli::tanzu_subcommands;
1919
use crate::output::TableStyle;
20-
use clap::{Arg, ArgAction, ArgGroup, Command, value_parser};
20+
use clap::{value_parser, Arg, ArgAction, ArgGroup, Command};
2121
use rabbitmq_http_client::commons::{
2222
BindingDestinationType, ExchangeType, PolicyTarget, QueueType, ShovelAcknowledgementMode,
2323
SupportedProtocol,
@@ -1187,6 +1187,20 @@ fn definitions_subcommands() -> [Command; 4] {
11871187
.help("output file path or '-' for standard output")
11881188
.required(false)
11891189
.default_value("-"),
1190+
)
1191+
.arg(
1192+
Arg::new("transformations")
1193+
.long("transformations")
1194+
.short('t')
1195+
.long_help(r#"
1196+
Comma-separated names of the transformations to apply to the definitions.
1197+
1198+
Supported names: strip_cmq_policies"
1199+
"#)
1200+
.num_args(1..)
1201+
.value_delimiter(',')
1202+
.action(ArgAction::Append)
1203+
.required(false),
11901204
);
11911205

11921206
let export_from_vhost_cmd = Command::new("export_from_vhost")

src/commands.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ use rabbitmq_http_client::commons::{ExchangeType, SupportedProtocol};
2121
use rabbitmq_http_client::commons::{PolicyTarget, VirtualHostLimitTarget};
2222
use rabbitmq_http_client::commons::{ShovelAcknowledgementMode, UserLimitTarget};
2323
use rabbitmq_http_client::requests::{
24-
Amqp10ShovelDestinationParams, Amqp10ShovelParams, Amqp10ShovelSourceParams,
2524
Amqp091ShovelDestinationParams, Amqp091ShovelParams, Amqp091ShovelSourceParams,
25+
Amqp10ShovelDestinationParams, Amqp10ShovelParams, Amqp10ShovelSourceParams,
2626
EnforcedLimitParams,
2727
};
2828
use std::fs;
2929
use std::process;
3030

3131
use rabbitmq_http_client::commons::BindingDestinationType;
3232
use rabbitmq_http_client::commons::QueueType;
33+
use rabbitmq_http_client::transformers::TransformationChain;
3334
use rabbitmq_http_client::{password_hashing, requests, responses};
3435

3536
type APIClient<'a> = Client<&'a str, &'a str, &'a str>;
@@ -907,6 +908,52 @@ pub fn rebalance_queues(client: APIClient) -> ClientResult<()> {
907908
pub fn export_cluster_wide_definitions(
908909
client: APIClient,
909910
command_args: &ArgMatches,
911+
) -> ClientResult<()> {
912+
let transformations = command_args
913+
.get_many::<String>("transformations")
914+
.unwrap_or_default();
915+
916+
if transformations.len() == 0 {
917+
export_cluster_wide_definitions_without_transformations(client, command_args)
918+
} else {
919+
let transformations = transformations
920+
.into_iter()
921+
.map(|t| String::from(t))
922+
.collect::<Vec<_>>();
923+
export_and_transform_cluster_wide_definitions(client, command_args, transformations)
924+
}
925+
}
926+
927+
fn export_and_transform_cluster_wide_definitions(
928+
client: APIClient,
929+
command_args: &ArgMatches,
930+
transformations: Vec<String>,
931+
) -> ClientResult<()> {
932+
match client.export_cluster_wide_definitions_as_data() {
933+
Ok(mut defs0) => {
934+
let chain = TransformationChain::from(transformations);
935+
let defs1 = chain.apply(&mut defs0);
936+
let json = serde_json::to_string_pretty(&defs1).unwrap();
937+
938+
let path = command_args.get_one::<String>("file").unwrap();
939+
match path.as_str() {
940+
"-" => {
941+
println!("{}", &json);
942+
Ok(())
943+
}
944+
file => {
945+
_ = fs::write(file, &json);
946+
Ok(())
947+
}
948+
}
949+
}
950+
Err(err) => Err(err),
951+
}
952+
}
953+
954+
fn export_cluster_wide_definitions_without_transformations(
955+
client: APIClient,
956+
command_args: &ArgMatches,
910957
) -> ClientResult<()> {
911958
match client.export_cluster_wide_definitions() {
912959
Ok(definitions) => {

0 commit comments

Comments
 (0)