Search in sources :

Example 1 with FunctionPropertyValue

use of org.alien4cloud.tosca.model.definitions.FunctionPropertyValue in project alien4cloud by alien4cloud.

the class SetNodeCapabilityPropertyAsSecretProcessor method processNodeOperation.

@Override
protected void processNodeOperation(Csar csar, Topology topology, SetNodeCapabilityPropertyAsSecretOperation operation, NodeTemplate nodeTemplate) {
    Capability capabilityTemplate = getOrFail(nodeTemplate.getCapabilities(), operation.getCapabilityName(), "Capability {} does not exist for node {}", operation.getCapabilityName(), operation.getNodeName());
    CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capabilityTemplate.getType());
    getOrFail(capabilityType.getProperties(), operation.getPropertyName(), "Property {} do not exist for capability {} of node {}", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName());
    if (operation.getCapabilityName().equals(FORBIDDEN_CAPABILITY)) {
        throw new UnsupportedSecretException("We cannot set a secret on the capability " + operation.getCapabilityName());
    }
    if ("".equals(operation.getSecretPath())) {
        throw new InvalidSecretPathException("The secret path to the property " + operation.getPropertyName() + "is null.");
    }
    FunctionPropertyValue getSecret = new FunctionPropertyValue();
    getSecret.setFunction(ToscaFunctionConstants.GET_SECRET);
    getSecret.setParameters(Arrays.asList(operation.getSecretPath()));
    nodeTemplate.getCapabilities().get(operation.getCapabilityName()).getProperties().put(operation.getPropertyName(), getSecret);
    log.debug("Set the property [ {} ] of capability template [ {} ] of node [ {} ] to the secret path [ {} ].", operation.getPropertyName(), operation.getCapabilityName(), operation.getNodeName(), topology.getId());
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) UnsupportedSecretException(org.alien4cloud.tosca.editor.exception.UnsupportedSecretException) Capability(org.alien4cloud.tosca.model.templates.Capability) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) InvalidSecretPathException(org.alien4cloud.tosca.editor.exception.InvalidSecretPathException)

Example 2 with FunctionPropertyValue

use of org.alien4cloud.tosca.model.definitions.FunctionPropertyValue in project alien4cloud by alien4cloud.

the class SetPolicyPropertyAsSecretProcessor method process.

@Override
protected void process(Csar csar, Topology topology, SetPolicyPropertyAsSecretOperation operation, PolicyTemplate policyTemplate) {
    PolicyType policyType = ToscaContext.getOrFail(PolicyType.class, policyTemplate.getType());
    AlienUtils.getOrFail(policyType.getProperties(), operation.getPropertyName(), "Property [ {} ] doesn't exists in type [ {} ] for policy [ {} ].", operation.getPropertyName(), policyTemplate.getType(), operation.getPolicyName());
    FunctionPropertyValue secretFunction = new FunctionPropertyValue();
    secretFunction.setFunction(ToscaFunctionConstants.GET_SECRET);
    secretFunction.setParameters(Arrays.asList(operation.getSecretPath()));
    policyTemplate.getProperties().put(operation.getPropertyName(), secretFunction);
    log.debug("Associate the property [ {} ] of the policy template [ {} ] as secret [ {} ] of the topology [ {} ].", operation.getPropertyName(), operation.getPolicyName(), operation.getSecretPath(), topology.getId());
}
Also used : PolicyType(org.alien4cloud.tosca.model.types.PolicyType) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)

Example 3 with FunctionPropertyValue

use of org.alien4cloud.tosca.model.definitions.FunctionPropertyValue in project alien4cloud by alien4cloud.

the class FunctionEvaluatorTest method nodeGetSecretProp.

