Skip to content

Commit

Permalink
erraform-boot new deploy and destroy service with script file content…
Browse files Browse the repository at this point in the history
…s in request body
  • Loading branch information
WangLiNaruto committed Aug 31, 2023
1 parent 2e6d99e commit 2756886
Show file tree
Hide file tree
Showing 12 changed files with 611 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.terraform.boot.models.TerraformBootSystemStatus;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyRequest;
import org.eclipse.xpanse.terraform.boot.models.request.async.TerraformAsyncDeployRequest;
import org.eclipse.xpanse.terraform.boot.models.request.async.TerraformAsyncDestroyRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployFromDirectoryRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployWithScriptsRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyFromDirectoryRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyWithScriptsRequest;
import org.eclipse.xpanse.terraform.boot.models.request.async.TerraformAsyncDeployFromDirectoryRequest;
import org.eclipse.xpanse.terraform.boot.models.request.async.TerraformAsyncDestroyFromDirectoryRequest;
import org.eclipse.xpanse.terraform.boot.models.response.TerraformResult;
import org.eclipse.xpanse.terraform.boot.models.validation.TerraformValidationResult;
import org.eclipse.xpanse.terraform.boot.terraform.TerraformExecutor;
import org.eclipse.xpanse.terraform.boot.terraform.service.TerraformDirectoryService;
import org.eclipse.xpanse.terraform.boot.terraform.service.TerraformScriptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
Expand All @@ -40,11 +44,16 @@
@RequestMapping("/terraform-boot")
public class TerraformApiController {

private final TerraformExecutor terraformExecutor;
private final TerraformDirectoryService terraformDirectoryService;
private final TerraformScriptsService terraformScriptsService;

@Autowired
public TerraformApiController(TerraformExecutor terraformExecutor) {
this.terraformExecutor = terraformExecutor;
public TerraformApiController(
@Qualifier("terraformDirectoryService")
TerraformDirectoryService terraformDirectoryService,
TerraformScriptsService terraformScriptsService) {
this.terraformDirectoryService = terraformDirectoryService;
this.terraformScriptsService = terraformScriptsService;
}

/**
Expand All @@ -57,7 +66,7 @@ public TerraformApiController(TerraformExecutor terraformExecutor) {
@GetMapping(value = "/health", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public TerraformBootSystemStatus healthCheck() {
return this.terraformExecutor.tfHealthCheck();
return terraformDirectoryService.tfHealthCheck();
}

/**
Expand All @@ -73,7 +82,7 @@ public TerraformValidationResult validate(
@Parameter(name = "module_directory",
description = "directory name where the Terraform module files exist.")
@PathVariable("module_directory") String moduleDirectory) {
return this.terraformExecutor.tfValidate(moduleDirectory);
return terraformDirectoryService.tfValidateFromDirectory(moduleDirectory);
}

/**
Expand All @@ -89,8 +98,10 @@ public TerraformResult deploy(
@Parameter(name = "module_directory",
description = "directory name where the Terraform module files exist.")
@PathVariable("module_directory") String moduleDirectory,
@Valid @RequestBody TerraformDeployRequest terraformDeployRequest) {
return this.terraformExecutor.deploy(terraformDeployRequest, moduleDirectory);
@Valid @RequestBody
TerraformDeployFromDirectoryRequest terraformDeployFromDirectoryRequest) {
return terraformDirectoryService.deployFromDirectory(terraformDeployFromDirectoryRequest,
moduleDirectory);
}

/**
Expand All @@ -107,41 +118,65 @@ public TerraformResult destroy(
@Parameter(name = "module_directory",
description = "directory name where the Terraform module files exist.")
@PathVariable("module_directory") String moduleDirectory,
@Valid @RequestBody TerraformDestroyRequest terraformDestroyRequest) {
return this.terraformExecutor.destroy(terraformDestroyRequest, moduleDirectory);
@Valid @RequestBody
TerraformDestroyFromDirectoryRequest terraformDestroyFromDirectoryRequest) {
return terraformDirectoryService.destroyFromDirectory(terraformDestroyFromDirectoryRequest,
moduleDirectory);
}

/**
* Method to async deploy resources requested in a workspace.
* Method to deploy resources by scripts.
*
* @return Returns the status of the deployment.
*/
@Tag(name = "Terraform", description = "APIs for running Terraform commands")
@Operation(description = "Deploy resources via Terraform")
@PostMapping(value = "/deploy/scripts", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public TerraformResult deployWithScripts(
@Valid @RequestBody TerraformDeployWithScriptsRequest request) {
return terraformScriptsService.deployWithScripts(request);
}

/**
* Method to destroy resources by scripts.
*
* @return Returns the status of to Destroy.
*/
@Tag(name = "Terraform", description = "APIs for running Terraform commands")
@Operation(description = "Destroy resources via Terraform")
@PostMapping(value = "/destroy/scripts", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public TerraformResult destroyWithScripts(
@Valid @RequestBody TerraformDestroyWithScriptsRequest request) {
return terraformScriptsService.destroyWithScripts(request);
}

/**
* Method to async deploy resources by scripts.
*/
@Tag(name = "Terraform", description = "APIs for running Terraform commands")
@Operation(description = "async deploy resources via Terraform")
@PostMapping(value = "/deploy/async/{module_directory}", produces =
@PostMapping(value = "/deploy/scripts/async", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void asyncDeploy(
@Parameter(name = "module_directory",
description = "directory name where the Terraform module files exist.")
@PathVariable("module_directory") String moduleDirectory,
@Valid @RequestBody TerraformAsyncDeployRequest asyncDeployRequest) {
terraformExecutor.asyncDeploy(asyncDeployRequest, moduleDirectory);
public void asyncDeployWithScripts(
@Valid @RequestBody TerraformAsyncDeployFromDirectoryRequest asyncDeployRequest) {
terraformScriptsService.asyncDeployWithScripts(asyncDeployRequest);
}

/**
* Method to async destroy resources requested in a workspace.
*
* Method to async destroy resources by scripts.
*/
@Tag(name = "Terraform", description = "APIs for running Terraform commands")
@Operation(description = "Async destroy the Terraform modules")
@DeleteMapping(value = "/destroy/async/{module_directory}",
@DeleteMapping(value = "/destroy/scripts/async",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void asyncDestroy(
@Parameter(name = "module_directory",
description = "directory name where the Terraform module files exist.")
@PathVariable("module_directory") String moduleDirectory,
@Valid @RequestBody TerraformAsyncDestroyRequest asyncDestroyRequest) {
terraformExecutor.asyncDestroy(asyncDestroyRequest, moduleDirectory);
public void asyncDestroyWithScripts(
@Valid @RequestBody TerraformAsyncDestroyFromDirectoryRequest asyncDestroyRequest) {
terraformScriptsService.asyncDestroyWithScripts(asyncDestroyRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public TerraformExecutorException(String message) {
public TerraformExecutorException(String message, String output) {
super("Executor Exception:" + message + System.lineSeparator() + output);
}

public TerraformExecutorException(String message, Throwable ex) {
super(message, ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
* Data model for the terraform deploy requests.
*/
@Data
public class TerraformDeployRequest {
public class TerraformDeployFromDirectoryRequest {

@NotNull
@Schema(description = "Task ID for deployment requests deployed via script")
private String taskId;

@NotNull
@Schema(description = "Flag to control if the deployment must only generate the terraform "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.terraform.boot.models.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.Data;

/**
* Terraform uses the request object deployed by the script.
*/
@Data
public class TerraformDeployWithScriptsRequest extends TerraformDeployFromDirectoryRequest {

@NotNull
@Schema(description = "List of script files for deployment requests deployed via scripts")
private List<String> scripts;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
* Data model for the terraform destroy requests.
*/
@Data
public class TerraformDestroyRequest {
public class TerraformDestroyFromDirectoryRequest {

@NotNull
@Schema(description = "Task ID for deployment requests deployed via script")
private String taskId;

@NotNull
@Schema(description = "Key-value pairs of regular variables that must be used to execute the "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.terraform.boot.models.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.Data;

/**
* Terraform uses the request object destroy by the script.
*/
@Data
public class TerraformDestroyWithScriptsRequest extends TerraformDestroyFromDirectoryRequest {

@NotNull
@Schema(description = "List of script files for destroy requests deployed via scripts")
private List<String> scripts;

@NotNull
@Schema(description = "The .tfState file content after deployment")
private String tfState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDeployWithScriptsRequest;

/**
* Data model for the terraform async deploy requests.
*/
@Data
public class TerraformAsyncDeployRequest extends TerraformDeployRequest {
public class TerraformAsyncDeployFromDirectoryRequest extends TerraformDeployWithScriptsRequest {

@NotNull
@Schema(description = "Configuration information of webhook.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyRequest;
import org.eclipse.xpanse.terraform.boot.models.request.TerraformDestroyWithScriptsRequest;

/**
* Data model for the terraform async destroy requests.
*/
@Data
public class TerraformAsyncDestroyRequest extends TerraformDestroyRequest {
public class TerraformAsyncDestroyFromDirectoryRequest extends TerraformDestroyWithScriptsRequest {

@NotNull
@Schema(description = "Configuration information of webhook.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.eclipse.xpanse.terraform.boot.models.response;

import java.util.Map;
import lombok.Builder;
import lombok.Data;

Expand All @@ -18,6 +19,8 @@ public class TerraformResult {
private boolean isCommandSuccessful;
private String commandStdOutput;
private String commandStdError;
private String terraformState;
private Map<String, String> importantFileContentMap;


}
Loading

0 comments on commit 2756886

Please sign in to comment.