Fixed package location. Added update to dns records

This commit is contained in:
nathan 2019-07-28 11:48:34 +02:00
parent 82e4c18b1a
commit af644f49fa
40 changed files with 187 additions and 95 deletions

View File

@ -13,23 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox
package dorkbox.kloudflare
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import dorkbox.api.CloudflareActions
import dorkbox.api.core.*
import dorkbox.api.dns.CreateDnsRecord
import dorkbox.api.dns.DeleteDnsRecord
import dorkbox.api.dns.DnsRecord
import dorkbox.api.dns.UpdateDnsRecord
import dorkbox.api.firewall.AccessRule
import dorkbox.api.user.BillingHistory
import dorkbox.api.user.BillingProfile
import dorkbox.api.user.User
import dorkbox.api.zone.RatePlan
import dorkbox.api.zone.Zone
import dorkbox.api.zone.settings.ZoneSetting
import dorkbox.kloudflare.api.CloudflareActions
import dorkbox.kloudflare.api.core.CfErrorResponse
import dorkbox.kloudflare.api.core.CfResponse
import dorkbox.kloudflare.api.core.DnsRecordTypeAdapter
import dorkbox.kloudflare.api.core.Error
import dorkbox.kloudflare.api.core.ISO8601Adapter
import dorkbox.kloudflare.api.dns.CreateDnsRecord
import dorkbox.kloudflare.api.dns.DeleteDnsRecord
import dorkbox.kloudflare.api.dns.DnsRecord
import dorkbox.kloudflare.api.dns.UpdateDnsRecord
import dorkbox.kloudflare.api.firewall.AccessRule
import dorkbox.kloudflare.api.user.BillingHistory
import dorkbox.kloudflare.api.user.BillingProfile
import dorkbox.kloudflare.api.user.User
import dorkbox.kloudflare.api.zone.RatePlan
import dorkbox.kloudflare.api.zone.Zone
import dorkbox.kloudflare.api.zone.settings.ZoneSetting
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
@ -51,14 +54,14 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
val errorConverter: Converter<ResponseBody, CfErrorResponse>
val cloudflare: CloudflareActions
val client: OkHttpClient
init {
// JSON mapping to java classes
val httpClient = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = httpClient
client = OkHttpClient.Builder()
// .addInterceptor(interceptor) // this is the raw HTTP interceptor
.build()
@ -67,7 +70,7 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
.add(DnsRecordTypeAdapter())
.build()
val adapter = moshi.adapter<List<String>>(Types.newParameterizedType(List::class.java, String::class.java))
// val adapter = moshi.adapter<List<String>>(Types.newParameterizedType(List::class.java, String::class.java))
val retrofit = Retrofit.Builder()
@ -76,7 +79,7 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
.client(client)
.build()
errorConverter = retrofit.responseBodyConverter<CfErrorResponse>(CfErrorResponse::class.java, CfErrorResponse::class.annotations.toTypedArray())
errorConverter = retrofit.responseBodyConverter(CfErrorResponse::class.java, CfErrorResponse::class.annotations.toTypedArray())
cloudflare = retrofit.create(CloudflareActions::class.java)
}
@ -108,7 +111,14 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
}
fun listZones(options: Map<String, String> = emptyMap()): List<Zone> {
return wrap(cloudflare.listZones(xAuthEmail, xAuthKey, options))
val zones = wrap(cloudflare.listZones(xAuthEmail, xAuthKey, options))
zones.forEach { zone ->
// have to assign
zone.kloudflare = this;
}
return zones
}
fun getZoneRatePlans(zone: Zone): RatePlan {
@ -120,7 +130,8 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
}
fun listDnsRecords(zone: Zone): List<DnsRecord> {
val wrap = wrap(cloudflare.listDnsRecords(xAuthEmail, xAuthKey, zone.id))
val wrap =
wrap(cloudflare.listDnsRecords(xAuthEmail, xAuthKey, zone.id))
wrap.forEach {
it.zone = zone
}
@ -128,13 +139,20 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
}
fun createDnsRecord(dnsRecord: CreateDnsRecord): DnsRecord {
val wrap = wrap(cloudflare.createDnsRecord(xAuthEmail, xAuthKey, dnsRecord.zone.id, dnsRecord))
val wrap =
wrap(cloudflare.createDnsRecord(xAuthEmail, xAuthKey, dnsRecord.zone.id, dnsRecord))
wrap.zone = dnsRecord.zone
return wrap
}
fun updateDnsRecord(updatedDnsRecord: UpdateDnsRecord): Any {
return wrap(cloudflare.updateDnsRecord(xAuthEmail, xAuthKey, updatedDnsRecord.zone.id, updatedDnsRecord.id, updatedDnsRecord))
return wrap(cloudflare.updateDnsRecord(xAuthEmail,
xAuthKey,
updatedDnsRecord.zone.id,
updatedDnsRecord.id,
updatedDnsRecord
)
)
}
fun deleteDnsRecord(dnsRecord: DnsRecord): DeleteDnsRecord {
@ -144,5 +162,13 @@ class Kloudflare(private val xAuthEmail: String, private val xAuthKey: String) {
fun listAccessRules(): List<AccessRule> {
return wrap(cloudflare.listAccessRules(xAuthEmail, xAuthKey))
}
fun shutdown() {
// shutdown the http client stuff
client.dispatcher.cancelAll()
client.dispatcher.executorService.shutdown()
client.connectionPool.evictAll()
client.cache?.close()
}
}

View File

@ -13,22 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api
package dorkbox.kloudflare.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.firewall.AccessRule
import dorkbox.api.user.BillingHistory
import dorkbox.api.user.BillingProfile
import dorkbox.api.user.User
import dorkbox.api.zone.RatePlan
import dorkbox.api.zone.Zone
import dorkbox.api.zone.settings.ZoneSetting
import dorkbox.kloudflare.api.core.CfResponse
import dorkbox.kloudflare.api.dns.CreateDnsRecord
import dorkbox.kloudflare.api.dns.DeleteDnsRecord
import dorkbox.kloudflare.api.dns.DnsRecord
import dorkbox.kloudflare.api.dns.UpdateDnsRecord
import dorkbox.kloudflare.api.firewall.AccessRule
import dorkbox.kloudflare.api.user.BillingHistory
import dorkbox.kloudflare.api.user.BillingProfile
import dorkbox.kloudflare.api.user.User
import dorkbox.kloudflare.api.zone.RatePlan
import dorkbox.kloudflare.api.zone.Zone
import dorkbox.kloudflare.api.zone.settings.ZoneSetting
import retrofit2.Call
import retrofit2.http.*
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.QueryMap
interface CloudflareActions {
/**

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.core
package dorkbox.kloudflare.api.core
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.core
package dorkbox.kloudflare.api.core
import com.squareup.moshi.Json

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.core
package dorkbox.kloudflare.api.core
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import dorkbox.api.dns.RecordType
import dorkbox.kloudflare.api.dns.RecordType
/** Converts byte arrays to base64 (so it looks better as a string...) */
internal class DnsRecordTypeAdapter {

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.core
package dorkbox.kloudflare.api.core
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.core
package dorkbox.kloudflare.api.core
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonQualifier

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.core
package dorkbox.kloudflare.api.core
/**
* https://api.cloudflare.com/#getting-started-requests

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.core
package dorkbox.kloudflare.api.core
import com.squareup.moshi.Json

View File

@ -13,20 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.dns
package dorkbox.kloudflare.api.dns
import com.squareup.moshi.Json
import dorkbox.kloudflare.api.zone.Zone
/**
* https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record
*/
open class CreateDnsRecord(dnsRecord: DnsRecord) {
open class CreateDnsRecord(zone: Zone) {
/**
* NOTE: This is not part of the Cloudflare API
*
* Used to associate this dns record with it's zone
*/
@Transient
var id = dnsRecord.id
val zone = zone
@Transient
var zone = dnsRecord.zone
/**
* Record type

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.dns
package dorkbox.kloudflare.api.dns
/**
* https://api.cloudflare.com/#dns-records-for-a-zone-properties

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.dns
package dorkbox.kloudflare.api.dns
import com.squareup.moshi.Json

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.dns
package dorkbox.kloudflare.api.dns
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.api.zone.Zone
import dorkbox.kloudflare.api.core.ISO8601
import dorkbox.kloudflare.api.zone.Zone
import java.time.LocalDateTime
/**
@ -25,12 +25,24 @@ import java.time.LocalDateTime
*/
open class DnsRecord {
/**
* NOTE: This is not part of the Cloudflare API
*
* Which zone this DNS record belongs to
*/
@Transient
lateinit var zone: Zone
fun prettyName(): String {
return when (val length = name.length - zone.name.length) {
0 -> "@"
else ->
name.subSequence(0, length - 1) as String // -1 because we don't want the '.'
}
}
/**
* DNS record identifier tag
*/

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.dns
package dorkbox.kloudflare.api.dns
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.dns
package dorkbox.kloudflare.api.dns
enum class RecordType {
A, AAAA, CNAME, TXT, SRV, LOC, MX, NS, SPF, CERT, DNSKEY, DS, NAPTR, SMIMEA, SSHFP, TLSA, URI

View File

@ -0,0 +1,24 @@
package dorkbox.kloudflare.api.dns
import com.squareup.moshi.Json
/**
* https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
*/
class UpdateDnsRecord(dnsRecord: DnsRecord) : CreateDnsRecord(dnsRecord.zone) {
/**
* DNS record identifier tag
*/
@field:[Json(name = "id")]
var id = dnsRecord.id
init {
type = dnsRecord.type
name = dnsRecord.name
content = dnsRecord.content
ttl = dnsRecord.ttl
priority = 0
proxied = dnsRecord.proxied
}
}

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.firewall
package dorkbox.kloudflare.api.firewall
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.kloudflare.api.core.ISO8601
import java.time.LocalDateTime
/**

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.firewall
package dorkbox.kloudflare.api.firewall
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.firewall
package dorkbox.kloudflare.api.firewall
import com.squareup.moshi.Json

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user
package dorkbox.kloudflare.api.user
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.api.zone.Zone
import dorkbox.kloudflare.api.core.ISO8601
import dorkbox.kloudflare.api.zone.Zone
import java.time.LocalDateTime
/**

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user
package dorkbox.kloudflare.api.user
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.kloudflare.api.core.ISO8601
import java.time.LocalDateTime
/**

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user
package dorkbox.kloudflare.api.user
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.kloudflare.api.core.ISO8601
import java.time.LocalDateTime
/**

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user
package dorkbox.kloudflare.api.user
import com.squareup.moshi.Json

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user.invite
package dorkbox.kloudflare.api.user.invite
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.kloudflare.api.core.ISO8601
import java.time.LocalDateTime
/**

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user.invite
package dorkbox.kloudflare.api.user.invite
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user.subscription
package dorkbox.kloudflare.api.user.subscription
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user.subscription
package dorkbox.kloudflare.api.user.subscription
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user.subscription
package dorkbox.kloudflare.api.user.subscription
import com.squareup.moshi.Json

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.user.subscription
package dorkbox.kloudflare.api.user.subscription
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.api.zone.Zone
import dorkbox.kloudflare.api.core.ISO8601
import dorkbox.kloudflare.api.zone.Zone
import java.time.LocalDateTime
/**

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone
package dorkbox.kloudflare.api.zone
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone
package dorkbox.kloudflare.api.zone
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone
package dorkbox.kloudflare.api.zone
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone
package dorkbox.kloudflare.api.zone
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone
package dorkbox.kloudflare.api.zone
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone
package dorkbox.kloudflare.api.zone
import com.squareup.moshi.Json

View File

@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone
package dorkbox.kloudflare.api.zone
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.kloudflare.Kloudflare
import dorkbox.kloudflare.api.core.ISO8601
import dorkbox.kloudflare.api.dns.DnsRecord
import java.time.LocalDateTime
/**
@ -24,6 +26,22 @@ import java.time.LocalDateTime
*/
class Zone {
/**
* NOTE: This is not part of the Cloudflare API
*/
@Transient
lateinit var kloudflare: Kloudflare
/**
* NOTE: This is not part of the Cloudflare API
*
* DNS records that were retrieved from this zone.
*/
@delegate:Transient
val dnsRecords: List<DnsRecord> by lazy { kloudflare.listDnsRecords(this) }
@field:[Json(name = "id")]
val id: String = ""
@ -81,7 +99,6 @@ class Zone {
@field:[Json(name = "owner")]
val owner = Owner()
/**
* Information about the account the zone belongs to
*/
@ -119,7 +136,6 @@ class Zone {
@field:[Json(name = "activated_on") ISO8601]
val activatedOn: LocalDateTime? = null
/**
* Indicates if the zone is only using Cloudflare DNS services. A true value means the zone will not receive security or performance benefits.
*/

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone.settings
package dorkbox.kloudflare.api.zone.settings
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone.settings
package dorkbox.kloudflare.api.zone.settings
import com.squareup.moshi.Json

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone.settings
package dorkbox.kloudflare.api.zone.settings
import com.squareup.moshi.Json

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.api.zone.settings
package dorkbox.kloudflare.api.zone.settings
import com.squareup.moshi.Json
import dorkbox.api.core.ISO8601
import dorkbox.kloudflare.api.core.ISO8601
import java.time.LocalDateTime
/**