From d8afd33d0763300cc942cdfbe85bc1c15fe38c5f Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:04:32 +0900 Subject: [PATCH] fix pdns api clients (retry 23x) --- .../space/mori/dnsapi/PowerDNSAPIClient.kt | 70 +++++++++++++------ .../mori/dnsapi/service/RecordService.kt | 5 +- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt b/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt index 098e18c..cec5d2d 100644 --- a/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt +++ b/src/main/kotlin/space/mori/dnsapi/PowerDNSAPIClient.kt @@ -42,7 +42,8 @@ class PowerDNSAPIClient() { val response = client.newCall(request).execute() if (!response.isSuccessful) { - val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java) + val str = response.body?.string() + val error = gson.fromJson(str, PowerDNSAPIError::class.java) throw PowerDNSAPIException(error) } return response @@ -64,7 +65,9 @@ class PowerDNSAPIClient() { val response = client.newCall(request).execute() if(!response.isSuccessful) { - val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java) + val str = response.body?.string() + println(str) + val error = gson.fromJson(str, PowerDNSAPIError::class.java) throw PowerDNSAPIException(error) } return response @@ -72,24 +75,32 @@ class PowerDNSAPIClient() { @Throws(PowerDNSAPIException::class) 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, + val rrset = mapOf( + "name" to "$recordName.$zoneName.", "type" to recordType, - "content" to recordContent, "ttl" to ttl, - "priority" to priority - )).toRequestBody("application/json".toMediaType()) + "changetype" to "REPLACE", + "records" to listOf( + mapOf( + "content" to recordContent, + "disabled" to false + ) + ) + ) + val body = gson.toJson(mapOf("rrsets" to listOf(rrset))).toRequestBody("application/json".toMediaType()) val request = Request.Builder() - .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records") + .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName.") .addHeader("X-API-Key", apiKey) .addHeader("Accept", "application/json") .addHeader("Content-Type", "application/json") - .post(body) + .patch(body) .build() val response = client.newCall(request).execute() if(!response.isSuccessful) { - val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java) + val str = response.body?.string() + println(str) + val error = gson.fromJson(str, PowerDNSAPIError::class.java) throw PowerDNSAPIException(error) } return response @@ -97,22 +108,32 @@ class PowerDNSAPIClient() { @Throws(PowerDNSAPIException::class) 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, + val rrset = mapOf( + "name" to "$recordName.$zoneName.", + "type" to recordType, "ttl" to ttl, - "priority" to priority - )).toRequestBody("application/json".toMediaType()) + "changetype" to "REPLACE", + "records" to listOf( + mapOf( + "content" to recordContent, + "disabled" to false + ) + ) + ) + val body = gson.toJson(mapOf("rrsets" to listOf(rrset))).toRequestBody("application/json".toMediaType()) val request = Request.Builder() - .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records/$recordName/$recordType") + .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName.") .addHeader("X-API-Key", apiKey) .addHeader("Accept", "application/json") .addHeader("Content-Type", "application/json") - .put(body) + .patch(body) .build() val response = client.newCall(request).execute() if(!response.isSuccessful) { - val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java) + val str = response.body?.string() + println(str) + val error = gson.fromJson(str, PowerDNSAPIError::class.java) throw PowerDNSAPIException(error) } return response @@ -120,17 +141,26 @@ class PowerDNSAPIClient() { @Throws(PowerDNSAPIException::class) fun deleteRecord(zoneName: String, recordName: String, recordType: String): Response { + val rrset = mapOf( + "name" to "$recordName.$zoneName.", + "type" to recordType, + "changetype" to "DELETE", + "records" to listOf>() // 빈 레코드 리스트 + ) + val body = gson.toJson(mapOf("rrsets" to listOf(rrset))).toRequestBody("application/json".toMediaType()) val request = Request.Builder() - .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records/$recordName/$recordType") + .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName.") .addHeader("X-API-Key", apiKey) .addHeader("Accept", "application/json") .addHeader("Content-Type", "application/json") - .delete() + .patch(body) .build() val response = client.newCall(request).execute() if(!response.isSuccessful) { - val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java) + val str = response.body?.string() + println(str) + val error = gson.fromJson(str, PowerDNSAPIError::class.java) throw PowerDNSAPIException(error) } return response diff --git a/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt b/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt index 6d4f49a..68164ed 100644 --- a/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt +++ b/src/main/kotlin/space/mori/dnsapi/service/RecordService.kt @@ -53,6 +53,7 @@ class RecordService( modifiedOn = Date(), comment = recordRequest.comment, ) + recordRepository.save(record) return RecordResponseDTO( id = record.cfid, @@ -63,7 +64,7 @@ class RecordService( proxied = false, ttl = record.ttl, locked = false, - zoneId = record.cfid, + zoneId = record.domain.cfid, zoneName = domain.name, createdOn = record.createdOn.getISOFormat(), modifiedOn = record.modifiedOn.getISOFormat(), @@ -182,6 +183,8 @@ class RecordService( if(domain.user.id != user.id) throw RuntimeException("Unauthorized to create record in API: $domain_id") + println("$domain, $record_id") + val record = recordRepository.findByDomainIdAndCfid(domain.id!!, record_id).orElseThrow { RuntimeException("Failed to find record in API: $record_id") }