initial commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.cfdeployer.controller;
|
||||
|
||||
import com.cfdeployer.model.CfDeployRequest;
|
||||
import com.cfdeployer.model.CfDeployResponse;
|
||||
import com.cfdeployer.service.CfCliService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/cf")
|
||||
@RequiredArgsConstructor
|
||||
public class CfDeployController {
|
||||
|
||||
private final CfCliService cfCliService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@PostMapping(value = "/deploy", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ResponseEntity<CfDeployResponse> deploy(
|
||||
@RequestPart("request") String requestJson,
|
||||
@RequestPart("jarFile") MultipartFile jarFile,
|
||||
@RequestPart("manifest") MultipartFile manifest) {
|
||||
|
||||
try {
|
||||
log.info("Received deployment request");
|
||||
|
||||
CfDeployRequest request = objectMapper.readValue(requestJson, CfDeployRequest.class);
|
||||
log.info("Deploying application: {} to org: {} space: {}",
|
||||
request.getAppName(), request.getOrganization(), request.getSpace());
|
||||
|
||||
validateFiles(jarFile, manifest);
|
||||
|
||||
CfDeployResponse response = cfCliService.deployApplication(request, jarFile, manifest);
|
||||
|
||||
if (Boolean.TRUE.equals(response.getSuccess())) {
|
||||
return ResponseEntity.ok(response);
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error processing deployment request", e);
|
||||
CfDeployResponse errorResponse = CfDeployResponse.failure(
|
||||
"Failed to process deployment request: " + e.getMessage(),
|
||||
e.toString()
|
||||
);
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateFiles(MultipartFile jarFile, MultipartFile manifest) {
|
||||
if (jarFile.isEmpty()) {
|
||||
throw new IllegalArgumentException("JAR file is empty");
|
||||
}
|
||||
|
||||
if (manifest.isEmpty()) {
|
||||
throw new IllegalArgumentException("Manifest file is empty");
|
||||
}
|
||||
|
||||
String jarFileName = jarFile.getOriginalFilename();
|
||||
if (jarFileName == null || !jarFileName.toLowerCase().endsWith(".jar")) {
|
||||
throw new IllegalArgumentException("Invalid JAR file. File must have .jar extension");
|
||||
}
|
||||
|
||||
String manifestFileName = manifest.getOriginalFilename();
|
||||
if (manifestFileName == null ||
|
||||
(!manifestFileName.toLowerCase().endsWith(".yml") && !manifestFileName.toLowerCase().endsWith(".yaml"))) {
|
||||
throw new IllegalArgumentException("Invalid manifest file. File must have .yml or .yaml extension");
|
||||
}
|
||||
|
||||
log.debug("File validation successful - JAR: {}, Manifest: {}", jarFileName, manifestFileName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user