use clap::Parser;
use rocketmq_admin_core::client_adapter::services::static_topic::RemappingStaticTopicRequest;
use rocketmq_admin_core::client_adapter::services::static_topic::StaticTopicMappingFileRequest;
use rocketmq_admin_core::client_adapter::services::static_topic::StaticTopicMappingPlan;
use rocketmq_admin_core::client_adapter::services::static_topic::StaticTopicService;
use rocketmq_error::Result as CanonicalResult;
use crate::commands::CommandExecute;
use crate::commands::CommonArgs;
use crate::commands::topic::static_topic_file;
#[derive(Debug, Clone, Parser)]
pub struct RemappingStaticTopicSubCommand {
#[command(flatten)]
common_args: CommonArgs,
#[arg(short = 't', long = "topic", required = true, help = "topic name")]
topic: String,
#[arg(
short = 'b',
long = "brokers",
required = false,
help = "remapping static topic to brokers, comma separated"
)]
brokers: Option<String>,
#[arg(
short = 'c',
long = "clusters",
required = false,
help = "remapping static topic to clusters, comma separated"
)]
clusters: Option<String>,
#[arg(short = 'm', long = "mapFile", required = false, help = "The mapping data file name")]
mapping_file: Option<String>,
#[arg(
short = 'f',
long = "forceReplace",
required = false,
help = "Force replace the old mapping"
)]
force_replace: Option<bool>,
}
impl RemappingStaticTopicSubCommand {
fn remapping_request(&self) -> CanonicalResult<RemappingStaticTopicRequest> {
RemappingStaticTopicRequest::try_new(
self.topic.clone(),
self.brokers.clone(),
self.clusters.clone(),
self.force_replace,
)
.map(|request| request.with_optional_namesrv_addr(self.common_args.namesrv_addr.clone()))
}
fn mapping_file_request(&self) -> CanonicalResult<StaticTopicMappingFileRequest> {
StaticTopicMappingFileRequest::try_new(self.topic.clone(), self.force_replace.unwrap_or(false))
.map(|request| request.with_optional_namesrv_addr(self.common_args.namesrv_addr.clone()))
}
pub async fn execute_from_file(
&self,
map_file_name: &str,
credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
) -> CanonicalResult<()> {
let map_file_name = map_file_name.trim();
if let Some(wrapper) = static_topic_file::read_mapping(map_file_name) {
StaticTopicService::remapping_static_topic_from_mapping_by_request_with_credentials(
self.mapping_file_request()?,
wrapper,
credentials,
client_runtime,
)
.await?;
}
Ok(())
}
fn write_mapping_plan(plan: &StaticTopicMappingPlan) -> CanonicalResult<()> {
let old_mapping_data_file = static_topic_file::write_mapping(&plan.old_mapping, false)?;
println!("The old mapping data is written to file {}", old_mapping_data_file);
let new_mapping_data_file = static_topic_file::write_mapping(&plan.new_mapping, true)?;
println!("The new mapping data is written to file {}", new_mapping_data_file);
Ok(())
}
}
impl CommandExecute for RemappingStaticTopicSubCommand {
async fn execute(
&self,
credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
) -> CanonicalResult<()> {
if let Some(f_name) = &self.mapping_file {
return self
.execute_from_file(f_name, credentials, client_runtime.clone())
.await;
}
let plan = StaticTopicService::remapping_static_topic_by_request_with_credentials(
self.remapping_request()?,
credentials,
client_runtime,
)
.await?;
Self::write_mapping_plan(&plan)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn remapping_static_topic_sub_command_parse_broker_mode() {
let args = vec!["remapping-static-topic", "-t", "test-topic", "-b", "broker-a,broker-b"];
let cmd = RemappingStaticTopicSubCommand::try_parse_from(args).unwrap();
assert_eq!(cmd.topic, "test-topic");
assert_eq!(cmd.brokers, Some("broker-a,broker-b".to_string()));
}
#[test]
fn remapping_static_topic_sub_command_parse_cluster_mode() {
let args = vec!["remapping-static-topic", "-t", "test-topic", "-c", "DefaultCluster"];
let cmd = RemappingStaticTopicSubCommand::try_parse_from(args).unwrap();
assert_eq!(cmd.topic, "test-topic");
assert_eq!(cmd.clusters, Some("DefaultCluster".to_string()));
}
#[test]
fn remapping_static_topic_sub_command_parse_with_file() {
let args = vec!["remapping-static-topic", "-t", "test-topic", "-m", "/tmp/mapping.json"];
let cmd = RemappingStaticTopicSubCommand::try_parse_from(args).unwrap();
assert_eq!(cmd.mapping_file, Some("/tmp/mapping.json".to_string()));
}
#[test]
fn remapping_static_topic_sub_command_builds_core_request() {
let args = vec![
"remapping-static-topic",
"-t",
" test-topic ",
"-b",
" broker-a,broker-b ",
"-f",
"true",
"-n",
" 127.0.0.1:9876 ",
];
let cmd = RemappingStaticTopicSubCommand::try_parse_from(args).unwrap();
let request = cmd.remapping_request().unwrap();
assert_eq!(request.topic().as_str(), "test-topic");
assert_eq!(
request
.broker_names()
.iter()
.map(|name| name.as_str())
.collect::<Vec<_>>(),
vec!["broker-a", "broker-b"]
);
assert!(request.force_replace());
assert_eq!(request.namesrv_addr(), Some("127.0.0.1:9876"));
}
}