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.

This commit is contained in:
2025-12-30 15:34:55 +01:00
parent 1abe888a65
commit c75f9d74ca
2 changed files with 41 additions and 21 deletions
@@ -194,13 +194,13 @@ public class CfDnsClient extends CfBasicHttpClient {
* @return the {@link RecordEntity} of the requested SLD and record type * @return the {@link RecordEntity} of the requested SLD and record type
* @throws CloudflareApiException if an error occurs during interaction with the Cloudflare API * @throws CloudflareApiException if an error occurs during interaction with the Cloudflare API
*/ */
public RecordEntity sldInfo(ZoneEntity zone, String sld, RecordType type) public List<RecordEntity> sldInfo(ZoneEntity zone, String sld, RecordType type)
throws CloudflareApiException { throws CloudflareApiException {
String fqdn = buildFqdn(zone, sld); String fqdn = buildFqdn(zone, sld);
String endpoint = CfRequest.RECORD_INFO_NAME_TYPE.buildPath(zone.getId(), fqdn, type); String endpoint = CfRequest.RECORD_INFO_NAME_TYPE.buildPath(zone.getId(), fqdn, type);
RecordMultipleResponse resp = getRequest(endpoint, RecordMultipleResponse.class); RecordMultipleResponse resp = getRequest(endpoint, RecordMultipleResponse.class);
checkResponse(resp, false); 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); String endpoint = CfRequest.RECORD_DELETE.buildPath(zone.getId(), id);
RecordSingleResponse resp = deleteRequest(endpoint); RecordSingleResponse resp = deleteRequest(endpoint);
checkResponse(resp); checkResponse(resp);
log.debug("Record {} successful deleted.", id); log.debug("Record id#{} successful deleted.", id);
return resp.getResult().getId().equals(id); return resp.getResult().getId().equals(id);
} }
@@ -329,11 +329,17 @@ public class CfDnsClient extends CfBasicHttpClient {
String fqdn = buildFqdn(zone, sld); String fqdn = buildFqdn(zone, sld);
for (RecordType recordType : recordTypes) { for (RecordType recordType : recordTypes) {
try { try {
RecordEntity rec = sldInfo(zone, sld, recordType); List<RecordEntity> recs = sldInfo(zone, sld, recordType);
recs.forEach(rec -> {
try {
recordDelete(zone, rec); recordDelete(zone, rec);
log.info("Record {} of type [{}] successful deleted.", fqdn, recordTypes); 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) { } catch (CloudflareNotFoundException e) {
log.debug("Record {} of type {} does not exist.", fqdn, recordTypes); log.debug("Record {} of type {} does not exist.", fqdn, recordType);
} }
} }
} }
@@ -119,13 +119,17 @@ public class CfClientTest {
assertNotNull(createdRe1.getModifiedOn()); assertNotNull(createdRe1.getModifiedOn());
// verify sldInfo for A // verify sldInfo for A
r = client.sldInfo(z, randomSld, RecordType.A); List<RecordEntity> aRecords = client.sldInfo(z, randomSld, RecordType.A);
assertEquals(1, aRecords.size());
r = aRecords.get(0);
assertEquals("130.0.0.3", r.getContent()); assertEquals("130.0.0.3", r.getContent());
// create AAAA record using recordCreateSld // create AAAA record using recordCreateSld
createdRe2 = createdRe2 =
client.recordCreateSld(z, randomSld, TTL, RecordType.AAAA, "2a0a:4cc0:c0:2e4::1"); client.recordCreateSld(z, randomSld, TTL, RecordType.AAAA, "2a0a:4cc0:c0:2e4::1");
r = client.sldInfo(z, randomSld, RecordType.AAAA); List<RecordEntity> aaaaRecords = client.sldInfo(z, randomSld, RecordType.AAAA);
assertEquals(1, aaaaRecords.size());
r = aaaaRecords.get(0);
assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent()); assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent());
assertEquals(RecordType.AAAA.getType(), r.getType()); assertEquals(RecordType.AAAA.getType(), r.getType());
@@ -145,11 +149,15 @@ public class CfClientTest {
// update AAAA record // update AAAA record
createdRe2.setContent("2a0a:4cc0:c0:2e4::2"); createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
client.recordUpdate(z, createdRe2); 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()); assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent());
// verify A record still intact // 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()); assertEquals("130.0.0.3", r.getContent());
// delete AAAA record and verify it's gone // 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 IP_PREFIX = "130.0.0.";
private static final String UPDATED_IP_PREFIX = "130.1.0."; private static final String UPDATED_IP_PREFIX = "130.1.0.";
@Test @Test
void testBatch() throws Exception { void testBatch() throws Exception {
// starting point: already existing zone 'mein-d-ns.de' // starting point: already existing zone 'mein-d-ns.de'
@@ -236,8 +245,9 @@ public class CfClientTest {
// Verify only the first 2 records // Verify only the first 2 records
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); List<RecordEntity> records1 = client.sldInfo(zone, sldNames.get(i), RecordType.A);
assertEquals(IP_PREFIX + (i + 1), record.getContent()); 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 // Use first 2 records for PATCH
List<RecordEntity> patchRecords = new ArrayList<>(); List<RecordEntity> patchRecords = new ArrayList<>();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); List<RecordEntity> records = client.sldInfo(zone, sldNames.get(i), RecordType.A);
RecordEntity record = records.get(0);
record.setContent(UPDATED_IP_PREFIX + (i + 1)); record.setContent(UPDATED_IP_PREFIX + (i + 1));
patchRecords.add(record); patchRecords.add(record);
} }
@@ -254,8 +265,9 @@ public class CfClientTest {
// Verify both records were updated // Verify both records were updated
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
RecordEntity updatedRecord = client.sldInfo(zone, sldNames.get(i), RecordType.A); List<RecordEntity> updatedRecords = client.sldInfo(zone, sldNames.get(i), RecordType.A);
assertEquals(UPDATED_IP_PREFIX + (i + 1), updatedRecord.getContent()); 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 // Delete first 2 records
List<RecordEntity> deleteRecords = new ArrayList<>(); List<RecordEntity> deleteRecords = new ArrayList<>();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); List<RecordEntity> records = client.sldInfo(zone, sldNames.get(i), RecordType.A);
deleteRecords.add(record); deleteRecords.add(records.get(0));
} }
client.recordBatch(zone, null, null, null, deleteRecords); client.recordBatch(zone, null, null, null, deleteRecords);
@@ -289,7 +301,8 @@ public class CfClientTest {
// Now use PUT to replace them // Now use PUT to replace them
List<RecordEntity> putRecords = new ArrayList<>(); List<RecordEntity> putRecords = new ArrayList<>();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
RecordEntity record = client.sldInfo(zone, sldNames.get(i), RecordType.A); List<RecordEntity> records = client.sldInfo(zone, sldNames.get(i), RecordType.A);
RecordEntity record = records.get(0);
record.setContent(UPDATED_IP_PREFIX + (i + 1)); record.setContent(UPDATED_IP_PREFIX + (i + 1));
putRecords.add(record); putRecords.add(record);
} }
@@ -297,8 +310,9 @@ public class CfClientTest {
// Verify both records were updated // Verify both records were updated
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
RecordEntity updatedRecord = client.sldInfo(zone, sldNames.get(i), RecordType.A); List<RecordEntity> updatedRecords = client.sldInfo(zone, sldNames.get(i), RecordType.A);
assertEquals(UPDATED_IP_PREFIX + (i + 1), updatedRecord.getContent()); assertEquals(1, updatedRecords.size());
assertEquals(UPDATED_IP_PREFIX + (i + 1), updatedRecords.get(0).getContent());
} }
} }