From 46b0419acc5c02dd7ef1b6f15ea36f4b84e08a18 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 21 Jun 2019 22:24:02 +0200 Subject: [PATCH] Added ability to add/delete/update dns records. Updated URL's in javadocs --- src/dorkbox/Kloudflare.kt | 34 +++++--- src/dorkbox/api/CloudflareActions.kt | 84 ++++++++++++++++++- .../api/{dns => core}/DnsRecordTypeAdapter.kt | 11 +-- src/dorkbox/api/dns/CreateDnsRecord.kt | 61 ++++++++++++++ src/dorkbox/api/dns/{ARecord.kt => Data.kt} | 6 +- src/dorkbox/api/dns/DeleteDnsRecord.kt | 29 +++++++ src/dorkbox/api/dns/DnsRecord.kt | 10 ++- src/dorkbox/api/dns/UpdateDnsRecord.kt | 8 ++ src/dorkbox/api/zone/RatePlan.kt | 4 +- src/dorkbox/api/zone/settings/ZoneSetting.kt | 3 +- 10 files changed, 220 insertions(+), 30 deletions(-) rename src/dorkbox/api/{dns => core}/DnsRecordTypeAdapter.kt (81%) create mode 100644 src/dorkbox/api/dns/CreateDnsRecord.kt rename src/dorkbox/api/dns/{ARecord.kt => Data.kt} (87%) create mode 100644 src/dorkbox/api/dns/DeleteDnsRecord.kt create mode 100644 src/dorkbox/api/dns/UpdateDnsRecord.kt diff --git a/src/dorkbox/Kloudflare.kt b/src/dorkbox/Kloudflare.kt index ed7a5c2..de44183 100644 --- a/src/dorkbox/Kloudflare.kt +++ b/src/dorkbox/Kloudflare.kt @@ -18,12 +18,10 @@ package dorkbox import com.squareup.moshi.Moshi import com.squareup.moshi.Types import dorkbox.api.CloudflareActions -import dorkbox.api.core.CfErrorResponse -import dorkbox.api.core.CfResponse -import dorkbox.api.core.Error -import dorkbox.api.core.ISO8601Adapter +import dorkbox.api.core.* +import dorkbox.api.dns.CreateDnsRecord +import dorkbox.api.dns.DeleteDnsRecord import dorkbox.api.dns.DnsRecord -import dorkbox.api.dns.DnsRecordTypeAdapter import dorkbox.api.user.BillingHistory import dorkbox.api.user.BillingProfile import dorkbox.api.user.User @@ -95,7 +93,7 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { } - fun getUser() : User { + fun getUser(): User { return wrap(cloudflare.getUser(xAuthEmail, xAuthKey)) } @@ -111,16 +109,28 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) { return wrap(cloudflare.listZones(xAuthEmail, xAuthKey, options)) } - fun getZoneRatePlans(zoneIdentifier: String): RatePlan { - return wrap(cloudflare.getZoneRatePlans(xAuthEmail, xAuthKey, zoneIdentifier)) + fun getZoneRatePlans(zone: Zone): RatePlan { + return wrap(cloudflare.getZoneRatePlans(xAuthEmail, xAuthKey, zone.id)) } - fun getZoneSettings(zoneIdentifier: String): ZoneSetting { - return wrap(cloudflare.getZoneSettings(xAuthEmail, xAuthKey, zoneIdentifier)) + fun getZoneSettings(zone: Zone): ZoneSetting { + return wrap(cloudflare.getZoneSettings(xAuthEmail, xAuthKey, zone.id)) } - fun listDnsRecords(zoneIdentifier: String): List { - return wrap(cloudflare.listDnsRecords(xAuthEmail, xAuthKey, zoneIdentifier)) + fun listDnsRecords(zone: Zone): List { + return wrap(cloudflare.listDnsRecords(xAuthEmail, xAuthKey, zone.id)) + } + + fun createDnsRecord(zone: Zone, dnsRecord: CreateDnsRecord): DnsRecord { + return wrap(cloudflare.createDnsRecord(xAuthEmail, xAuthKey, zone.id, dnsRecord)) + } + + fun updateDnsRecord(zone: Zone, newDnsRecord: CreateDnsRecord): Any { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + fun deleteDnsRecord(zone: Zone, dnsRecord: DnsRecord): DeleteDnsRecord { + return wrap(cloudflare.deleteDnsRecord(xAuthEmail, xAuthKey, zone.id, dnsRecord.id)) } } diff --git a/src/dorkbox/api/CloudflareActions.kt b/src/dorkbox/api/CloudflareActions.kt index 6c8d8a4..6888f70 100644 --- a/src/dorkbox/api/CloudflareActions.kt +++ b/src/dorkbox/api/CloudflareActions.kt @@ -16,7 +16,10 @@ package dorkbox.api import dorkbox.api.core.CfResponse +import dorkbox.api.dns.CreateDnsRecord +import dorkbox.api.dns.DeleteDnsRecord import dorkbox.api.dns.DnsRecord +import dorkbox.api.dns.UpdateDnsRecord import dorkbox.api.user.BillingHistory import dorkbox.api.user.BillingProfile import dorkbox.api.user.User @@ -27,6 +30,11 @@ import retrofit2.Call import retrofit2.http.* interface CloudflareActions { + /** + * Gets the User details + * + * https://api.cloudflare.com/#user-properties + */ @Headers("Content-Type: application/json") @GET("user") fun getUser( @@ -34,6 +42,11 @@ interface CloudflareActions { @Header("X-Auth-Key") key: String ): Call> + /** + * Gets the user's Billing Profile + * + * https://api.cloudflare.com/#user-billing-profile-billing-profile + */ @Headers("Content-Type: application/json") @GET("user/billing/profile") fun getUserBillingProfile( @@ -41,6 +54,11 @@ interface CloudflareActions { @Header("X-Auth-Key") key: String ): Call> + /** + * Gets the users Billing History + * + * https://api.cloudflare.com/#user-billing-history-billing-history + */ @Headers("Content-Type: application/json") @GET("user/billing/history") fun getUserBillingHistory( @@ -48,6 +66,11 @@ interface CloudflareActions { @Header("X-Auth-Key") key: String ): Call> + /** + * Gets the list of Zone's owned by this user + * + * https://api.cloudflare.com/#zone-properties + */ @Headers("Content-Type: application/json") @GET("zones") fun listZones( @@ -56,7 +79,11 @@ interface CloudflareActions { @QueryMap options: Map ): Call>> - + /** + * Gets the zone rate plan for the specified zone from the billing service + * + * https://api.cloudflare.com/#zone-rate-plan-properties + */ @Headers("Content-Type: application/json") @GET("zones/{zone_identifier}/available_rate_plans") fun getZoneRatePlans( @@ -65,7 +92,11 @@ interface CloudflareActions { @Path("zone_identifier") zoneIdentifier: String ): Call> - + /** + * Gets the zone settings for the specified zone + * + * https://api.cloudflare.com/#zone-settings-properties + */ @Headers("Content-Type: application/json") @GET("zones/{zone_identifier}/settings") fun getZoneSettings( @@ -74,6 +105,11 @@ interface CloudflareActions { @Path("zone_identifier") zoneIdentifier: String ): Call> + /** + * Lists the DNS records for a specified zone + * + * https://api.cloudflare.com/#dns-records-for-a-zone-properties + */ @Headers("Content-Type: application/json") @GET("zones/{zone_identifier}/dns_records") fun listDnsRecords( @@ -82,4 +118,48 @@ interface CloudflareActions { @Path("zone_identifier") zoneIdentifier: String ): Call>> + + /** + * Creates a new DNS record in the specified zone + * + * https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record + */ + @Headers("Content-Type: application/json") + @POST("zones/{zone_identifier}/dns_records") + fun createDnsRecord( + @Header("X-Auth-Email") email: String, + @Header("X-Auth-Key") key: String, + @Path("zone_identifier") zoneIdentifier: String, + @Body data: CreateDnsRecord + ): Call> + + /** + * Updates a DNS record for the specified zone + dns record + * + * https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record + */ + @Headers("Content-Type: application/json") + @PUT("zones/{zone_identifier}/dns_records/{identifier}") + fun updateDnsRecord( + @Header("X-Auth-Email") email: String, + @Header("X-Auth-Key") key: String, + @Path("zone_identifier") zoneIdentifier: String, + @Path("identifier") identifier: String, + @Body data: UpdateDnsRecord + ): Call> + + /** + * Deletes a DNS record for the specified zone + dns record + * + * https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record + */ + @Headers("Content-Type: application/json") + @DELETE("zones/{zone_identifier}/dns_records/{identifier}") + fun deleteDnsRecord( + @Header("X-Auth-Email") email: String, + @Header("X-Auth-Key") key: String, + @Path("zone_identifier") zoneIdentifier: String, + @Path("identifier") identifier: String + ): Call> + } diff --git a/src/dorkbox/api/dns/DnsRecordTypeAdapter.kt b/src/dorkbox/api/core/DnsRecordTypeAdapter.kt similarity index 81% rename from src/dorkbox/api/dns/DnsRecordTypeAdapter.kt rename to src/dorkbox/api/core/DnsRecordTypeAdapter.kt index 08a9e72..22e47e4 100644 --- a/src/dorkbox/api/dns/DnsRecordTypeAdapter.kt +++ b/src/dorkbox/api/core/DnsRecordTypeAdapter.kt @@ -13,25 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dorkbox.api.dns +package dorkbox.api.core import com.squareup.moshi.FromJson -import com.squareup.moshi.JsonQualifier import com.squareup.moshi.ToJson - -@Retention(AnnotationRetention.RUNTIME) -@JsonQualifier -annotation class DnsType +import dorkbox.api.dns.RecordType /** Converts byte arrays to base64 (so it looks better as a string...) */ internal class DnsRecordTypeAdapter { @ToJson - fun toJson(@DnsType recordType: RecordType): String { + fun toJson(recordType: RecordType): String { return recordType.name } @FromJson - @DnsType fun fromJson(recordType: String): RecordType { return RecordType.valueOf(recordType) } diff --git a/src/dorkbox/api/dns/CreateDnsRecord.kt b/src/dorkbox/api/dns/CreateDnsRecord.kt new file mode 100644 index 0000000..17284d0 --- /dev/null +++ b/src/dorkbox/api/dns/CreateDnsRecord.kt @@ -0,0 +1,61 @@ +/* + * Copyright 2019 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.api.dns + +import com.squareup.moshi.Json + +/** + * https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record + */ +open class CreateDnsRecord { + /** + * Record type + * A, AAAA, CNAME, TXT, SRV, LOC, MX, NS, SPF, CERT, DNSKEY, DS, NAPTR, SMIMEA, SSHFP, TLSA, URI + */ + @field:[Json(name = "type")] + var type = RecordType.A + + /** + * DNS record name + */ + @field:[Json(name = "name")] + var name = "" + + /** + * A valid IPv4 address + */ + @field:[Json(name = "content")] + var content = "" + + + /** + * Time to live for DNS record. Value of 1 is 'automatic' + */ + @field:[Json(name = "ttl")] + var ttl = 1 + + /** + * Used with some records like MX and SRV to determine priority. If you do not supply a priority for an MX record, a default value of 0 will be set + */ + @field:[Json(name = "priority")] + var priority = 0 + + /** + * Whether the record is receiving the performance and security benefits of Cloudflare + */ + @field:[Json(name = "proxied")] + var proxied = false +} diff --git a/src/dorkbox/api/dns/ARecord.kt b/src/dorkbox/api/dns/Data.kt similarity index 87% rename from src/dorkbox/api/dns/ARecord.kt rename to src/dorkbox/api/dns/Data.kt index 8ac69f2..122e7e3 100644 --- a/src/dorkbox/api/dns/ARecord.kt +++ b/src/dorkbox/api/dns/Data.kt @@ -15,7 +15,9 @@ */ package dorkbox.api.dns -class ARecord : DnsRecord() { - val asd = "" +/** + * https://api.cloudflare.com/#dns-records-for-a-zone-properties + */ +class Data { } diff --git a/src/dorkbox/api/dns/DeleteDnsRecord.kt b/src/dorkbox/api/dns/DeleteDnsRecord.kt new file mode 100644 index 0000000..b0fa01c --- /dev/null +++ b/src/dorkbox/api/dns/DeleteDnsRecord.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2019 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.api.dns + +import com.squareup.moshi.Json + +/** + * https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record + */ +open class DeleteDnsRecord { + /** + * DNS record identifier tag + */ + @field:[Json(name = "id")] + var id = "" +} diff --git a/src/dorkbox/api/dns/DnsRecord.kt b/src/dorkbox/api/dns/DnsRecord.kt index e2544ec..6fa54e6 100644 --- a/src/dorkbox/api/dns/DnsRecord.kt +++ b/src/dorkbox/api/dns/DnsRecord.kt @@ -33,14 +33,14 @@ open class DnsRecord { * Record type * A, AAAA, CNAME, TXT, SRV, LOC, MX, NS, SPF, CERT, DNSKEY, DS, NAPTR, SMIMEA, SSHFP, TLSA, URI */ - @field:[Json(name = "type") DnsType] + @field:[Json(name = "type")] var type = RecordType.A /** * DNS record name */ @field:[Json(name = "name")] - var name= "" + var name = "" /** * A valid IPv4 address @@ -93,11 +93,15 @@ open class DnsRecord { @field:[Json(name = "created_on") ISO8601] var createdOn = LocalDateTime.now() + /** + * Extra Cloudflare-specific information about the record + */ @field:[Json(name = "meta")] var meta = Meta() /** * Metadata about the record */ - var data: String? = null + @field:[Json(name = "data")] + var data: Data = Data() } diff --git a/src/dorkbox/api/dns/UpdateDnsRecord.kt b/src/dorkbox/api/dns/UpdateDnsRecord.kt new file mode 100644 index 0000000..daa7751 --- /dev/null +++ b/src/dorkbox/api/dns/UpdateDnsRecord.kt @@ -0,0 +1,8 @@ +package dorkbox.api.dns + +/** + * https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record + * + * This is the "same" object as creating a new record. This is a different type in order to prevent confusion and simplify naming conventions + */ +class UpdateDnsRecord : CreateDnsRecord() diff --git a/src/dorkbox/api/zone/RatePlan.kt b/src/dorkbox/api/zone/RatePlan.kt index 8215fbe..98b04a9 100644 --- a/src/dorkbox/api/zone/RatePlan.kt +++ b/src/dorkbox/api/zone/RatePlan.kt @@ -18,9 +18,9 @@ package dorkbox.api.zone import com.squareup.moshi.Json /** - * https://api.cloudflare.com/#zone-properties + * https://api.cloudflare.com/#zone-rate-plan-properties */ -class RatePlan { +class RatePlan { /** * Plan identifier tag diff --git a/src/dorkbox/api/zone/settings/ZoneSetting.kt b/src/dorkbox/api/zone/settings/ZoneSetting.kt index 4ac38ea..eee1a8b 100644 --- a/src/dorkbox/api/zone/settings/ZoneSetting.kt +++ b/src/dorkbox/api/zone/settings/ZoneSetting.kt @@ -788,12 +788,13 @@ class HTTP2EdgePrioritization : ZoneSetting() { /** - * @see [https://api.cloudflare.com](https://api.cloudflare.com/#zone-properties) + * https://api.cloudflare.com/#zone-settings-properties */ open class ZoneSetting { /** * ID of the zone setting + * * always_online, advanced_ddos, brotli, browser_cache_ttl, browser_check, flatten_at_root, cache_level, challenge_ttl, * development_mode, edge_cache_ttl, origin_error_page_pass_thru, sort_query_string_for_cache, email_obfuscation, * hotlink_protection, ip_geolocation, ipv6, websockets, sha1_support, tls_1_2_only, minify, max_upload, mobile_redirect,