-
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
8cc1646
commit f74b8fa
Showing
11 changed files
with
185 additions
and
7 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
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/xquare/app/xquareinfra/domain/container/adapter/V2ContainerWebAdapter.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,20 @@ | ||
package xquare.app.xquareinfra.domain.container.adapter | ||
|
||
import org.springframework.web.bind.annotation.* | ||
import xquare.app.xquareinfra.domain.container.adapter.dto.request.SetContainerConfigRequest | ||
import xquare.app.xquareinfra.domain.container.application.port.`in`.SetContainerConfigUseCase | ||
import java.util.* | ||
|
||
@RequestMapping("/v2/container") | ||
@RestController | ||
class V2ContainerWebAdapter( | ||
private val setContainerConfigUseCase: SetContainerConfigUseCase | ||
) { | ||
@PutMapping("/config") | ||
fun setContainerConfig( | ||
@RequestParam(name = "deployId", required = true) | ||
deployId: UUID, | ||
@RequestBody | ||
setContainerConfigRequest: SetContainerConfigRequest | ||
) = setContainerConfigUseCase.setContainerConfig(deployId, setContainerConfigRequest) | ||
} |
12 changes: 12 additions & 0 deletions
12
.../xquare/app/xquareinfra/domain/container/adapter/dto/request/SetContainerConfigRequest.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.domain.container.adapter.dto.request | ||
|
||
data class SetContainerConfigRequest( | ||
val stag: ContainerConfigDetails?, | ||
val prod: ContainerConfigDetails? | ||
) | ||
|
||
data class ContainerConfigDetails( | ||
val branch: String, | ||
val containerPort: Int | ||
) | ||
|
12 changes: 12 additions & 0 deletions
12
.../xquare/app/xquareinfra/domain/container/application/port/in/SetContainerConfigUseCase.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.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 | ||
|
||
interface SetContainerConfigUseCase { | ||
fun setContainerConfig( | ||
deployId: UUID, | ||
setContainerConfigRequest: SetContainerConfigRequest | ||
) | ||
} |
80 changes: 80 additions & 0 deletions
80
.../xquare/app/xquareinfra/domain/container/application/service/SetContainerConfigService.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,80 @@ | ||
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.adapter.dto.request.ContainerConfigDetails | ||
import xquare.app.xquareinfra.domain.container.adapter.dto.request.SetContainerConfigRequest | ||
import xquare.app.xquareinfra.domain.container.application.port.`in`.SetContainerConfigUseCase | ||
import xquare.app.xquareinfra.domain.container.application.port.out.FindContainerPort | ||
import xquare.app.xquareinfra.domain.container.application.port.out.SaveContainerPort | ||
import xquare.app.xquareinfra.domain.container.domain.Container | ||
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment | ||
import xquare.app.xquareinfra.domain.deploy.application.port.out.FindDeployPort | ||
import xquare.app.xquareinfra.domain.deploy.domain.Deploy | ||
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.time.LocalDateTime | ||
import java.util.* | ||
|
||
@Transactional | ||
@Service | ||
class SetContainerConfigService( | ||
private val findDeployPort: FindDeployPort, | ||
private val findContainerPort: FindContainerPort, | ||
private val githubClient: GithubClient, | ||
private val githubProperties: GithubProperties, | ||
private val saveContainerPort: SaveContainerPort | ||
|
||
) : SetContainerConfigUseCase{ | ||
override fun setContainerConfig(deployId: UUID, setContainerConfigRequest: SetContainerConfigRequest) { | ||
val deploy = findDeployPort.findById(deployId) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND | ||
|
||
setContainerConfigRequest.stag?.let { | ||
processContainerConfig(deploy, it, ContainerEnvironment.stag) | ||
} | ||
|
||
setContainerConfigRequest.prod?.let { | ||
processContainerConfig(deploy, it, ContainerEnvironment.prod) | ||
} | ||
} | ||
|
||
private fun processContainerConfig(deploy: Deploy, config: ContainerConfigDetails, environment: ContainerEnvironment) { | ||
var container = findContainerPort.findByDeployAndEnvironment(deploy, environment) | ||
var containerId: UUID? = null | ||
if (container != null) { | ||
containerId = container.id | ||
} | ||
|
||
container = saveContainerPort.save( | ||
Container( | ||
id = containerId, | ||
deploy = deploy, | ||
containerEnvironment = environment, | ||
lastDeploy = LocalDateTime.now(), | ||
subDomain = null, | ||
environmentVariable = container?.environmentVariable ?: mapOf(), | ||
githubBranch = config.branch, | ||
containerPort = config.containerPort | ||
) | ||
) | ||
|
||
githubClient.dispatchWorkflow( | ||
authorization = "Bearer ${githubProperties.token}", | ||
accept = "application/vnd.github.v3+json", | ||
request = DispatchEventRequest( | ||
event_type = "write-values", | ||
client_payload = mapOf( | ||
"club" to deploy.team.teamNameEn, | ||
"name" to deploy.deployName, | ||
"organization" to deploy.organization, | ||
"repository" to deploy.repository, | ||
"branch" to container.githubBranch!!, | ||
"environment" to container.containerEnvironment.name, | ||
"containerPort" to container.containerPort!! | ||
) | ||
) | ||
) | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/xquare/app/xquareinfra/infrastructure/feign/client/github/GithubClient.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,19 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.github | ||
|
||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestHeader | ||
import xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request.DispatchEventRequest | ||
|
||
|
||
@FeignClient(name = "github", url = "\${github.url}") | ||
interface GithubClient { | ||
@PostMapping("/repos/team-xquare/xquare-gitops-repo/dispatches") | ||
fun dispatchWorkflow( | ||
@RequestHeader("Authorization") authorization: String?, | ||
@RequestHeader("Accept") accept: String?, | ||
@RequestBody request: DispatchEventRequest? | ||
) | ||
} |
6 changes: 6 additions & 0 deletions
6
...re/app/xquareinfra/infrastructure/feign/client/github/dto/request/DispatchEventRequest.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,6 @@ | ||
package xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request | ||
|
||
data class DispatchEventRequest( | ||
val event_type: String, | ||
val client_payload: Map<String, Any> | ||
) |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/xquare/app/xquareinfra/infrastructure/global/env/github/GithubProperties.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,10 @@ | ||
package xquare.app.xquareinfra.infrastructure.global.env.github | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.ConstructorBinding | ||
|
||
@ConstructorBinding | ||
@ConfigurationProperties(value = "github") | ||
data class GithubProperties( | ||
val token: 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