Package codes.thischwa.cf
Class CfDnsClient
java.lang.Object
codes.thischwa.cf.CfDnsClient
CfDnsClient is a client interface to interact with Cloudflare DNS service. It allows managing DNS
records and zones within the Cloudflare system, including creating, updating, retrieving, and
deleting DNS records.
Example:
// 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 subdomain
List<RecordEntity> records = cfDnsClient.sldListAll(zone, "sld");
records.forEach(record ->
System.out.println("Record Type: " + record.getType() + ", Value: " + record.getContent())
);
// Create a record for the subdomain "api"
RecordEntity created = client.recordCreateSld(zone, "api", 60, RecordType.A, "192.168.10);
System.out.println("Created Record ID: " + created.getId());
-
Constructor Summary
ConstructorsConstructorDescriptionCfDnsClient(boolean emptyResultThrowsException, String authEmail, String authKey) Constructs a new instance ofCfDnsClient.CfDnsClient(boolean emptyResultThrowsException, String baseUrl, String authEmail, String authKey) Constructs a new instance ofCfDnsClient.CfDnsClient(String authEmail, String authKey) Constructs a new instance ofCfDnsClient.CfDnsClient(String baseUrl, String authEmail, String authKey) Constructs a new instance ofCfDnsClient. -
Method Summary
Modifier and TypeMethodDescriptionrecordCreate(ZoneEntity zone, RecordEntity rec) Creates a new DNS record in the specified zone using the Cloudflare API.recordCreate(ZoneEntity zone, String name, int ttl, RecordType type, String content) Creates a DNS record in the specified DNS zone with the provided details.recordCreateSld(ZoneEntity zone, String sld, int ttl, RecordType type, String content) Creates a new DNS record for a given second-level domain (SLD) within the specified zone.booleanrecordDelete(ZoneEntity zone, RecordEntity rec) Deletes a DNS record of the specified type within a given zone on the Cloudflare API.booleanrecordDelete(ZoneEntity zone, String id) Deletes a DNS record of the specified type within a given zone on the Cloudflare API.voidrecordDeleteTypeIfExists(ZoneEntity zone, String sld, RecordType... recordTypes) Deletes DNS records of a specific type within a given zone if they exist.recordUpdate(ZoneEntity zone, RecordEntity rec) Updates an existing DNS record in a specified Cloudflare zone.sldInfo(ZoneEntity zone, String sld, RecordType type) Retrieves detailed information about a specific second-level domain (SLD) record for a given zone and record type from the Cloudflare API.sldListAll(ZoneEntity zone, String sld) Retrieves all record entities for a specific second-level domain (SLD) within a given DNS zone.sldListAll(ZoneEntity zone, String sld, PagingRequest pagingRequest) Retrieves all record entities for a specific second-level domain (SLD) within a given DNS zone.Retrieves detailed information about a specific zone by its name.Retrieves a list of all zones from the Cloudflare API.zoneListAll(PagingRequest pagingRequest) Retrieves a list of all DNS zones using the provided paging request parameters.
-
Constructor Details
-
CfDnsClient
Constructs a new instance ofCfDnsClient.- Parameters:
authEmail- The email address associated with the Cloudflare account, used for authentication.authKey- The API key of the Cloudflare account, used as part of the authentication process.
-
CfDnsClient
Constructs a new instance ofCfDnsClient.- Parameters:
baseUrl- The base URL of the Cloudflare API to be used for requests.authEmail- The email address associated with the Cloudflare account, used for authentication.authKey- The API key of the Cloudflare account, used as part of the authentication process.
-
CfDnsClient
Constructs a new instance ofCfDnsClient.- Parameters:
emptyResultThrowsException- A boolean value indicating whether an exception should be thrown when the result is empty, it's valid for 'list requests' only. Default is true.authEmail- The email address associated with the Cloudflare account, used for authentication.authKey- The API key of the Cloudflare account, used as part of the authentication process.
-
CfDnsClient
public CfDnsClient(boolean emptyResultThrowsException, String baseUrl, String authEmail, String authKey) Constructs a new instance ofCfDnsClient.- Parameters:
emptyResultThrowsException- A boolean value indicating whether an exception should be thrown when the result is empty, it's valid for 'list requests' only. Default is true.baseUrl- The base URL for the Cloudflare API endpoint.authEmail- The email associated with the Cloudflare account for authentication.authKey- The API key for authenticating the client with Cloudflare services.
-
-
Method Details
-
zoneListAll
Retrieves a list of all zones from the Cloudflare API.- Returns:
- A list of ZoneEntity objects representing the zones retrieved from the Cloudflare API.
- Throws:
CloudflareApiException- If an error occurs during the API request or response handling.
-
zoneListAll
Retrieves a list of all DNS zones using the provided paging request parameters.- Parameters:
pagingRequest- the pagination request object containing parameters for paging and filtering zone data- Returns:
- a list of
ZoneEntityobjects representing the DNS zones retrieved from the API - Throws:
CloudflareApiException- if there is an error during the API request or response processing
-
zoneInfo
Retrieves detailed information about a specific zone by its name.- Parameters:
name- The name of the zone to retrieve information for.- Returns:
- A
ZoneEntityobject that contains details of the specified zone. - Throws:
CloudflareApiException- If an error occurs while making the API request or processing the response.
-
sldListAll
Retrieves all record entities for a specific second-level domain (SLD) within a given DNS zone.- Parameters:
zone- The DNS zone entity for which the SLD records are to be fetched.sld- The second-level domain name for which the records are retrieved.- Returns:
- A list of
RecordEntityassociated with the desired SLD. - Throws:
CloudflareApiException- If an error occurs while interacting with the Cloudflare API.
-
sldListAll
public List<RecordEntity> sldListAll(ZoneEntity zone, String sld, PagingRequest pagingRequest) throws CloudflareApiException Retrieves all record entities for a specific second-level domain (SLD) within a given DNS zone.- Parameters:
zone- The DNS zone entity for which the SLD records are to be fetched.sld- The second-level domain name for which the records are retrieved.pagingRequest- The paging request.- Returns:
- A list of
RecordEntityassociated with the desired SLD. - Throws:
CloudflareApiException- If an error occurs while interacting with the Cloudflare API.
-
sldInfo
public RecordEntity sldInfo(ZoneEntity zone, String sld, RecordType type) throws CloudflareApiException Retrieves detailed information about a specific second-level domain (SLD) record for a given zone and record type from the Cloudflare API.- Parameters:
zone- the zone entity that contains information about the DNS zonesld- the second-level domain (SLD) for which the record information is requestedtype- the type of DNS record (e.g., A, AAAA, CNAME) being queried- Returns:
- the
RecordEntityof the requested SLD and record type - Throws:
CloudflareApiException- if an error occurs during interaction with the Cloudflare API
-
recordCreateSld
public RecordEntity recordCreateSld(ZoneEntity zone, String sld, int ttl, RecordType type, String content) throws CloudflareApiException Creates a new DNS record for a given second-level domain (SLD) within the specified zone.- Parameters:
zone- The ZoneEntity representing the DNS zone where the record is to be created.sld- The second-level domain (SLD) for which the DNS record is being created.ttl- The time-to-live (TTL) value for the DNS record in seconds.type- The RecordType specifying the type of the DNS record (e.g., A, AAAA, CNAME).content- The content of the DNS record (e.g., IP address for A/AAAA records, target domain for CNAME).- Returns:
- The created RecordEntity object containing details of the newly created DNS record.
- Throws:
CloudflareApiException- If an error occurs while communicating with the Cloudflare API or creating the record.
-
recordCreate
public RecordEntity recordCreate(ZoneEntity zone, String name, int ttl, RecordType type, String content) throws CloudflareApiException Creates a DNS record in the specified DNS zone with the provided details.- Parameters:
zone- the DNS zone in which the record will be createdname- the name of the DNS record (e.g., www.example.com)ttl- the time-to-live (TTL) value for the DNS recordtype- the type of the DNS record (e.g., A, AAAA, CNAME)content- the content or value of the DNS record- Returns:
- the created DNS record as a
RecordEntityobject - Throws:
CloudflareApiException- if an error occurs while interacting with the Cloudflare API
-
recordCreate
Creates a new DNS record in the specified zone using the Cloudflare API.- Parameters:
zone- The zone entity where the record will be created. Contains details such as zone ID.rec- The record entity representing the DNS record to be created, including its attributes.- Returns:
- The created record entity as returned by the Cloudflare API.
- Throws:
CloudflareApiException- If an error occurs while interacting with the Cloudflare API.
-
recordDelete
Deletes a DNS record of the specified type within a given zone on the Cloudflare API.- Parameters:
zone- The zone entity that specifies the zone in which the record exists.rec- The record entity that represents the DNS record to be deleted.- Returns:
trueif the DNS record was successfully deleted;falseotherwise.- Throws:
CloudflareApiException- if there is an issue during the API communication, or the request fails for any reason.
-
recordDelete
Deletes a DNS record of the specified type within a given zone on the Cloudflare API.- Parameters:
zone- The zone entity that specifies the zone in which the record exists.id- The record entity that represents the DNS record to be deleted.- Returns:
trueif the DNS record was successfully deleted;falseotherwise.- Throws:
CloudflareApiException- if there is an issue during the API communication or the request fails for any reason.
-
recordUpdate
Updates an existing DNS record in a specified Cloudflare zone.- Parameters:
zone- the zone entity containing the ID of the target zonerec- the record entity containing the ID of the DNS record to be updated and its updated data- Returns:
- the updated record entity as returned by the Cloudflare API
- Throws:
CloudflareApiException- if an error occurs while interacting with the Cloudflare API
-
recordDeleteTypeIfExists
public void recordDeleteTypeIfExists(ZoneEntity zone, String sld, RecordType... recordTypes) throws CloudflareApiException 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.- Parameters:
zone- The DNS zone entity in which the record exists.sld- The second-level domain for which the record is being checked.recordTypes- The types of DNS records that should be deleted, if they exist.- Throws:
CloudflareApiException- If an error occurs during API communication.
-