From c75f9d74ca6695c3f57cfc78e31620f4d560e776 Mon Sep 17 00:00:00 2001 From: Thilo Schwarz Date: Tue, 30 Dec 2025 15:34:55 +0100 Subject: [PATCH] fix #7: Refactor `sldInfo` to return a list of records, update related methods and tests accordingly. Improve logging and exception handling for batch DNS operations. --- .../java/codes/thischwa/cf/CfDnsClient.java | 20 +++++---- .../java/codes/thischwa/cf/CfClientTest.java | 42 ++++++++++++------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/main/java/codes/thischwa/cf/CfDnsClient.java b/src/main/java/codes/thischwa/cf/CfDnsClient.java index be60193..394cb18 100644 --- a/src/main/java/codes/thischwa/cf/CfDnsClient.java +++ b/src/main/java/codes/thischwa/cf/CfDnsClient.java @@ -194,13 +194,13 @@ public class CfDnsClient extends CfBasicHttpClient { * @return the {@link RecordEntity} of the requested SLD and record type * @throws CloudflareApiException if an error occurs during interaction with the Cloudflare API */ - public RecordEntity sldInfo(ZoneEntity zone, String sld, RecordType type) + public List sldInfo(ZoneEntity zone, String sld, RecordType type) throws CloudflareApiException { String fqdn = buildFqdn(zone, sld); String endpoint = CfRequest.RECORD_INFO_NAME_TYPE.buildPath(zone.getId(), fqdn, type); RecordMultipleResponse resp = getRequest(endpoint, RecordMultipleResponse.class); checkResponse(resp, false); - return resp.getResult().get(0); + return resp.getResult(); } /** @@ -290,7 +290,7 @@ public class CfDnsClient extends CfBasicHttpClient { String endpoint = CfRequest.RECORD_DELETE.buildPath(zone.getId(), id); RecordSingleResponse resp = deleteRequest(endpoint); checkResponse(resp); - log.debug("Record {} successful deleted.", id); + log.debug("Record id#{} successful deleted.", id); return resp.getResult().getId().equals(id); } @@ -329,11 +329,17 @@ public class CfDnsClient extends CfBasicHttpClient { String fqdn = buildFqdn(zone, sld); for (RecordType recordType : recordTypes) { try { - RecordEntity rec = sldInfo(zone, sld, recordType); - recordDelete(zone, rec); - log.info("Record {} of type [{}] successful deleted.", fqdn, recordTypes); + List recs = sldInfo(zone, sld, recordType); + recs.forEach(rec -> { + try { + recordDelete(zone, rec); + log.info("Record {} of type [{}] successful deleted.", fqdn, recordType); + } catch (CloudflareApiException e) { + log.error("Failed to delete record {} of type {} for zone {}: {}", fqdn, recordType, zone.getName(), e.getMessage()); + } + }); } catch (CloudflareNotFoundException e) { - log.debug("Record {} of type {} does not exist.", fqdn, recordTypes); + log.debug("Record {} of type {} does not exist.", fqdn, recordType); } } } diff --git a/src/test/java/codes/thischwa/cf/CfClientTest.java b/src/test/java/codes/thischwa/cf/CfClientTest.java index 12f4a96..a8bee66 100644 --- a/src/test/java/codes/thischwa/cf/CfClientTest.java +++ b/src/test/java/codes/thischwa/cf/CfClientTest.java @@ -119,13 +119,17 @@ public class CfClientTest { assertNotNull(createdRe1.getModifiedOn()); // verify sldInfo for A - r = client.sldInfo(z, randomSld, RecordType.A); + List aRecords = client.sldInfo(z, randomSld, RecordType.A); + assertEquals(1, aRecords.size()); + r = aRecords.get(0); assertEquals("130.0.0.3", r.getContent()); // create AAAA record using recordCreateSld createdRe2 = client.recordCreateSld(z, randomSld, TTL, RecordType.AAAA, "2a0a:4cc0:c0:2e4::1"); - r = client.sldInfo(z, randomSld, RecordType.AAAA); + List aaaaRecords = client.sldInfo(z, randomSld, RecordType.AAAA); + assertEquals(1, aaaaRecords.size()); + r = aaaaRecords.get(0); assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent()); assertEquals(RecordType.AAAA.getType(), r.getType()); @@ -145,11 +149,15 @@ public class CfClientTest { // update AAAA record createdRe2.setContent("2a0a:4cc0:c0:2e4::2"); client.recordUpdate(z, createdRe2); - r = client.sldInfo(z, randomSld, RecordType.AAAA); + aaaaRecords = client.sldInfo(z, randomSld, RecordType.AAAA); + assertEquals(1, aaaaRecords.size()); + r = aaaaRecords.get(0); assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent()); // verify A record still intact - r = client.sldInfo(z, randomSld, RecordType.A); + aRecords = client.sldInfo(z, randomSld, RecordType.A); + assertEquals(1, aRecords.size()); + r = aRecords.get(0); assertEquals("130.0.0.3", r.getContent()); // delete AAAA record and verify it's gone @@ -183,6 +191,7 @@ public class CfClientTest { private static final String IP_PREFIX = "130.0.0."; private static final String UPDATED_IP_PREFIX = "130.1.0."; + @Test void testBatch() throws Exception { // starting point: already existing zone 'mein-d-ns.de' @@ -236,8 +245,9 @@ public class CfClientTest { // Verify only the first 2 records for (int i = 0; i < 2; i++) { - RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); - assertEquals(IP_PREFIX + (i + 1), record.getContent()); + List records1 = client.sldInfo(zone, sldNames.get(i), RecordType.A); + assertEquals(1, records1.size()); + assertEquals(IP_PREFIX + (i + 1), records1.get(0).getContent()); } } @@ -245,7 +255,8 @@ public class CfClientTest { // Use first 2 records for PATCH List patchRecords = new ArrayList<>(); for (int i = 0; i < 2; i++) { - RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); + List records = client.sldInfo(zone, sldNames.get(i), RecordType.A); + RecordEntity record = records.get(0); record.setContent(UPDATED_IP_PREFIX + (i + 1)); patchRecords.add(record); } @@ -254,8 +265,9 @@ public class CfClientTest { // Verify both records were updated for (int i = 0; i < 2; i++) { - RecordEntity updatedRecord = client.sldInfo(zone, sldNames.get(i), RecordType.A); - assertEquals(UPDATED_IP_PREFIX + (i + 1), updatedRecord.getContent()); + List updatedRecords = client.sldInfo(zone, sldNames.get(i), RecordType.A); + assertEquals(1, updatedRecords.size()); + assertEquals(UPDATED_IP_PREFIX + (i + 1), updatedRecords.get(0).getContent()); } } @@ -263,8 +275,8 @@ public class CfClientTest { // Delete first 2 records List deleteRecords = new ArrayList<>(); for (int i = 0; i < 2; i++) { - RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); - deleteRecords.add(record); + List records = client.sldInfo(zone, sldNames.get(i), RecordType.A); + deleteRecords.add(records.get(0)); } client.recordBatch(zone, null, null, null, deleteRecords); @@ -289,7 +301,8 @@ public class CfClientTest { // Now use PUT to replace them List putRecords = new ArrayList<>(); for (int i = 0; i < 2; i++) { - RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); + List records = client.sldInfo(zone, sldNames.get(i), RecordType.A); + RecordEntity record = records.get(0); record.setContent(UPDATED_IP_PREFIX + (i + 1)); putRecords.add(record); } @@ -297,8 +310,9 @@ public class CfClientTest { // Verify both records were updated for (int i = 0; i < 2; i++) { - RecordEntity updatedRecord = client.sldInfo(zone, sldNames.get(i), RecordType.A); - assertEquals(UPDATED_IP_PREFIX + (i + 1), updatedRecord.getContent()); + List updatedRecords = client.sldInfo(zone, sldNames.get(i), RecordType.A); + assertEquals(1, updatedRecords.size()); + assertEquals(UPDATED_IP_PREFIX + (i + 1), updatedRecords.get(0).getContent()); } }