Skip to content

Commit

Permalink
Implement 'definitions import_into_vhost'
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Feb 8, 2025
1 parent b8e8510 commit b0bd824
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 8 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# rabbitmqadmin-ng Change Log

## v0.24.0 (in development)
## v0.25.0 (in development)

No (documented) changes yet.


## v0.24.0 (Feb 8, 2025)

### Enhancements

* `definitions export_from_vhost` is a new command that exports definitions from a single virtual host
instead of the entire cluster
(as opposed to definitions for the entire cluster)

* `definitions import_into_vhost` is a new command that imports virtual host-specific definitions
(as opposed to definitions for the entire cluster)


## v0.23.0 (Feb 2, 2025)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ reqwest = { version = "0.12.12", features = [
"__rustls",
"rustls-tls-native-roots"
]}
rabbitmq_http_client = { version = "0.21.0", features = [
rabbitmq_http_client = { version = "0.22.0", features = [
"core",
"blocking",
"tabled"
Expand Down
22 changes: 22 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,28 @@ pub fn import_definitions(client: APIClient, command_args: &ArgMatches) -> Clien
}
}

pub fn import_vhost_definitions(
client: APIClient,
vhost: &str,
command_args: &ArgMatches,
) -> ClientResult<()> {
let file = command_args.get_one::<String>("file").unwrap();
let definitions = fs::read_to_string(file);
match definitions {
Ok(defs) => {
let defs_json = serde_json::from_str(defs.as_str()).unwrap_or_else(|err| {
eprintln!("`{}` is not a valid JSON file: {}", file, err);
process::exit(1)
});
client.import_vhost_definitions(vhost, defs_json)
}
Err(err) => {
eprintln!("`{}` could not be read: {}", file, err);
process::exit(1)
}
}
}

pub fn publish_message(
client: APIClient,
vhost: &str,
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,14 @@ fn dispatch_common_subcommand(
let result = commands::export_vhost_definitions(client, &vhost, second_level_args);
res_handler.no_output_on_success(result);
}

("definitions", "import") => {
let result = commands::import_definitions(client, second_level_args);
res_handler.no_output_on_success(result);
}
("definitions", "import_into_vhost") => {
let result = commands::import_vhost_definitions(client, &vhost, second_level_args);
res_handler.no_output_on_success(result);
}
("export", "definitions") => {
let result = commands::export_cluster_wide_definitions(client, second_level_args);
res_handler.no_output_on_success(result);
Expand Down
27 changes: 25 additions & 2 deletions tests/definitions_import_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,38 @@
// limitations under the License.

mod test_helpers;
use crate::test_helpers::delete_vhost;
use test_helpers::run_succeeds;

#[test]
fn test_import_definitions() -> Result<(), Box<dyn std::error::Error>> {
fn test_import_cluster_definitions() -> Result<(), Box<dyn std::error::Error>> {
run_succeeds([
"definitions",
"import",
"--file",
"tests/fixtures/definitions/definitions1.json",
"tests/fixtures/definitions/cluster.definitions.1.json",
]);

Ok(())
}

#[test]
fn test_import_vhost_definitions() -> Result<(), Box<dyn std::error::Error>> {
let vh = "test_import_vhost_definitions.1";

delete_vhost(vh).expect("failed to delete a virtual host");
run_succeeds(["declare", "vhost", "--name", vh]);

run_succeeds([
"--vhost",
vh,
"definitions",
"import_into_vhost",
"--file",
"tests/fixtures/definitions/vhost.definitions.1.json",
]);

run_succeeds(["delete", "vhost", "--name", vh, "--idempotently"]);

Ok(())
}
67 changes: 67 additions & 0 deletions tests/fixtures/definitions/vhost.definitions.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"rabbit_version": "4.0.5",
"rabbitmq_version": "4.0.5",
"product_name": "RabbitMQ",
"product_version": "4.0.5",
"rabbitmq_definition_format": "single_virtual_host",
"original_vhost_name": "vh3",
"explanation": "Definitions of virtual host 'vh3'",
"metadata": {
"description": "",
"tags": [],
"default_queue_type": "classic"
},
"description": "",
"default_queue_type": "classic",
"limits": {
"max-connections": 9999
},
"parameters": [
{
"value": {
"max-connections": 9999
},
"component": "vhost-limits",
"name": "limits"
}
],
"policies": [
{
"name": "cq.length.limit",
"pattern": "^cq",
"apply-to": "classic_queues",
"definition": {
"max-length": 9999
},
"priority": 0
}
],
"queues": [
{
"name": "stream.1",
"durable": true,
"auto_delete": false,
"arguments": {
"x-queue-type": "stream"
}
},
{
"name": "qq.1",
"durable": true,
"auto_delete": false,
"arguments": {
"x-queue-type": "quorum"
}
},
{
"name": "cq.1",
"durable": true,
"auto_delete": false,
"arguments": {
"x-queue-type": "classic"
}
}
],
"exchanges": [],
"bindings": []
}

0 comments on commit b0bd824

Please sign in to comment.