Skip to content

Commit

Permalink
handle all types in service deployment properties
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopar committed Oct 24, 2023
1 parent e98ccc3 commit ebe8114
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 72 deletions.
1 change: 0 additions & 1 deletion modules/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>workflow</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.api.config;

import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import java.util.Map;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* This customization is needed to work around an open issue in swagger-core which is used by
* spring-doc for generating the openapi schema.
* <a href="https://github.com/swagger-api/swagger-core/issues/4316">...</a>
* without this fix, the datatype for Object will be shown as Map always. So a
* {@code Map<String, Object>}`
* will be {@code Map<String, Map<String, Object>} and this causes issues in the client.
*/
@Configuration
public class OpenApiCustomizerConfig {

@Bean
public OpenApiCustomizer enableArbitraryObjects() {
return openApi -> openApi.getComponents().getSchemas().values().forEach(
this::enableArbitraryObjects);
}

private void enableArbitraryObjects(Schema<Object> schema) {
if (schema instanceof MapSchema) {
if (schema.getAdditionalProperties() instanceof Schema
&& ((Schema<?>) schema.getAdditionalProperties()).getType()
.equalsIgnoreCase("object")) {
schema.setAdditionalProperties(true);
}
} else if (schema.getType() != null && schema.getType().equalsIgnoreCase("object")
&& schema.getProperties() != null) {
Map<String, Schema> properties = schema.getProperties();
properties.values().forEach(this::enableArbitraryObjects);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void testTransResourceEntity() {
@Test
void testTransResourceEntity_emptyProperties() {
deployResourceEntity.setProperties(properties);
deployResource.setProperties(properties);
deployResource.setProperties(new HashMap<>());
List<DeployResourceEntity> entities = List.of(deployResourceEntity);
final List<DeployResource> expectedResult = List.of(deployResource);
final List<DeployResource> result = EntityTransUtils.transResourceEntity(entities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public Deployment getDeployHandler(DeployTask deployTask) {
}

private void encodeDeployVariable(ServiceTemplateEntity serviceTemplate,
Map<String, String> serviceRequestProperties) {
Map<String, Object> serviceRequestProperties) {
if (Objects.isNull(serviceTemplate.getOcl().getDeployment())
||
CollectionUtils.isEmpty(serviceTemplate.getOcl().getDeployment().getVariables())
Expand All @@ -179,7 +179,8 @@ private void encodeDeployVariable(ServiceTemplateEntity serviceTemplate,
.equals(variable.getSensitiveScope().toValue())
&& serviceRequestProperties.containsKey(variable.getName())) {
serviceRequestProperties.put(variable.getName(),
aesUtil.encode(serviceRequestProperties.get(variable.getName())));
aesUtil.encode(
serviceRequestProperties.get(variable.getName()).toString()));
}
});
}
Expand Down Expand Up @@ -570,10 +571,10 @@ private void maskSensitiveFields(DeployServiceEntity deployServiceEntity) {
if (Objects.nonNull(deployServiceEntity.getDeployRequest().getServiceRequestProperties())) {
for (DeployVariable deployVariable
: deployServiceEntity.getDeployRequest().getOcl().getDeployment()
.getVariables()) {
.getVariables()) {
if (deployVariable.getSensitiveScope() != SensitiveScope.NONE
&& (deployServiceEntity.getDeployRequest().getServiceRequestProperties()
.containsKey(deployVariable.getName()))) {
.containsKey(deployVariable.getName()))) {
deployServiceEntity.getDeployRequest().getServiceRequestProperties()
.put(deployVariable.getName(), "********");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private TerraformAsyncDestroyFromDirectoryRequest getDestroyRequest(DeployTask t
new TerraformAsyncDestroyFromDirectoryRequest();
request.setScripts(getFiles(task));
request.setTfState(stateFile);
request.setVariables(getInputVariables(task));
request.setVariables(getInputVariables(task, false));
request.setEnvVariables(getEnvironmentVariables(task));
WebhookConfig hookConfig = new WebhookConfig();
String callbackUrl = getClientRequestBaseUrl(port)
Expand All @@ -147,7 +147,7 @@ private TerraformAsyncDeployFromDirectoryRequest getDeployRequest(DeployTask tas
new TerraformAsyncDeployFromDirectoryRequest();
request.setIsPlanOnly(false);
request.setScripts(getFiles(task));
request.setVariables(getInputVariables(task));
request.setVariables(getInputVariables(task, true));
request.setEnvVariables(getEnvironmentVariables(task));
WebhookConfig hookConfig = new WebhookConfig();
String callbackUrl = getClientRequestBaseUrl(port)
Expand Down Expand Up @@ -189,9 +189,9 @@ private List<String> getFilesByOcl(Ocl ocl) {
return Arrays.asList(provider, deployer);
}

private Map<String, Object> getInputVariables(DeployTask deployTask) {
private Map<String, Object> getInputVariables(DeployTask deployTask, boolean isDeployRequest) {
Map<String, Object> inputVariables = new HashMap<>();
inputVariables.putAll(this.deployEnvironments.getVariables(deployTask));
inputVariables.putAll(this.deployEnvironments.getVariables(deployTask, isDeployRequest));
inputVariables.putAll(this.deployEnvironments.getFlavorVariables(deployTask));
return inputVariables;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public DeployResult deploy(DeployTask task) {
createScriptFile(task.getDeployRequest().getCsp(), task.getDeployRequest().getRegion(),
workspace, task.getOcl().getDeployment().getDeployer());
// Execute the terraform command.
TerraformExecutor executor = getExecutorForDeployTask(task, workspace);
TerraformExecutor executor = getExecutorForDeployTask(task, workspace, true);
executor.deploy();
String tfState = executor.getTerraformState();

Expand Down Expand Up @@ -134,7 +134,7 @@ public DeployResult destroy(DeployTask task, String tfState) {
String workspace = getWorkspacePath(taskId);
createDestroyScriptFile(task.getDeployRequest().getCsp(),
task.getDeployRequest().getRegion(), workspace, tfState);
TerraformExecutor executor = getExecutorForDeployTask(task, workspace);
TerraformExecutor executor = getExecutorForDeployTask(task, workspace, false);
executor.destroy();
DeployResult result = new DeployResult();
result.setId(task.getId());
Expand Down Expand Up @@ -248,12 +248,16 @@ private void deleteWorkSpace(String workspace) {
/**
* Get a TerraformExecutor.
*
* @param task the task for the deployment.
* @param workspace the workspace of the deployment.
* @param task the task for the deployment.
* @param workspace the workspace of the deployment.
* @param isDeployTask if the task is for deploying a service.
*/
private TerraformExecutor getExecutorForDeployTask(DeployTask task, String workspace) {
private TerraformExecutor getExecutorForDeployTask(DeployTask task,
String workspace,
boolean isDeployTask) {
Map<String, String> envVariables = this.deployEnvironments.getEnv(task);
Map<String, String> inputVariables = this.deployEnvironments.getVariables(task);
Map<String, Object> inputVariables = this.deployEnvironments.getVariables(
task, isDeployTask);
// load flavor variables also as input variables for terraform executor.
inputVariables.putAll(this.deployEnvironments.getFlavorVariables(task));
// load credential variables also as env variables for terraform executor.
Expand All @@ -263,7 +267,7 @@ private TerraformExecutor getExecutorForDeployTask(DeployTask task, String works
}

private TerraformExecutor getExecutor(Map<String, String> envVariables,
Map<String, String> inputVariables, String workspace) {
Map<String, Object> inputVariables, String workspace) {
if (terraformLocalConfig.isDebugEnabled()) {
log.info("Debug enabled for Terraform CLI with level {}",
terraformLocalConfig.getDebugLogLevel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TerraformExecutor {
}

private final Map<String, String> env;
private final Map<String, String> variables;
private final Map<String, Object> variables;
private final String workspace;

/**
Expand All @@ -50,7 +50,7 @@ public class TerraformExecutor {
* @param variables variables for the terraform command line.
* @param workspace workspace for the terraform command line.
*/
TerraformExecutor(Map<String, String> env, Map<String, String> variables,
TerraformExecutor(Map<String, String> env, Map<String, Object> variables,
String workspace) {
this.env = env;
this.variables = variables;
Expand All @@ -60,7 +60,7 @@ public class TerraformExecutor {
/**
* Executes terraform init command.
*
* @return Returns result of SystemCmd executes.
* @return Returns result of SystemCmd executed.
*/
public SystemCmdResult tfInit() {
return execute("terraform init -no-color");
Expand Down Expand Up @@ -105,7 +105,7 @@ private SystemCmdResult executeWithVariables(StringBuilder command) {
command.append(" -var-file=");
command.append(VARS_FILE_NAME);
SystemCmdResult systemCmdResult = execute(command.toString());
cleanUpVariablesFile();
// cleanUpVariablesFile();
return systemCmdResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public class DeployEnvironments {
* Constructor to initialize DeployEnvironments bean.
*
* @param credentialCenter CredentialCenter bean
* @param aesUtil AesUtil bean
* @param pluginManager PluginManager bean
* @param environment Environment bean
* @param aesUtil AesUtil bean
* @param pluginManager PluginManager bean
* @param environment Environment bean
*/

@Autowired
Expand All @@ -66,16 +66,16 @@ public DeployEnvironments(CredentialCenter credentialCenter, AesUtil aesUtil,
*/
public Map<String, String> getEnv(DeployTask task) {
Map<String, String> variables = new HashMap<>();
Map<String, String> request = task.getDeployRequest().getServiceRequestProperties();
Map<String, Object> request = task.getDeployRequest().getServiceRequestProperties();
for (DeployVariable variable : task.getOcl().getDeployment().getVariables()) {
if (variable.getKind() == DeployVariableKind.ENV) {
if (request.containsKey(variable.getName())
&& request.get(variable.getName()) != null) {
variables.put(variable.getName(),
!SensitiveScope.NONE.toValue()
.equals(variable.getSensitiveScope().toValue())
? aesUtil.decode(request.get(variable.getName()))
: request.get(variable.getName()));
? aesUtil.decode(request.get(variable.getName()).toString())
: request.get(variable.getName()).toString());
} else {
variables.put(variable.getName(), System.getenv(variable.getName()));
}
Expand Down Expand Up @@ -115,17 +115,18 @@ public Map<String, String> getFlavorVariables(DeployTask task) {
*
* @param task the DeployTask.
*/
public Map<String, String> getVariables(DeployTask task) {
Map<String, String> variables = new HashMap<>();
Map<String, String> request = task.getDeployRequest().getServiceRequestProperties();
public Map<String, Object> getVariables(DeployTask task, boolean isDeployRequest) {
Map<String, Object> variables = new HashMap<>();
Map<String, Object> request = task.getDeployRequest().getServiceRequestProperties();
for (DeployVariable variable : task.getOcl().getDeployment().getVariables()) {
if (variable.getKind() == DeployVariableKind.VARIABLE) {
if (request.containsKey(variable.getName())
&& request.get(variable.getName()) != null) {
variables.put(variable.getName(),
!SensitiveScope.NONE.toValue()
.equals(variable.getSensitiveScope().toValue())
? aesUtil.decode(request.get(variable.getName()))
(variable.getSensitiveScope() != SensitiveScope.NONE
&& isDeployRequest)
? aesUtil.decodeBackToOriginalType(variable.getDataType(),
request.get(variable.getName()).toString())
: request.get(variable.getName()));
} else {
variables.put(variable.getName(), System.getenv(variable.getName()));
Expand All @@ -138,8 +139,7 @@ public Map<String, String> getVariables(DeployTask task) {

if (variable.getKind() == DeployVariableKind.FIX_VARIABLE) {
variables.put(variable.getName(),
!SensitiveScope.NONE.toValue()
.equals(variable.getSensitiveScope().toValue())
(variable.getSensitiveScope() != SensitiveScope.NONE && isDeployRequest)
? aesUtil.decode(variable.getValue()) : variable.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void setUp() {
deployVariable.setSensitiveScope(SensitiveScope.ALWAYS);
deployment.setVariables(List.of(deployVariable));

Map<String, String> requestProperties = new HashMap<>();
Map<String, Object> requestProperties = new HashMap<>();
requestProperties.put("test", "test");
Ocl ocl = new Ocl();
ocl.setName("oclName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void testDeploy() {
.thenReturn("result");

// Configure DeployEnvironments.getVariables(...).
final Map<String, String> stringStringMap = Map.ofEntries(Map.entry("value", "value"));
final Map<String, Object> stringStringMap = Map.ofEntries(Map.entry("value", "value"));
final DeployTask task = new DeployTask();
task.setId(UUID.fromString("800b6caa-5710-4064-8bae-cb446a351cc1"));
final DeployRequest deployRequest1 = new DeployRequest();
Expand All @@ -103,7 +103,7 @@ void testDeploy() {
deployment1.setDeployer("deployer");
ocl1.setDeployment(deployment1);
task.setOcl(ocl1);
when(mockDeployEnvironments.getVariables(task)).thenReturn(stringStringMap);
when(mockDeployEnvironments.getVariables(task, true)).thenReturn(stringStringMap);

// Configure DeployEnvironments.getFlavorVariables(...).
final Map<String, String> stringStringMap1 = Map.ofEntries(Map.entry("value", "value"));
Expand Down Expand Up @@ -247,7 +247,7 @@ void testDestroy() {
.thenReturn("result");

// Configure DeployEnvironments.getVariables(...).
final Map<String, String> stringStringMap = Map.ofEntries(Map.entry("value", "value"));
final Map<String, Object> stringStringMap = Map.ofEntries(Map.entry("value", "value"));
final DeployTask task1 = new DeployTask();
task1.setId(UUID.fromString("800b6caa-5710-4064-8bae-cb446a351cc1"));
final DeployRequest deployRequest1 = new DeployRequest();
Expand All @@ -265,7 +265,7 @@ void testDestroy() {
deployment1.setDeployer("deployer");
ocl1.setDeployment(deployment1);
task1.setOcl(ocl1);
when(mockDeployEnvironments.getVariables(task1)).thenReturn(stringStringMap);
when(mockDeployEnvironments.getVariables(task1, true)).thenReturn(stringStringMap);

// Configure DeployEnvironments.getFlavorVariables(...).
final Map<String, String> stringStringMap1 = Map.ofEntries(Map.entry("value", "value"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void basicTest() throws Exception {
deployRequest.setVersion(ocl.getServiceVersion());
deployRequest.setFlavor(ocl.getFlavors().get(0).getName());

Map<String, String> property = new HashMap<>();
Map<String, Object> property = new HashMap<>();
property.put("secgroup_id", "1234567890");
deployRequest.setServiceRequestProperties(property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DeployEnvironmentsTest {

@BeforeEach
void setUp() {
Map<String, String> serviceRequestProperties = new HashMap<>();
Map<String, Object> serviceRequestProperties = new HashMap<>();
serviceRequestProperties.put("name", "value");
serviceRequestProperties.put("key2", "value2");
serviceRequestProperties.put("example", null);
Expand Down Expand Up @@ -170,7 +170,7 @@ void testGetVariables() {
expectedResult.put("key2", "value2");
expectedResult.put("example", null);

final Map<String, String> result = deployEnvironmentsUnderTest.getVariables(task);
final Map<String, Object> result = deployEnvironmentsUnderTest.getVariables(task, true);

// Verify the results
assertThat(result).isEqualTo(expectedResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public abstract class AbstractCredentialInfo {
*/
@Getter
@NotNull
@Schema(description = "The type of the credential,"
+ "this field is provided by he the plugin of cloud service provider.")
@Schema(description = "The type of the credential, "
+ "this field is provided by the the plugin of cloud service provider.")
CredentialType type;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@Data
public class DeployRequestBase implements Serializable {
/**
* The id of user who ordered the Service.
* The id of the user who ordered the Service.
*/
@Hidden
private String userId;
Expand Down Expand Up @@ -90,5 +90,5 @@ public class DeployRequestBase implements Serializable {
* The property of the Service.
*/
@Schema(description = "The properties for the requested service")
private Map<String, String> serviceRequestProperties;
private Map<String, Object> serviceRequestProperties;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ServiceVariablesJsonSchemaValidator {
* @param deployProperty deploy property map
*/
public void validateDeployVariables(List<DeployVariable> deployVariables,
Map<String, String> deployProperty,
Map<String, Object> deployProperty,
JsonObjectSchema jsonObjectSchema) {
if (!CollectionUtils.isEmpty(deployVariables) && !CollectionUtils.isEmpty(deployProperty)) {
try {
Expand Down

0 comments on commit ebe8114

Please sign in to comment.