@Test
public void nodeGetSecretProp() {
    FunctionEvaluatorContext context = getEvaluationContext();
    NodeTemplate template = context.getTopology().getNodeTemplates().get("my_node");
    AbstractPropertyValue resolved = FunctionEvaluator.tryResolveValue(context, template, template.getProperties(), template.getProperties().get("get_secret_prop"));
    Assert.assertNotNull(resolved);
    Assert.assertEquals(FunctionPropertyValue.class, resolved.getClass());
    Assert.assertEquals("get_secret", ((FunctionPropertyValue) resolved).getFunction());
    Assert.assertEquals("my/path", ((FunctionPropertyValue) resolved).getParameters().get(0));
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 4 with FunctionPropertyValue

use of org.alien4cloud.tosca.model.definitions.FunctionPropertyValue in project alien4cloud by alien4cloud.

the class ToscaSerializerTest method buildSamplePropertyValueMap.

private Map<String, AbstractPropertyValue> buildSamplePropertyValueMap() {
    Map<String, AbstractPropertyValue> result = new HashMap<String, AbstractPropertyValue>();
    result.put("prop1", new ScalarPropertyValue("value1"));
    FunctionPropertyValue fpv1 = new FunctionPropertyValue();
    fpv1.setFunction("get_property");
    fpv1.setParameters(Lists.newArrayList("p1", "p2"));
    result.put("prop2", fpv1);
    FunctionPropertyValue fpv2 = new FunctionPropertyValue();
    fpv2.setFunction("get_input");
    fpv2.setParameters(Lists.newArrayList("p1"));
    result.put("prop3", fpv2);
    result.put("prop4", null);
    result.put("prop5", new ScalarPropertyValue("a value containing a ["));
    result.put("prop6", new ScalarPropertyValue("a value containing a ]"));
    result.put("prop7", new ScalarPropertyValue("a value containing a {"));
    result.put("prop8", new ScalarPropertyValue("a value containing a }"));
    result.put("prop9", new ScalarPropertyValue("a value containing a :"));
    result.put("prop9", new ScalarPropertyValue("a value containing a \""));
    result.put("prop9", new ScalarPropertyValue("a value containing a : and a \""));
    result.put("prop10", new ScalarPropertyValue(" a value starting with a space"));
    result.put("prop11", new ScalarPropertyValue("a value ending with a space "));
    return result;
}
Also used : HashMap(java.util.HashMap) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 5 with FunctionPropertyValue

use of org.alien4cloud.tosca.model.definitions.FunctionPropertyValue in project alien4cloud by alien4cloud.

the class FunctionEvaluator method parseAttribute.

/**
 * Parse an attribute value that can be : {@link ConcatPropertyValue} / {@link AttributeDefinition}
 *
 * @param attributeId
 * @param attributeValue
 * @param topology
 * @param runtimeInformations
 * @param currentInstance
 * @param basePaaSTemplate
 * @param builtPaaSTemplates
 * @return
 */
public static String parseAttribute(String attributeId, IValue attributeValue, Topology topology, Map<String, Map<String, InstanceInformation>> runtimeInformations, String currentInstance, IPaaSTemplate<? extends AbstractToscaType> basePaaSTemplate, Map<String, PaaSNodeTemplate> builtPaaSTemplates) {
    if (attributeValue == null) {
        return null;
    }
    // handle AttributeDefinition type
    if (attributeValue instanceof AttributeDefinition) {
        String runtimeAttributeValue = extractRuntimeInformationAttribute(runtimeInformations, currentInstance, Lists.newArrayList(basePaaSTemplate), attributeId);
        if (runtimeAttributeValue != null) {
            if (!runtimeAttributeValue.contains("=Error!]") && !runtimeAttributeValue.equals("")) {
                return runtimeAttributeValue;
            }
        }
        return ((AttributeDefinition) attributeValue).getDefault();
    }
    // handle concat function
    if (attributeValue instanceof ConcatPropertyValue) {
        StringBuilder evaluatedAttribute = new StringBuilder();
        ConcatPropertyValue concatPropertyValue = (ConcatPropertyValue) attributeValue;
        for (IValue concatParam : concatPropertyValue.getParameters()) {
            // scalar type
            if (concatParam instanceof ScalarPropertyValue) {
                // scalar case
                evaluatedAttribute.append(((ScalarPropertyValue) concatParam).getValue());
            } else if (concatParam instanceof PropertyDefinition) {
                // Definition case
                // TODO : ?? what should i do here ?? currently returns default value in the definition
                evaluatedAttribute.append(((PropertyDefinition) concatParam).getDefault());
            } else if (concatParam instanceof FunctionPropertyValue) {
                // Function case
                FunctionPropertyValue functionPropertyValue = (FunctionPropertyValue) concatParam;
                List<? extends IPaaSTemplate> paasTemplates = getPaaSTemplatesFromKeyword(basePaaSTemplate, functionPropertyValue.getTemplateName(), builtPaaSTemplates);
                switch(functionPropertyValue.getFunction()) {
                    case ToscaFunctionConstants.GET_ATTRIBUTE:
                        evaluatedAttribute.append(extractRuntimeInformationAttribute(runtimeInformations, currentInstance, paasTemplates, functionPropertyValue.getElementNameToFetch()));
                        break;
                    case ToscaFunctionConstants.GET_PROPERTY:
                        evaluatedAttribute.append(extractRuntimeInformationProperty(topology, functionPropertyValue.getElementNameToFetch(), paasTemplates));
                        break;
                    case ToscaFunctionConstants.GET_OPERATION_OUTPUT:
                        String defaultValue = "<" + functionPropertyValue.getElementNameToFetch() + ">";
                        evaluatedAttribute.append(extractRuntimeInformationOperationOutput(runtimeInformations, currentInstance, paasTemplates, functionPropertyValue, defaultValue));
                        break;
                    default:
                        log.warn("Function [{}] is not yet handled in concat operation.", functionPropertyValue.getFunction());
                        break;
                }
            }
        }
        return evaluatedAttribute.toString();
    }
    // handle functions. For now, only support Get_OPERATION_OUTPUT on attributes scope
    if (attributeValue instanceof FunctionPropertyValue) {
        FunctionPropertyValue function = (FunctionPropertyValue) attributeValue;
        switch(function.getFunction()) {
            case ToscaFunctionConstants.GET_OPERATION_OUTPUT:
                List<? extends IPaaSTemplate> paasTemplates = getPaaSTemplatesFromKeyword(basePaaSTemplate, function.getTemplateName(), builtPaaSTemplates);
                return extractRuntimeInformationOperationOutput(runtimeInformations, currentInstance, paasTemplates, function, null);
            default:
                return null;
        }
    }
    return null;
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) AttributeDefinition(org.alien4cloud.tosca.model.definitions.AttributeDefinition) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Aggregations

FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)37 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)12 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)10 Map (java.util.Map)7 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)6 And (cucumber.api.java.en.And)5 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)5 When (cucumber.api.java.en.When)4 RestResponse (alien4cloud.rest.model.RestResponse)3 TopologyDTO (alien4cloud.topology.TopologyDTO)3 JavaType (com.fasterxml.jackson.databind.JavaType)3 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)3 Capability (org.alien4cloud.tosca.model.templates.Capability)3 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)3 DeploymentTopologyDTO (alien4cloud.deployment.DeploymentTopologyDTO)2 NotFoundException (alien4cloud.exception.NotFoundException)2 ParsingError (alien4cloud.tosca.parser.ParsingError)2 ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)2