Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform-boot - Enable async processing of deploy and destroy API me… #11

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.io.File;
import java.util.UUID;
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 +46,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 +68,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 +84,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 +100,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 request) {
return terraformDirectoryService.deployFromDirectory(request,
moduleDirectory + File.separator + UUID.randomUUID());
}

/**
Expand All @@ -107,41 +120,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 request) {
return terraformDirectoryService.destroyFromDirectory(request,
moduleDirectory + File.separator + UUID.randomUUID());
}

/**
* 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,7 @@
* Data model for the terraform deploy requests.
*/
@Data
public class TerraformDeployRequest {
public class TerraformDeployFromDirectoryRequest {

@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,7 @@
* Data model for the terraform destroy requests.
*/
@Data
public class TerraformDestroyRequest {
public class TerraformDestroyFromDirectoryRequest {

@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