Skip to content

Commit b8e8510

Browse files
Implement 'definitions export_from_vhost'
1 parent b5661ca commit b8e8510

File tree

7 files changed

+110
-17
lines changed

7 files changed

+110
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## v0.24.0 (in development)
44

5+
### Enhancements
6+
7+
* `definitions export_from_vhost` is a new command that exports definitions from a single virtual host
8+
instead of the entire cluster
9+
10+
511
## v0.23.0 (Feb 2, 2025)
612

713
### Enhancements

Cargo.lock

Lines changed: 2 additions & 2 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 = { version = "0.20.0", features = [
20+
rabbitmq_http_client = { version = "0.21.0", features = [
2121
"core",
2222
"blocking",
2323
"tabled"

src/cli.rs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub fn parser() -> Command {
253253
.subcommand_value_name("queues")
254254
.subcommands(rebalance_subcommands()),
255255
Command::new("definitions")
256-
.about("operations on definitions")
256+
.about("operations on definitions (everything except for messages: virtual hosts, queues, streams, exchanges, bindings, users, etc)")
257257
.after_long_help(color_print::cformat!(
258258
"<bold>Doc guide</bold>: {}",
259259
DEFINITION_GUIDE_URL
@@ -1130,33 +1130,67 @@ fn close_subcommands() -> [Command; 2] {
11301130
[close_connection, close_user_connections]
11311131
}
11321132

1133-
fn definitions_subcommands() -> [Command; 2] {
1133+
fn definitions_subcommands() -> [Command; 4] {
11341134
let export_cmd = Command::new("export")
1135-
.about("export cluster-wide definitions (everything except for messages: virtual hosts, queues, streams, exchanges, bindings, users, etc)")
1135+
.about("export cluster-wide definitions")
11361136
.after_long_help(color_print::cformat!(
1137-
"<bold>Doc guide</bold>: {}", DEFINITION_GUIDE_URL
1137+
"<bold>Doc guide</bold>: {}",
1138+
DEFINITION_GUIDE_URL
1139+
))
1140+
.arg(
1141+
Arg::new("file")
1142+
.long("file")
1143+
.help("output file path or '-' for standard output")
1144+
.required(false)
1145+
.default_value("-"),
1146+
);
1147+
1148+
let export_from_vhost_cmd = Command::new("export_from_vhost")
1149+
.about("export definitions of a specific virtual host")
1150+
.after_long_help(color_print::cformat!(
1151+
"<bold>Doc guide</bold>: {}",
1152+
DEFINITION_GUIDE_URL
11381153
))
11391154
.arg(
11401155
Arg::new("file")
11411156
.long("file")
1142-
.help("output path or '-' for standard output")
1157+
.help("output file path or '-' for standard output")
11431158
.required(false)
11441159
.default_value("-"),
11451160
);
11461161

11471162
let import_cmd = Command::new("import")
1148-
.about("import definitions (everything except for messages: virtual hosts, queues, streams, exchanges, bindings, users, etc")
1163+
.about("import cluster-wide definitions (of multiple virtual hosts)")
1164+
.after_long_help(color_print::cformat!(
1165+
"<bold>Doc guide</bold>: {}",
1166+
DEFINITION_GUIDE_URL
1167+
))
1168+
.arg(
1169+
Arg::new("file")
1170+
.long("file")
1171+
.help("JSON file with cluster-wide definitions")
1172+
.required(true),
1173+
);
1174+
1175+
let import_into_vhost_cmd = Command::new("import_into_vhost")
1176+
.about("import a virtual host-specific definitions file into a virtual host")
11491177
.after_long_help(color_print::cformat!(
1150-
"<bold>Doc guide</bold>: {}", DEFINITION_GUIDE_URL
1178+
"<bold>Doc guide</bold>: {}",
1179+
DEFINITION_GUIDE_URL
11511180
))
11521181
.arg(
11531182
Arg::new("file")
11541183
.long("file")
1155-
.help("JSON file with definitions")
1184+
.help("JSON file with virtual host-specific definitions")
11561185
.required(true),
11571186
);
11581187

1159-
[export_cmd, import_cmd]
1188+
[
1189+
export_cmd,
1190+
export_from_vhost_cmd,
1191+
import_cmd,
1192+
import_into_vhost_cmd,
1193+
]
11601194
}
11611195

11621196
fn export_subcommands() -> [Command; 1] {

src/commands.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,34 @@ pub fn rebalance_queues(client: APIClient) -> ClientResult<()> {
682682
client.rebalance_queue_leaders()
683683
}
684684

685-
pub fn export_definitions(client: APIClient, command_args: &ArgMatches) -> ClientResult<()> {
686-
match client.export_definitions() {
685+
pub fn export_cluster_wide_definitions(
686+
client: APIClient,
687+
command_args: &ArgMatches,
688+
) -> ClientResult<()> {
689+
match client.export_cluster_wide_definitions() {
690+
Ok(definitions) => {
691+
let path = command_args.get_one::<String>("file").unwrap();
692+
match path.as_str() {
693+
"-" => {
694+
println!("{}", &definitions);
695+
Ok(())
696+
}
697+
file => {
698+
_ = fs::write(file, &definitions);
699+
Ok(())
700+
}
701+
}
702+
}
703+
Err(err) => Err(err),
704+
}
705+
}
706+
707+
pub fn export_vhost_definitions(
708+
client: APIClient,
709+
vhost: &str,
710+
command_args: &ArgMatches,
711+
) -> ClientResult<()> {
712+
match client.export_vhost_definitions(vhost) {
687713
Ok(definitions) => {
688714
let path = command_args.get_one::<String>("file").unwrap();
689715
match path.as_str() {

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,20 @@ fn dispatch_common_subcommand(
498498
res_handler.no_output_on_success(result);
499499
}
500500
("definitions", "export") => {
501-
let result = commands::export_definitions(client, second_level_args);
501+
let result = commands::export_cluster_wide_definitions(client, second_level_args);
502502
res_handler.no_output_on_success(result);
503503
}
504+
("definitions", "export_from_vhost") => {
505+
let result = commands::export_vhost_definitions(client, &vhost, second_level_args);
506+
res_handler.no_output_on_success(result);
507+
}
508+
504509
("definitions", "import") => {
505510
let result = commands::import_definitions(client, second_level_args);
506511
res_handler.no_output_on_success(result);
507512
}
508513
("export", "definitions") => {
509-
let result = commands::export_definitions(client, second_level_args);
514+
let result = commands::export_cluster_wide_definitions(client, second_level_args);
510515
res_handler.no_output_on_success(result);
511516
}
512517
("import", "definitions") => {

tests/definitions_export_tests.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,33 @@
1515
use predicates::prelude::*;
1616

1717
mod test_helpers;
18+
use crate::test_helpers::delete_vhost;
1819
use test_helpers::run_succeeds;
1920

2021
#[test]
21-
fn test_export_definitions() -> Result<(), Box<dyn std::error::Error>> {
22+
fn test_export_cluster_wide_definitions() -> Result<(), Box<dyn std::error::Error>> {
2223
run_succeeds(["definitions", "export"]).stdout(predicate::str::contains("guest"));
2324

2425
Ok(())
2526
}
27+
28+
#[test]
29+
fn test_export_vhost_definitions() -> Result<(), Box<dyn std::error::Error>> {
30+
let vh = "test_export_vhost_definitions.1";
31+
delete_vhost(vh).expect("failed to delete a virtual host");
32+
run_succeeds(["declare", "vhost", "--name", vh]);
33+
34+
let q = "qq.test_export_vhost_definitions.1";
35+
run_succeeds([
36+
"-V", vh, "declare", "queue", "--name", q, "--type", "quorum",
37+
]);
38+
39+
run_succeeds(["--vhost", vh, "definitions", "export_from_vhost"])
40+
.stdout(predicate::str::contains(q));
41+
run_succeeds(["--vhost", "/", "definitions", "export_from_vhost"])
42+
.stdout(predicate::str::contains(q).not());
43+
44+
delete_vhost(vh).expect("failed to delete a virtual host");
45+
46+
Ok(())
47+
}

0 commit comments

Comments
 (0)