diff --git a/src/main/java/codes/thischwa/cf/CfDnsClient.java b/src/main/java/codes/thischwa/cf/CfDnsClient.java index a753f9a..282430a 100644 --- a/src/main/java/codes/thischwa/cf/CfDnsClient.java +++ b/src/main/java/codes/thischwa/cf/CfDnsClient.java @@ -86,6 +86,10 @@ public class CfDnsClient extends CfBasicHttpClient { this.emptyResultThrowsException = emptyResultThrowsException; } + private static String buildFqdn(ZoneEntity zone, String sld) { + return sld + "." + zone.getName(); + } + /** * Retrieves a list of all zones from the Cloudflare API. * @@ -152,7 +156,7 @@ public class CfDnsClient extends CfBasicHttpClient { */ public List sldListAll(ZoneEntity zone, String sld, PagingRequest pagingRequest) throws CloudflareApiException { - String fqdn = sld + "." + zone.getName(); + String fqdn = buildFqdn(zone, sld); String endpoint = pagingRequest.addQueryString(CfRequest.RECORD_INFO_NAME.buildPath(zone.getId(), fqdn)); RecordMultipleResponse resp = getRequest(endpoint, RecordMultipleResponse.class); @@ -173,13 +177,32 @@ public class CfDnsClient extends CfBasicHttpClient { */ public RecordEntity sldInfo(ZoneEntity zone, String sld, RecordType type) throws CloudflareApiException { - String fqdn = sld + "." + zone.getName(); + 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, true); return resp.getResult().get(0); } + /** + * Creates a DNS record in the specified DNS zone with the provided details. + * + * @param zone the DNS zone in which the record will be created + * @param sld the second-level domain (SLD) used for constructing the fully qualified domain name (FQDN) + * @param ttl the time-to-live (TTL) value for the DNS record + * @param type the type of the DNS record (e.g., A, AAAA, CNAME) + * @param content the content or value of the DNS record + * @return the created DNS record as a {@link RecordEntity} object + * @throws CloudflareApiException if an error occurs while interacting with the Cloudflare API + */ + public RecordEntity recordCreate( + ZoneEntity zone, String sld, int ttl, RecordType type, String content) + throws CloudflareApiException { + String fqdn = buildFqdn(zone, sld); + RecordEntity rec = RecordEntity.build(fqdn, type, ttl, content); + return recordCreate(zone, rec); + } + /** * Creates a new DNS record in the specified zone using the Cloudflare API. * @@ -259,18 +282,20 @@ public class CfDnsClient extends CfBasicHttpClient { * @param zone The zone in which the DNS record resides. It provides information about the domain. * @param sld The second-level domain (SLD) of the fully qualified domain name (FQDN) for which * the record is being deleted. - * @param type The type of the DNS record to be deleted (e.g., A, CNAME, TXT). + * @param recordTypes The type of the DNS record to be deleted (e.g., A, CNAME, TXT). * @throws CloudflareApiException If an error occurs while interacting with the Cloudflare API. */ - public void recordDeleteTypeIfExists(ZoneEntity zone, String sld, RecordType type) + public void recordDeleteTypeIfExists(ZoneEntity zone, String sld, RecordType... recordTypes) throws CloudflareApiException { - String fqdn = sld + "." + zone.getName(); - try { - RecordEntity rec = sldInfo(zone, sld, type); - recordDelete(zone, rec); - log.debug("Record {} of type {} successful deleted.", fqdn, type); - } catch (CloudflareNotFoundException e) { - log.debug("Record {} of type {} does not exist.", fqdn, type); + String fqdn = buildFqdn(zone, sld); + for (RecordType recordType : recordTypes) { + try { + RecordEntity rec = sldInfo(zone, sld, recordType); + recordDelete(zone, rec); + log.debug("Record {} of type {} successful deleted.", fqdn, recordTypes); + } catch (CloudflareNotFoundException e) { + log.debug("Record {} of type {} does not exist.", fqdn, recordTypes); + } } } diff --git a/src/test/java/codes/thischwa/cf/CfClientTest.java b/src/test/java/codes/thischwa/cf/CfClientTest.java index cb2f700..23ae6e5 100644 --- a/src/test/java/codes/thischwa/cf/CfClientTest.java +++ b/src/test/java/codes/thischwa/cf/CfClientTest.java @@ -11,18 +11,17 @@ import java.util.List; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; -// TODO: #testDns should be clean-up it's test data @Slf4j public class CfClientTest { - private static final String zoneStr = "mein-d-ns.de"; - private static final String sldStr = "devsld"; - private static int ttl = 60; + private static final String ZONE_STR = "mein-d-ns.de"; + private static final String SLD_STR = "devsld"; + private static final int TTL = 60; - private final String email = System.getenv("API_EMAIL"); - private final String apiKey = System.getenv("API_KEY"); + private final String API_EMAIL = System.getenv("API_EMAIL"); + private final String API_KEY = System.getenv("API_KEY"); - private final CfDnsClient client = new CfDnsClient(email, apiKey); + private final CfDnsClient client = new CfDnsClient(API_EMAIL, API_KEY); @Test void testList() throws Exception { @@ -33,17 +32,17 @@ public class CfClientTest { assertFalse(rList.isEmpty()); assertThrows( - CloudflareNotFoundException.class, () -> client.sldListAll(zList.get(0), "notexisting")); + CloudflareNotFoundException.class, () -> client.sldListAll(zList.get(0), "not-existing")); client.setEmptyResultThrowsException(false); - rList = client.sldListAll(zList.get(0), "notexisting"); + rList = client.sldListAll(zList.get(0), "not-existing"); assertTrue(rList.isEmpty()); client.setEmptyResultThrowsException(true); } @Test void testDns() throws Exception { - ZoneEntity z = client.zoneInfo(zoneStr); + ZoneEntity z = client.zoneInfo(ZONE_STR); assertEquals("0a83dd6e7f8c46039f2517bbded8115e", z.getId()); assertEquals("mein-d-ns.de", z.getName()); assertEquals("active", z.getStatus()); @@ -61,33 +60,47 @@ public class CfClientTest { assertEquals("test.mein-d-ns.de", r.getName()); assertEquals("A", r.getType()); assertEquals("129.0.0.3", r.getContent()); + r = client.sldInfo(z, "test", RecordType.AAAA); + assertEquals("f76c420362220e0c8bab80cd08028214", r.getId()); + assertEquals("test.mein-d-ns.de", r.getName()); + assertEquals(RecordType.AAAA.getType(), r.getType()); + assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent()); + String domain = SLD_STR + "." + ZONE_STR; + client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A, RecordType.AAAA); RecordEntity createdRe1 = client.recordCreate( - z, RecordEntity.build(sldStr + "." + zoneStr, RecordType.A, ttl, "130.0.0.3")); - r = client.sldInfo(z, sldStr, RecordType.A); + z, RecordEntity.build(domain, RecordType.A, TTL, "130.0.0.3")); + assertNotNull(createdRe1.getId()); + assertEquals(domain, createdRe1.getName()); + assertEquals(RecordType.A.getType(), createdRe1.getType()); + assertEquals(TTL, createdRe1.getTtl()); + assertEquals("130.0.0.3", createdRe1.getContent()); + assertNotNull(createdRe1.getCreatedOn()); + assertNotNull(createdRe1.getModifiedOn()); + + r = client.sldInfo(z, SLD_STR, RecordType.A); assertEquals("130.0.0.3", r.getContent()); RecordEntity createdRe2 = client.recordCreate( - z, - RecordEntity.build( - sldStr + "." + zoneStr, RecordType.AAAA, ttl, "2a0a:4cc0:c0:2e4::1")); - r = client.sldInfo(z, sldStr, RecordType.AAAA); + z, SLD_STR, TTL, RecordType.AAAA, "2a0a:4cc0:c0:2e4::1"); + r = client.sldInfo(z, SLD_STR, RecordType.AAAA); assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent()); + assertEquals(RecordType.AAAA.getType(), r.getType()); createdRe2.setContent("2a0a:4cc0:c0:2e4::2"); client.recordUpdate(z, createdRe2); - r = client.sldInfo(z, sldStr, RecordType.AAAA); + r = client.sldInfo(z, SLD_STR, RecordType.AAAA); assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent()); - r = client.sldInfo(z, sldStr, RecordType.A); + r = client.sldInfo(z, SLD_STR, RecordType.A); assertEquals("130.0.0.3", r.getContent()); assertTrue(client.recordDelete(z, createdRe2)); assertThrows( - CloudflareNotFoundException.class, () -> client.sldInfo(z, sldStr, RecordType.AAAA)); + CloudflareNotFoundException.class, () -> client.sldInfo(z, SLD_STR, RecordType.AAAA)); - client.recordDeleteTypeIfExists(z, sldStr, RecordType.A); - assertThrows(CloudflareNotFoundException.class, () -> client.sldInfo(z, sldStr, RecordType.A)); + client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A); + assertThrows(CloudflareNotFoundException.class, () -> client.sldInfo(z, SLD_STR, RecordType.A)); } @Test