From 1a51597cec17df009a7a1dc54dff2c03acad03e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gst=C3=B6hl?= Date: Wed, 23 Feb 2022 16:30:25 +0100 Subject: [PATCH] sync: only delete remote rules if they no longer exist locally --- .../verifier/sync/syncer/DgcRulesClient.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java index 34dd63e8..4c584a9a 100644 --- a/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java +++ b/ch-covidcertificate-backend-verifier/ch-covidcertificate-backend-verifier-sync/src/main/java/ch/admin/bag/covidcertificate/backend/verifier/sync/syncer/DgcRulesClient.java @@ -19,11 +19,13 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -70,17 +72,15 @@ public Map download() { } /** - * deletes all rules for Switzerland - * + * deletes the rules with the given IDs + * @param identifiers IDs of the rules to delete * @return successfully deleted rule IDs */ - public RulesSyncResult deleteAll(){ - logger.info("Deleting all Swiss rules"); - Map rules = download(); - Set ruleIds = rules.keySet(); + public RulesSyncResult deleteRules(Collection identifiers){ + logger.info("Deleting {} rules", identifiers); List deletedRuleIds = new ArrayList<>(); List failedRuleIds = new ArrayList<>(); - ruleIds.forEach(ruleId -> { + identifiers.forEach(ruleId -> { try { // sign payload String cms = null; @@ -115,7 +115,7 @@ public RulesSyncResult deleteAll(){ } }); - logger.info("Finished uploading Swiss rules"); + logger.info("Finished deleting rules. {} succeeded, {} failed", deletedRuleIds.size(), failedRuleIds.size()); return new RulesSyncResult(deletedRuleIds, failedRuleIds); } @@ -126,7 +126,7 @@ public RulesSyncResult deleteAll(){ * @return successfully uploaded rule ids */ public RulesSyncResult upload(JsonNode rules) { - deleteAll(); + Set existingRules = download().keySet(); logger.info("Uploading Swiss rules"); List uploadedRuleIds = new ArrayList<>(); List failedRuleIds = new ArrayList<>(); @@ -137,6 +137,7 @@ public RulesSyncResult upload(JsonNode rules) { Entry ruleArray = fieldIterator.next(); for (var rule : ruleArray.getValue()) { String ruleId = ruleArray.getKey(); + existingRules.remove(ruleId); try { // sign payload String cms = null; @@ -170,7 +171,6 @@ public RulesSyncResult upload(JsonNode rules) { logger.error("[FAILED CMS] {}", cms); logger.error("Upload of rule {} failed", ruleId, e); } - continue; } } catch (Exception ex) { failedRuleIds.add(ruleId); @@ -179,6 +179,13 @@ public RulesSyncResult upload(JsonNode rules) { } } logger.info("Finished uploading Swiss rules"); + if (failedRuleIds.isEmpty()) { + logger.info( + "Deleting {} remote rules that no longer exist locally", existingRules.size()); + deleteRules(existingRules); + }else{ + logger.warn("There were upload failures. Skipping rule deletion"); + } return new RulesSyncResult(uploadedRuleIds, failedRuleIds); } }