Skip to content

Commit

Permalink
Merge pull request #16 from Alice1319/feature/add_service_modify
Browse files Browse the repository at this point in the history
introduce option to modify specifications of a service.
  • Loading branch information
swaroopar authored Apr 2, 2024
2 parents d223041 + d044aef commit bb81fa9
Show file tree
Hide file tree
Showing 16 changed files with 490 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.eclipse.xpanse.tofu.maker.models.plan.OpenTofuPlanFromDirectoryRequest;
import org.eclipse.xpanse.tofu.maker.models.request.directory.OpenTofuAsyncDeployFromDirectoryRequest;
import org.eclipse.xpanse.tofu.maker.models.request.directory.OpenTofuAsyncDestroyFromDirectoryRequest;
import org.eclipse.xpanse.tofu.maker.models.request.directory.OpenTofuAsyncModifyFromDirectoryRequest;
import org.eclipse.xpanse.tofu.maker.models.request.directory.OpenTofuDeployFromDirectoryRequest;
import org.eclipse.xpanse.tofu.maker.models.request.directory.OpenTofuDestroyFromDirectoryRequest;
import org.eclipse.xpanse.tofu.maker.models.request.directory.OpenTofuModifyFromDirectoryRequest;
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;
Expand Down Expand Up @@ -101,6 +103,30 @@ public OpenTofuResult deployFromDirectory(
return openTofuDirectoryService.deployFromDirectory(request, moduleDirectory);
}

/**
* Method to modify resources requested in a workspace.
*
* @return Returns the status of the deployment.
*/
@Tag(name = "OpenTofuFromDirectory", description =
"APIs for running OpenTofu commands inside a provided directory.")
@Operation(description = "Modify resources via OpenTofu from the given directory.")
@PostMapping(value = "/modify/{module_directory}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public OpenTofuResult modifyFromDirectory(
@Parameter(name = "module_directory",
description = "directory name where the OpenTofu module files exist.")
@PathVariable("module_directory") String moduleDirectory,
@Valid @RequestBody
OpenTofuModifyFromDirectoryRequest request,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
return openTofuDirectoryService.modifyFromDirectory(request, moduleDirectory);
}

