Skip to content

Commit

Permalink
add custom requestId header option to terraform-boot
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Nov 27, 2023
1 parent a4a6547 commit f08af3c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.Objects;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.terraform.boot.models.TerraformBootSystemStatus;
import org.eclipse.xpanse.terraform.boot.models.plan.TerraformPlan;
Expand All @@ -24,6 +26,7 @@
import org.eclipse.xpanse.terraform.boot.models.validation.TerraformValidationResult;
import org.eclipse.xpanse.terraform.boot.terraform.service.TerraformDirectoryService;
import org.eclipse.xpanse.terraform.boot.terraform.service.TerraformScriptsService;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
Expand All @@ -34,6 +37,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -151,8 +155,13 @@ public TerraformValidationResult validateWithScripts(
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public TerraformResult deployWithScripts(
@Valid @RequestBody TerraformDeployWithScriptsRequest request) {
return terraformScriptsService.deployWithScripts(request);
@Valid @RequestBody TerraformDeployWithScriptsRequest request,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
return terraformScriptsService.deployWithScripts(request, uuid);
}

/**
Expand All @@ -166,8 +175,13 @@ public TerraformResult deployWithScripts(
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public TerraformResult destroyWithScripts(
@Valid @RequestBody TerraformDestroyWithScriptsRequest request) {
return terraformScriptsService.destroyWithScripts(request);
@Valid @RequestBody TerraformDestroyWithScriptsRequest request,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
return terraformScriptsService.destroyWithScripts(request, uuid);
}

/**
Expand All @@ -179,8 +193,13 @@ public TerraformResult destroyWithScripts(
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void asyncDeployWithScripts(
@Valid @RequestBody TerraformAsyncDeployFromDirectoryRequest asyncDeployRequest) {
terraformScriptsService.asyncDeployWithScripts(asyncDeployRequest);
@Valid @RequestBody TerraformAsyncDeployFromDirectoryRequest asyncDeployRequest,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
terraformScriptsService.asyncDeployWithScripts(asyncDeployRequest, uuid);
}

/**
Expand All @@ -192,8 +211,13 @@ public void asyncDeployWithScripts(
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void asyncDestroyWithScripts(
@Valid @RequestBody TerraformAsyncDestroyFromDirectoryRequest asyncDestroyRequest) {
terraformScriptsService.asyncDestroyWithScripts(asyncDestroyRequest);
@Valid @RequestBody TerraformAsyncDestroyFromDirectoryRequest asyncDestroyRequest,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
terraformScriptsService.asyncDestroyWithScripts(asyncDestroyRequest, uuid);
}

/**
Expand Down Expand Up @@ -226,7 +250,12 @@ public TerraformPlan plan(
@PostMapping(value = "/plan", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public TerraformPlan planWithScripts(
@Valid @RequestBody TerraformPlanWithScriptsRequest request) {
return terraformScriptsService.getTerraformPlanFromScripts(request);
@Valid @RequestBody TerraformPlanWithScriptsRequest request,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
return terraformScriptsService.getTerraformPlanFromScripts(request, uuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,43 +59,46 @@ public TerraformScriptsService(TerraformExecutor executor, RestTemplate restTemp
*/
public TerraformValidationResult validateWithScripts(
TerraformDeployWithScriptsRequest request) {
String moduleDirectory = buildDeployEnv(request.getScripts());
return tfValidateFromDirectory(moduleDirectory);
UUID uuid = UUID.randomUUID();
buildDeployEnv(request.getScripts(), uuid);
return tfValidateFromDirectory(uuid.toString());
}

/**
* Method of deployment a service using a script.
*/
public TerraformResult deployWithScripts(TerraformDeployWithScriptsRequest request) {
String moduleDirectory = buildDeployEnv(request.getScripts());
return deployFromDirectory(request, moduleDirectory);
public TerraformResult deployWithScripts(TerraformDeployWithScriptsRequest request, UUID uuid) {
buildDeployEnv(request.getScripts(), uuid);
return deployFromDirectory(request, uuid.toString());
}

/**
* Method of destroy a service using a script.
*/
public TerraformResult destroyWithScripts(TerraformDestroyWithScriptsRequest request) {
String moduleDirectory = buildDestroyEnv(request.getScripts(), request.getTfState());
return destroyFromDirectory(request, moduleDirectory);
public TerraformResult destroyWithScripts(TerraformDestroyWithScriptsRequest request,
UUID uuid) {
buildDestroyEnv(request.getScripts(), request.getTfState(), uuid);
return destroyFromDirectory(request, uuid.toString());
}

/**
* Method of destroy a service using a script.
*/
public TerraformPlan getTerraformPlanFromScripts(TerraformPlanWithScriptsRequest request) {
String moduleDirectory = buildDeployEnv(request.getScripts());
return getTerraformPlanFromDirectory(request, moduleDirectory);
public TerraformPlan getTerraformPlanFromScripts(TerraformPlanWithScriptsRequest request,
UUID uuid) {
buildDeployEnv(request.getScripts(), uuid);
return getTerraformPlanFromDirectory(request, uuid.toString());
}

/**
* Async deploy a source by terraform.
*/
@Async("taskExecutor")
public void asyncDeployWithScripts(
TerraformAsyncDeployFromDirectoryRequest asyncDeployRequest) {
TerraformAsyncDeployFromDirectoryRequest asyncDeployRequest, UUID uuid) {
TerraformResult result;
try {
result = deployWithScripts(asyncDeployRequest);
result = deployWithScripts(asyncDeployRequest, uuid);
} catch (RuntimeException e) {
result = TerraformResult.builder()
.commandStdOutput(null)
Expand All @@ -114,10 +117,11 @@ public void asyncDeployWithScripts(
* Async destroy resource of the service.
*/
@Async("taskExecutor")
public void asyncDestroyWithScripts(TerraformAsyncDestroyFromDirectoryRequest request) {
public void asyncDestroyWithScripts(TerraformAsyncDestroyFromDirectoryRequest request,
UUID uuid) {
TerraformResult result;
try {
result = destroyWithScripts(request);
result = destroyWithScripts(request, uuid);
} catch (RuntimeException e) {
result = TerraformResult.builder()
.commandStdOutput(null)
Expand All @@ -133,29 +137,26 @@ public void asyncDestroyWithScripts(TerraformAsyncDestroyFromDirectoryRequest re
restTemplate.postForLocation(url, result);
}

private String buildDeployEnv(List<String> scripts) {
String moduleDirectory = UUID.randomUUID().toString();
String workspace = executor.getModuleFullPath(moduleDirectory);
private void buildDeployEnv(List<String> scripts, UUID uuid) {
String workspace = executor.getModuleFullPath(uuid.toString());
buildWorkspace(workspace);
buildScriptFiles(workspace, scripts);
return moduleDirectory;
}

private String buildDestroyEnv(List<String> scripts, String tfState) {
String moduleDirectory = buildDeployEnv(scripts);
private void buildDestroyEnv(List<String> scripts, String tfState, UUID uuid) {
buildDeployEnv(scripts, uuid);
if (StringUtils.isBlank(tfState)) {
throw new TerraformExecutorException("terraform .tfState file create error");
}
String fileName =
executor.getModuleFullPath(moduleDirectory) + File.separator + STATE_FILE_NAME;
executor.getModuleFullPath(uuid.toString()) + File.separator + STATE_FILE_NAME;
try (FileWriter scriptWriter = new FileWriter(fileName)) {
scriptWriter.write(tfState);
log.info("terraform .tfState file create success, fileName: {}", fileName);
} catch (IOException ex) {
log.error("terraform .tfState file create failed.", ex);
throw new TerraformExecutorException("terraform .tfState file create failed.", ex);
}
return moduleDirectory;
}

private void buildWorkspace(String workspace) {
Expand Down

0 comments on commit f08af3c

Please sign in to comment.