rocketmq-admin-cli 1.1.0

Command-line interface for managing RocketMQ-Rust and Apache RocketMQ clusters
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use clap::Parser;
use rocketmq_admin_core::client_adapter::services::export_data::ExportConfigsRequest;
use rocketmq_admin_core::client_adapter::services::export_data::ExportConfigsResult;
use rocketmq_admin_core::client_adapter::services::export_data::ExportService;
use rocketmq_error::Result as CanonicalResult;

const DEFAULT_FILE_PATH: &str = "/tmp/rocketmq/export";

use crate::commands::CommandExecute;
use crate::commands::CommonArgs;

#[derive(Debug, Clone, Parser)]
pub struct ExportConfigsSubCommand {
    #[command(flatten)]
    common_args: CommonArgs,

    #[arg(
        short = 'c',
        long = "clusterName",
        required = true,
        help = "choose a cluster to export"
    )]
    cluster_name: String,

    #[arg(
        short = 'f',
        long = "filePath",
        required = false,
        default_value = DEFAULT_FILE_PATH,
        help = "export configs.json path | default /tmp/rocketmq/export"
    )]
    file_path: String,
}

impl ExportConfigsSubCommand {
    fn request(&self) -> CanonicalResult<ExportConfigsRequest> {
        ExportConfigsRequest::try_new(self.cluster_name.clone())
            .map(|request| request.with_optional_namesrv_addr(self.common_args.namesrv_addr.clone()))
    }
}

impl CommandExecute for ExportConfigsSubCommand {
    async fn execute(
        &self,
        credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
        client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
    ) -> CanonicalResult<()> {
        let file_path = self.file_path.trim();
        let result = ExportService::export_configs_by_request_with_credentials(
            self.request()?,
            credentials,
            client_runtime.clone(),
        )
        .await?;

        Self::write_result(&result, file_path)
    }
}

impl ExportConfigsSubCommand {
    fn write_result(export_result: &ExportConfigsResult, file_path: &str) -> CanonicalResult<()> {
        let mut result = serde_json::Map::new();

        let mut cluster_scale_map = serde_json::Map::new();
        cluster_scale_map.insert(
            "namesrvSize".to_string(),
            serde_json::Value::Number(serde_json::Number::from(export_result.name_servers.len() as i64)),
        );
        cluster_scale_map.insert(
            "masterBrokerSize".to_string(),
            serde_json::Value::Number(serde_json::Number::from(export_result.master_broker_size)),
        );
        cluster_scale_map.insert(
            "slaveBrokerSize".to_string(),
            serde_json::Value::Number(serde_json::Number::from(export_result.slave_broker_size)),
        );

        result.insert(
            "brokerConfigs".to_string(),
            serde_json::to_value(&export_result.broker_configs)
                .map_err(|source| crate::errors::serialization_failed_by("JSON", source))?,
        );
        result.insert("clusterScale".to_string(), serde_json::Value::Object(cluster_scale_map));

        let path = format!("{}/configs.json", file_path);
        let json_content = serde_json::to_string_pretty(&result)
            .map_err(|source| crate::errors::serialization_failed_by("JSON", source))?;

        rocketmq_runtime::common::file_utils::string_to_file(&json_content, &path).map_err(crate::runtime_error)?;
        println!("export {} success", path);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn export_configs_sub_command_builds_core_request() {
        let cmd = ExportConfigsSubCommand::try_parse_from([
            "exportConfigs",
            "-c",
            " DefaultCluster ",
            "-n",
            " 127.0.0.1:9876 ",
            "-f",
            " C:/tmp/export ",
        ])
        .unwrap();

        let request = cmd.request().unwrap();

        assert_eq!(request.cluster_name().as_str(), "DefaultCluster");
        assert_eq!(request.namesrv_addr(), Some("127.0.0.1:9876"));
        assert_eq!(cmd.file_path.trim(), "C:/tmp/export");
    }
}