mirror of
https://github.com/dalbodeule/sh0rt.kr-pdns.git
synced 2025-06-08 18:58:20 +00:00
fix pdns api clients (retry 21x)
This commit is contained in:
parent
d423075da8
commit
7b408eb946
@ -25,23 +25,31 @@ class PowerDNSAPIClient() {
|
|||||||
|
|
||||||
@Throws(PowerDNSAPIException::class)
|
@Throws(PowerDNSAPIException::class)
|
||||||
fun createZone(zoneName: String): Response {
|
fun createZone(zoneName: String): Response {
|
||||||
val body = gson.toJson(mapOf(
|
try {
|
||||||
"name" to "$zoneName."
|
val body = gson.toJson(
|
||||||
)).toRequestBody()
|
mapOf(
|
||||||
val request = Request.Builder()
|
"name" to "$zoneName.",
|
||||||
.url("$apiUrl/api/v1/servers/localhost/zones")
|
"kind" to "Primary"
|
||||||
.addHeader("X-API-Key", apiKey)
|
)
|
||||||
.addHeader("Accept", "application/json")
|
).toRequestBody()
|
||||||
.addHeader("Content-Type", "application/json")
|
val request = Request.Builder()
|
||||||
.post(body)
|
.url("$apiUrl/api/v1/servers/localhost/zones")
|
||||||
.build()
|
.addHeader("X-API-Key", apiKey)
|
||||||
|
.addHeader("Accept", "application/json")
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.post(body)
|
||||||
|
.build()
|
||||||
|
|
||||||
val response = client.newCall(request).execute()
|
val response = client.newCall(request).execute()
|
||||||
if(!response.isSuccessful) {
|
if (!response.isSuccessful) {
|
||||||
val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java)
|
val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java)
|
||||||
throw PowerDNSAPIException(error)
|
throw PowerDNSAPIException(error)
|
||||||
|
}
|
||||||
|
return response
|
||||||
|
} catch(ex: Exception) {
|
||||||
|
println(ex)
|
||||||
|
throw ex
|
||||||
}
|
}
|
||||||
return response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(PowerDNSAPIException::class)
|
@Throws(PowerDNSAPIException::class)
|
||||||
@ -63,11 +71,13 @@ class PowerDNSAPIClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Throws(PowerDNSAPIException::class)
|
@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(
|
val body = gson.toJson(mapOf(
|
||||||
"name" to recordName,
|
"name" to recordName,
|
||||||
"type" to recordType,
|
"type" to recordType,
|
||||||
"content" to recordContent
|
"content" to recordContent,
|
||||||
|
"ttl" to ttl,
|
||||||
|
"priority" to priority
|
||||||
)).toRequestBody("application/json".toMediaType())
|
)).toRequestBody("application/json".toMediaType())
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records")
|
.url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records")
|
||||||
@ -86,9 +96,11 @@ class PowerDNSAPIClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Throws(PowerDNSAPIException::class)
|
@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(
|
val body = gson.toJson(mapOf(
|
||||||
"content" to recordContent
|
"content" to recordContent,
|
||||||
|
"ttl" to ttl,
|
||||||
|
"priority" to priority
|
||||||
)).toRequestBody("application/json".toMediaType())
|
)).toRequestBody("application/json".toMediaType())
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records/$recordName/$recordType")
|
.url("$apiUrl/api/v1/servers/localhost/zones/$zoneName./records/$recordName/$recordType")
|
||||||
|
@ -11,10 +11,10 @@ data class RecordRequestDTO(
|
|||||||
val name: String,
|
val name: String,
|
||||||
|
|
||||||
@Schema(description = "Record data", example = "192.0.2.1")
|
@Schema(description = "Record data", example = "192.0.2.1")
|
||||||
val content: String,
|
val value: String,
|
||||||
|
|
||||||
@Schema(description = "TTL (Time to Live)", example = "3600")
|
@Schema(description = "TTL (Time to Live)", example = "3600")
|
||||||
val ttl: Int = 300,
|
var ttl: Int = 300,
|
||||||
|
|
||||||
@Schema(description = "Priority", example = "0")
|
@Schema(description = "Priority", example = "0")
|
||||||
val priority: Int? = null,
|
val priority: Int? = null,
|
||||||
|
@ -32,13 +32,19 @@ class RecordService(
|
|||||||
if(domain.user.id != user.id)
|
if(domain.user.id != user.id)
|
||||||
throw RuntimeException("Unauthorized to create record in API: $domain_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(
|
val record = DomainRecord(
|
||||||
domain = domain,
|
domain = domain,
|
||||||
name = recordRequest.name,
|
name = recordRequest.name,
|
||||||
type = recordRequest.type,
|
type = recordRequest.type,
|
||||||
content = recordRequest.content,
|
content = recordRequest.value,
|
||||||
ttl = recordRequest.ttl,
|
ttl = recordRequest.ttl,
|
||||||
prio = recordRequest.priority ?: 0,
|
prio = recordRequest.priority ?: 0,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
@ -134,13 +140,19 @@ class RecordService(
|
|||||||
// 레코드 업데이트
|
// 레코드 업데이트
|
||||||
record.name = updatedRecord.name
|
record.name = updatedRecord.name
|
||||||
record.type = updatedRecord.type
|
record.type = updatedRecord.type
|
||||||
record.content = updatedRecord.content
|
record.content = updatedRecord.value
|
||||||
record.ttl = updatedRecord.ttl
|
record.ttl = updatedRecord.ttl
|
||||||
record.prio = updatedRecord.priority ?: 0
|
record.prio = updatedRecord.priority ?: 0
|
||||||
record.comment = updatedRecord.comment
|
record.comment = updatedRecord.comment
|
||||||
record.modifiedOn = Date()
|
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)
|
val savedRecord = recordRepository.save(record)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user