From e19f2737a629188f612c3ccd12e9b45bb8e4a536 Mon Sep 17 00:00:00 2001 From: RWDai <27391645+RWDai@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:45:17 +0800 Subject: [PATCH] fix(sync): fix manual sync config (#635) --- support/iam/src/basic/serv/iam_cert_serv.rs | 17 +++----- .../src/console_system/api/iam_cs_cert_api.rs | 14 +------ support/iam/tests/test_iam_cert_sync.rs | 39 ++----------------- 3 files changed, 10 insertions(+), 60 deletions(-) diff --git a/support/iam/src/basic/serv/iam_cert_serv.rs b/support/iam/src/basic/serv/iam_cert_serv.rs index 9f1800ad3..36544cdfc 100644 --- a/support/iam/src/basic/serv/iam_cert_serv.rs +++ b/support/iam/src/basic/serv/iam_cert_serv.rs @@ -1338,8 +1338,7 @@ impl IamCertServ { } /// 第三方集成手动同步方法入口 - /// 如果手动导入,那么third_integration_config必须Some - pub async fn third_integration_sync(sync_config: Option, 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::().cache_key_sync_ldap_task_lock).await?.and_then(|task_id| task_id.parse().ok()) { @@ -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")); diff --git a/support/iam/src/console_system/api/iam_cs_cert_api.rs b/support/iam/src/console_system/api/iam_cs_cert_api.rs index a299e9946..b94184360 100644 --- a/support/iam/src/console_system/api/iam_cs_cert_api.rs +++ b/support/iam/src/console_system/api/iam_cs_cert_api.rs @@ -166,21 +166,11 @@ impl IamCsCertApi { /// /// 手动触发第三方集成同步,如果有其他同步正在进行中,那么就会返回错误。 #[oai(path = "/sync", method = "post")] - async fn third_integration_sync(&self, account_sync_from: Json, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult> { + async fn manual_third_integration_sync(&self, account_sync_from: Json, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult> { 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? { diff --git a/support/iam/tests/test_iam_cert_sync.rs b/support/iam/tests/test_iam_cert_sync.rs index 2a8840633..4683434b0 100644 --- a/support/iam/tests/test_iam_cert_sync.rs +++ b/support/iam/tests/test_iam_cert_sync.rs @@ -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 { @@ -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; @@ -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> = 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);