issue #5 - Update pom.xml version to 0.2.0-beta.1-SNAPSHOT, enhance logging, and refine DNS batch record processing method.
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>codes.thischwa</groupId>
|
<groupId>codes.thischwa</groupId>
|
||||||
<artifactId>cloudflaredns</artifactId>
|
<artifactId>cloudflaredns</artifactId>
|
||||||
<version>0.1.1-SNAPSHOT</version>
|
<version>0.2.0-beta.1-SNAPSHOT</version>
|
||||||
<name>CloudflareDNS-java</name>
|
<name>CloudflareDNS-java</name>
|
||||||
<inceptionYear>2025</inceptionYear>
|
<inceptionYear>2025</inceptionYear>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ abstract class CfBasicHttpClient {
|
|||||||
throws CloudflareApiException {
|
throws CloudflareApiException {
|
||||||
try {
|
try {
|
||||||
String jsonPayload = objectMapper.writeValueAsString(requestPayload);
|
String jsonPayload = objectMapper.writeValueAsString(requestPayload);
|
||||||
log.debug("Request payload: {}", jsonPayload);
|
log.trace("Request methode [{}] payload: {}", request.getMethod(), jsonPayload);
|
||||||
request.setEntity(new StringEntity(jsonPayload,
|
request.setEntity(new StringEntity(jsonPayload,
|
||||||
ContentType.APPLICATION_JSON));
|
ContentType.APPLICATION_JSON));
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
|
|||||||
@@ -339,20 +339,19 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Records a batch of DNS record operations, including creating, updating, and deleting records
|
* Processes a batch of DNS record operations (POST, PUT, PATCH, DELETE) for a specified zone.
|
||||||
* within a specific zone. This method processes the provided put, patch, and delete operations
|
* This method builds and cleans the input records, sends the batch request to the Cloudflare API,
|
||||||
* into a clean format before sending a batch request to the Cloudflare API.
|
* and returns a result containing processed batch entries.
|
||||||
*
|
*
|
||||||
* @param zone the zone entity representing the DNS zone where the changes will be applied
|
* @param zone The zone entity to which the records belong.
|
||||||
* @param ttl the time-to-live (TTL) value assigned to the new records being added
|
* @param postRecords A list of DNS records to be created (POST). This parameter is nullable.
|
||||||
* @param postRecords a list of records to be created; each record must contain the necessary
|
* @param putRecords A list of DNS records to be fully replaced (PUT). This parameter is nullable.
|
||||||
* attributes for creation
|
* @param patchRecords A list of DNS records to be partially updated (PATCH). This parameter is nullable.
|
||||||
* @param patchRecords a list of records to be updated; only specific attributes (e.g., content)
|
* @param deleteRecords A list of DNS records to be deleted (DELETE). This parameter is nullable.
|
||||||
* will be modified
|
* @return The resulting {@link BatchEntry} containing the processed records after the batch operation.
|
||||||
* @param deleteRecords a list of records to be deleted; each record must contain name, type, and content
|
* @throws CloudflareApiException If an error occurs while communicating with the Cloudflare API.
|
||||||
* @throws CloudflareApiException if there is an error while communicating with the Cloudflare API
|
|
||||||
*/
|
*/
|
||||||
public void recordBatch(ZoneEntity zone, int ttl, @Nullable List<RecordEntity> postRecords,
|
public BatchEntry recordBatch(ZoneEntity zone, @Nullable List<RecordEntity> postRecords, @Nullable List<RecordEntity> putRecords,
|
||||||
@Nullable List<RecordEntity> patchRecords, @Nullable List<RecordEntity> deleteRecords)
|
@Nullable List<RecordEntity> patchRecords, @Nullable List<RecordEntity> deleteRecords)
|
||||||
throws CloudflareApiException {
|
throws CloudflareApiException {
|
||||||
BatchEntry batchEntry = new BatchEntry();
|
BatchEntry batchEntry = new BatchEntry();
|
||||||
@@ -363,6 +362,12 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
rec -> cleanedPosts.add(RecordEntity.build(rec.getId(), rec.getName(), rec.getType(), rec.getTtl(), rec.getContent())));
|
rec -> cleanedPosts.add(RecordEntity.build(rec.getId(), rec.getName(), rec.getType(), rec.getTtl(), rec.getContent())));
|
||||||
batchEntry.setPosts(cleanedPosts);
|
batchEntry.setPosts(cleanedPosts);
|
||||||
}
|
}
|
||||||
|
if (putRecords != null) {
|
||||||
|
List<RecordEntity> cleanedPuts = new ArrayList<>();
|
||||||
|
putRecords.forEach(
|
||||||
|
rec -> cleanedPuts.add(RecordEntity.build(rec.getId(), rec.getName(), rec.getType(), rec.getTtl(), rec.getContent())));
|
||||||
|
batchEntry.setPuts(cleanedPuts);
|
||||||
|
}
|
||||||
if (patchRecords != null) {
|
if (patchRecords != null) {
|
||||||
List<RecordEntity> cleanedPatches = new ArrayList<>();
|
List<RecordEntity> cleanedPatches = new ArrayList<>();
|
||||||
patchRecords.forEach(rec -> cleanedPatches.add(RecordEntity.build(rec.getId(), rec.getContent())));
|
patchRecords.forEach(rec -> cleanedPatches.add(RecordEntity.build(rec.getId(), rec.getContent())));
|
||||||
@@ -378,6 +383,19 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
String endpoint = CfRequest.RECORD_BATCH.buildPath(zone.getId());
|
String endpoint = CfRequest.RECORD_BATCH.buildPath(zone.getId());
|
||||||
BatchResponse resp = postRequest(endpoint, batchEntry, BatchResponse.class);
|
BatchResponse resp = postRequest(endpoint, batchEntry, BatchResponse.class);
|
||||||
checkResponse(resp);
|
checkResponse(resp);
|
||||||
|
|
||||||
|
// set zone id
|
||||||
|
BatchEntry result = resp.getResult();
|
||||||
|
if (result.getPosts() != null) {
|
||||||
|
result.getPosts().forEach(rec -> rec.setZoneId(zone.getId()));
|
||||||
|
}
|
||||||
|
if (result.getPuts() != null) {
|
||||||
|
result.getPuts().forEach(rec -> rec.setZoneId(zone.getId()));
|
||||||
|
}
|
||||||
|
if (result.getPatches() != null) {
|
||||||
|
result.getPatches().forEach(rec -> rec.setZoneId(zone.getId()));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkResponse(AbstractResponse resp) throws CloudflareApiException {
|
private void checkResponse(AbstractResponse resp) throws CloudflareApiException {
|
||||||
|
|||||||
@@ -53,6 +53,23 @@ public class RecordEntity extends AbstractEntity {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the name of the DNS record.
|
||||||
|
* If the name contains a dot ('.'), only the substring before the first dot is returned.
|
||||||
|
*
|
||||||
|
* @return the name of the DNS record, potentially truncated before the first dot,
|
||||||
|
* or the full name if no dot is present.
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
if (name != null) {
|
||||||
|
int pos = name.indexOf('.');
|
||||||
|
if (pos > 0) {
|
||||||
|
return name.substring(0, pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds and returns a {@link RecordEntity} instance with the specified attributes.
|
* Builds and returns a {@link RecordEntity} instance with the specified attributes.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||||
|
|
||||||
|
import codes.thischwa.cf.model.BatchEntry;
|
||||||
import codes.thischwa.cf.model.RecordEntity;
|
import codes.thischwa.cf.model.RecordEntity;
|
||||||
import codes.thischwa.cf.model.RecordType;
|
import codes.thischwa.cf.model.RecordType;
|
||||||
import codes.thischwa.cf.model.ZoneEntity;
|
import codes.thischwa.cf.model.ZoneEntity;
|
||||||
@@ -157,6 +158,7 @@ public class CfClientTest {
|
|||||||
String sld1 = SLD_STR + "-1";
|
String sld1 = SLD_STR + "-1";
|
||||||
String sld2 = SLD_STR + "-2";
|
String sld2 = SLD_STR + "-2";
|
||||||
String sld3 = SLD_STR + "-3";
|
String sld3 = SLD_STR + "-3";
|
||||||
|
String sld4 = SLD_STR + "-4";
|
||||||
RecordEntity r1 = RecordEntity.build(sld1, RecordType.A, TTL, "130.0.0.1");
|
RecordEntity r1 = RecordEntity.build(sld1, RecordType.A, TTL, "130.0.0.1");
|
||||||
RecordEntity r2 = RecordEntity.build(sld2, RecordType.A, TTL, "130.0.0.2");
|
RecordEntity r2 = RecordEntity.build(sld2, RecordType.A, TTL, "130.0.0.2");
|
||||||
RecordEntity r3 = RecordEntity.build(sld3, RecordType.A, TTL, "130.0.0.3");
|
RecordEntity r3 = RecordEntity.build(sld3, RecordType.A, TTL, "130.0.0.3");
|
||||||
@@ -167,8 +169,15 @@ public class CfClientTest {
|
|||||||
client.recordDeleteTypeIfExists(z, sld3, RecordType.A);
|
client.recordDeleteTypeIfExists(z, sld3, RecordType.A);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// test put
|
// test pos
|
||||||
client.recordBatch(z, 30, List.of(r1, r2, r3), null, null);
|
BatchEntry batchEntry = client.recordBatch(z, List.of(r1, r2, r3), null, null, null);
|
||||||
|
assertEquals(3, batchEntry.getPosts().size());
|
||||||
|
RecordEntity batchedRec = batchEntry.getPosts().get(0);
|
||||||
|
assertNotNull(batchedRec.getId());
|
||||||
|
assertEquals(r1.getName(), batchedRec.getName());
|
||||||
|
assertEquals(r1.getType(), batchedRec.getType());
|
||||||
|
assertNotNull(batchedRec.getCreatedOn());
|
||||||
|
|
||||||
RecordEntity testRec = client.sldInfo(z, sld1, RecordType.A);
|
RecordEntity testRec = client.sldInfo(z, sld1, RecordType.A);
|
||||||
assertEquals("130.0.0.1", testRec.getContent());
|
assertEquals("130.0.0.1", testRec.getContent());
|
||||||
testRec = client.sldInfo(z, sld2, RecordType.A);
|
testRec = client.sldInfo(z, sld2, RecordType.A);
|
||||||
@@ -179,14 +188,20 @@ public class CfClientTest {
|
|||||||
// test patch
|
// test patch
|
||||||
r1 = client.sldInfo(z, sld1, RecordType.A);
|
r1 = client.sldInfo(z, sld1, RecordType.A);
|
||||||
r1.setContent("130.1.0.1");
|
r1.setContent("130.1.0.1");
|
||||||
client.recordBatch(z, 30, null, List.of(r1), null);
|
client.recordBatch(z, null, null, List.of(r1), null);
|
||||||
testRec = client.sldInfo(z, sld1, RecordType.A);
|
testRec = client.sldInfo(z, sld1, RecordType.A);
|
||||||
assertEquals("130.1.0.1", testRec.getContent());
|
assertEquals("130.1.0.1", testRec.getContent());
|
||||||
|
|
||||||
// test delete
|
// test delete
|
||||||
client.recordBatch(z, 30, null, null, List.of(r1));
|
client.recordBatch(z, null, null, null, List.of(r1));
|
||||||
assertThrows(CloudflareNotFoundException.class,
|
assertThrows(CloudflareNotFoundException.class,
|
||||||
() -> client.sldInfo(z, sld1, RecordType.A));
|
() -> client.sldInfo(z, sld1, RecordType.A));
|
||||||
|
|
||||||
|
// test put
|
||||||
|
r1 = RecordEntity.build(sld4, RecordType.A, TTL, "130.1.0.2");
|
||||||
|
client.recordBatch(z, List.of(r1), null, null, null);
|
||||||
|
testRec = client.sldInfo(z, sld4, RecordType.A);
|
||||||
|
assertEquals("130.1.0.2", testRec.getContent());
|
||||||
} finally {
|
} finally {
|
||||||
client.recordDeleteTypeIfExists(z, sld1, RecordType.A);
|
client.recordDeleteTypeIfExists(z, sld1, RecordType.A);
|
||||||
client.recordDeleteTypeIfExists(z, sld2, RecordType.A);
|
client.recordDeleteTypeIfExists(z, sld2, RecordType.A);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
<logger name="org.apache.hc.client5.http" level="info"/>
|
<logger name="org.apache.hc.client5.http" level="info"/>
|
||||||
|
<logger name="codes.thischwa.cf.CfBasicHttpClient" level="trace"/>
|
||||||
|
|
||||||
<root level="debug">
|
<root level="debug">
|
||||||
<appender-ref ref="current"/>
|
<appender-ref ref="current"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user