Skip to content

Commit

Permalink
fix(sync): fix manual sync config (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
RWDai authored Mar 14, 2024
1 parent 380433a commit e19f273
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 60 deletions.
17 changes: 5 additions & 12 deletions support/iam/src/basic/serv/iam_cert_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,8 +1338,7 @@ impl IamCertServ {
}

/// 第三方集成手动同步方法入口
/// 如果手动导入,那么third_integration_config必须Some
pub async fn third_integration_sync(sync_config: Option<IamThirdIntegrationConfigDto>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
pub async fn manual_third_integration_sync(account_sync_from: IamCertExtKind, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let task_ctx = ctx.clone();
let sync = SYNC_LOCK.try_lock().map_err(|_| funs.err().conflict("third_integration_config", "sync", "The last synchronization has not ended yet", "iam-sync-not-ended"))?;
if let Some(task_id) = funs.cache().get(&funs.conf::<IamConfig>().cache_key_sync_ldap_task_lock).await?.and_then(|task_id| task_id.parse().ok()) {
Expand All @@ -1354,16 +1353,10 @@ impl IamCertServ {
let sync = sync;
let funs = iam_constants::get_tardis_inst();

let sync_config = if let Some(sync_config) = sync_config {
sync_config
} else if let Some(sync_config) = IamCertServ::get_sync_third_integration_config(&funs, &task_ctx).await? {
if sync_config.len() == 1 {
sync_config.into_iter().last().expect("")
} else {
match sync_config.into_iter().find(|sync_config| sync_config.account_sync_cron.is_none()) {
Some(config) => config,
None => return Err(funs.err().conflict("ldap_account", "sync", "should have sync config!", "iam-not-found-sync-config")),
}
let sync_config = if let Some(sync_config) = IamCertServ::get_sync_third_integration_config(&funs, &task_ctx).await? {
match sync_config.into_iter().find(|sync_config| sync_config.account_sync_cron.is_none() && sync_config.account_sync_from == account_sync_from) {
Some(config) => config,
None => return Err(funs.err().conflict("ldap_account", "sync", "should have sync config!", "iam-not-found-sync-config")),
}
} else {
return Err(funs.err().conflict("ldap_account", "sync", "should have sync config!", "iam-not-found-sync-config"));
Expand Down
14 changes: 2 additions & 12 deletions support/iam/src/console_system/api/iam_cs_cert_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,11 @@ impl IamCsCertApi {
///
/// 手动触发第三方集成同步,如果有其他同步正在进行中,那么就会返回错误。
#[oai(path = "/sync", method = "post")]
async fn third_integration_sync(&self, account_sync_from: Json<IamCertExtKind>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Option<String>> {
async fn manual_third_integration_sync(&self, account_sync_from: Json<IamCertExtKind>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Option<String>> {
add_remote_ip(request, &ctx.0).await?;
let mut funs = iam_constants::get_tardis_inst();
funs.begin().await?;
IamCertServ::third_integration_sync(
Some(IamThirdIntegrationConfigDto {
account_sync_from: account_sync_from.0,
account_sync_cron: None,
account_way_to_add: Default::default(),
account_way_to_delete: Default::default(),
}),
&funs,
&ctx.0,
)
.await?;
IamCertServ::manual_third_integration_sync(account_sync_from.0, &funs, &ctx.0).await?;
funs.commit().await?;
ctx.0.execute_task().await?;
if let Some(task_id) = TaskProcessor::get_task_id_with_ctx(&ctx.0).await? {
Expand Down
39 changes: 3 additions & 36 deletions support/iam/tests/test_iam_cert_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,7 @@ pub async fn test(ldap_account_num: u64, conf_ldap_add_or_modify_req: IamCertCon
assert_eq!(account_page.total_size, 2);

info!("【exec manual sync】");
IamCertServ::third_integration_sync(
Some(IamThirdIntegrationConfigDto {
account_sync_from: IamCertExtKind::Ldap,
account_sync_cron: None,
account_way_to_add: WayToAdd::default(),
account_way_to_delete: WayToDelete::default(),
}),
&funs,
admin_ctx,
)
.await
.unwrap();
IamCertServ::manual_third_integration_sync(IamCertExtKind::Ldap, &funs, admin_ctx).await.unwrap();

if let Some(task_id) = TaskProcessor::get_task_id_with_ctx(admin_ctx).await.unwrap() {
tokio::spawn(async move {
Expand All @@ -105,18 +94,7 @@ pub async fn test(ldap_account_num: u64, conf_ldap_add_or_modify_req: IamCertCon
}

info!("【When a sync task is in progress, executing the sync method again will response an error】");
assert!(IamCertServ::third_integration_sync(
Some(IamThirdIntegrationConfigDto {
account_sync_from: IamCertExtKind::Ldap,
account_sync_cron: None,
account_way_to_add: WayToAdd::default(),
account_way_to_delete: WayToDelete::default(),
}),
&funs,
admin_ctx,
)
.await
.is_err());
assert!(IamCertServ::manual_third_integration_sync(IamCertExtKind::Ldap, &funs, admin_ctx,).await.is_err());

//wait for sync task done;
sleep(Duration::from_millis(500)).await;
Expand All @@ -139,18 +117,7 @@ pub async fn test(ldap_account_num: u64, conf_ldap_add_or_modify_req: IamCertCon
.unwrap();

info!("【exec manual sync 2】");
IamCertServ::third_integration_sync(
Some(IamThirdIntegrationConfigDto {
account_sync_from: IamCertExtKind::Ldap,
account_sync_cron: None,
account_way_to_add: WayToAdd::default(),
account_way_to_delete: WayToDelete::default(),
}),
&funs,
admin_ctx,
)
.await
.unwrap();
IamCertServ::manual_third_integration_sync(IamCertExtKind::Ldap, &funs, admin_ctx).await.unwrap();
let account_ldap_cert: Vec<Option<&String>> = account_page.records.iter().map(|a| a.certs.get(&conf_ldap_add_or_modify_req.name)).filter(|o| o.is_some()).collect();

assert_eq!(account_ldap_cert.len() as u64, ldap_account_num);
Expand Down

0 comments on commit e19f273

Please sign in to comment.