Skip to content

Commit

Permalink
Merge pull request Team-Goraebab#65 from whitem4rk/feature/Team-Gorae…
Browse files Browse the repository at this point in the history
…bab#53

[Feature/Team-Goraebab#53] Add container execution status to response
  • Loading branch information
whitem4rk authored Nov 5, 2024
2 parents b6674e5 + 4082af5 commit 9f1a2cb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import api.goraebab.domain.blueprint.dto.BlueprintReqDto;
import api.goraebab.domain.blueprint.dto.BlueprintResDto;
import api.goraebab.domain.blueprint.dto.BlueprintsResDto;
import api.goraebab.domain.blueprint.dto.SyncResultDto;
import api.goraebab.domain.blueprint.service.BlueprintServiceImpl;
import api.goraebab.global.exception.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -171,11 +172,11 @@ public ResponseEntity<BlueprintResDto> getBlueprint(@RequestParam(required = fal
"{\"status\": \"CONTAINER_CREATION_FAILED\", \"code\": 500, \"message\": \"Failed to create the specified Docker container.\", \"errors\": []}"))
)
})
public ResponseEntity<Void> saveBlueprint(@RequestParam(required = false) @Schema(description = "The unique identifier of the storage. If null, the blueprint is considered to be in the local storage.") Long storageId,
public ResponseEntity<SyncResultDto> saveBlueprint(@RequestParam(required = false) @Schema(description = "The unique identifier of the storage. If null, the blueprint is considered to be in the local storage.") Long storageId,
@RequestBody @Valid BlueprintReqDto blueprintReqDto) {
blueprintService.saveBlueprint(storageId, blueprintReqDto);
SyncResultDto syncResultDto = blueprintService.saveBlueprint(storageId, blueprintReqDto);

return ResponseEntity.ok().build();
return ResponseEntity.ok(syncResultDto);
}

@Operation(summary = "Modify the blueprint",
Expand Down Expand Up @@ -214,12 +215,13 @@ public ResponseEntity<Void> saveBlueprint(@RequestParam(required = false) @Schem
"{\"status\": \"MODIFY_FAILED\", \"code\": 500, \"message\": \"Failed to modify blueprint.\", \"errors\": []}"))
)
})
public ResponseEntity<Void> modifyBlueprint(@RequestParam(required = false) @Schema(description = "The unique identifier of the storage. If null, the blueprint is considered to be in the local storage.") Long storageId,
public ResponseEntity<SyncResultDto> modifyBlueprint(@RequestParam(required = false) @Schema(description = "The unique identifier of the storage. If null, the blueprint is considered to be in the local storage.") Long storageId,
@PathVariable @Schema(description = "The unique identifier of the blueprint.") Long blueprintId,
@RequestBody @Valid BlueprintReqDto blueprintReqDto) {
blueprintService.modifyBlueprint(storageId, blueprintId, blueprintReqDto);
SyncResultDto syncResultDto = blueprintService.modifyBlueprint(storageId, blueprintId,
blueprintReqDto);

return ResponseEntity.ok().build();
return ResponseEntity.ok(syncResultDto);
}

@Operation(summary = "Delete the blueprint",
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/api/goraebab/domain/blueprint/dto/SyncResultDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package api.goraebab.domain.blueprint.dto;

import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class SyncResultDto {

private List<Map<String, Object>> failedContainers;

private List<Map<String, Object>> succeededContainers;


@Builder
public SyncResultDto(List<Map<String, Object>> failedContainers,
List<Map<String, Object>> succeededContainers) {
this.failedContainers = failedContainers;
this.succeededContainers = succeededContainers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import api.goraebab.domain.blueprint.dto.BlueprintResDto;
import api.goraebab.domain.blueprint.dto.BlueprintsResDto;

import api.goraebab.domain.blueprint.dto.SyncResultDto;
import java.util.List;

public interface BlueprintService {
Expand All @@ -12,9 +13,9 @@ public interface BlueprintService {

BlueprintResDto getBlueprintById(Long storageId, Long blueprintId);

void saveBlueprint(Long storageId, BlueprintReqDto blueprintReqDto);
SyncResultDto saveBlueprint(Long storageId, BlueprintReqDto blueprintReqDto);

void modifyBlueprint(Long storageId, Long blueprintId, BlueprintReqDto blueprintReqDto);
SyncResultDto modifyBlueprint(Long storageId, Long blueprintId, BlueprintReqDto blueprintReqDto);

void deleteBlueprint(Long storageId, Long blueprintId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import api.goraebab.domain.blueprint.dto.BlueprintReqDto;
import api.goraebab.domain.blueprint.dto.BlueprintResDto;
import api.goraebab.domain.blueprint.dto.BlueprintsResDto;
import api.goraebab.domain.blueprint.dto.SyncResultDto;
import api.goraebab.domain.blueprint.entity.Blueprint;
import api.goraebab.domain.blueprint.mapper.BlueprintMapper;
import api.goraebab.domain.blueprint.repository.BlueprintRepository;
Expand All @@ -12,7 +13,6 @@
import api.goraebab.global.exception.CustomException;
import api.goraebab.global.exception.ErrorCode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -30,6 +30,12 @@ public class BlueprintServiceImpl implements BlueprintService {
private final StorageRepository storageRepository;
private final DockerSyncServiceImpl dockerSyncService;
private final ObjectMapper objectMapper;
private static final String CONTAINER_SUCCESS = "success";
private static final String CONTAINER_FAILED = "failed";
private static final String FAIL_TO_CREATE_MESSAGE = "Failed to create some containers";
private static final String FAIL_TO_UPDATE_MESSAGE = "Failed to update some containers";
private static final String STATUS_KEY = "status";


@Override
@Transactional(readOnly = true)
Expand Down Expand Up @@ -68,78 +74,83 @@ public BlueprintResDto getBlueprintById(Long storageId, Long blueprintId) {

@Override
@Transactional
public void saveBlueprint(Long storageId, BlueprintReqDto blueprintReqDto) {
try {
Storage storage = null;
public SyncResultDto saveBlueprint(Long storageId, BlueprintReqDto blueprintReqDto) {
Storage storage = null;

if (storageId != null) {
storage = storageRepository.findById(storageId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_VALUE));
}
if (storageId != null) {
storage = storageRepository.findById(storageId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_VALUE));
}

String processedData = convertProcessedDataToJson(blueprintReqDto.getProcessedData());
String processedData = convertProcessedDataToJson(blueprintReqDto.getProcessedData());

Blueprint blueprint = Blueprint.builder()
.name(blueprintReqDto.getBlueprintName())
.data(processedData)
.isRemote(false)
.storage(storage)
.build();
Blueprint blueprint = Blueprint.builder()
.name(blueprintReqDto.getBlueprintName())
.data(processedData)
.isRemote(false)
.storage(storage)
.build();

blueprintRepository.save(blueprint);
blueprintRepository.save(blueprint);

List<Map<String, Object>> syncResults = dockerSyncService.syncDockerWithBlueprintData(blueprintReqDto.getProcessedData());
List<Map<String, Object>> syncResults = dockerSyncService.syncDockerWithBlueprintData(blueprintReqDto.getProcessedData());

List<Map<String, Object>> failedContainers = syncResults.stream()
.filter(result -> "failed".equals(result.get("status")))
.collect(Collectors.toList());
List<Map<String, Object>> failedContainers = syncResults.stream()
.filter(result -> CONTAINER_FAILED.equals(result.get(STATUS_KEY)))
.collect(Collectors.toList());

List<Map<String, Object>> succeededContainers = syncResults.stream()
.filter(result -> "success".equals(result.get("status")))
.collect(Collectors.toList());
List<Map<String, Object>> succeededContainers = syncResults.stream()
.filter(result -> CONTAINER_SUCCESS.equals(result.get(STATUS_KEY)))
.collect(Collectors.toList());

if (!failedContainers.isEmpty()) {
throw new CustomException(ErrorCode.SAVE_FAILED, FAIL_TO_CREATE_MESSAGE,
failedContainers, succeededContainers);
} else {
return SyncResultDto.builder()
.failedContainers(failedContainers)
.succeededContainers(succeededContainers)
.build();
}

if (!failedContainers.isEmpty()) {
throw new CustomException(ErrorCode.SAVE_FAILED, "Failed to create some containers", failedContainers, succeededContainers);
}

} catch (CustomException e) {
throw e;
}
}

@Override
@Transactional
public void modifyBlueprint(Long storageId, Long blueprintId, BlueprintReqDto blueprintReqDto) {
try {
Blueprint blueprint;
public SyncResultDto modifyBlueprint(Long storageId, Long blueprintId, BlueprintReqDto blueprintReqDto) {
Blueprint blueprint;

if (storageId == null) {
blueprint = blueprintRepository.findById(blueprintId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_VALUE));
} else {
blueprint = findBlueprintByStorageAndId(storageId, blueprintId);
}

String processedData = convertProcessedDataToJson(blueprintReqDto.getProcessedData());
blueprint.modify(blueprintReqDto.getBlueprintName(), processedData);

List<Map<String, Object>> syncResults = dockerSyncService.syncDockerWithBlueprintData(blueprintReqDto.getProcessedData());

List<Map<String, Object>> failedContainers = syncResults.stream()
.filter(result -> CONTAINER_FAILED.equals(result.get(STATUS_KEY)))
.collect(Collectors.toList());

List<Map<String, Object>> succeededContainers = syncResults.stream()
.filter(result -> CONTAINER_SUCCESS.equals(result.get(STATUS_KEY)))
.collect(Collectors.toList());

if (!failedContainers.isEmpty()) {
throw new CustomException(ErrorCode.MODIFY_FAILED,
FAIL_TO_UPDATE_MESSAGE, failedContainers, succeededContainers);
} else {
return SyncResultDto.builder()
.failedContainers(failedContainers)
.succeededContainers(succeededContainers)
.build();
}

if (storageId == null) {
blueprint = blueprintRepository.findById(blueprintId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_VALUE));
} else {
blueprint = findBlueprintByStorageAndId(storageId, blueprintId);
}

String processedData = convertProcessedDataToJson(blueprintReqDto.getProcessedData());
blueprint.modify(blueprintReqDto.getBlueprintName(), processedData);

List<Map<String, Object>> syncResults = dockerSyncService.syncDockerWithBlueprintData(blueprintReqDto.getProcessedData());

List<Map<String, Object>> failedContainers = syncResults.stream()
.filter(result -> "failed".equals(result.get("status")))
.collect(Collectors.toList());

List<Map<String, Object>> succeededContainers = syncResults.stream()
.filter(result -> "success".equals(result.get("status")))
.collect(Collectors.toList());

if (!failedContainers.isEmpty()) {
throw new CustomException(ErrorCode.MODIFY_FAILED, "Failed to update some containers", failedContainers, succeededContainers);
}

} catch (CustomException e) {
throw e;
}
}


Expand Down

0 comments on commit 9f1a2cb

Please sign in to comment.