fix pdns api clients (retry 14x)

This commit is contained in:
dalbodeule 2024-06-07 12:17:53 +09:00
parent 1bc653db3b
commit 030b31cbdd
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
3 changed files with 60 additions and 43 deletions

View File

@ -23,7 +23,7 @@ class PowerDNSAPIClient() {
private val gson = Gson()
private val client = OkHttpClient()
@Throws(PowerDNSAPIError::class)
@Throws(PowerDNSAPIException::class)
fun createZone(zoneName: String): Response {
val body = gson.toJson(mapOf(
"name" to zoneName,
@ -40,12 +40,12 @@ class PowerDNSAPIClient() {
val response = client.newCall(request).execute()
if(!response.isSuccessful) {
val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java)
throw error
throw PowerDNSAPIException(error)
}
return response
}
@Throws(PowerDNSAPIError::class)
@Throws(PowerDNSAPIException::class)
fun deleteZone(zoneName: String): Response {
val request = Request.Builder()
.url("$apiUrl/api/v1/servers/localhost/zones/$zoneName")
@ -58,12 +58,12 @@ class PowerDNSAPIClient() {
val response = client.newCall(request).execute()
if(!response.isSuccessful) {
val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java)
throw error
throw PowerDNSAPIException(error)
}
return response
}
@Throws(PowerDNSAPIError::class)
@Throws(PowerDNSAPIException::class)
fun createRecord(zoneName: String, recordName: String, recordType: String, recordContent: String): Response {
val body = gson.toJson(mapOf(
"name" to recordName,
@ -81,12 +81,12 @@ class PowerDNSAPIClient() {
val response = client.newCall(request).execute()
if(!response.isSuccessful) {
val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java)
throw error
throw PowerDNSAPIException(error)
}
return response
}
@Throws(PowerDNSAPIError::class)
@Throws(PowerDNSAPIException::class)
fun updateRecord(zoneName: String, recordName: String, recordType: String, recordContent: String): Response {
val body = gson.toJson(mapOf(
"content" to recordContent
@ -102,12 +102,12 @@ class PowerDNSAPIClient() {
val response = client.newCall(request).execute()
if(!response.isSuccessful) {
val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java)
throw error
throw PowerDNSAPIException(error)
}
return response
}
@Throws(PowerDNSAPIError::class)
@Throws(PowerDNSAPIException::class)
fun deleteRecord(zoneName: String, recordName: String, recordType: String): Response {
val request = Request.Builder()
.url("$apiUrl/api/v1/servers/localhost/zones/$zoneName/records/$recordName/$recordType")
@ -120,10 +120,14 @@ class PowerDNSAPIClient() {
val response = client.newCall(request).execute()
if(!response.isSuccessful) {
val error = gson.fromJson(response.body?.string(), PowerDNSAPIError::class.java)
throw error
throw PowerDNSAPIException(error)
}
return response
}
}
class PowerDNSAPIError(val error: String, val errors: List<String>): RuntimeException(error)
class PowerDNSAPIError(val error: String, val errors: List<String>)
class PowerDNSAPIException(private val error: PowerDNSAPIError): RuntimeException(error.error) {
val errors: List<String>
get() = error.errors
}

View File

@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException
import space.mori.dnsapi.PowerDNSAPIError
import space.mori.dnsapi.PowerDNSAPIException
import space.mori.dnsapi.db.Domain
import space.mori.dnsapi.dto.*
import space.mori.dnsapi.service.DomainService
@ -31,12 +31,12 @@ class DomainController(
fun allDomains(): ApiResponseDTO<List<DomainResponseDTO?>> {
try {
return ApiResponseDTO(result = domainService.getAllDomains().map { it.toDTO() })
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
throw ResponseStatusException(
HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}
@ -53,11 +53,12 @@ class DomainController(
): ApiResponseDTO<DomainResponseDTO> {
try {
return ApiResponseDTO(result = domainService.getDomainById(cfid!!).toDTO())
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}
@ -72,11 +73,12 @@ class DomainController(
fun createDomain(@RequestBody domain: DomainRequestDTO): ApiResponseDTO<DomainResponseDTO> {
try {
return ApiResponseDTO(result = domainService.createDomain(domain).toDTO())
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}
@ -93,11 +95,17 @@ class DomainController(
domainService.deleteDomain(domain_id!!)
return ApiResponseDTO(result = DeleteResponseWithId(domain_id))
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch (e: PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
var idx = 0
throw ResponseStatusException(
HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(
false,
errors = errors.map { ErrorOrMessage(idx++, it ?: "") },
result = listOf(null)
).toString()
)
}
}

View File

@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException
import space.mori.dnsapi.PowerDNSAPIError
import space.mori.dnsapi.PowerDNSAPIException
import space.mori.dnsapi.dto.*
import space.mori.dnsapi.service.RecordService
@ -29,11 +29,12 @@ class RecordController(
fun allRecords(@PathVariable zone_id: String): ApiResponseDTO<List<RecordResponseDTO>> {
try {
return ApiResponseDTO(result = recordService.getRecordsByDomain(zone_id)?.map{ it } ?: listOf())
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}
@ -48,11 +49,12 @@ class RecordController(
fun getRecordByCfid(@PathVariable zone_id: String, @PathVariable dns_record_id: String): ApiResponseDTO<RecordResponseDTO> {
try {
return ApiResponseDTO(result = recordService.getRecord(zone_id, dns_record_id))
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}
@ -67,11 +69,12 @@ class RecordController(
fun createRecord(@PathVariable zone_id: String, @RequestBody record: RecordRequestDTO): ApiResponseDTO<RecordResponseDTO> {
try {
return ApiResponseDTO(result = recordService.createRecord(zone_id, record))
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}
@ -87,11 +90,12 @@ class RecordController(
try {
val record_id = recordService.deleteRecord(zone_id, dns_record_id)
return ApiResponseDTO(result = DeleteResponseWithId(record_id))
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}
@ -106,11 +110,12 @@ class RecordController(
fun updateRecord(@PathVariable zone_id: String, @PathVariable dns_record_id: String, @RequestBody record: RecordRequestDTO): ApiResponseDTO<RecordResponseDTO> {
try {
return ApiResponseDTO(result = recordService.updateRecord(zone_id, dns_record_id, record))
} catch(e : PowerDNSAPIError) {
val errors = mutableListOf(e.error)
} catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.message)
errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED,
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString()
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(idx++, it ?: "") }, result = listOf(null)).toString()
)
}
}