/**
* Method to destroy resources requested in a workspace.
*
Expand Down Expand Up @@ -173,6 +199,28 @@ public void asyncDeployFromDirectory(
openTofuDirectoryService.asyncDeployWithScripts(asyncDeployRequest, moduleDirectory);
}

/**
* Method to async modify resources from the given directory.
*/
@Tag(name = "OpenTofuFromDirectory", description =
"APIs for running OpenTofu commands inside a provided directory.")
@Operation(description = "async modify resources via OpenTofu from the given directory.")
@PostMapping(value = "/modify/async/{module_directory}", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void asyncModifyFromDirectory(
@Parameter(name = "module_directory",
description = "directory name where the OpenTofu module files exist.")
@PathVariable("module_directory") String moduleDirectory,
@Valid @RequestBody OpenTofuAsyncModifyFromDirectoryRequest asyncModifyRequest,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
openTofuDirectoryService.asyncModifyWithScripts(asyncModifyRequest, moduleDirectory);
}

/**
* Method to async destroy resources from the given directory.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.eclipse.xpanse.tofu.maker.models.plan.OpenTofuPlanFromGitRepoRequest;
import org.eclipse.xpanse.tofu.maker.models.request.git.OpenTofuAsyncDeployFromGitRepoRequest;
import org.eclipse.xpanse.tofu.maker.models.request.git.OpenTofuAsyncDestroyFromGitRepoRequest;
import org.eclipse.xpanse.tofu.maker.models.request.git.OpenTofuAsyncModifyFromGitRepoRequest;
import org.eclipse.xpanse.tofu.maker.models.request.git.OpenTofuDeployFromGitRepoRequest;
import org.eclipse.xpanse.tofu.maker.models.request.git.OpenTofuDestroyFromGitRepoRequest;
import org.eclipse.xpanse.tofu.maker.models.request.git.OpenTofuModifyFromGitRepoRequest;
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.OpenTofuGitRepoService;
Expand Down Expand Up @@ -110,6 +112,27 @@ public OpenTofuResult deployFromGitRepo(
return openTofuGitRepoService.deployFromGitRepo(request, uuid);
}

/**
* Method to modify resources using scripts from the GIT Repo provided.
*
* @return Returns the status of the deployment.
*/
@Tag(name = "OpenTofuFromGitRepo", description =
"APIs for running OpenTofu commands using OpenTofu scripts from a GIT Repo.")
@Operation(description = "Modify resources via OpenTofu")
@PostMapping(value = "/modify", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public OpenTofuResult modifyFromGitRepo(
@Valid @RequestBody OpenTofuModifyFromGitRepoRequest request,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
return openTofuGitRepoService.modifyFromGitRepo(request, uuid);
}

/**
* MMethod to deploy resources using scripts from the GIT Repo provided.
*
Expand Down Expand Up @@ -150,6 +173,25 @@ public void asyncDeployFromGitRepo(
openTofuGitRepoService.asyncDeployFromGitRepo(asyncDeployRequest, uuid);
}

/**
* Method to async modify resources from the provided GIT Repo.
*/
@Tag(name = "OpenTofuFromGitRepo", description =
"APIs for running OpenTofu commands using OpenTofu scripts from a GIT Repo.")
@Operation(description = "async modify resources via OpenTofu")
@PostMapping(value = "/modify/async", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void asyncModifyFromGitRepo(
@Valid @RequestBody OpenTofuAsyncModifyFromGitRepoRequest asyncModifyRequest,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
openTofuGitRepoService.asyncModifyFromGitRepo(asyncModifyRequest, uuid);
}

/**
* Method to async destroy resources by scripts.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.eclipse.xpanse.tofu.maker.models.plan.OpenTofuPlanWithScriptsRequest;
import org.eclipse.xpanse.tofu.maker.models.request.scripts.OpenTofuAsyncDeployFromScriptsRequest;
import org.eclipse.xpanse.tofu.maker.models.request.scripts.OpenTofuAsyncDestroyFromScriptsRequest;
import org.eclipse.xpanse.tofu.maker.models.request.scripts.OpenTofuAsyncModifyFromScriptsRequest;
import org.eclipse.xpanse.tofu.maker.models.request.scripts.OpenTofuDeployWithScriptsRequest;
import org.eclipse.xpanse.tofu.maker.models.request.scripts.OpenTofuDestroyWithScriptsRequest;
import org.eclipse.xpanse.tofu.maker.models.request.scripts.OpenTofuModifyWithScriptsRequest;
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.OpenTofuScriptsService;
Expand Down Expand Up @@ -89,6 +91,28 @@ public OpenTofuResult deployWithScripts(
return openTofuScriptsService.deployWithScripts(request, uuid);
}

/**
* Method to modify resources by scripts.
*
* @return Returns the status of to Modify.
*/
@Tag(name = "OpenTofuFromScripts", description =
"APIs for running OpenTofu commands on the scripts sent via request body.")
@Operation(description = "Modify resources via OpenTofu")
@PostMapping(value = "/modify", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public OpenTofuResult modifyWithScripts(
@Valid @RequestBody OpenTofuModifyWithScriptsRequest request,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
return openTofuScriptsService.modifyWithScripts(request, uuid);
}


/**
* Method to destroy resources by scripts.
*
Expand Down Expand Up @@ -129,6 +153,25 @@ public void asyncDeployWithScripts(
openTofuScriptsService.asyncDeployWithScripts(asyncDeployRequest, uuid);
}

/**
* Method to async modify resources by scripts.
*/
@Tag(name = "OpenTofuFromScripts", description =
"APIs for running OpenTofu commands on the scripts sent via request body.")
@Operation(description = "async modify resources via OpenTofu")
@PostMapping(value = "/modify/async", produces =
MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void asyncModifyWithScripts(
@Valid @RequestBody OpenTofuAsyncModifyFromScriptsRequest asyncModifyRequest,
@RequestHeader(name = "X-Custom-RequestId", required = false) UUID uuid) {
if (Objects.isNull(uuid)) {
uuid = UUID.randomUUID();
}
MDC.put("TASK_ID", uuid.toString());
openTofuScriptsService.asyncModifyWithScripts(asyncModifyRequest, uuid);
}

/**
* Method to async destroy resources by scripts.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,37 @@
import org.eclipse.xpanse.tofu.maker.models.exceptions.UnsupportedEnumValueException;

/**
* Destroy Scenario. The destroy scenario is used to set into OpenTofuResult and sent back to the
* client which sent the destroy request in what scenario.
* The deployment scenario is used to set into OpenTofuResult and sent back to the
* client which sent the deployment request in what scenario.
*/
public enum DestroyScenario {
public enum DeploymentScenario {
DEPLOY("deploy"),
MODIFY("modify"),
DESTROY("destroy"),
ROLLBACK("rollback"),
PURGE("purge");
private final String scenario;

DestroyScenario(String scenario) {
DeploymentScenario(String scenario) {
this.scenario = scenario;
}

/**
* For DestroyScenario deserialize.
* For DeploymentScenario deserialize.
*/
@JsonCreator
public static DestroyScenario getByValue(String scenario) {
for (DestroyScenario destroyScenario : values()) {
if (StringUtils.equalsIgnoreCase(destroyScenario.scenario, scenario)) {
return destroyScenario;
public static DeploymentScenario getByValue(String scenario) {
for (DeploymentScenario deploymentScenario : values()) {
if (StringUtils.equalsIgnoreCase(deploymentScenario.scenario, scenario)) {
return deploymentScenario;
}
}
throw new UnsupportedEnumValueException(
String.format("DestroyScenario value %s is not supported.", scenario));
String.format("DeploymentScenario value %s is not supported.", scenario));
}

/**
* For DestroyScenario serialize.
* For DeploymentScenario serialize.
*/
@JsonValue
public String toValue() {
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.tofu.maker.models.request.directory;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.eclipse.xpanse.tofu.maker.models.request.webhook.WebhookConfig;

/**
* Data model for the OpenTofu async modify requests.
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class OpenTofuAsyncModifyFromDirectoryRequest extends OpenTofuModifyFromDirectoryRequest {

@NotNull
@Schema(description = "Configuration information of webhook.")
private WebhookConfig webhookConfig;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import org.eclipse.xpanse.tofu.maker.models.enums.DeploymentScenario;

/**
* Data model for the OpenTofu deploy requests.
Expand All @@ -19,9 +20,15 @@ public class OpenTofuDeployFromDirectoryRequest {

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

@Schema(description = "This value can be set by the client if they wish to know the type of"
+ "request for which the callback response is generated from tofu-maker. There will be"
+ "no difference in the way request is executed. This information is only set in the"
+ "callback response again for the client to handle the callback response accordingly.")
DeploymentScenario deploymentScenario;

@NotNull
@Schema(description = "Key-value pairs of variables that must be used to execute the "
+ "OpenTofu request.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import org.eclipse.xpanse.tofu.maker.models.enums.DestroyScenario;
import org.eclipse.xpanse.tofu.maker.models.enums.DeploymentScenario;

/**
* Data model for the OpenTofu destroy requests.
*/
@Data
public class OpenTofuDestroyFromDirectoryRequest {

@Schema(description = "The destroy scenario when the Xpanse client send the destroy request. "
+ "Valid values: destroy,rollback,purge.")
DestroyScenario destroyScenario;
@Schema(description = "This value can be set by the client if they wish to know the type of"
+ "request for which the callback response is generated from tofu-maker. There will be"
+ "no difference in the way request is executed. This information is only set in the"
+ "callback response again for the client to handle the callback response accordingly.")
DeploymentScenario deploymentScenario;

@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,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.tofu.maker.models.request.directory;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import org.eclipse.xpanse.tofu.maker.models.enums.DeploymentScenario;

/**
* Data model for the OpenTofu modify requests.
*/
@Data
public class OpenTofuModifyFromDirectoryRequest {

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

@Schema(description = "This value can be set by the client if they wish to know the type of"
+ "request for which the callback response is generated from tofu-maker. There will be"
+ "no difference in the way request is executed. This information is only set in the"
+ "callback response again for the client to handle the callback response accordingly.")
DeploymentScenario deploymentScenario;

@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;

@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<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.tofu.maker.models.request.git;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.eclipse.xpanse.tofu.maker.models.request.webhook.WebhookConfig;

/**
* Data model for open tofu async modify requests using scripts from a GIT Repo.
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class OpenTofuAsyncModifyFromGitRepoRequest extends OpenTofuModifyFromGitRepoRequest {

@NotNull
@Schema(description = "Configuration information of webhook.")
private WebhookConfig webhookConfig;

}
Loading

0 comments on commit bb81fa9

Please sign in to comment.