rocketmq-admin-cli 1.0.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_error::Result as CanonicalResult;

use crate::commands::CommandExecute;
use crate::commands::CommonArgs;
use rocketmq_admin_core::client_adapter::services::namesrv::NameServerService;
use rocketmq_admin_core::client_adapter::services::namesrv::WritePermRequest;
use rocketmq_admin_core::client_adapter::services::namesrv::WritePermResult;

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

    #[arg(short = 'b', long = "brokerName", required = true, help = "broker name")]
    broker_name: String,
}

impl WipeWritePermSubCommand {
    fn request(&self) -> CanonicalResult<WritePermRequest> {
        Ok(WritePermRequest::try_new(self.broker_name.clone())?
            .with_optional_namesrv_addr(self.common_args.namesrv_addr.clone()))
    }

    fn print_result(result: WritePermResult) {
        for entry in result.entries {
            if let Some(affected_count) = entry.affected_count {
                println!(
                    "wipe write perm of broker[{}] in name server[{}] OK, {}",
                    result.broker_name, entry.namesrv_addr, affected_count
                );
            } else {
                println!(
                    "wipe write perm of broker[{}] in name server[{}] Failed",
                    result.broker_name, entry.namesrv_addr
                );
                if let Some(error) = entry.error {
                    println!("{error}");
                }
            }
        }
    }
}

impl CommandExecute for WipeWritePermSubCommand {
    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 result = NameServerService::wipe_write_perm_by_request_with_credentials(
            self.request()?,
            credentials,
            client_runtime,
        )
        .await?;
        Self::print_result(result);
        Ok(())
    }
}

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

    #[test]
    fn wipe_write_perm_sub_command_parse() {
        let cmd = WipeWritePermSubCommand::try_parse_from(["wipeWritePerm", "-b", "broker-a", "-n", "127.0.0.1:9876"])
            .unwrap();

        assert_eq!(cmd.request().unwrap().broker_name().as_str(), "broker-a");
    }
}