mirror of
https://github.com/dalbodeule/sh0rt.kr-pdns.git
synced 2025-06-08 18:58:20 +00:00
fix pdns api clients (retry 10x)
This commit is contained in:
parent
ccb02f0f50
commit
f0f9bd4760
@ -6,7 +6,6 @@ import okhttp3.OkHttpClient
|
|||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import okhttp3.internal.concat
|
|
||||||
import org.springframework.beans.factory.annotation.Value
|
import org.springframework.beans.factory.annotation.Value
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
@ -39,8 +38,7 @@ 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(), Error::class.java)
|
throw gson.fromJson(response.body?.string(), Error::class.java)
|
||||||
throw RuntimeException("Unexpected code ${error.error}, ${error.errors.concat(", ")}")
|
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@ -56,8 +54,7 @@ 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(), Error::class.java)
|
throw gson.fromJson(response.body?.string(), Error::class.java)
|
||||||
throw RuntimeException("Unexpected code ${error.error}, ${error.errors.concat(", ")}")
|
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@ -78,8 +75,7 @@ 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(), Error::class.java)
|
throw gson.fromJson(response.body?.string(), Error::class.java)
|
||||||
throw RuntimeException("Unexpected code ${error.error}, ${error.errors.concat(", ")}")
|
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@ -98,8 +94,7 @@ 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(), Error::class.java)
|
throw gson.fromJson(response.body?.string(), Error::class.java)
|
||||||
throw RuntimeException("Unexpected code ${error.error}, ${error.errors.concat(", ")}")
|
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@ -115,11 +110,10 @@ 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(), Error::class.java)
|
throw gson.fromJson(response.body?.string(), Error::class.java)
|
||||||
throw RuntimeException("Unexpected code ${error.error}, ${error.errors.concat(", ")}")
|
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data class Error(val error: String, val errors: Array<String>)
|
data class PowerDNSAPIError(val error: String, val errors: List<String>): RuntimeException(error)
|
@ -7,12 +7,9 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse
|
|||||||
import io.swagger.v3.oas.annotations.responses.ApiResponses
|
import io.swagger.v3.oas.annotations.responses.ApiResponses
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.web.bind.annotation.*
|
import org.springframework.web.bind.annotation.*
|
||||||
|
import space.mori.dnsapi.PowerDNSAPIError
|
||||||
import space.mori.dnsapi.db.Domain
|
import space.mori.dnsapi.db.Domain
|
||||||
import space.mori.dnsapi.dto.ApiResponseDTO
|
import space.mori.dnsapi.dto.*
|
||||||
import space.mori.dnsapi.dto.DeleteResponseWithId
|
|
||||||
import space.mori.dnsapi.dto.DomainRequestDTO
|
|
||||||
import space.mori.dnsapi.dto.DomainResponseDTO
|
|
||||||
import space.mori.dnsapi.filter.getCurrentUser
|
|
||||||
import space.mori.dnsapi.service.DomainService
|
import space.mori.dnsapi.service.DomainService
|
||||||
|
|
||||||
|
|
||||||
@ -30,7 +27,13 @@ class DomainController(
|
|||||||
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))])
|
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))])
|
||||||
])
|
])
|
||||||
fun allDomains(): ApiResponseDTO<List<DomainResponseDTO?>> {
|
fun allDomains(): ApiResponseDTO<List<DomainResponseDTO?>> {
|
||||||
return ApiResponseDTO(result = domainService.getAllDomains().map { it.toDTO() })
|
try {
|
||||||
|
return ApiResponseDTO(result = domainService.getAllDomains().map { it.toDTO() })
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "Get domain", tags = ["domain"])
|
@Operation(summary = "Get domain", tags = ["domain"])
|
||||||
@ -43,7 +46,13 @@ class DomainController(
|
|||||||
fun getDomainByCfid(
|
fun getDomainByCfid(
|
||||||
@PathVariable cfid: String?
|
@PathVariable cfid: String?
|
||||||
): ApiResponseDTO<DomainResponseDTO> {
|
): ApiResponseDTO<DomainResponseDTO> {
|
||||||
return ApiResponseDTO(result = domainService.getDomainById(cfid!!).toDTO())
|
try {
|
||||||
|
return ApiResponseDTO(result = domainService.getDomainById(cfid!!).toDTO())
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "Create domain", tags = ["domain"])
|
@Operation(summary = "Create domain", tags = ["domain"])
|
||||||
@ -54,7 +63,13 @@ class DomainController(
|
|||||||
])
|
])
|
||||||
@PostMapping
|
@PostMapping
|
||||||
fun createDomain(@RequestBody domain: DomainRequestDTO): ApiResponseDTO<DomainResponseDTO> {
|
fun createDomain(@RequestBody domain: DomainRequestDTO): ApiResponseDTO<DomainResponseDTO> {
|
||||||
return ApiResponseDTO(result = domainService.createDomain(domain).toDTO())
|
try {
|
||||||
|
return ApiResponseDTO(result = domainService.createDomain(domain).toDTO())
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "Delete domain", tags = ["domain"])
|
@Operation(summary = "Delete domain", tags = ["domain"])
|
||||||
@ -65,9 +80,15 @@ class DomainController(
|
|||||||
])
|
])
|
||||||
@DeleteMapping("/{domain_id}")
|
@DeleteMapping("/{domain_id}")
|
||||||
fun deleteDomain(@PathVariable domain_id: String?): ApiResponseDTO<DeleteResponseWithId> {
|
fun deleteDomain(@PathVariable domain_id: String?): ApiResponseDTO<DeleteResponseWithId> {
|
||||||
domainService.deleteDomain(domain_id!!)
|
try {
|
||||||
|
domainService.deleteDomain(domain_id!!)
|
||||||
|
|
||||||
return ApiResponseDTO(result=DeleteResponseWithId(domain_id))
|
return ApiResponseDTO(result = DeleteResponseWithId(domain_id))
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Domain.toDTO() = DomainResponseDTO(id = cfid, name = name)
|
private fun Domain.toDTO() = DomainResponseDTO(id = cfid, name = name)
|
||||||
|
@ -7,9 +7,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse
|
|||||||
import io.swagger.v3.oas.annotations.responses.ApiResponses
|
import io.swagger.v3.oas.annotations.responses.ApiResponses
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.web.bind.annotation.*
|
import org.springframework.web.bind.annotation.*
|
||||||
|
import space.mori.dnsapi.PowerDNSAPIError
|
||||||
import space.mori.dnsapi.dto.*
|
import space.mori.dnsapi.dto.*
|
||||||
import space.mori.dnsapi.db.Record as DomainRecord
|
|
||||||
import space.mori.dnsapi.getISOFormat
|
|
||||||
import space.mori.dnsapi.service.RecordService
|
import space.mori.dnsapi.service.RecordService
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@ -26,7 +25,13 @@ class RecordController(
|
|||||||
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
||||||
])
|
])
|
||||||
fun allRecords(@PathVariable zone_id: String): ApiResponseDTO<List<RecordResponseDTO>> {
|
fun allRecords(@PathVariable zone_id: String): ApiResponseDTO<List<RecordResponseDTO>> {
|
||||||
return ApiResponseDTO(result = recordService.getRecordsByDomain(zone_id)?.map{ it } ?: listOf())
|
try {
|
||||||
|
return ApiResponseDTO(result = recordService.getRecordsByDomain(zone_id)?.map{ it } ?: listOf())
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("{zone_id}/dns_records/{dns_record_id}")
|
@GetMapping("{zone_id}/dns_records/{dns_record_id}")
|
||||||
@ -37,7 +42,13 @@ class RecordController(
|
|||||||
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
||||||
])
|
])
|
||||||
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> {
|
||||||
return ApiResponseDTO(result = recordService.getRecord(zone_id, dns_record_id))
|
try {
|
||||||
|
return ApiResponseDTO(result = recordService.getRecord(zone_id, dns_record_id))
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("{zone_id}/dns_records")
|
@PostMapping("{zone_id}/dns_records")
|
||||||
@ -48,7 +59,13 @@ class RecordController(
|
|||||||
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
||||||
])
|
])
|
||||||
fun createRecord(@PathVariable zone_id: String, @RequestBody record: RecordRequestDTO): ApiResponseDTO<RecordResponseDTO> {
|
fun createRecord(@PathVariable zone_id: String, @RequestBody record: RecordRequestDTO): ApiResponseDTO<RecordResponseDTO> {
|
||||||
return ApiResponseDTO(result = recordService.createRecord(zone_id, record))
|
try {
|
||||||
|
return ApiResponseDTO(result = recordService.createRecord(zone_id, record))
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("{zone_id}/dns_records/{dns_record_id}")
|
@DeleteMapping("{zone_id}/dns_records/{dns_record_id}")
|
||||||
@ -59,8 +76,14 @@ class RecordController(
|
|||||||
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
||||||
])
|
])
|
||||||
fun deleteRecord(@PathVariable zone_id: String, @PathVariable dns_record_id: String): ApiResponseDTO<DeleteResponseWithId> {
|
fun deleteRecord(@PathVariable zone_id: String, @PathVariable dns_record_id: String): ApiResponseDTO<DeleteResponseWithId> {
|
||||||
val record_id = recordService.deleteRecord(zone_id, dns_record_id)
|
try {
|
||||||
return ApiResponseDTO(result = DeleteResponseWithId(record_id))
|
val record_id = recordService.deleteRecord(zone_id, dns_record_id)
|
||||||
|
return ApiResponseDTO(result = DeleteResponseWithId(record_id))
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("{zone_id}/dns_records/{dns_record_id}")
|
@PatchMapping("{zone_id}/dns_records/{dns_record_id}")
|
||||||
@ -71,20 +94,12 @@ class RecordController(
|
|||||||
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
content = [Content(schema = Schema(implementation = ApiResponseDTO::class))]),
|
||||||
])
|
])
|
||||||
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> {
|
||||||
return ApiResponseDTO(result = recordService.updateRecord(zone_id, dns_record_id, record))
|
try {
|
||||||
|
return ApiResponseDTO(result = recordService.updateRecord(zone_id, dns_record_id, record))
|
||||||
|
} catch(e : PowerDNSAPIError) {
|
||||||
|
val errors = mutableListOf(e.error)
|
||||||
|
errors.addAll(e.errors)
|
||||||
|
return ApiResponseDTO(false, errors = errors.map { ErrorOrMessage(1, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun DomainRecord.toDTO() = RecordResponseDTO(
|
|
||||||
id = cfid,
|
|
||||||
type = type,
|
|
||||||
name = name,
|
|
||||||
content = content,
|
|
||||||
zoneId = domain.cfid,
|
|
||||||
zoneName = domain.name,
|
|
||||||
priority = prio,
|
|
||||||
ttl = ttl,
|
|
||||||
createdOn = createdOn.getISOFormat(),
|
|
||||||
modifiedOn = modifiedOn.getISOFormat(),
|
|
||||||
comment = comment
|
|
||||||
)
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user