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 gson = Gson()
private val client = OkHttpClient() private val client = OkHttpClient()
@Throws(PowerDNSAPIError::class) @Throws(PowerDNSAPIException::class)
fun createZone(zoneName: String): Response { fun createZone(zoneName: String): Response {
val body = gson.toJson(mapOf( val body = gson.toJson(mapOf(
"name" to zoneName, "name" to zoneName,
@ -40,12 +40,12 @@ class PowerDNSAPIClient() {
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 error throw PowerDNSAPIException(error)
} }
return response return response
} }
@Throws(PowerDNSAPIError::class) @Throws(PowerDNSAPIException::class)
fun deleteZone(zoneName: String): Response { fun deleteZone(zoneName: String): Response {
val request = Request.Builder() val request = Request.Builder()
.url("$apiUrl/api/v1/servers/localhost/zones/$zoneName") .url("$apiUrl/api/v1/servers/localhost/zones/$zoneName")
@ -58,12 +58,12 @@ class PowerDNSAPIClient() {
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 error throw PowerDNSAPIException(error)
} }
return response return response
} }
@Throws(PowerDNSAPIError::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): Response {
val body = gson.toJson(mapOf( val body = gson.toJson(mapOf(
"name" to recordName, "name" to recordName,
@ -81,12 +81,12 @@ class PowerDNSAPIClient() {
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 error throw PowerDNSAPIException(error)
} }
return response return response
} }
@Throws(PowerDNSAPIError::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): Response {
val body = gson.toJson(mapOf( val body = gson.toJson(mapOf(
"content" to recordContent "content" to recordContent
@ -102,12 +102,12 @@ class PowerDNSAPIClient() {
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 error throw PowerDNSAPIException(error)
} }
return response return response
} }
@Throws(PowerDNSAPIError::class) @Throws(PowerDNSAPIException::class)
fun deleteRecord(zoneName: String, recordName: String, recordType: String): Response { fun deleteRecord(zoneName: String, recordName: String, recordType: String): Response {
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")
@ -120,10 +120,14 @@ class PowerDNSAPIClient() {
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 error throw PowerDNSAPIException(error)
} }
return response 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.http.HttpStatus
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException 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.db.Domain
import space.mori.dnsapi.dto.* import space.mori.dnsapi.dto.*
import space.mori.dnsapi.service.DomainService import space.mori.dnsapi.service.DomainService
@ -31,12 +31,12 @@ class DomainController(
fun allDomains(): ApiResponseDTO<List<DomainResponseDTO?>> { fun allDomains(): ApiResponseDTO<List<DomainResponseDTO?>> {
try { try {
return ApiResponseDTO(result = domainService.getAllDomains().map { it.toDTO() }) return ApiResponseDTO(result = domainService.getAllDomains().map { it.toDTO() })
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
throw ResponseStatusException( var idx = 0
HttpStatus.EXPECTATION_FAILED, 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()
) )
} }
} }
@ -53,11 +53,12 @@ class DomainController(
): ApiResponseDTO<DomainResponseDTO> { ): ApiResponseDTO<DomainResponseDTO> {
try { try {
return ApiResponseDTO(result = domainService.getDomainById(cfid!!).toDTO()) return ApiResponseDTO(result = domainService.getDomainById(cfid!!).toDTO())
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, 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> { fun createDomain(@RequestBody domain: DomainRequestDTO): ApiResponseDTO<DomainResponseDTO> {
try { try {
return ApiResponseDTO(result = domainService.createDomain(domain).toDTO()) return ApiResponseDTO(result = domainService.createDomain(domain).toDTO())
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, 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!!) domainService.deleteDomain(domain_id!!)
return ApiResponseDTO(result = DeleteResponseWithId(domain_id)) return ApiResponseDTO(result = DeleteResponseWithId(domain_id))
} catch(e : PowerDNSAPIError) { } catch (e: PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, var idx = 0
ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) }, result = listOf(null)).toString() 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.http.HttpStatus
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import org.springframework.web.server.ResponseStatusException 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.dto.*
import space.mori.dnsapi.service.RecordService import space.mori.dnsapi.service.RecordService
@ -29,11 +29,12 @@ class RecordController(
fun allRecords(@PathVariable zone_id: String): ApiResponseDTO<List<RecordResponseDTO>> { fun allRecords(@PathVariable zone_id: String): ApiResponseDTO<List<RecordResponseDTO>> {
try { try {
return ApiResponseDTO(result = recordService.getRecordsByDomain(zone_id)?.map{ it } ?: listOf()) return ApiResponseDTO(result = recordService.getRecordsByDomain(zone_id)?.map{ it } ?: listOf())
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, 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> { fun getRecordByCfid(@PathVariable zone_id: String, @PathVariable dns_record_id: String): ApiResponseDTO<RecordResponseDTO> {
try { try {
return ApiResponseDTO(result = recordService.getRecord(zone_id, dns_record_id)) return ApiResponseDTO(result = recordService.getRecord(zone_id, dns_record_id))
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, 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> { fun createRecord(@PathVariable zone_id: String, @RequestBody record: RecordRequestDTO): ApiResponseDTO<RecordResponseDTO> {
try { try {
return ApiResponseDTO(result = recordService.createRecord(zone_id, record)) return ApiResponseDTO(result = recordService.createRecord(zone_id, record))
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, 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 { try {
val record_id = recordService.deleteRecord(zone_id, dns_record_id) val record_id = recordService.deleteRecord(zone_id, dns_record_id)
return ApiResponseDTO(result = DeleteResponseWithId(record_id)) return ApiResponseDTO(result = DeleteResponseWithId(record_id))
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, 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> { fun updateRecord(@PathVariable zone_id: String, @PathVariable dns_record_id: String, @RequestBody record: RecordRequestDTO): ApiResponseDTO<RecordResponseDTO> {
try { try {
return ApiResponseDTO(result = recordService.updateRecord(zone_id, dns_record_id, record)) return ApiResponseDTO(result = recordService.updateRecord(zone_id, dns_record_id, record))
} catch(e : PowerDNSAPIError) { } catch(e : PowerDNSAPIException) {
val errors = mutableListOf(e.error) val errors = mutableListOf(e.message)
errors.addAll(e.errors) errors.addAll(e.errors)
var idx = 0
throw ResponseStatusException(HttpStatus.EXPECTATION_FAILED, 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()
) )
} }
} }