Skip to content

Commit

Permalink
Extend OCL to provide auto-fill for deploy variables
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Feb 22, 2024
1 parent af6b0eb commit 9c7a9fe
Show file tree
Hide file tree
Showing 10 changed files with 1,311 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ public static DeployResourceKind getByValue(String kind) {
public String toValue() {
return this.kind;
}

/**
* For XpanseResourceKind deserialize.
*/
public DeployResourceKind getParent() {
return this.parent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.modules.models.servicetemplate;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.eclipse.xpanse.modules.models.service.deploy.enums.DeployResourceKind;

/**
* Define autofill properties for deployment variables.
*/
@Data
public class AutoFill {

@NotNull
@Schema(description = "Type of the cloud resource to be reused.")
private DeployResourceKind deployResourceKind;

@NotNull
@Schema(description = " defines if the required cloud resource can be newly created "
+ "or should the existing resources must only be used.")
private Boolean isAllowCreate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ public class DeployVariable implements Serializable {

@Schema(description = "Sensitive scope of variable storage")
private SensitiveScope sensitiveScope = SensitiveScope.NONE;

@Schema(description = "Variable autofill")
private AutoFill autoFill;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DeployVariableTest {
private static final Map<String,Object> validatorMap = Map.of("minLength","10");
private static final SensitiveScope sensitiveScope = SensitiveScope.ONCE;
private static DeployVariable deployVariable;
private static AutoFill autoFill = null;

@BeforeEach
void setUp() {
Expand All @@ -44,6 +45,7 @@ void setUp() {
deployVariable.setMandatory(mandatory);
deployVariable.setValueSchema(validatorMap);
deployVariable.setSensitiveScope(sensitiveScope);
deployVariable.setAutoFill(autoFill);
}

@Test
Expand Down Expand Up @@ -131,6 +133,12 @@ void testEqualsAndHashCode() {
assertNotEquals(deployVariable1, deployVariable2);
assertEquals(deployVariable.hashCode(), deployVariable1.hashCode());
assertNotEquals(deployVariable1.hashCode(), deployVariable2.hashCode());

deployVariable1.setAutoFill(autoFill);
assertEquals(deployVariable, deployVariable1);
assertNotEquals(deployVariable1, deployVariable2);
assertEquals(deployVariable.hashCode(), deployVariable1.hashCode());
assertNotEquals(deployVariable1.hashCode(), deployVariable2.hashCode());
}

@Test
Expand All @@ -145,6 +153,7 @@ void testToString() {
", mandatory=" + mandatory + "" +
", valueSchema=" + validatorMap + "" +
", sensitiveScope=" + sensitiveScope + "" +
", autoFill=" + autoFill + "" +
")";
assertEquals(expectedString, deployVariable.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.xpanse.modules.orchestrator.deployment.DeploymentScriptValidationResult;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.eclipse.xpanse.modules.security.common.CurrentUserInfo;
import org.eclipse.xpanse.modules.servicetemplate.utils.DeployVariableAutoFillValidator;
import org.eclipse.xpanse.modules.servicetemplate.utils.IconProcessorUtil;
import org.eclipse.xpanse.modules.servicetemplate.utils.ServiceTemplateOpenApiGenerator;
import org.springframework.security.access.AccessDeniedException;
Expand Down Expand Up @@ -183,6 +184,8 @@ public ServiceTemplateEntity registerServiceTemplate(Ocl ocl) {
log.error(errorMsg);
throw new ServiceTemplateAlreadyRegistered(errorMsg);
}
DeployVariableAutoFillValidator.validateDeployVariableAutoFill(
newEntity.getOcl().getDeployment().getVariables());
JsonObjectSchema jsonObjectSchema =
serviceVariablesJsonSchemaGenerator.buildJsonObjectSchema(
newEntity.getOcl().getDeployment().getVariables());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.modules.servicetemplate.utils;

import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.modules.models.response.ResultType;
import org.eclipse.xpanse.modules.models.service.deploy.enums.DeployResourceKind;
import org.eclipse.xpanse.modules.models.servicetemplate.DeployVariable;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.InvalidValueSchemaException;

/**
* Deployment variables automatically fill in the verification class.
*/
@Slf4j
public class DeployVariableAutoFillValidator {

/**
* Define a method to verify automatic filling of deployment variables.
*
* @param deployVariables deployVariables.
*/
public static void validateDeployVariableAutoFill(List<DeployVariable> deployVariables) {
if (!hasAutoFillAndParentKind(deployVariables)) {
log.error("variable schema definition invalid");
throw new InvalidValueSchemaException(
List.of(ResultType.VARIABLE_SCHEMA_DEFINITION_INVALID.toValue()));
}
return;
}

private static boolean hasAutoFillAndParentKind(List<DeployVariable> deployVariables) {
for (DeployVariable deployVariable : deployVariables) {
if (Objects.nonNull(deployVariable.getAutoFill())) {
DeployResourceKind currentResourceKind =
deployVariable.getAutoFill().getDeployResourceKind();
DeployResourceKind parentKind = currentResourceKind.getParent();
if (Objects.nonNull(parentKind)) {
for (DeployVariable otherVariable : deployVariables) {
if (otherVariable != deployVariable
&& Objects.nonNull(otherVariable.getAutoFill())
&& otherVariable.getAutoFill().getDeployResourceKind()
== parentKind) {
return true;
}
}
return false;
}
}
}
return true;
}

}

0 comments on commit 9c7a9fe

Please sign in to comment.