Skip to content

Commit

Permalink
Optimize method for reading the files generated by the OpenTofu
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Nov 8, 2024
1 parent 5e1b1ea commit a8b45d1
Show file tree
Hide file tree
Showing 20 changed files with 577 additions and 640 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.tofu.maker.models.response.OpenTofuResult;
import org.eclipse.xpanse.tofu.maker.models.validation.OpenTofuValidationResult;
import org.eclipse.xpanse.tofu.maker.opentofu.service.OpenTofuDirectoryService;
import org.eclipse.xpanse.tofu.maker.opentofu.service.OpenTofuScriptsHelper;
import org.eclipse.xpanse.tofu.maker.opentofu.tool.OpenTofuVersionsHelper;
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 @@ -51,15 +53,10 @@
@RestController
@RequestMapping("/tofu-maker/directory")
public class OpenTofuMakerFromDirectoryApi {

private final OpenTofuDirectoryService openTofuDirectoryService;

@Autowired
public OpenTofuMakerFromDirectoryApi(
@Qualifier("openTofuDirectoryService")
OpenTofuDirectoryService openTofuDirectoryService) {
this.openTofuDirectoryService = openTofuDirectoryService;
}
@Resource
private OpenTofuDirectoryService directoryService;
@Resource
private OpenTofuScriptsHelper scriptsHelper;

/**
* Method to validate OpenTofu modules.
Expand All @@ -82,7 +79,7 @@ public OpenTofuValidationResult validateFromDirectory(
@PathVariable("opentofu_version") String openTofuVersion) {
UUID uuid = UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
return openTofuDirectoryService.tfValidateFromDirectory(moduleDirectory, openTofuVersion);
return directoryService.tfValidateFromDirectory(moduleDirectory, openTofuVersion);
}

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

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

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

/**
Expand All @@ -173,8 +173,7 @@ public OpenTofuPlan plan(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
return openTofuDirectoryService.getOpenTofuPlanFromDirectory(request,
moduleDirectory);
return directoryService.getOpenTofuPlanFromDirectory(request, moduleDirectory);
}

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

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

/**
Expand All @@ -237,6 +238,7 @@ public void asyncDestroyFromDirectory(
? request.getRequestId() : UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
request.setRequestId(uuid);
openTofuDirectoryService.asyncDestroyWithScripts(request, moduleDirectory);
List<File> scriptFiles = scriptsHelper.getDeploymentFilesFromTaskWorkspace(moduleDirectory);
directoryService.asyncDestroyWithScripts(request, moduleDirectory, scriptFiles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class OpenTofuPlan {

@NotNull
@Schema(description = "OpenTofu plan as a JSON string")
String plan;
private String plan;

@Schema(description = "The version of the OpenTofu binary used to execute scripts.")
String openTofuVersionUsed;
private String openTofuVersionUsed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@
public class OpenTofuPlanFromDirectoryRequest {

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

@NotNull
@NotBlank
@Pattern(regexp = OpenTofuVersionsHelper.OPENTOFU_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the OpenTofu which will execute the scripts.")
String openTofuVersion;
private String openTofuVersion;

@NotNull
@Schema(description = "Key-value pairs of variables that must be used to execute the "
+ "OpenTofu request.",
additionalProperties = Schema.AdditionalPropertiesValue.TRUE)
Map<String, Object> variables;
private Map<String, Object> variables;

@Schema(description = "Key-value pairs of variables that must be injected as environment "
+ "variables to open tofu process.",
additionalProperties = Schema.AdditionalPropertiesValue.TRUE)
Map<String, String> envVariables = new HashMap<>();
private Map<String, String> envVariables = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
public class OpenTofuPlanFromGitRepoRequest extends OpenTofuPlanFromDirectoryRequest {

@Schema(description = "GIT Repo details from where the scripts can be fetched.")
OpenTofuScriptGitRepoDetails gitRepoDetails;
private OpenTofuScriptGitRepoDetails gitRepoDetails;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
public class OpenTofuDeployFromDirectoryRequest {

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

@NotNull
@NotBlank
@Pattern(regexp = OpenTofuVersionsHelper.OPENTOFU_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the OpenTofu which will execute the scripts.")
String openTofuVersion;
private String openTofuVersion;

@NotNull
@Schema(description = "Flag to control if the deployment must only generate the OpenTofu "
+ "or it must also apply the changes.")
Boolean isPlanOnly;
private Boolean isPlanOnly;

@NotNull
@Schema(description = "Key-value pairs of variables that must be used to execute the "
+ "OpenTofu request.",
additionalProperties = Schema.AdditionalPropertiesValue.TRUE)
Map<String, Object> variables;
private Map<String, Object> variables;

@Schema(description = "Key-value pairs of variables that must be injected as environment "
+ "variables to OpenTofu process.",
additionalProperties = Schema.AdditionalPropertiesValue.TRUE)
Map<String, String> envVariables = new HashMap<>();
private Map<String, String> envVariables = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
public class OpenTofuDestroyFromDirectoryRequest {

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

@NotNull
@NotBlank
@Pattern(regexp = OpenTofuVersionsHelper.OPENTOFU_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the OpenTofu which will execute the scripts.")
String openTofuVersion;
private String openTofuVersion;

@NotNull
@Schema(description = "Key-value pairs of regular variables that must be used to execute the "
+ "OpenTofu request.", additionalProperties = TRUE)
Map<String, Object> variables;
private Map<String, Object> variables;

@Schema(description = "Key-value pairs of variables that must be injected as environment "
+ "variables to OpenTofu process.", additionalProperties = TRUE)
Map<String, String> envVariables = new HashMap<>();
private Map<String, String> envVariables = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@
public class OpenTofuModifyFromDirectoryRequest {

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

@NotNull
@NotBlank
@Pattern(regexp = OpenTofuVersionsHelper.OPENTOFU_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the OpenTofu which will execute the scripts.")
String openTofuVersion;
private String openTofuVersion;

@NotNull
@Schema(description = "Flag to control if the deployment must only generate the OpenTofu "
+ "or it must also apply the changes.")
Boolean isPlanOnly;
private Boolean isPlanOnly;

@NotNull
@Schema(description = "Key-value pairs of variables that must be used to execute the "
+ "OpenTofu request.", additionalProperties = TRUE)
Map<String, Object> variables;
private Map<String, Object> variables;

@Schema(description = "Key-value pairs of variables that must be injected as environment "
+ "variables to OpenTofu process.", additionalProperties = TRUE)
Map<String, String> envVariables = new HashMap<>();
private Map<String, String> envVariables = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public class OpenTofuDeployFromGitRepoRequest extends OpenTofuDeployFromDirector

@NotNull
@Schema(description = "GIT Repo details from where the scripts can be fetched.")
OpenTofuScriptGitRepoDetails gitRepoDetails;
private OpenTofuScriptGitRepoDetails gitRepoDetails;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class OpenTofuDestroyFromGitRepoRequest extends OpenTofuDestroyFromDirectoryRequest {

@Schema(description = "GIT Repo details from where the scripts can be fetched.")
OpenTofuScriptGitRepoDetails gitRepoDetails;
private OpenTofuScriptGitRepoDetails gitRepoDetails;

@NotNull
@Schema(description = "The .tfState file content after deployment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class OpenTofuModifyFromGitRepoRequest extends OpenTofuModifyFromDirectoryRequest {

@Schema(description = "GIT Repo details from where the scripts can be fetched.")
OpenTofuScriptGitRepoDetails gitRepoDetails;
private OpenTofuScriptGitRepoDetails gitRepoDetails;

@NotNull
@Schema(description = "The .tfState file content after deployment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class OpenTofuDestroyWithScriptsRequest extends OpenTofuDestroyFromDirectoryRequest {

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

@NotNull
@Schema(description = "List of script files for destroy requests deployed via scripts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class OpenTofuResult {
private String terraformState;
@Schema(description = "Data of all other files generated by the openTofu 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 OpenTofu binary used to execute scripts.")
String openTofuVersionUsed;
private String openTofuVersionUsed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.eclipse.xpanse.tofu.maker.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 OpenTofuValidateDiagnostics {

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

}
Loading

0 comments on commit a8b45d1

Please sign in to comment.