Refactor record creation method to improve clarity

Renamed `recordCreate` to `recordCreateSld` to better align with its function of creating records specifically for SLDs. Updated relevant tests, documentation, and method references to use the new naming for consistency and improved readability.
This commit is contained in:
2025-04-23 16:29:05 +02:00
parent 41b44b22c2
commit 51a96d3583
3 changed files with 69 additions and 54 deletions
+3 -3
View File
@@ -139,11 +139,11 @@ Create a new DNS record in a specific zone.
- **Parameters**:
- `ZoneEntity zone` - DNS zone object.
- `RecordEntity rec` - Details of the new record (name, type, content).
- `String sld` - The sub-tld of the new record.
- `int ttl` - The time-to-live in seconds of the new rcord.
```java
RecordEntity newRecord = new RecordEntity("api.example.com", RecordType.A, "192.168.1.1");
RecordEntity created = cfDnsClient.recordCreate(zone, newRecord);
RecordEntity created = client.recordCreateSld(zone, "api", 60, RecordType.A, "192.168.1.1");
System.out.println("Created Record ID: " + created.getId());
```
@@ -20,18 +20,15 @@ import lombok.extern.slf4j.Slf4j;
* deleting DNS records.
*
* <p>Example:
*
* <pre><code>
* // Create a new CfDnsClient instance
* CfDnsClient cfDnsClient = new CfDnsClient(
* "email@example.com",
* "yourApiKey"
* );
*
* // Retrieve a zone
* ZoneEntity zone = cfDnsClient.zoneInfo("example.com");
* System.out.println("Zone ID: " + zone.getId());
*
* // Retrieve records of a zone
* List&lt;{@link RecordEntity}&gt; records = cfDnsClient.sldListAll(zone, "sld");
* records.forEach(record ->
@@ -78,11 +75,13 @@ public class CfDnsClient extends CfBasicHttpClient {
* @param emptyResultThrowsException Specifies if an exception should be thrown when the API
* response is empty. Default is true.
* @param baseUrl The base URL for the Cloudflare API endpoint.
* @param authEmail The email associated with the Cloudflare account for authentication.
* @param authKey The API key for authenticating the client with Cloudflare services.
* @param authEmail The email associated with the Cloudflare account for
* authentication.
* @param authKey The API key for authenticating the client with Cloudflare
* services.
*/
public CfDnsClient(
boolean emptyResultThrowsException, String baseUrl, String authEmail, String authKey) {
public CfDnsClient(boolean emptyResultThrowsException, String baseUrl, String authEmail,
String authKey) {
super(baseUrl, authEmail, authKey);
this.emptyResultThrowsException = emptyResultThrowsException;
}
@@ -133,12 +132,12 @@ public class CfDnsClient extends CfBasicHttpClient {
}
/**
* Retrieves all record entities for a specific second-level domain (SLD) within a given DNS zone.
* Retrieves all record entities for a specific second-level domain (SLD) within a given DNS
* zone.
*
* @param zone The DNS zone entity for which the SLD records are to be fetched.
* @param sld The second-level domain name for which the records are retrieved.
* @return A list of {@code RecordEntity} objects representing the DNS records associated with the
* provided SLD.
* @return A list of {@code RecordEntity} associated with the desired SLD.
* @throws CloudflareApiException If an error occurs while interacting with the Cloudflare API.
*/
public List<RecordEntity> sldListAll(ZoneEntity zone, String sld) throws CloudflareApiException {
@@ -146,13 +145,13 @@ public class CfDnsClient extends CfBasicHttpClient {
}
/**
* Retrieves all record entities for a specific second-level domain (SLD) within a given DNS zone.
* Retrieves all record entities for a specific second-level domain (SLD) within a given DNS
* zone.
*
* @param zone The DNS zone entity for which the SLD records are to be fetched.
* @param sld The second-level domain name for which the records are retrieved.
* @param pagingRequest The paging request.
* @return A list of {@code RecordEntity} objects representing the DNS records associated with the
* provided SLD.
* @return A list of {@code RecordEntity} associated with the desired SLD.
* @throws CloudflareApiException If an error occurs while interacting with the Cloudflare API.
*/
public List<RecordEntity> sldListAll(ZoneEntity zone, String sld, PagingRequest pagingRequest)
@@ -172,8 +171,7 @@ public class CfDnsClient extends CfBasicHttpClient {
* @param zone the zone entity that contains information about the DNS zone
* @param sld the second-level domain (SLD) for which the record information is requested
* @param type the type of DNS record (e.g., A, AAAA, CNAME) being queried
* @return the record entity containing detailed information about 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
*/
public RecordEntity sldInfo(ZoneEntity zone, String sld, RecordType type)
@@ -189,25 +187,42 @@ public class CfDnsClient extends CfBasicHttpClient {
* 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 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 {
public RecordEntity recordCreateSld(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, fqdn, ttl, type, content);
}
/**
* 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 name the name of the DNS record (e.g., www.example.com)
* @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 name, int ttl, RecordType type,
String content) throws CloudflareApiException {
RecordEntity rec = RecordEntity.build(name, type, ttl, content);
return recordCreate(zone, rec);
}
/**
* Creates a new DNS record in the specified zone using the Cloudflare API.
*
* @param zone The zone entity where the record will be created. Contains details such as zone ID.
* @param zone The zone entity where the record will be created. Contains details such as zone
* ID.
* @param rec The record entity representing the DNS record to be created, including its
* attributes.
* @return The created record entity as returned by the Cloudflare API.
@@ -228,8 +243,8 @@ public class CfDnsClient extends CfBasicHttpClient {
* @param zone The zone entity that specifies the zone in which the record exists.
* @param rec The record entity that represents the DNS record to be deleted.
* @return {@code true} if the DNS record was successfully deleted; {@code false} otherwise.
* @throws CloudflareApiException if there is an issue during the API communication or the request
* fails for any reason.
* @throws CloudflareApiException if there is an issue during the API communication, or the
* request fails for any reason.
*/
public boolean recordDelete(ZoneEntity zone, RecordEntity rec) throws CloudflareApiException {
boolean changed = recordDelete(zone, rec.getId());
@@ -280,8 +295,8 @@ public class CfDnsClient extends CfBasicHttpClient {
}
/**
* Deletes DNS records of a specific type within a given zone if they exist.
* If no record of the specified type exists, it logs this occurrence without throwing an exception.
* Deletes DNS records of a specific type within a given zone if they exist. If no record of the
* specified type exists, it logs this occurrence without throwing an exception.
*
* @param zone The DNS zone entity in which the record exists.
* @param sld The second-level domain for which the record is being checked.
@@ -84,7 +84,7 @@ public class CfClientTest {
r = client.sldInfo(z, SLD_STR, RecordType.A);
assertEquals("130.0.0.3", r.getContent());
RecordEntity createdRe2 =
client.recordCreate(z, SLD_STR, TTL, RecordType.AAAA, "2a0a:4cc0:c0:2e4::1");
client.recordCreateSld(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());