-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTerraformController.java
More file actions
152 lines (140 loc) · 6.59 KB
/
TerraformController.java
File metadata and controls
152 lines (140 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.blockcloud.controller;
import com.blockcloud.dto.RequestDto.TerraformApplyRequestDto;
import com.blockcloud.dto.RequestDto.TerraformPlanRequestDto;
import com.blockcloud.dto.RequestDto.TerraformValidateRequestDto;
import com.blockcloud.dto.ResponseDto.DeploymentListResponseDto;
import com.blockcloud.dto.ResponseDto.DeploymentStatusResponseDto;
import com.blockcloud.dto.ResponseDto.TerraformApplyResponseDto;
import com.blockcloud.dto.ResponseDto.TerraformDestroyResponseDto;
import com.blockcloud.dto.ResponseDto.TerraformPlanResponseDto;
import com.blockcloud.dto.ResponseDto.TerraformValidateResponseDto;
import com.blockcloud.dto.common.ResponseDto;
import com.blockcloud.dto.oauth.CustomUserDetails;
import com.blockcloud.service.TerraformService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
/**
* Terraform 관련 API 요청을 처리하는 컨트롤러입니다. Terraform 코드 검증, 배포, 배포 상태 조회 기능을 제공합니다.
*/
@Tag(name = "Terraform API", description = "Terraform 코드 검증, 배포, 배포 상태 조회 관련 API")
@RestController
@RequestMapping("/api/projects/{projectId}/terraform")
@RequiredArgsConstructor
public class TerraformController {
private final TerraformService terraformService;
/**
* Terraform 코드를 검증합니다.
*
* @param requestDto Terraform 코드 검증 요청
* @return 검증 결과가 담긴 응답 객체
*/
@Operation(
summary = "Terraform 코드 검증",
description = "Terraform 코드의 문법과 구성을 검증합니다. 유효성 검사 결과와 에러/경고 메시지를 반환합니다."
)
@PostMapping("/validate")
public ResponseDto<TerraformValidateResponseDto> validateTerraform(
@Valid @RequestBody TerraformValidateRequestDto requestDto) {
return ResponseDto.ok(terraformService.validateTerraform(requestDto));
}
/**
* Terraform 코드의 변경 사항을 미리 확인합니다.
*
* @param projectId 프로젝트 ID
* @param requestDto Terraform plan 요청
* @return plan 결과가 담긴 응답 객체
*/
@Operation(
summary = "Terraform 코드 Plan",
description = "Terraform 코드를 실행했을 때 어떤 변경 사항이 발생할지 미리 확인합니다. 실제 배포는 하지 않습니다."
)
@PostMapping("/plan")
public ResponseDto<TerraformPlanResponseDto> planTerraform(
@Parameter(description = "프로젝트 ID", required = true) @PathVariable Long projectId,
@Valid @RequestBody TerraformPlanRequestDto requestDto) {
return ResponseDto.ok(terraformService.planTerraform(projectId, requestDto));
}
/**
* Terraform 코드를 적용하여 배포를 시작합니다.
*
* @param projectId 프로젝트 ID
* @param requestDto Terraform 배포 요청
* @param authentication 인증 정보
* @return 배포 시작 정보가 담긴 응답 객체
*/
@Operation(
summary = "Terraform 코드 배포",
description = "Terraform 코드를 실제 클라우드 환경에 적용하여 인프라를 배포합니다. 배포는 비동기로 실행되며, 배포 ID를 반환합니다."
)
@PostMapping("/apply")
public ResponseDto<TerraformApplyResponseDto> applyTerraform(
@Parameter(description = "프로젝트 ID", required = true) @PathVariable Long projectId,
@Valid @RequestBody TerraformApplyRequestDto requestDto,
Authentication authentication) {
CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
return ResponseDto.ok(terraformService.applyTerraform(projectId, requestDto, userDetails.getUsername()));
}
/**
* 특정 배포의 인프라를 삭제합니다.
*
* @param projectId 프로젝트 ID
* @param deploymentId 배포 ID
* @param authentication 인증 정보
* @return 삭제 결과가 담긴 응답 객체
*/
@Operation(
summary = "특정 배포 인프라 삭제",
description = "특정 배포의 인프라를 삭제합니다. 원본 배포의 Terraform 코드를 사용하여 삭제를 수행합니다."
)
@DeleteMapping("/deployments/{deploymentId}/destroy")
public ResponseDto<TerraformDestroyResponseDto> destroyTerraformByDeployment(
@Parameter(description = "프로젝트 ID", required = true) @PathVariable Long projectId,
@Parameter(description = "배포 ID", required = true) @PathVariable Long deploymentId,
Authentication authentication) {
CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
return ResponseDto.ok(terraformService.destroyTerraformByDeployment(projectId, deploymentId, userDetails.getUsername()));
}
/**
* 특정 배포의 상태를 조회합니다.
*
* @param projectId 프로젝트 ID
* @param deploymentId 배포 ID
* @return 배포 상태 정보가 담긴 응답 객체
*/
@Operation(
summary = "배포 상태 조회",
description = "특정 배포의 현재 상태를 조회합니다. PENDING, RUNNING, SUCCESS, FAILED 상태와 상세 정보를 반환합니다."
)
@GetMapping("/deployments/{deploymentId}")
public ResponseDto<DeploymentStatusResponseDto> getDeploymentStatus(
@Parameter(description = "프로젝트 ID", required = true) @PathVariable Long projectId,
@Parameter(description = "배포 ID", required = true) @PathVariable Long deploymentId) {
return ResponseDto.ok(terraformService.getDeploymentStatus(projectId, deploymentId));
}
/**
* 프로젝트의 배포 이력을 조회합니다.
*
* @param projectId 프로젝트 ID
* @param lastId 마지막으로 조회된 배포 ID (첫 조회 시 null)
* @param size 한 번에 조회할 배포 수 (기본값: 10)
* @return 배포 이력 목록이 담긴 응답 객체
*/
@Operation(
summary = "배포 이력 조회",
description = "프로젝트의 배포 이력을 무한스크롤 방식으로 조회합니다. `lastId`를 넘기면 해당 ID보다 작은 배포부터 조회하며, `size`로 가져올 개수를 지정할 수 있습니다."
)
@GetMapping("/deployments")
public ResponseDto<DeploymentListResponseDto> getDeploymentHistory(
@Parameter(description = "프로젝트 ID", required = true) @PathVariable Long projectId,
@Parameter(description = "마지막으로 조회한 배포 ID (첫 호출 시 생략 가능)")
@RequestParam(required = false) Long lastId,
@Parameter(description = "가져올 데이터 개수 (기본값 10)")
@RequestParam(defaultValue = "10") int size) {
return ResponseDto.ok(terraformService.getDeploymentHistory(projectId, lastId, size));
}
}