From 4abe88b0c95849388753eba12f7ea4eb7919c459 Mon Sep 17 00:00:00 2001 From: HyunSu1768 Date: Mon, 15 Jul 2024 23:00:15 +0900 Subject: [PATCH] =?UTF-8?q?feat=20::=20=EB=AA=B0=EB=9D=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/V2ContainerWebAdapter.kt | 42 ++++++++++++++- .../request/CreateGradleDockerfileRequest.kt | 8 +++ .../request/CreateNodeDockerfileRequest.kt | 9 ++++ .../CreateNodeWithNginxDockerfileRequest.kt | 9 ++++ .../port/in/CreateGradleDockerfileUseCase.kt | 13 +++++ .../port/in/CreateNodeDockerfileUseCase.kt | 13 +++++ .../CreateNodeWithNginxDockerfileUseCase.kt | 13 +++++ .../port/in/SetContainerConfigUseCase.kt | 3 +- .../service/CreateGradleDockerfileService.kt | 47 +++++++++++++++++ .../service/CreateNodeDockerfileService.kt | 49 ++++++++++++++++++ .../CreateNodeWithNginxDockerfileService.kt | 51 +++++++++++++++++++ 11 files changed, 254 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateGradleDockerfileRequest.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeDockerfileRequest.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeWithNginxDockerfileRequest.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateGradleDockerfileUseCase.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeDockerfileUseCase.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeWithNginxDockerfileUseCase.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateGradleDockerfileService.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeDockerfileService.kt create mode 100644 src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeWithNginxDockerfileService.kt diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/V2ContainerWebAdapter.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/V2ContainerWebAdapter.kt index 9c5aca1..dbc1caf 100644 --- a/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/V2ContainerWebAdapter.kt +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/V2ContainerWebAdapter.kt @@ -1,14 +1,24 @@ package xquare.app.xquareinfra.domain.container.adapter import org.springframework.web.bind.annotation.* +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateGradleDockerfileRequest +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateNodeDockerfileRequest +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateNodeWithNginxDockerfileRequest import xquare.app.xquareinfra.domain.container.adapter.dto.request.SetContainerConfigRequest +import xquare.app.xquareinfra.domain.container.application.port.`in`.CreateGradleDockerfileUseCase +import xquare.app.xquareinfra.domain.container.application.port.`in`.CreateNodeDockerfileUseCase +import xquare.app.xquareinfra.domain.container.application.port.`in`.CreateNodeWithNginxDockerfileUseCase import xquare.app.xquareinfra.domain.container.application.port.`in`.SetContainerConfigUseCase +import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment import java.util.* @RequestMapping("/v2/container") @RestController class V2ContainerWebAdapter( - private val setContainerConfigUseCase: SetContainerConfigUseCase + private val setContainerConfigUseCase: SetContainerConfigUseCase, + private val createGradleDockerfileUseCase: CreateGradleDockerfileUseCase, + private val createNodeWithNginxDockerfileUseCase: CreateNodeWithNginxDockerfileUseCase, + private val createNodeDockerfileUseCase: CreateNodeDockerfileUseCase ) { @PutMapping("/config") fun setContainerConfig( @@ -17,4 +27,34 @@ class V2ContainerWebAdapter( @RequestBody setContainerConfigRequest: SetContainerConfigRequest ) = setContainerConfigUseCase.setContainerConfig(deployId, setContainerConfigRequest) + + @PostMapping("/gradle") + fun createGradleDockerfile( + @RequestParam(name = "deployId", required = true) + deployId: UUID, + @RequestParam(name = "environment", required = true) + containerEnvironment: ContainerEnvironment, + @RequestBody + createGradleDockerfileRequest: CreateGradleDockerfileRequest + ) = createGradleDockerfileUseCase.createGradleDockerfile(deployId, containerEnvironment, createGradleDockerfileRequest) + + @PostMapping("/node") + fun createNodeDockerfile( + @RequestParam(name = "deployId", required = true) + deployId: UUID, + @RequestParam(name = "environment", required = true) + containerEnvironment: ContainerEnvironment, + @RequestBody + createNodeDockerfileRequest: CreateNodeDockerfileRequest + ) = createNodeDockerfileUseCase.createNodeDockerfile(deployId, containerEnvironment, createNodeDockerfileRequest) + + @PostMapping("/node-with-nginx") + fun createNodeWithNginxDockerfile( + @RequestParam(name = "deployId", required = true) + deployId: UUID, + @RequestParam(name = "environment", required = true) + containerEnvironment: ContainerEnvironment, + @RequestBody + createNodeWithNginxDockerfileRequest: CreateNodeWithNginxDockerfileRequest + ) = createNodeWithNginxDockerfileUseCase.createNodeWithNginxDockerfile(deployId, containerEnvironment, createNodeWithNginxDockerfileRequest) } \ No newline at end of file diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateGradleDockerfileRequest.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateGradleDockerfileRequest.kt new file mode 100644 index 0000000..5e00748 --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateGradleDockerfileRequest.kt @@ -0,0 +1,8 @@ +package xquare.app.xquareinfra.domain.container.adapter.dto.request + +data class CreateGradleDockerfileRequest( + val jdkVersion: Int, + val outputDir: String, + val builder: String = "gradle", + val commands: List = listOf(), +) diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeDockerfileRequest.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeDockerfileRequest.kt new file mode 100644 index 0000000..bdbff75 --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeDockerfileRequest.kt @@ -0,0 +1,9 @@ +package xquare.app.xquareinfra.domain.container.adapter.dto.request + +data class CreateNodeDockerfileRequest( + val nodeVersion: String, + val buildCommands: List = emptyList(), + val builder: String = "node", + val command: String, + var port: Int? = null +) \ No newline at end of file diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeWithNginxDockerfileRequest.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeWithNginxDockerfileRequest.kt new file mode 100644 index 0000000..3ae9051 --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/dto/request/CreateNodeWithNginxDockerfileRequest.kt @@ -0,0 +1,9 @@ +package xquare.app.xquareinfra.domain.container.adapter.dto.request + +data class CreateNodeWithNginxDockerfileRequest( + val nodeVersion: Int, + val commands: List = emptyList(), + val builder: String = "node_with_nginx", + val outputDir: String, + var port: Int? +) diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateGradleDockerfileUseCase.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateGradleDockerfileUseCase.kt new file mode 100644 index 0000000..891a15a --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateGradleDockerfileUseCase.kt @@ -0,0 +1,13 @@ +package xquare.app.xquareinfra.domain.container.application.port.`in` + +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateGradleDockerfileRequest +import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment +import java.util.UUID + +interface CreateGradleDockerfileUseCase { + fun createGradleDockerfile( + deployId: UUID, + containerEnvironment: ContainerEnvironment, + createGradleDockerfileRequest: CreateGradleDockerfileRequest + ) +} \ No newline at end of file diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeDockerfileUseCase.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeDockerfileUseCase.kt new file mode 100644 index 0000000..95ed1a3 --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeDockerfileUseCase.kt @@ -0,0 +1,13 @@ +package xquare.app.xquareinfra.domain.container.application.port.`in` + +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateNodeDockerfileRequest +import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment +import java.util.* + +interface CreateNodeDockerfileUseCase { + fun createNodeDockerfile( + deployId: UUID, + containerEnvironment: ContainerEnvironment, + createNodeDockerfileRequest: CreateNodeDockerfileRequest + ) +} \ No newline at end of file diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeWithNginxDockerfileUseCase.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeWithNginxDockerfileUseCase.kt new file mode 100644 index 0000000..2bfc853 --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/CreateNodeWithNginxDockerfileUseCase.kt @@ -0,0 +1,13 @@ +package xquare.app.xquareinfra.domain.container.application.port.`in` + +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateNodeWithNginxDockerfileRequest +import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment +import java.util.* + +interface CreateNodeWithNginxDockerfileUseCase { + fun createNodeWithNginxDockerfile( + deployId: UUID, + containerEnvironment: ContainerEnvironment, + createNodeDockerfileRequest: CreateNodeWithNginxDockerfileRequest + ) +} \ No newline at end of file diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/SetContainerConfigUseCase.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/SetContainerConfigUseCase.kt index 489aa26..0644c9e 100644 --- a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/SetContainerConfigUseCase.kt +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/port/in/SetContainerConfigUseCase.kt @@ -1,8 +1,7 @@ package xquare.app.xquareinfra.domain.container.application.port.`in` import xquare.app.xquareinfra.domain.container.adapter.dto.request.SetContainerConfigRequest -import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment -import java.util.UUID +import java.util.* interface SetContainerConfigUseCase { fun setContainerConfig( diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateGradleDockerfileService.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateGradleDockerfileService.kt new file mode 100644 index 0000000..71c47f3 --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateGradleDockerfileService.kt @@ -0,0 +1,47 @@ +package xquare.app.xquareinfra.domain.container.application.service + +import com.fasterxml.jackson.databind.ObjectMapper +import org.springframework.stereotype.Service +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateGradleDockerfileRequest +import xquare.app.xquareinfra.domain.container.application.port.`in`.CreateGradleDockerfileUseCase +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.feign.client.github.GithubClient +import xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request.DispatchEventRequest +import xquare.app.xquareinfra.infrastructure.global.env.github.GithubProperties +import java.util.* + +@Service +class CreateGradleDockerfileService( + private val findDeployPort: FindDeployPort, + private val githubClient: GithubClient, + private val findContainerPort: FindContainerPort, + private val githubProperties: GithubProperties, + private val objectMapper: ObjectMapper +) : CreateGradleDockerfileUseCase{ + override fun createGradleDockerfile( + deployId: UUID, + containerEnvironment: ContainerEnvironment, + createGradleDockerfileRequest: CreateGradleDockerfileRequest + ) { + val deploy = findDeployPort.findById(deployId) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND + val container = findContainerPort.findByDeployAndEnvironment(deploy, containerEnvironment) + ?: throw BusinessLogicException.CONTAINER_NOT_FOUND + + githubClient.dispatchWorkflow( + authorization = "Bearer ${githubProperties.token}", + accept = "application/vnd.github.v3+json", + request = DispatchEventRequest( + event_type = "write-dockerfile", + client_payload = mapOf( + "name" to deploy.deployName, + "environment" to container.containerEnvironment.name, + "template_json" to objectMapper.writeValueAsString(createGradleDockerfileRequest), + "builder" to createGradleDockerfileRequest.builder + ) + ) + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeDockerfileService.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeDockerfileService.kt new file mode 100644 index 0000000..b67407f --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeDockerfileService.kt @@ -0,0 +1,49 @@ +package xquare.app.xquareinfra.domain.container.application.service + +import com.fasterxml.jackson.databind.ObjectMapper +import org.springframework.stereotype.Service +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateNodeDockerfileRequest +import xquare.app.xquareinfra.domain.container.application.port.`in`.CreateNodeDockerfileUseCase +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.feign.client.github.GithubClient +import xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request.DispatchEventRequest +import xquare.app.xquareinfra.infrastructure.global.env.github.GithubProperties +import java.util.* + +@Service +class CreateNodeDockerfileService( + private val findDeployPort: FindDeployPort, + private val githubClient: GithubClient, + private val findContainerPort: FindContainerPort, + private val githubProperties: GithubProperties, + private val objectMapper: ObjectMapper +) : CreateNodeDockerfileUseCase { + override fun createNodeDockerfile( + deployId: UUID, + containerEnvironment: ContainerEnvironment, + createNodeDockerfileRequest: CreateNodeDockerfileRequest + ) { + val deploy = findDeployPort.findById(deployId) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND + val container = findContainerPort.findByDeployAndEnvironment(deploy, containerEnvironment) + ?: throw BusinessLogicException.CONTAINER_NOT_FOUND + + createNodeDockerfileRequest.port = container.containerPort + + githubClient.dispatchWorkflow( + authorization = "Bearer ${githubProperties.token}", + accept = "application/vnd.github.v3+json", + request = DispatchEventRequest( + event_type = "write-dockerfile", + client_payload = mapOf( + "name" to deploy.deployName, + "environment" to container.containerEnvironment.name, + "template_json" to objectMapper.writeValueAsString(createNodeDockerfileRequest), + "builder" to createNodeDockerfileRequest.builder + ) + ) + ) + } +} \ No newline at end of file diff --git a/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeWithNginxDockerfileService.kt b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeWithNginxDockerfileService.kt new file mode 100644 index 0000000..c638d6f --- /dev/null +++ b/src/main/kotlin/xquare/app/xquareinfra/domain/container/application/service/CreateNodeWithNginxDockerfileService.kt @@ -0,0 +1,51 @@ +package xquare.app.xquareinfra.domain.container.application.service + +import com.fasterxml.jackson.databind.ObjectMapper +import org.springframework.stereotype.Service +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateNodeDockerfileRequest +import xquare.app.xquareinfra.domain.container.adapter.dto.request.CreateNodeWithNginxDockerfileRequest +import xquare.app.xquareinfra.domain.container.application.port.`in`.CreateNodeDockerfileUseCase +import xquare.app.xquareinfra.domain.container.application.port.`in`.CreateNodeWithNginxDockerfileUseCase +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.feign.client.github.GithubClient +import xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request.DispatchEventRequest +import xquare.app.xquareinfra.infrastructure.global.env.github.GithubProperties +import java.util.* + +@Service +class CreateNodeWithNginxDockerfileService( + private val findDeployPort: FindDeployPort, + private val githubClient: GithubClient, + private val findContainerPort: FindContainerPort, + private val githubProperties: GithubProperties, + private val objectMapper: ObjectMapper +) : CreateNodeWithNginxDockerfileUseCase { + override fun createNodeWithNginxDockerfile( + deployId: UUID, + containerEnvironment: ContainerEnvironment, + createNodeDockerfileRequest: CreateNodeWithNginxDockerfileRequest + ) { + val deploy = findDeployPort.findById(deployId) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND + val container = findContainerPort.findByDeployAndEnvironment(deploy, containerEnvironment) + ?: throw BusinessLogicException.CONTAINER_NOT_FOUND + + createNodeDockerfileRequest.port = container.containerPort + + githubClient.dispatchWorkflow( + authorization = "Bearer ${githubProperties.token}", + accept = "application/vnd.github.v3+json", + request = DispatchEventRequest( + event_type = "write-dockerfile", + client_payload = mapOf( + "name" to deploy.deployName, + "environment" to container.containerEnvironment.name, + "template_json" to objectMapper.writeValueAsString(createNodeDockerfileRequest), + "builder" to createNodeDockerfileRequest.builder + ) + ) + ) + } +} \ No newline at end of file