use clap::ArgGroup;
use clap::Parser;
use rocketmq_admin_core::client_adapter::services::auth::AuthService;
use rocketmq_admin_core::client_adapter::services::auth::CreateAclRequest;
use rocketmq_error::Result as CanonicalResult;
use crate::commands::CommandExecute;
#[derive(Debug, Clone, Parser)]
#[command(group(ArgGroup::new("target")
.required(true)
.args(&["cluster_name", "broker_addr"])))]
pub struct CreateAclSubCommand {
#[arg(
short = 'c',
long = "clusterName",
required = false,
help = "create acl to which cluster"
)]
cluster_name: Option<String>,
#[arg(
short = 'b',
long = "brokerAddr",
required = false,
help = "create acl to which broker"
)]
broker_addr: Option<String>,
#[arg(
short = 's',
long = "subject",
required = true,
help = "the subject of acl to create"
)]
subject: String,
#[arg(
short = 'r',
long = "resources",
required = true,
help = "the resources of acl to create"
)]
resources: String,
#[arg(
short = 'a',
long = "actions",
required = true,
help = "the actions of acl to create"
)]
actions: String,
#[arg(
short = 'd',
long = "decision",
required = true,
help = "the decision of acl to create"
)]
decision: String,
#[arg(
short = 'i',
long = "sourceIp",
required = false,
help = "the sourceIps of acl to create"
)]
source_ip: Option<String>,
}
impl CommandExecute for CreateAclSubCommand {
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 request = CreateAclRequest::try_new(
self.broker_addr.clone(),
self.cluster_name.clone(),
self.subject.clone(),
self.resources.clone(),
self.actions.clone(),
self.decision.clone(),
self.source_ip.clone(),
)?;
let result =
AuthService::create_acl_by_request_with_credentials(request, credentials, client_runtime.clone()).await?;
for broker_addr in result.broker_addrs {
println!("create acl to {} success.", broker_addr);
}
Ok(())
}
}