-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4dc6b94
commit c8acadc
Showing
12 changed files
with
207 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...xquare/app/xquareinfra/domain/container/application/port/in/SyncContainerDomainUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package xquare.app.xquareinfra.domain.container.application.port.`in` | ||
|
||
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment | ||
|
||
interface SyncContainerDomainUseCase { | ||
fun syncContainerDomain(deployName: String, containerEnvironment: ContainerEnvironment, domain: String) | ||
} |
64 changes: 64 additions & 0 deletions
64
...xquare/app/xquareinfra/domain/container/application/service/SyncContainerDomainService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package xquare.app.xquareinfra.domain.container.application.service | ||
|
||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import xquare.app.xquareinfra.domain.container.application.port.`in`.SyncContainerDomainUseCase | ||
import xquare.app.xquareinfra.domain.container.application.port.out.FindContainerPort | ||
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment | ||
import xquare.app.xquareinfra.domain.deploy.application.port.out.FindDeployPort | ||
import xquare.app.xquareinfra.infrastructure.exception.BusinessLogicException | ||
import xquare.app.xquareinfra.infrastructure.exception.XquareException | ||
import xquare.app.xquareinfra.infrastructure.feign.client.cloudflare.CloudflareClient | ||
import xquare.app.xquareinfra.infrastructure.feign.client.cloudflare.dto.request.CreateDnsRecordRequest | ||
import xquare.app.xquareinfra.infrastructure.global.env.cloudflare.CloudflareProperties | ||
import xquare.app.xquareinfra.infrastructure.kubernetes.env.XquareProperties | ||
|
||
@Transactional | ||
@Service | ||
class SyncContainerDomainService( | ||
private val findDeployPort: FindDeployPort, | ||
private val findContainerPort: FindContainerPort, | ||
private val cloudflareClient: CloudflareClient, | ||
private val cloudflareProperties: CloudflareProperties, | ||
private val xquareProperties: XquareProperties | ||
) : SyncContainerDomainUseCase{ | ||
override fun syncContainerDomain( | ||
deployName: String, | ||
containerEnvironment: ContainerEnvironment, | ||
domain: String | ||
) { | ||
val deploy = findDeployPort.findByDeployName(deployName) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND | ||
val container = findContainerPort.findByDeployAndEnvironment(deploy, containerEnvironment) ?: throw BusinessLogicException.CONTAINER_NOT_FOUND | ||
|
||
container.updateDomain(domain) | ||
|
||
val listResponse = cloudflareClient.listDnsRecords( | ||
cloudflareProperties.zoneId, | ||
cloudflareProperties.xAuthEmail, | ||
cloudflareProperties.xAuthKey | ||
) | ||
|
||
if(listResponse.statusCode.isError) { | ||
throw XquareException.INTERNAL_SERVER_ERROR | ||
} | ||
|
||
val records = listResponse.body | ||
if(!records!!.result.any { it.name == domain }) { | ||
val createResponse = cloudflareClient.createDnsRecords( | ||
cloudflareProperties.zoneId, | ||
cloudflareProperties.xAuthEmail, | ||
cloudflareProperties.xAuthKey, | ||
CreateDnsRecordRequest( | ||
content = xquareProperties.gatewayDns, | ||
name = domain, | ||
proxied = false, | ||
type = "CNAME" | ||
) | ||
) | ||
|
||
if(createResponse.statusCode.isError) { | ||
throw XquareException.INTERNAL_SERVER_ERROR | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
.../kotlin/xquare/app/xquareinfra/infrastructure/feign/client/cloudflare/CloudflareClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.cloudflare | ||
|
||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
import xquare.app.xquareinfra.infrastructure.feign.client.cloudflare.dto.request.CreateDnsRecordRequest | ||
import xquare.app.xquareinfra.infrastructure.feign.client.cloudflare.dto.response.ListDnsRecordsResponse | ||
import xquare.app.xquareinfra.infrastructure.feign.config.FeignConfig | ||
|
||
@FeignClient( | ||
name = "cloudflare-client", | ||
url = "\${url.cloudflare}", | ||
configuration = [FeignConfig::class] | ||
) | ||
interface CloudflareClient { | ||
@GetMapping("/zones/{zone_id}/dns_records") | ||
fun listDnsRecords( | ||
@PathVariable("zone_id") zoneId: String, | ||
@RequestHeader("X-Auth-Email") xAuthEmail: String, | ||
@RequestHeader("X-Auth-Key") xAuthKey: String, | ||
): ResponseEntity<ListDnsRecordsResponse> | ||
|
||
@PostMapping("/zones/{zone_id}/dns_records") | ||
fun createDnsRecords( | ||
@PathVariable("zone_id") zoneId: String, | ||
@RequestHeader("X-Auth-Email") xAuthEmail: String, | ||
@RequestHeader("X-Auth-Key") xAuthKey: String, | ||
@RequestBody createDnsRecordRequest: CreateDnsRecordRequest | ||
): ResponseEntity<Any> | ||
} |
8 changes: 8 additions & 0 deletions
8
.../xquareinfra/infrastructure/feign/client/cloudflare/dto/request/CreateDnsRecordRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.cloudflare.dto.request | ||
|
||
data class CreateDnsRecordRequest( | ||
val content: String, | ||
val name: String, | ||
val proxied: Boolean, | ||
val type: String | ||
) |
59 changes: 59 additions & 0 deletions
59
...xquareinfra/infrastructure/feign/client/cloudflare/dto/response/ListDnsRecordsResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.cloudflare.dto.response | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class ListDnsRecordsResponse( | ||
val result: List<Result>, | ||
val success: Boolean, | ||
val errors: List<Any?>, | ||
val messages: List<Any?>, | ||
@JsonProperty("result_info") | ||
val resultInfo: ResultInfo, | ||
) | ||
|
||
data class Result( | ||
val id: String, | ||
@JsonProperty("zone_id") | ||
val zoneId: String, | ||
@JsonProperty("zone_name") | ||
val zoneName: String, | ||
val name: String, | ||
val type: String, | ||
val content: String, | ||
val proxiable: Boolean, | ||
val proxied: Boolean, | ||
val ttl: Long, | ||
val locked: Boolean, | ||
val meta: Meta, | ||
val comment: Any?, | ||
val tags: List<Any?>, | ||
@JsonProperty("created_on") | ||
val createdOn: String, | ||
@JsonProperty("modified_on") | ||
val modifiedOn: String, | ||
val priority: Long?, | ||
) | ||
|
||
data class Meta( | ||
@JsonProperty("auto_added") | ||
val autoAdded: Boolean, | ||
@JsonProperty("managed_by_apps") | ||
val managedByApps: Boolean, | ||
@JsonProperty("managed_by_argo_tunnel") | ||
val managedByArgoTunnel: Boolean, | ||
@JsonProperty("email_routing") | ||
val emailRouting: Boolean?, | ||
@JsonProperty("read_only") | ||
val readOnly: Boolean?, | ||
) | ||
|
||
data class ResultInfo( | ||
val page: Long, | ||
@JsonProperty("per_page") | ||
val perPage: Long, | ||
val count: Long, | ||
@JsonProperty("total_count") | ||
val totalCount: Long, | ||
@JsonProperty("total_pages") | ||
val totalPages: Long, | ||
) |
12 changes: 12 additions & 0 deletions
12
...otlin/xquare/app/xquareinfra/infrastructure/global/env/cloudflare/CloudflareProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package xquare.app.xquareinfra.infrastructure.global.env.cloudflare | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.ConstructorBinding | ||
|
||
@ConstructorBinding | ||
@ConfigurationProperties(value = "cloudflare") | ||
data class CloudflareProperties( | ||
val zoneId: String, | ||
val xAuthKey: String, | ||
val xAuthEmail: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters