use clap::Parser;
use rocketmq_admin_core::client_adapter::services::auth::AuthService;
use rocketmq_admin_core::client_adapter::services::auth::CopyAclRequest;
use rocketmq_admin_core::client_adapter::services::auth::CopyAclResult;
use rocketmq_error::Result as CanonicalResult;
use crate::commands::CommandExecute;
#[derive(Debug, Clone, Parser)]
pub struct CopyAclSubCommand {
#[arg(
short = 'f',
long = "fromBroker",
required = true,
help = "the source broker that the acls copy from"
)]
from_broker: String,
#[arg(
short = 't',
long = "toBroker",
required = true,
help = "the target broker that the acls copy to"
)]
to_broker: String,
#[arg(
short = 's',
long = "subjects",
required = false,
help = "the subject list of acl to copy"
)]
subjects: Option<String>,
}
impl CommandExecute for CopyAclSubCommand {
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 = CopyAclRequest::try_new(self.from_broker.clone(), self.to_broker.clone(), self.subjects.clone())?;
let from_broker = request.from_broker().clone();
let to_broker = request.to_broker().clone();
let result =
AuthService::copy_acl_by_request_with_credentials(request, credentials, client_runtime.clone()).await?;
render_copy_acl_result(result, from_broker.as_str(), to_broker.as_str());
Ok(())
}
}
fn render_copy_acl_result(result: CopyAclResult, source_broker: &str, target_broker: &str) {
if result.copied_subjects.is_empty() && result.skipped_subjects.is_empty() && result.failures.is_empty() {
println!("No ACLs found to copy from {}.", source_broker);
return;
}
for subject in result.skipped_subjects {
if subject.is_empty() {
eprintln!("Warning: ACL has no subject, skipping.");
} else {
eprintln!("Warning: Could not find ACL {} at broker {}", subject, source_broker);
}
}
for subject in result.copied_subjects {
println!(
"copy acl of {} from {} to {} success.",
subject, source_broker, target_broker
);
}
for failure in result.failures {
if failure.broker_addr.as_str() == source_broker {
eprintln!(
"Warning: Failed to get ACL from {} while copying to {}: {}",
source_broker, target_broker, failure.error
);
} else {
eprintln!(
"copy acl from {} to {} failed: {}",
source_broker, target_broker, failure.error
);
}
}
}