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 rocketmq_model::common::topic::TopicValidator;

use crate::commands::CommandExecute;
use crate::commands::CommonArgs;
use rocketmq_admin_core::client_adapter::services::topic::TopicService;
use rocketmq_admin_core::client_adapter::services::topic::TopicTarget;
use rocketmq_admin_core::client_adapter::services::topic::UpdateTopicPermRequest;
use rocketmq_admin_core::client_adapter::services::topic::UpdateTopicPermResult;

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

    #[arg(
        short = 'b',
        long = "brokerAddr",
        required = false,
        conflicts_with = "cluster_name",
        help = "create topic to which broker"
    )]
    broker_addr: Option<String>,

    #[arg(
        short = 'c',
        long = "clusterName",
        required = false,
        help = "cluster that owns the topic"
    )]
    cluster_name: Option<String>,

    #[arg(short = 't', long = "topic", required = true, help = "Topic name")]
    topic: String,

    #[arg(
        short = 'p',
        long = "perm",
        required = true,
        help = "set topic's permission(2|4|6), intro[2:W; 4:R; 6:RW]"
    )]
    perm: String,
}

impl UpdateTopicPermSubCommand {
    fn request(&self) -> CanonicalResult<UpdateTopicPermRequest> {
        let target = if let Some(broker_addr) = &self.broker_addr {
            TopicTarget::Broker(broker_addr.trim().into())
        } else if let Some(cluster_name) = &self.cluster_name {
            TopicTarget::Cluster(cluster_name.trim().into())
        } else {
            return Err(crate::errors::argument_invalid(
                "UpdateTopicPermSubCommand: Either brokerAddr (-b) or clusterName (-c) must be provided",
            ));
        };
        let perm = self.perm.parse::<i32>().map_err(|e| {
            crate::errors::argument_invalid(format!(
                "UpdateTopicPermSubCommand: Invalid perm value '{}': {}",
                self.perm, e
            ))
        })?;

        Ok(UpdateTopicPermRequest::try_new(self.topic.clone(), target, perm)?
            .with_optional_namesrv_addr(self.common_args.namesrv_addr.clone()))
    }

    fn print_result(result: UpdateTopicPermResult) {
        match result.target {
            TopicTarget::Broker(broker_addr) => {
                println!("update topic perm to {} in {} success.", result.perm, broker_addr)
            }
            TopicTarget::Cluster(cluster_name) => {
                println!(
                    "update topic perm to {} in cluster {} success.",
                    result.perm, cluster_name
                )
            }
        }
    }
}

impl CommandExecute for UpdateTopicPermSubCommand {
    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 validation_result = TopicValidator::validate_topic(&self.topic);
        if !validation_result.valid() {
            return Err(crate::errors::argument_invalid(format!(
                "UpdateTopicPermSubCommand: Invalid topic name: {}",
                validation_result.remark().as_str()
            )));
        }

        let result =
            TopicService::update_topic_perm_by_request_with_credentials(self.request()?, credentials, client_runtime)
                .await?;
        Self::print_result(result);
        Ok(())
    }
}

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

    #[test]
    fn test_update_topic_perm_sub_command_parse() {
        // Test parsing with broker address
        let args =
            UpdateTopicPermSubCommand::try_parse_from(["test", "-b", "127.0.0.1:10911", "-t", "TestTopic", "-p", "6"])
                .unwrap();

        assert_eq!(args.broker_addr, Some("127.0.0.1:10911".to_string()));
        assert_eq!(args.cluster_name, None);
        assert_eq!(args.topic, "TestTopic");
        assert_eq!(args.perm, "6");
    }

    #[test]
    fn test_update_topic_perm_sub_command_parse_with_cluster() {
        // Test parsing with cluster name
        let args =
            UpdateTopicPermSubCommand::try_parse_from(["test", "-c", "DefaultCluster", "-t", "TestTopic", "-p", "4"])
                .unwrap();

        assert_eq!(args.broker_addr, None);
        assert_eq!(args.cluster_name, Some("DefaultCluster".to_string()));
        assert_eq!(args.topic, "TestTopic");
        assert_eq!(args.perm, "4");
    }

    #[tokio::test]
    async fn test_execute_fails_without_broker_or_cluster() {
        let command = UpdateTopicPermSubCommand {
            common_args: CommonArgs {
                namesrv_addr: None,
                skip_confirm: false,
            },
            broker_addr: None,
            cluster_name: None,
            topic: "TestTopic".to_string(),
            perm: "6".to_string(),
        };

        let error = command
            .execute(None, crate::commands::test_client_runtime())
            .await
            .expect_err("a broker or cluster target is required");
        assert_eq!(error.descriptor(), &rocketmq_error::CORE_ARGUMENT_INVALID);
        assert_eq!(error.to_string(), "core.argument.invalid: Argument is invalid");
    }

    #[tokio::test]
    async fn test_execute_fails_with_invalid_topic() {
        let command = UpdateTopicPermSubCommand {
            common_args: CommonArgs {
                namesrv_addr: None,
                skip_confirm: false,
            },
            broker_addr: Some("127.0.0.1:10911".to_string()),
            cluster_name: None,
            topic: "".to_string(), // Invalid topic name
            perm: "6".to_string(),
        };

        let error = command
            .execute(None, crate::commands::test_client_runtime())
            .await
            .expect_err("an invalid topic must be rejected");
        assert_eq!(error.descriptor(), &rocketmq_error::CORE_ARGUMENT_INVALID);
        assert_eq!(error.to_string(), "core.argument.invalid: Argument is invalid");
    }

    #[test]
    fn test_command_structure() {
        // Verify the command structure
        let command = UpdateTopicPermSubCommand {
            common_args: CommonArgs {
                namesrv_addr: Some("127.0.0.1:9876".to_string()),
                skip_confirm: false,
            },
            broker_addr: Some("127.0.0.1:10911".to_string()),
            cluster_name: None,
            topic: "TestTopic".to_string(),
            perm: "6".to_string(),
        };

        assert_eq!(command.topic, "TestTopic");
        assert_eq!(command.perm, "6");
        assert_eq!(command.broker_addr, Some("127.0.0.1:10911".to_string()));
        assert_eq!(command.cluster_name, None);
    }
}