From 5e372cde4abf2b1869bbe1529566ec86d575607b Mon Sep 17 00:00:00 2001 From: pbccc Date: Wed, 28 Aug 2024 14:13:44 +0900 Subject: [PATCH 1/2] file arrange --- .../kr/co/mcmp/api/response/ResponseCode.java | 72 ------- .../co/mcmp/api/response/ResponseWrapper.java | 42 ---- .../kr/co/mcmp/dto/oss/NexusFormatType.java | 20 -- .../co/mcmp/dto/oss/NexusRepositoryDto.java | 193 ------------------ .../mcmp/manifest/K8SDeployYamlGenerator.java | 78 ++++--- .../java/kr/co/mcmp/manifest/K8SPodDTO.java | 45 ++++ .../mcmp/manifest/YamlGenerateController.java | 6 + .../co/mcmp/manifest/YamlGenerateService.java | 24 +++ .../mcmp/service/oss/NexusAdapterFactory.java | 25 --- .../mcmp/service/oss/NexusAdapterService.java | 12 -- .../oss/NexusDockerAdapterService.java | 36 ---- .../service/oss/NexusHelmAdapterService.java | 37 ---- .../service/oss/NexusRawAdapterService.java | 34 --- .../oss/NexusRepositoryAdapterClient.java | 190 ----------------- .../service/oss/NexusRepositoryService.java | 38 ---- 15 files changed, 130 insertions(+), 722 deletions(-) delete mode 100644 src/main/java/kr/co/mcmp/api/response/ResponseCode.java delete mode 100644 src/main/java/kr/co/mcmp/api/response/ResponseWrapper.java delete mode 100644 src/main/java/kr/co/mcmp/dto/oss/NexusFormatType.java delete mode 100644 src/main/java/kr/co/mcmp/dto/oss/NexusRepositoryDto.java create mode 100644 src/main/java/kr/co/mcmp/manifest/K8SPodDTO.java delete mode 100644 src/main/java/kr/co/mcmp/service/oss/NexusAdapterFactory.java delete mode 100644 src/main/java/kr/co/mcmp/service/oss/NexusAdapterService.java delete mode 100644 src/main/java/kr/co/mcmp/service/oss/NexusDockerAdapterService.java delete mode 100644 src/main/java/kr/co/mcmp/service/oss/NexusHelmAdapterService.java delete mode 100644 src/main/java/kr/co/mcmp/service/oss/NexusRawAdapterService.java delete mode 100644 src/main/java/kr/co/mcmp/service/oss/NexusRepositoryAdapterClient.java delete mode 100644 src/main/java/kr/co/mcmp/service/oss/NexusRepositoryService.java diff --git a/src/main/java/kr/co/mcmp/api/response/ResponseCode.java b/src/main/java/kr/co/mcmp/api/response/ResponseCode.java deleted file mode 100644 index 2c7037a..0000000 --- a/src/main/java/kr/co/mcmp/api/response/ResponseCode.java +++ /dev/null @@ -1,72 +0,0 @@ -package kr.co.mcmp.api.response; - -import kr.co.mcmp.exception.McmpException; -import lombok.Getter; - -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -@Getter -public enum ResponseCode { - - OK(200, "정상처리 되었습니다."), - - /* common */ - BAD_REQUEST(400, "BAD REQUEST"), - UNAUTHORIZED(401, "UNAUTHORIZED"), - FORBIDDEN(403, "FORBIDDEN"), - NOT_FOUND(404, "NOT FOUND"), - METHOD_NOT_ALLOWED(405,"METHOD NOT ALLOWED"), - CLIENT_ERROR(408,"CLIENT_ERROR"), - CONFLICT(409,"CONFLICT"), - INTERNAL_SERVER_ERROR(500,"INTERNAL SERVER ERROR"), - - /* 공통 코드 */ - COMMON_CODE_EXISTS(9001, "EXISTS COMMON CODE"), - COMMON_CODE_DELETE_NOT_ALLOWED(9002,"DELETE NOT ALLOWED COMMON CODE"), - - /* Exception */ - UNKNOWN_ERROR(9999, "Unknown error"), - - /* GitLab-Project */ - ALREADY_EXISTS(1001, "ALREADY EXISTS"), - CREATE_FAILED_PROJECT_SOURCE(1003, "CREATE FAILED PROJECT SOURCE"), - - /* Jenkins */ - CREATE_FAILED_JENKINS_JOB(1103, "CREATE FAILED JENKINS JOB"), - EXISTS_JENKINS_JOB(1104, "EXISTS JENKINS JOB"), - NOT_EXISTS_JENKINS_JOB(1105, "NOT EXISTS JENKINS JOB"), - ERROR_JENKINS_API(1106, "ERROR JENKINS API"), - - /* Workload Deploy */ - RUN_FAILED_DEPLOY(1201, "RUN FAILED DEPLOY"), - - /* Catalog Deploy */ - CREATE_FAILED_APPLICATION(1301, "CREATE FAILED APPLICATION"), - NOT_EXISTS_APPLICATION(1302, "NOT EXISTS APPLICATION"), - EXISTS_APPLICATION(1303, "EXISTS APPLICATION"), - - /* K8S Client */ - CONNECTION_FAILED_CLUSTER(1401, "CONNECTION FAILED CLUSTER"), - - /* OSS */ - IN_USE_OSS(1501, "OSS IN USE CANNOT BE DELETED"), - IS_NOT_MAPPED_OSS(1502, "IS NOT MAPPED OSS TO SERVICE GROUP."); - - private final int code; - private final String message; - - ResponseCode(int code, String message) { - this.code = code; - this.message = message; - } - - private static final Map map = - Stream.of(values()).collect(Collectors.toMap(ResponseCode::getCode, e -> e)); - - public static ResponseCode findByCode(int code) { - return Optional.ofNullable(map.get(code)).orElseThrow(() -> new McmpException(String.valueOf(ResponseCode.UNKNOWN_ERROR))); - } -} diff --git a/src/main/java/kr/co/mcmp/api/response/ResponseWrapper.java b/src/main/java/kr/co/mcmp/api/response/ResponseWrapper.java deleted file mode 100644 index b81a2dc..0000000 --- a/src/main/java/kr/co/mcmp/api/response/ResponseWrapper.java +++ /dev/null @@ -1,42 +0,0 @@ -package kr.co.mcmp.api.response; - -import lombok.Getter; - -import java.io.Serializable; - -@Getter -public class ResponseWrapper implements Serializable { - - private static final long serialVersionUID = -1745006582949878939L; - - private int code; - private String message; - private String detail; - private T data; - - public ResponseWrapper() { - this(ResponseCode.OK); - } - - public ResponseWrapper(ResponseCode status){ - this.code = status.getCode(); - this.message = status.getMessage(); - } - - public ResponseWrapper(T data) { - this(); - this.data = data; - } - - public ResponseWrapper(ResponseCode status, String detail){ - this(status); - this.detail = detail; - } - - public ResponseWrapper(int code, String message, String detail){ - this.code = code; - this.message = message; - this.detail = detail; - } - -} diff --git a/src/main/java/kr/co/mcmp/dto/oss/NexusFormatType.java b/src/main/java/kr/co/mcmp/dto/oss/NexusFormatType.java deleted file mode 100644 index 0039776..0000000 --- a/src/main/java/kr/co/mcmp/dto/oss/NexusFormatType.java +++ /dev/null @@ -1,20 +0,0 @@ -package kr.co.mcmp.dto.oss; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class NexusFormatType { - - @Schema(title = "레포지토리 포맷 유형") - private String format; - - @Schema(title = "레포지토리 타입 유형") - private String type; -} diff --git a/src/main/java/kr/co/mcmp/dto/oss/NexusRepositoryDto.java b/src/main/java/kr/co/mcmp/dto/oss/NexusRepositoryDto.java deleted file mode 100644 index aa28b7a..0000000 --- a/src/main/java/kr/co/mcmp/dto/oss/NexusRepositoryDto.java +++ /dev/null @@ -1,193 +0,0 @@ -package kr.co.mcmp.dto.oss; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import javax.validation.Valid; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; - -@Getter -public class NexusRepositoryDto { - - @Getter - @Builder - @NoArgsConstructor - @AllArgsConstructor - public static class ResGetRepositoryDto { - @Schema(title = "레포지토리 이름", required = true) - @NotBlank - private String name; - - @Schema(title = "레포지토리 포맷 유형", required = true) - @NotBlank - private String format; - - @Schema(title = "레포지토리 타입 유형", required = true) - @NotBlank - private String type; - - @Schema(title = "레포지토리 접근 url", required = true) - @NotBlank - private String url; - - @Schema(title = "레포지토리 사용자 접근 가능 여부", required = true) - @NotNull - private Boolean online; - - @Valid - private ResGetStorageDto storage; - - @Valid - private ResGetDockerDto docker; - - @Getter - @Builder - @NoArgsConstructor - @AllArgsConstructor - public static class ResGetStorageDto { - @Schema(title = "아티팩트를 저장하는 물리적 저장소 이름", required = true, example = "default") - @NotBlank - private String blobStoreName; - - @Schema(title = "저장되는 아티팩트 유형 일치 여부 검증", required = true) - @NotNull - private Boolean strictContentTypeValidation; - - @Schema(title = "레포지토리 읽기/쓰기 설정", required = true, example = "allow, allow_once, read_only") - @NotBlank - private String writePolicy; - } - - @Getter - @Builder - @NoArgsConstructor - @AllArgsConstructor - public static class ResGetDockerDto { - @Schema(title = "도커 registry 버전 지원(false: v2 지원)", required = true) - @NotNull - private Boolean v1Enabled; - - @Schema(title = "도커 클라이언트가 레포지토리에 접근할 때 기본 인증 사용 여부", required = true) - @NotNull - private Boolean forceBasicAuth; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 http 포트") - private Integer httpPort; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 https 포트") - private Integer httpsPort; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 서브도메인") - private String subdomain; - } - } - - @Getter - public static class ReqCreateRepositoryDto { - @Schema(title = "레포지토리 이름", required = true) - @NotBlank - private String name; - - @Schema(title = "레포지토리 사용자 접근 가능 여부", required = true) - @NotNull - private Boolean online; - - @Valid - private ReqCreateStorageDto storage; - - @Valid - private ReqCreateDockerDto docker; - - @Getter - public static class ReqCreateStorageDto { - @Schema(title = "아티팩트를 저장하는 물리적 저장소 이름", required = true, example = "default") - @NotBlank - private String blobStoreName; - - @Schema(title = "저장되는 아티팩트 유형 일치 여부 검증", required = true) - @NotNull - private Boolean strictContentTypeValidation; - - @Schema(title = "레포지토리 읽기/쓰기 설정", required = true, example = "allow, allow_once, read_only") - @NotBlank - private String writePolicy; - } - - @Getter - public static class ReqCreateDockerDto { - @Schema(title = "도커 registry 버전 지원(false: v2 지원)", required = true) - @NotNull - private Boolean v1Enabled; - - @Schema(title = "도커 클라이언트가 레포지토리에 접근할 때 기본 인증 사용 여부", required = true) - @NotNull - private Boolean forceBasicAuth; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 http 포트") - private Integer httpPort; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 https 포트") - private Integer httpsPort; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 서브도메인") - private String subdomain; - } - } - - @Getter - public static class ReqUpdateRepositoryDto { - @Schema(title = "레포지토리 이름", required = true) - @JsonIgnore - private String name; - - @Schema(title = "레포지토리 사용자 접근 가능 여부", required = true) - @NotNull - private Boolean online; - - @Valid - private ReqUpdateStorageDto storage; - - @Valid - private ReqUpdateDockerDto docker; - - @Getter - public static class ReqUpdateStorageDto { - @Schema(title = "아티팩트를 저장하는 물리적 저장소 이름", required = true, example = "default") - @JsonIgnore - private String blobStoreName; - - @Schema(title = "저장되는 아티팩트 유형 일치 여부 검증", required = true) - @NotNull - private Boolean strictContentTypeValidation; - - @Schema(title = "레포지토리 읽기/쓰기 설정", required = true, example = "allow, allow_once, read_only") - @NotBlank - private String writePolicy; - } - - @Getter - public static class ReqUpdateDockerDto { - @Schema(title = "도커 registry 버전 지원(false: v2 지원)", required = true) - @NotNull - private Boolean v1Enabled; - - @Schema(title = "도커 클라이언트가 레포지토리에 접근할 때 기본 인증 사용 여부", required = true) - @NotNull - private Boolean forceBasicAuth; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 http 포트") - private Integer httpPort; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 https 포트") - private Integer httpsPort; - - @Schema(title = "도커 레포지토리에 접근할 때 사용할 서브도메인") - private String subdomain; - } - } -} diff --git a/src/main/java/kr/co/mcmp/manifest/K8SDeployYamlGenerator.java b/src/main/java/kr/co/mcmp/manifest/K8SDeployYamlGenerator.java index b455c30..8713142 100644 --- a/src/main/java/kr/co/mcmp/manifest/K8SDeployYamlGenerator.java +++ b/src/main/java/kr/co/mcmp/manifest/K8SDeployYamlGenerator.java @@ -20,6 +20,58 @@ @Component public class K8SDeployYamlGenerator { + public String getPod(K8SPodDTO podDto) { + + V1Pod pod = new V1Pod(); + pod.setApiVersion(K8S.Controller.Pod.getApiVersion()); + pod.setKind("Pod"); + + V1ObjectMeta metadata = new V1ObjectMeta(); + metadata.setName(podDto.getPodName()); + metadata.setNamespace(podDto.getNamespace()); + metadata.setLabels(podDto.getLabels()); + pod.setMetadata(metadata); + + V1PodSpec podSpec = new V1PodSpec(); + + List dtoContainers = podDto.getContainers(); + + // for in for 바꿔야하나 실제 시간복잡도가 높지 않아 걍 씀 + List containers = new ArrayList<>(); + for(K8SPodDTO.Container cont: dtoContainers){ + V1Container podContainer = new V1Container(); + podContainer.setImage(cont.getImage()); + podContainer.setImage(cont.getName()); + List podPorts = new ArrayList<>(); + for(K8SPodDTO.Port port: cont.getPorts()) { + V1ContainerPort contPort = new V1ContainerPort(); + contPort.setContainerPort(port.getContainerPort()); + contPort.setHostPort(port.getHostPort()); + contPort.setName(port.getName()); + contPort.setProtocol(port.getProtocol()); + podContainer.addPortsItem(contPort); + } + containers.add(podContainer); + } + + podSpec.setContainers(containers); + podSpec.setRestartPolicy(podDto.getRestartPolicy()); + + pod.setSpec(podSpec); + + StringBuffer buffer = new StringBuffer(); + appendYaml(buffer, pod); + String yaml = buffer.toString(); + + return yaml; + } + + + + + + + public String generateDeployYaml(K8SDeployDTO deploy) { StringBuffer buffer = new StringBuffer(); @@ -51,9 +103,9 @@ public String generateDeployYaml(K8SDeployDTO deploy) { case CronJob: appendYaml(buffer, getCronJob(deploy)); break; - case Pod: - appendYaml(buffer, getPod(deploy)); - break; +// case Pod: +// appendYaml(buffer, getPod(deploy)); +// break; } // service @@ -158,26 +210,6 @@ private V1Deployment getDeployment(K8SDeployDTO deploy) { } - private V1Pod getPod(K8SDeployDTO deploy) { - - V1Pod pod = new V1Pod(); - pod.setApiVersion(K8S.Controller.Pod.getApiVersion()); - pod.setKind(deploy.getController()); - - // metadata - V1ObjectMeta metadata = getControllerMetadata(deploy.getController(), deploy.getName(), deploy.getNamespace(), deploy.getLabels()); - pod.setMetadata(metadata); - - // spec - V1PodSpec podSpec = new V1PodSpec(); - V1Container container = new V1Container(); - container.setName(deploy.getName().replace("KIND", "pod")); - container.setImage(deploy.getImage()); - //pod.setSpec(podSpec); - podSpec.addContainersItem(container); - - return pod; - } diff --git a/src/main/java/kr/co/mcmp/manifest/K8SPodDTO.java b/src/main/java/kr/co/mcmp/manifest/K8SPodDTO.java new file mode 100644 index 0000000..b139227 --- /dev/null +++ b/src/main/java/kr/co/mcmp/manifest/K8SPodDTO.java @@ -0,0 +1,45 @@ +package kr.co.mcmp.manifest; + +import io.kubernetes.client.custom.Quantity; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; +import java.util.Map; + +@Getter +@Setter +public class K8SPodDTO { + + public String podName; + public String namespace; + public List containers; + public Map labels; + public String restartPolicy; + + @Getter + @Setter + public class Container{ + public String image; + public String name; + public List ports; + public Map resource; + /*limits: + cpu: 500m + memory: 1Gi + requests: + cpu: 200m + memory: 256Mi*/ + } + + @Getter + @Setter + public class Port{ + public Integer containerPort; + public Integer hostPort; + public String name; + public String protocol; + } + +} diff --git a/src/main/java/kr/co/mcmp/manifest/YamlGenerateController.java b/src/main/java/kr/co/mcmp/manifest/YamlGenerateController.java index 01cf456..3e2d972 100644 --- a/src/main/java/kr/co/mcmp/manifest/YamlGenerateController.java +++ b/src/main/java/kr/co/mcmp/manifest/YamlGenerateController.java @@ -44,5 +44,11 @@ public String generateConfigmapYaml(K8SDeployDTO k8sDto){ return null; } + @Operation(summary = "yaml generate for HPA") + @PostMapping("/hpa") + public String generateHPAYaml(K8SDeployDTO k8sDto){ + return null; + } + } diff --git a/src/main/java/kr/co/mcmp/manifest/YamlGenerateService.java b/src/main/java/kr/co/mcmp/manifest/YamlGenerateService.java index 31197c7..26f935e 100644 --- a/src/main/java/kr/co/mcmp/manifest/YamlGenerateService.java +++ b/src/main/java/kr/co/mcmp/manifest/YamlGenerateService.java @@ -1,7 +1,9 @@ package kr.co.mcmp.manifest; +import io.kubernetes.client.openapi.models.V1Pod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @@ -9,7 +11,29 @@ public class YamlGenerateService { Logger logger = LoggerFactory.getLogger(YamlGenerateService.class); + @Autowired + K8SDeployYamlGenerator yamlGen; + public String generatePodYaml(K8SPodDTO podContents){ + return yamlGen.getPod(podContents); + } + + public String generateDeploymentYaml(){ + return ""; + } + + public String generateConfigmapYaml(){ + return ""; + } + + public String generateHPAYaml(){ + return ""; + } + + public String generateServiceYaml(){ + return ""; + } + } diff --git a/src/main/java/kr/co/mcmp/service/oss/NexusAdapterFactory.java b/src/main/java/kr/co/mcmp/service/oss/NexusAdapterFactory.java deleted file mode 100644 index df8250f..0000000 --- a/src/main/java/kr/co/mcmp/service/oss/NexusAdapterFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -package kr.co.mcmp.service.oss; - -import kr.co.mcmp.dto.oss.NexusFormatType; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -@Service -@RequiredArgsConstructor -public class NexusAdapterFactory { - - private final NexusRawAdapterService rawAdapterService; - private final NexusDockerAdapterService dockerAdapterService; - private final NexusHelmAdapterService helmAdapterService; - - public NexusAdapterService getAdapterService(NexusFormatType formatType) { - if ("raw".equals(formatType.getFormat())) { - return rawAdapterService; - } else if ("docker".equals(formatType.getFormat())) { - return dockerAdapterService; - } else if ("helm".equals(formatType.getFormat())) { - return helmAdapterService; - } - throw new IllegalArgumentException("unknown format: " + formatType.getFormat()); - } -} diff --git a/src/main/java/kr/co/mcmp/service/oss/NexusAdapterService.java b/src/main/java/kr/co/mcmp/service/oss/NexusAdapterService.java deleted file mode 100644 index 4ee4dee..0000000 --- a/src/main/java/kr/co/mcmp/service/oss/NexusAdapterService.java +++ /dev/null @@ -1,12 +0,0 @@ -package kr.co.mcmp.service.oss; - -import kr.co.mcmp.dto.oss.NexusFormatType; -import kr.co.mcmp.dto.oss.NexusRepositoryDto; - -public interface NexusAdapterService { - - NexusRepositoryDto.ResGetRepositoryDto getRepositoryByName(NexusFormatType formatType, String name); - - void createRepository(NexusFormatType formatType, NexusRepositoryDto.ReqCreateRepositoryDto repositoryDto); - -} diff --git a/src/main/java/kr/co/mcmp/service/oss/NexusDockerAdapterService.java b/src/main/java/kr/co/mcmp/service/oss/NexusDockerAdapterService.java deleted file mode 100644 index ccbcb10..0000000 --- a/src/main/java/kr/co/mcmp/service/oss/NexusDockerAdapterService.java +++ /dev/null @@ -1,36 +0,0 @@ -package kr.co.mcmp.service.oss; - -import kr.co.mcmp.dto.oss.NexusFormatType; -import kr.co.mcmp.dto.oss.NexusRepositoryDto; -import lombok.RequiredArgsConstructor; -import lombok.extern.log4j.Log4j2; -import org.springframework.stereotype.Service; - -import java.util.Collections; -import java.util.Optional; - -@Log4j2 -@Service -@RequiredArgsConstructor -public class NexusDockerAdapterService implements NexusAdapterService { - - private final NexusRepositoryAdapterClient repositoryAdapterClient; - - @Override - public NexusRepositoryDto.ResGetRepositoryDto getRepositoryByName(NexusFormatType formatType, String name) { - return Optional.ofNullable(repositoryAdapterClient.getRepositoryByName(formatType, name)) - .orElseThrow(() -> new IllegalArgumentException("레포지토리 " + name + "을(를) 찾을 수 없습니다.")); - } - - @Override - public void createRepository(NexusFormatType formatType, NexusRepositoryDto.ReqCreateRepositoryDto repositoryDto) { - boolean duplicateNameCheck = Optional.ofNullable(repositoryAdapterClient.getRepositoryList()) - .orElse(Collections.emptyList()).stream() - .noneMatch(r -> r.getName().equals(repositoryDto.getName())); - - if (duplicateNameCheck) { - repositoryAdapterClient.createRepository(formatType, repositoryDto); - } - throw new IllegalArgumentException("중복된 이름의 레포지토리가 존재합니다."); - } -} diff --git a/src/main/java/kr/co/mcmp/service/oss/NexusHelmAdapterService.java b/src/main/java/kr/co/mcmp/service/oss/NexusHelmAdapterService.java deleted file mode 100644 index 396dc12..0000000 --- a/src/main/java/kr/co/mcmp/service/oss/NexusHelmAdapterService.java +++ /dev/null @@ -1,37 +0,0 @@ -package kr.co.mcmp.service.oss; - -import kr.co.mcmp.dto.oss.NexusFormatType; -import kr.co.mcmp.dto.oss.NexusRepositoryDto; -import kr.co.mcmp.exception.AlreadyExistsException; -import lombok.RequiredArgsConstructor; -import lombok.extern.log4j.Log4j2; -import org.springframework.stereotype.Service; - -import java.util.Collections; -import java.util.Optional; - -@Log4j2 -@Service -@RequiredArgsConstructor -public class NexusHelmAdapterService implements NexusAdapterService { - - private final NexusRepositoryAdapterClient repositoryAdapterClient; - - @Override - public NexusRepositoryDto.ResGetRepositoryDto getRepositoryByName(NexusFormatType formatType, String name) { - return Optional.ofNullable(repositoryAdapterClient.getRepositoryByName(formatType, name)) - .orElseThrow(() -> new IllegalArgumentException("레포지토리 " + name + "을(를) 찾을 수 없습니다.")); - } - - @Override - public void createRepository(NexusFormatType formatType, NexusRepositoryDto.ReqCreateRepositoryDto repositoryDto) { - boolean duplicateNameCheck = Optional.ofNullable(repositoryAdapterClient.getRepositoryList()) - .orElse(Collections.emptyList()).stream() - .noneMatch(r -> r.getName().equals(repositoryDto.getName())); - - if (duplicateNameCheck) { - repositoryAdapterClient.createRepository(formatType, repositoryDto); - } - throw new IllegalArgumentException("중복된 이름의 레포지토리가 존재합니다."); - } -} diff --git a/src/main/java/kr/co/mcmp/service/oss/NexusRawAdapterService.java b/src/main/java/kr/co/mcmp/service/oss/NexusRawAdapterService.java deleted file mode 100644 index 5b283bd..0000000 --- a/src/main/java/kr/co/mcmp/service/oss/NexusRawAdapterService.java +++ /dev/null @@ -1,34 +0,0 @@ -package kr.co.mcmp.service.oss; - -import kr.co.mcmp.dto.oss.NexusFormatType; -import kr.co.mcmp.dto.oss.NexusRepositoryDto; -import kr.co.mcmp.exception.AlreadyExistsException; -import lombok.RequiredArgsConstructor; -import lombok.extern.log4j.Log4j2; -import org.springframework.stereotype.Service; - -import java.util.Collections; -import java.util.Optional; - -@Log4j2 -@Service -@RequiredArgsConstructor -public class NexusRawAdapterService implements NexusAdapterService { - - private final NexusRepositoryAdapterClient repositoryAdapterClient; - - @Override - public NexusRepositoryDto.ResGetRepositoryDto getRepositoryByName(NexusFormatType formatType, String name) { - return repositoryAdapterClient.getRepositoryByName(formatType, name); - } - - @Override - public void createRepository(NexusFormatType formatType, NexusRepositoryDto.ReqCreateRepositoryDto repositoryDto) { - NexusRepositoryDto.ResGetRepositoryDto repositoryOne = repositoryAdapterClient.getRepositoryOne(repositoryDto.getName()); - - if (repositoryOne != null) { - repositoryAdapterClient.createRepository(formatType, repositoryDto); - } - } - -} diff --git a/src/main/java/kr/co/mcmp/service/oss/NexusRepositoryAdapterClient.java b/src/main/java/kr/co/mcmp/service/oss/NexusRepositoryAdapterClient.java deleted file mode 100644 index ec295c0..0000000 --- a/src/main/java/kr/co/mcmp/service/oss/NexusRepositoryAdapterClient.java +++ /dev/null @@ -1,190 +0,0 @@ -package kr.co.mcmp.service.oss; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import kr.co.mcmp.config.oss.RestTemplateProvider; -import kr.co.mcmp.dto.oss.NexusFormatType; -import kr.co.mcmp.dto.oss.NexusRepositoryDto; -import kr.co.mcmp.exception.NexusClientException; -import kr.co.mcmp.util.Base64Util; -import lombok.extern.log4j.Log4j2; -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.*; -import org.springframework.stereotype.Service; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.RestClientException; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Collections; -import java.util.List; - -@Log4j2 -@Service -public class NexusRepositoryAdapterClient { - - private static final String NEXUS_ID = "admin"; - private static final String NEXUS_PWD = "tjxjfkxh!23"; - - private static final String BASE_URL = "http://210.217.178.130:8081/service/rest"; - private static final String AUTHORIZATION = "Authorization"; - - private static final String GET_REPO_LIST = "/v1/repositories"; - private static final String GET_REPO_ONE = "/v1/repositories/{name}"; - private static final String GET_REPO_BY_NAME = "/v1/repositories/{format}/{type}/{name}"; - private static final String POST_CREATE_REPO = "/v1/repositories/{format}/{type}"; - - public List getRepositoryList() { - String url = UriComponentsBuilder.fromHttpUrl(BASE_URL) - .path(GET_REPO_LIST) - .toUriString(); - - HttpEntity request = getRequest(null); - RestTemplate template = RestTemplateProvider.get(); - - try { - ResponseEntity> response = template.exchange( - url, - HttpMethod.GET, - request, - new ParameterizedTypeReference>() {}); - - if (response.getStatusCode() == HttpStatus.OK) { - return response.getBody(); - } else { - throw new IllegalArgumentException("Unexpected Response Status: " + response.getStatusCode() + " from URL: " + url); - } - - } catch (HttpClientErrorException e) { - String errorMessage = e.getResponseBodyAsString(); - String parseMessage = parseErrorMessage(errorMessage); - throw new IllegalArgumentException(parseMessage, e); - } - } - - public NexusRepositoryDto.ResGetRepositoryDto getRepositoryOne(String name) { - String url = UriComponentsBuilder.fromHttpUrl(BASE_URL) - .path(GET_REPO_ONE) - .buildAndExpand(name) - .toUriString(); - - HttpEntity request = getRequest(null); - RestTemplate template = RestTemplateProvider.get(); - - try { - ResponseEntity response = template.exchange( - url, - HttpMethod.GET, - request, - new ParameterizedTypeReference() {}); - - if (response.getStatusCode() == HttpStatus.OK) { - return response.getBody(); - } else { - throw new NexusClientException("Unexpected Response Status: " + response.getStatusCode() + " from URL: " + url); - } - - } catch (HttpClientErrorException e) { - String errorMessage = e.getResponseBodyAsString(); - String parseMessage = parseErrorMessage(errorMessage); - throw new NexusClientException(parseMessage); - } - } - - public NexusRepositoryDto.ResGetRepositoryDto getRepositoryByName(NexusFormatType formatType, String name) { - String url = UriComponentsBuilder.fromHttpUrl(BASE_URL) - .path(GET_REPO_BY_NAME) - .buildAndExpand(formatType.getFormat(), formatType.getType(), name) - .toUriString(); - - HttpEntity request = getRequest(null); - RestTemplate template = RestTemplateProvider.get(); - - try { - ResponseEntity response = template.exchange( - url, - HttpMethod.GET, - request, - new ParameterizedTypeReference() {}); - - if (response.getStatusCode() == HttpStatus.OK) { - return response.getBody(); - } else { - throw new NexusClientException("Unexpected Response Status: " + response.getStatusCode() + " from URL: " + url); - } - - } catch (HttpClientErrorException e) { - String errorMessage = e.getResponseBodyAsString(); - String parseMessage = parseErrorMessage(errorMessage); - throw new NexusClientException(parseMessage); - } - } - - public Object createRepository(NexusFormatType formatType, NexusRepositoryDto.ReqCreateRepositoryDto repositoryDto) { - String url = UriComponentsBuilder.fromHttpUrl(BASE_URL) - .path(POST_CREATE_REPO) - .buildAndExpand(formatType.getFormat(), formatType.getType()) - .toUriString(); - - HttpEntity request = getRequest(repositoryDto); - RestTemplate template = RestTemplateProvider.get(); - - try { - ResponseEntity response = template.exchange( - url, - HttpMethod.POST, - request, - new ParameterizedTypeReference() {}); - - if (response.getStatusCode() == HttpStatus.OK) { - return response.getBody(); - } else { - throw new IllegalArgumentException("Unexpected Response Status: " + response.getStatusCode() + " from URL: " + url); - } - - } catch (HttpClientErrorException e) { - String errorMessage = e.getResponseBodyAsString(); - String parseMessage = parseErrorMessage(errorMessage); - throw new IllegalArgumentException(parseMessage, e); - } - } - - private static HttpEntity getRequest(T body) { - String basicToken = createToken(); - HttpHeaders headers = getHeaders(basicToken); - if (body == null) { - return new HttpEntity(headers); - } - return new HttpEntity(body, headers); - } - - private static HttpHeaders getHeaders(String basicToken) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - headers.set(AUTHORIZATION, basicToken); - return headers; - } - - private static String createToken() { - String auth = NEXUS_ID + ":" + NEXUS_PWD; - String encodedAuth = Base64Util.base64Encoding(auth); - return "Basic " + encodedAuth; - } - - private String parseErrorMessage(String errorMessage) { - ObjectMapper mapper = new ObjectMapper(); - try { - String jsonPart = errorMessage.substring(errorMessage.indexOf("{")); - - JsonNode rootNode = mapper.readTree(jsonPart); - JsonNode messageNode = rootNode.path("message"); - - String message = messageNode.asText(); - return message.replace("\\\"", "\"").replace("\"", ""); - - } catch (Exception e) { - return "Message Parsing Error"; - } - } -} diff --git a/src/main/java/kr/co/mcmp/service/oss/NexusRepositoryService.java b/src/main/java/kr/co/mcmp/service/oss/NexusRepositoryService.java deleted file mode 100644 index 6b1413f..0000000 --- a/src/main/java/kr/co/mcmp/service/oss/NexusRepositoryService.java +++ /dev/null @@ -1,38 +0,0 @@ -package kr.co.mcmp.service.oss; - -import kr.co.mcmp.dto.oss.NexusFormatType; -import kr.co.mcmp.dto.oss.NexusRepositoryDto; -import lombok.RequiredArgsConstructor; -import lombok.extern.log4j.Log4j2; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Log4j2 -@Service -@RequiredArgsConstructor -public class NexusRepositoryService { - - private final NexusRepositoryAdapterClient repositoryAdapterClient; - private final NexusAdapterFactory adapterFactory; - - public List getRepositoryList() { - return repositoryAdapterClient.getRepositoryList(); - } - - public NexusRepositoryDto.ResGetRepositoryDto getRepositoryOne(String name) { - return repositoryAdapterClient.getRepositoryOne(name); - } - - public NexusRepositoryDto.ResGetRepositoryDto getRepositoryByName(NexusFormatType formatType, String name) { - NexusAdapterService adapterService = adapterFactory.getAdapterService(formatType); - return adapterService.getRepositoryByName(formatType, name); - } - - public void createRepository(NexusFormatType formatType, NexusRepositoryDto.ReqCreateRepositoryDto repositoryDto) { - NexusAdapterService adapterService = adapterFactory.getAdapterService(formatType); - adapterService.createRepository(formatType, repositoryDto); - } - - //public updateRepository(String name, NexusRepositoryDto.ReqUpdateRepositoryDto repositoryDto) -} From 726e1ca729f6b2104a09a3eb12c2a231e8ddb1e0 Mon Sep 17 00:00:00 2001 From: pbccc Date: Wed, 28 Aug 2024 14:15:21 +0900 Subject: [PATCH 2/2] dockercompose edit --- docker-compose.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 2d9308e..bf80746 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -58,8 +58,8 @@ networks: # application-manager mc-application-manager: - image: pbccc/devops:0.2.5 - container_name: cb-mapui + image: m-cmp/mc-application-manager + container_name: application-manager build: context: ./ dockerfile: Dockerfile