use clap::Parser;
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_admin_core::client_adapter::services::static_topic::UpdateStaticTopicRequest;
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 UpdateStaticTopicSubCommand {
#[command(flatten)]
common_args: CommonArgs,
#[arg(short = 't', long = "topic", required = true, help = "topic name")]
topic: String,
#[arg(
short = 'b',
long = "brokerAddr",
required = true,
help = "create topic to which broker"
)]
broker_addr: String,
#[arg(short = 'q', long = "totalQueueNum", required = true, help = "totalQueueNum")]
queue_num: String,
#[arg(
short = 'c',
long = "clusterName",
required = false,
help = "cluster that owns the static topic"
)]
cluster_name: 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<String>,
}
impl UpdateStaticTopicSubCommand {
fn update_request(&self) -> CanonicalResult<UpdateStaticTopicRequest> {
UpdateStaticTopicRequest::try_new(
self.topic.clone(),
self.broker_addr.clone(),
&self.queue_num,
self.cluster_name.clone(),
)
.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())
.map(|request| request.with_optional_namesrv_addr(self.common_args.namesrv_addr.clone()))
}
fn force_replace(&self) -> bool {
self.force_replace
.as_ref()
.and_then(|value| value.trim().parse::<bool>().ok())
.unwrap_or(false)
}
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) {
let request = self.mapping_file_request()?;
StaticTopicService::update_static_topic_from_mapping_by_request_with_credentials(
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 UpdateStaticTopicSubCommand {
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::update_static_topic_by_request_with_credentials(
self.update_request()?,
credentials,
client_runtime.clone(),
)
.await?;
Self::write_mapping_plan(&plan)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn update_static_topic_sub_command_builds_core_request() {
let cmd = UpdateStaticTopicSubCommand::try_parse_from([
"updateStaticTopic",
"-t",
" TopicA ",
"-b",
" broker-a,broker-b ",
"-q",
" 8 ",
"-c",
" ClusterA ",
"-n",
" 127.0.0.1:9876 ",
])
.unwrap();
let request = cmd.update_request().unwrap();
assert_eq!(request.topic().as_str(), "TopicA");
assert_eq!(
request
.broker_names()
.iter()
.map(|name| name.as_str())
.collect::<Vec<_>>(),
vec!["broker-a", "broker-b"]
);
assert_eq!(
request
.cluster_names()
.iter()
.map(|name| name.as_str())
.collect::<Vec<_>>(),
vec!["ClusterA"]
);
assert_eq!(request.queue_num(), 8);
assert_eq!(request.namesrv_addr(), Some("127.0.0.1:9876"));
}
}