fix pdns api clients (retry 23x)

This commit is contained in:
dalbodeule 2024-06-07 22:04:32 +09:00
parent 25a7b6ca85
commit d8afd33d07
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
2 changed files with 54 additions and 21 deletions

View File

@ -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<Map<String, Any>>() // 빈 레코드 리스트
)
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

View File

@ -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")
}