From 7b408eb946ec7b9355d04821ae46e6c0fb8c3792 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Fri, 7 Jun 2024 19:57:44 +0900 Subject: [PATCH] fix pdns api clients (retry 21x) --- .../space/mori/dnsapi/PowerDNSAPIClient.kt | 50 ++++++++++++------- .../space/mori/dnsapi/dto/RecordRequestDTO.kt | 4 +- .../mori/dnsapi/service/RecordService.kt | 20 ++++++-- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt b/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt index 3217cc7..098e18c 100644 --- a/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt +++ b/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt @@ -25,23 +25,31 @@ class PowerDNSAPIClient() { @Throws(PowerDNSAPIException::class) fun createZone(zoneName: String): Response { - val body = gson.toJson(mapOf( - "name" to "$zoneName." - )).toRequestBody() - val request = Request.Builder() - .url("$apiUrl/api/v1/servers/localhost/zones") - .addHeader("X-API-Key", apiKey) - .addHeader("Accept", "application/json") - .addHeader("Content-Type", "application/json") - .post(body) - .build() + try { + val body = gson.toJson( + mapOf( + "name" to "$zoneName.", + "kind" to "Primary" + ) + ).toRequestBody() + val request = Request.Builder() + .url("$apiUrl/api/v1/servers/localhost/zones") + .addHeader("X-API-Key", apiKey) + .addHeader("Accept", "application/json") + .addHeader("Content-Type", "application/json") + .post(body) + .build() - val response = client.newCall(request).execute() - if(!response.isSuccessful) { - val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java) - throw PowerDNSAPIException(error) + val response = client.newCall(request).execute() + if (!response.isSuccessful) { + val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java) + throw PowerDNSAPIException(error) + } + return response + } catch(ex: Exception) { + println(ex) + throw ex } - return response } @Throws(PowerDNSAPIException::class) @@ -63,11 +71,13 @@ class PowerDNSAPIClient() { } @Throws(PowerDNSAPIException::class) - fun createRecord(zoneName: String, recordName: String, recordType: String, recordContent: String): Response { + fun createRecord(zoneName: String, recordName: String, recordType: String, recordContent: String, ttl: Int = 300, priority: Int = 0): Response { val body = gson.toJson(mapOf( "name" to recordName, "type" to recordType, - "content" to recordContent + "content" to recordContent, + "ttl" to ttl, + "priority" to priority )).toRequestBody("application/json".toMediaType()) val request = Request.Builder() .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records") @@ -86,9 +96,11 @@ class PowerDNSAPIClient() { } @Throws(PowerDNSAPIException::class) - fun updateRecord(zoneName: String, recordName: String, recordType: String, recordContent: String): Response { + fun updateRecord(zoneName: String, recordName: String, recordType: String, recordContent: String, ttl: Int = 300, priority: Int = 0): Response { val body = gson.toJson(mapOf( - "content" to recordContent + "content" to recordContent, + "ttl" to ttl, + "priority" to priority )).toRequestBody("application/json".toMediaType()) val request = Request.Builder() .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records/$recordName/$recordType") diff --git a/src/main/kotlin/space/mori/dnsapi/dto/RecordRequestDTO.kt b/src/main/kotlin/space/mori/dnsapi/dto/RecordRequestDTO.kt index 8bb37a3..173de39 100644 --- a/src/main/kotlin/space/mori/dnsapi/dto/RecordRequestDTO.kt +++ b/src/main/kotlin/space/mori/dnsapi/dto/RecordRequestDTO.kt @@ -11,10 +11,10 @@ data class RecordRequestDTO( val name: String, @Schema(description = "Record data", example = "192.0.2.1") - val content: String, + val value: String, @Schema(description = "TTL (Time to Live)", example = "3600") - val ttl: Int = 300, + var ttl: Int = 300, @Schema(description = "Priority", example = "0") val priority: Int? = null, diff --git a/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt b/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt index 91575f3..6d4f49a 100644 --- a/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt +++ b/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt @@ -32,13 +32,19 @@ class RecordService( if(domain.user.id != user.id) throw RuntimeException("Unauthorized to create record in API: $domain_id") - powerDNSApiClient.createRecord(domain.name, recordRequest.name, recordRequest.type, recordRequest.content) + recordRequest.ttl = when { + recordRequest.ttl == 1 -> 300 + recordRequest.ttl < 300 -> 300 + else -> recordRequest.ttl + } + + powerDNSApiClient.createRecord(domain.name, recordRequest.name, recordRequest.type, recordRequest.value, recordRequest.ttl, recordRequest.priority ?: 0) val record = DomainRecord( domain = domain, name = recordRequest.name, type = recordRequest.type, - content = recordRequest.content, + content = recordRequest.value, ttl = recordRequest.ttl, prio = recordRequest.priority ?: 0, disabled = false, @@ -134,13 +140,19 @@ class RecordService( // 레코드 업데이트 record.name = updatedRecord.name record.type = updatedRecord.type - record.content = updatedRecord.content + record.content = updatedRecord.value record.ttl = updatedRecord.ttl record.prio = updatedRecord.priority ?: 0 record.comment = updatedRecord.comment record.modifiedOn = Date() - powerDNSApiClient.updateRecord(domain!!.name, updatedRecord.name, updatedRecord.type, updatedRecord.content) + record.ttl = when { + record.ttl == 1 -> 300 + record.ttl < 300 -> 300 + else -> record.ttl + } + + powerDNSApiClient.updateRecord(domain!!.name, updatedRecord.name, updatedRecord.type, updatedRecord.value) // 저장 val savedRecord = recordRepository.save(record)