Skip to content

Commit

Permalink
bug :: 환경변수 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunSu1768 committed Feb 14, 2024
1 parent 56f6969 commit 15be9e8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package com.example.mergebackend.domain.deploy.entity

import com.example.mergebackend.domain.deploy.entity.type.EnvType
import com.example.mergebackend.domain.deploy.entity.type.ServiceType
import com.example.mergebackend.domain.project.entity.Project
import javax.persistence.*

@Entity(name = "environment_variable")
class EnvironmentVariable(
id: Long? = null,
project: Project,
deploy: Deploy,
envType: EnvType,
serviceType: ServiceType,
variableList: Map<String, String>
) {
@Id
Expand All @@ -20,19 +18,14 @@ class EnvironmentVariable(
protected set

@ManyToOne(fetch = FetchType.LAZY)
var project: Project = project
var deploy: Deploy = deploy
protected set

@Enumerated(EnumType.STRING)
@Column(name = "env_type", nullable = false)
var envType: EnvType = envType
protected set

@Enumerated(EnumType.STRING)
@Column(name = "service_type", nullable = false)
var serviceType: ServiceType = serviceType
protected set

@ElementCollection
@MapKeyColumn(name="variable_key", length=100)
@Column(name = "variable_value", columnDefinition = "TEXT")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.mergebackend.domain.deploy.exception

import com.example.mergebackend.global.config.error.data.ErrorCode
import com.example.mergebackend.global.config.error.exception.BusinessException

object DeployNotFoundException : BusinessException(ErrorCode.DEPLOY_NOT_FOUND_EXCEPTION)
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ data class CreateEnvironmentVariableRequest(
@field:NotNull(message = "variableList는 null이 될 수 없습니다.")
val variableList: Map<String, String>?,

val projectId: UUID
val deployId: UUID
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.example.mergebackend.domain.deploy.repository

import com.example.mergebackend.domain.deploy.entity.Deploy
import com.example.mergebackend.domain.deploy.entity.EnvironmentVariable
import com.example.mergebackend.domain.project.entity.Project
import org.springframework.data.jpa.repository.JpaRepository

interface EnvironmentVariableRepository : JpaRepository<EnvironmentVariable, Long> {
fun existsByProject(project: Project): Boolean
fun existsByDeploy(deploy: Deploy): Boolean

fun findByProject(project: Project): EnvironmentVariable?
fun findByDeploy(deploy: Deploy): EnvironmentVariable?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package com.example.mergebackend.domain.deploy.service

import com.example.mergebackend.domain.deploy.entity.EnvironmentVariable
import com.example.mergebackend.domain.deploy.exception.AlreadyExistsEnvironmentVariable
import com.example.mergebackend.domain.deploy.exception.DeployNotFoundException
import com.example.mergebackend.domain.deploy.exception.EnvironmentVariableNotFound
import com.example.mergebackend.domain.deploy.presentation.dto.request.CreateEnvironmentVariableRequest
import com.example.mergebackend.domain.deploy.presentation.dto.request.UpdateEnvironmentVariableRequest
import com.example.mergebackend.domain.deploy.repository.DeployRepository
import com.example.mergebackend.domain.deploy.repository.EnvironmentVariableRepository
import com.example.mergebackend.domain.project.exception.ProjectNotFoundException
import com.example.mergebackend.domain.project.repository.ProjectRepository
import com.example.mergebackend.infra.kubernetes.KubernetesClientUtil
import com.example.mergebackend.infra.vault.VaultUtil
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -18,37 +19,36 @@ import org.springframework.transaction.annotation.Transactional
@Service
private class EnvironmentVariableServiceImpl(
private val environmentVariableRepository: EnvironmentVariableRepository,
private val projectRepository: ProjectRepository,
private val deployRepository: DeployRepository,
private val vaultUtil: VaultUtil,
private val kubernetesClientUtil: KubernetesClientUtil
): EnvironmentVariableService{
override fun create(req: CreateEnvironmentVariableRequest) {
val project = projectRepository.findByIdOrNull(req.projectId) ?: throw ProjectNotFoundException
val deploy = deployRepository.findByIdOrNull(req.deployId) ?: throw DeployNotFoundException

if(environmentVariableRepository.existsByProject(project)) {
if(environmentVariableRepository.existsByDeploy(deploy)) {
throw AlreadyExistsEnvironmentVariable
}

val environmentVariable = environmentVariableRepository.save(
req.run {
EnvironmentVariable(
project = project,
deploy = deploy,
envType = envType!!,
serviceType = serviceType!!,
variableList = variableList!!
)
}
)
val path = req.run { "${project.projectNameEn.lowercase()}-${serviceType.toString().lowercase()}-${envType.toString().lowercase()}" }
val path = req.run { "${deploy.project.projectNameEn.lowercase()}-${serviceType.toString().lowercase()}-${envType.toString().lowercase()}" }
vaultUtil.addSecret(req.variableList!!, path)

val namespace = "${environmentVariable.project.teamNameEn.lowercase()}-${environmentVariable.envType.toString().lowercase()}"
val namespace = "${environmentVariable.deploy.project.teamNameEn.lowercase()}-${environmentVariable.envType.toString().lowercase()}"
kubernetesClientUtil.deleteSecret(namespace, path)
}

override fun update(req: UpdateEnvironmentVariableRequest) {
val project = projectRepository.findByIdOrNull(req.projectId) ?: throw ProjectNotFoundException
val environmentVariable = environmentVariableRepository.findByProject(project) ?: throw EnvironmentVariableNotFound
val deploy = deployRepository.findByIdOrNull(req.projectId) ?: throw DeployNotFoundException
val environmentVariable = environmentVariableRepository.findByDeploy(deploy) ?: throw EnvironmentVariableNotFound
environmentVariable.updateVariableList(req.variableList!!)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum class ErrorCode(
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다."),
PROJECT_NOT_FOUND(HttpStatus.NOT_FOUND, "프로젝트를 찾을 수 없습니다."),
ENVIRONMENT_VARIABLE_NOT_FOUND(HttpStatus.NOT_FOUND, "환경변수를 찾을 수 없습니다."),
DEPLOY_NOT_FOUND_EXCEPTION(HttpStatus.NOT_FOUND, "배포를 찾을 수 없습니다."),

// 409
ALREADY_SIGN_UP(HttpStatus.CONFLICT, "이미 가입한 계정입니다."),
Expand Down

0 comments on commit 15be9e8

Please sign in to comment.