Skip to content

Commit

Permalink
Optimize method for reading the files generated by the Terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Nov 12, 2024
1 parent 7e2ec28 commit 0d1d39d
Show file tree
Hide file tree
Showing 14 changed files with 533 additions and 489 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -27,10 +30,9 @@
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.service.TerraformDirectoryService;
import org.eclipse.xpanse.terraform.boot.terraform.service.TerraformScriptsHelper;
import org.eclipse.xpanse.terraform.boot.terraform.tool.TerraformVersionsHelper;
import org.slf4j.MDC;
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 @@ -53,14 +55,10 @@
@RequestMapping("/terraform-boot/directory")
public class TerraformBootFromDirectoryApi {

private final TerraformDirectoryService terraformDirectoryService;

@Autowired
public TerraformBootFromDirectoryApi(
@Qualifier("terraformDirectoryService")
TerraformDirectoryService terraformDirectoryService) {
this.terraformDirectoryService = terraformDirectoryService;
}
@Resource
private TerraformDirectoryService directoryService;
@Resource
private TerraformScriptsHelper scriptsHelper;

/**
* Method to validate Terraform modules.
Expand All @@ -83,7 +81,7 @@ public TerraformValidationResult validateFromDirectory(
@PathVariable("terraform_version") String terraformVersion) {
UUID uuid = UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
return terraformDirectoryService.tfValidateFromDirectory(moduleDirectory, terraformVersion);
return directoryService.tfValidateFromDirectory(moduleDirectory, terraformVersion);
}

/**
Expand All @@ -105,7 +103,8 @@ public TerraformResult deployFromDirectory(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
return terraformDirectoryService.deployFromDirectory(request, moduleDirectory);
List<File> scriptFiles = scriptsHelper.getDeploymentFilesFromTaskWorkspace(moduleDirectory);
return directoryService.deployFromDirectory(request, moduleDirectory, scriptFiles);
}

/**
Expand All @@ -127,7 +126,8 @@ public TerraformResult modifyFromDirectory(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
return terraformDirectoryService.modifyFromDirectory(request, moduleDirectory);
List<File> scriptFiles = scriptsHelper.getDeploymentFilesFromTaskWorkspace(moduleDirectory);
return directoryService.modifyFromDirectory(request, moduleDirectory, scriptFiles);
}

/**
Expand All @@ -150,7 +150,8 @@ public TerraformResult destroyFromDirectory(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
return terraformDirectoryService.destroyFromDirectory(request, moduleDirectory);
List<File> scriptFiles = scriptsHelper.getDeploymentFilesFromTaskWorkspace(moduleDirectory);
return directoryService.destroyFromDirectory(request, moduleDirectory, scriptFiles);
}

/**
Expand All @@ -174,8 +175,7 @@ public TerraformPlan plan(
: (Objects.nonNull(uuid) ? uuid : UUID.randomUUID());
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
return terraformDirectoryService.getTerraformPlanFromDirectory(request,
moduleDirectory);
return directoryService.getTerraformPlanFromDirectory(request, moduleDirectory);
}

/**
Expand All @@ -196,7 +196,8 @@ public void asyncDeployFromDirectory(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
terraformDirectoryService.asyncDeployWithScripts(request, moduleDirectory);
List<File> scriptFiles = scriptsHelper.getDeploymentFilesFromTaskWorkspace(moduleDirectory);
directoryService.asyncDeployWithScripts(request, moduleDirectory, scriptFiles);
}

/**
Expand All @@ -217,7 +218,8 @@ public void asyncModifyFromDirectory(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
terraformDirectoryService.asyncModifyWithScripts(request, moduleDirectory);
List<File> scriptFiles = scriptsHelper.getDeploymentFilesFromTaskWorkspace(moduleDirectory);
directoryService.asyncModifyWithScripts(request, moduleDirectory, scriptFiles);
}

/**
Expand All @@ -238,6 +240,7 @@ public void asyncDestroyFromDirectory(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
terraformDirectoryService.asyncDestroyWithScripts(request, moduleDirectory);
List<File> scriptFiles = scriptsHelper.getDeploymentFilesFromTaskWorkspace(moduleDirectory);
directoryService.asyncDestroyWithScripts(request, moduleDirectory, scriptFiles);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

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

/**
* Defines possible exceptions returned by Terraform scripts invalid.
*/
public class InvalidTerraformScriptsException extends RuntimeException {

public InvalidTerraformScriptsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Response handleGitRepoCloneException(GitRepoCloneException ex) {
}

/**
* Exception handler for UnsupportedEnumValueException.
* Exception handler for InvalidTerraformToolException.
*/
@ExceptionHandler({InvalidTerraformToolException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
Expand All @@ -128,4 +128,16 @@ public Response handleInvalidTerraformToolException(
return Response.errorResponse(ResultType.INVALID_TERRAFORM_TOOL,
Collections.singletonList(ex.getMessage()));
}

/**
* Exception handler for InvalidTerraformScriptsException.
*/
@ExceptionHandler({InvalidTerraformScriptsException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Response handleInvalidTerraformScriptsException(
InvalidTerraformScriptsException ex) {
return Response.errorResponse(ResultType.INVALID_TERRAFORM_SCRIPTS,
Collections.singletonList(ex.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class TerraformDestroyFromDirectoryRequest {

@Schema(description = "Id of the request")
UUID requestId;
private UUID requestId;

@NotNull
@NotBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class TerraformModifyFromDirectoryRequest {

@Schema(description = "Id of the request")
UUID requestId;
private UUID requestId;

@NotNull
@NotBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public enum ResultType {
SERVICE_UNAVAILABLE("Service Unavailable"),
UNAUTHORIZED("Unauthorized"),
INVALID_GIT_REPO_DETAILS("Invalid Git Repo Details"),
INVALID_TERRAFORM_TOOL("Invalid Terraform Tool");
INVALID_TERRAFORM_TOOL("Invalid Terraform Tool"),

INVALID_TERRAFORM_SCRIPTS("Invalid Terraform Scripts");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TerraformResult {
private String terraformState;
@Schema(description = "Data of all other files generated by the terraform execution."
+ "The map key contains the file name and value is the file contents as string.")
private Map<String, String> importantFileContentMap;
private Map<String, String> generatedFileContentMap;
@Schema(description = "The version of the Terraform binary used to execute scripts.")
private String terraformVersionUsed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.eclipse.xpanse.terraform.boot.models.validation;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
Expand All @@ -15,6 +16,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class TerraformValidateDiagnostics {

@Schema(description = "Detail of validation error.")
private String detail;

}
Loading

0 comments on commit 0d1d39d

Please sign in to comment.