Search in sources :

Example 16 with PropertyValue

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

the class ToscaPropertyDefaultValueConstraintsValidator method isValid.

@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
    PropertyValue defaultValue = value.getDefault();
    if (defaultValue == null) {
        // no default value is specified.
        return true;
    }
    // validate that the default value matches the defined constraints.
    IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
    if (toscaType == null) {
        return false;
    }
    if (!(defaultValue instanceof ScalarPropertyValue)) {
        // No constraint can be made on other thing than scalar values
        return false;
    }
    String defaultValueAsString = ((ScalarPropertyValue) defaultValue).getValue();
    Object parsedDefaultValue;
    try {
        parsedDefaultValue = toscaType.parse(defaultValueAsString);
    } catch (InvalidPropertyValueException e) {
        return false;
    }
    if (value.getConstraints() != null) {
        for (PropertyConstraint constraint : value.getConstraints()) {
            try {
                constraint.validate(parsedDefaultValue);
            } catch (ConstraintViolationException e) {
                return false;
            }
        }
    }
    return true;
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)

Example 17 with PropertyValue

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

the class DeploymentInputService method synchronizeInputs.

/**
 * Ensure that the specified input values matches the eventually updated input definitions.
 *
 * @param inputDefinitions Inputs definitions as specified in the topology.
 * @param inputValues Input properties values as specified by the user.
 * @return true if there is an update on inputValues (removal or addition). false if nothing has changed
 */
public boolean synchronizeInputs(Map<String, PropertyDefinition> inputDefinitions, Map<String, AbstractPropertyValue> inputValues) {
    boolean updated = false;
    if (!MapUtils.isEmpty(inputValues)) {
        // Ensure that previous defined values are still compatible with the latest input definition (as the topology may have changed).
        Iterator<Map.Entry<String, AbstractPropertyValue>> inputPropertyEntryIterator = inputValues.entrySet().iterator();
        while (inputPropertyEntryIterator.hasNext()) {
            Map.Entry<String, AbstractPropertyValue> inputPropertyEntry = inputPropertyEntryIterator.next();
            // remove if the value is null, or the input is not register as one
            if (inputPropertyEntry.getValue() == null || !safe(inputDefinitions).containsKey(inputPropertyEntry.getKey())) {
                inputPropertyEntryIterator.remove();
            } else if (!(inputPropertyEntry.getValue() instanceof FunctionPropertyValue)) {
                try {
                    ConstraintPropertyService.checkPropertyConstraint(inputPropertyEntry.getKey(), ((PropertyValue) inputPropertyEntry.getValue()).getValue(), inputDefinitions.get(inputPropertyEntry.getKey()));
                } catch (ConstraintViolationException | ConstraintValueDoNotMatchPropertyTypeException e) {
                    // Property is not valid anymore for the input, remove the old value
                    inputPropertyEntryIterator.remove();
                    updated = true;
                }
            }
        }
    }
    // set default values for every unset property.
    for (Map.Entry<String, PropertyDefinition> inputDefinitionEntry : safe(inputDefinitions).entrySet()) {
        AbstractPropertyValue existingValue = inputValues.get(inputDefinitionEntry.getKey());
        if (existingValue == null) {
            // If user has not specified a value and there is
            PropertyValue defaultValue = inputDefinitionEntry.getValue().getDefault();
            if (defaultValue != null) {
                inputValues.put(inputDefinitionEntry.getKey(), defaultValue);
                updated = true;
            }
        }
    }
    return updated;
}
Also used : FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 18 with PropertyValue

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

the class InputService method getLocationContextualInputs.

/**
 * Get input values matching the requested input definitions as defined in location meta properties.
 *
 * @param locations        The map of locations from which to fetch meta properties based inputs.
 * @param inputDefinitions The input definitions that may define request for location meta based inputs.
 * @return A map of <Input name, Property value> computed from the location meta properties.
 */
public Map<String, PropertyValue> getLocationContextualInputs(Map<String, Location> locations, Map<String, PropertyDefinition> inputDefinitions) {
    if (inputDefinitions == null) {
        return Maps.newHashMap();
    }
    Map<String, String> metaPropertiesValuesMap = Maps.newHashMap();
    for (Location location : safe(locations).values()) {
        metaPropertiesValuesMap.putAll(safe(location.getMetaProperties()));
    }
    Map<String, PropertyValue> inputs = Maps.newHashMap();
    prefixAndAddContextInput(inputDefinitions, inputs, LOC_META, metaPropertiesValuesMap, true);
    return inputs;
}
Also used : ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Location(alien4cloud.model.orchestrators.locations.Location)

Example 19 with PropertyValue

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

the class ArchiveRootPostProcessor method processRepositoriesDefinitions.

private void processRepositoriesDefinitions(Map<String, RepositoryDefinition> repositories) {
    if (MapUtils.isNotEmpty(repositories)) {
        DataType credentialType = ToscaContext.get(DataType.class, NormativeCredentialConstant.DATA_TYPE);
        repositories.values().forEach(repositoryDefinition -> {
            if (repositoryDefinition.getCredential() != null) {
                credentialType.getProperties().forEach((propertyName, propertyDefinition) -> {
                    // Fill with default value
                    if (!repositoryDefinition.getCredential().getValue().containsKey(propertyName)) {
                        AbstractPropertyValue defaultValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(propertyDefinition);
                        if (defaultValue instanceof PropertyValue) {
                            repositoryDefinition.getCredential().getValue().put(propertyName, ((PropertyValue) defaultValue).getValue());
                        }
                    }
                });
                Node credentialNode = ParsingContextExecution.getObjectToNodeMap().get(repositoryDefinition.getCredential());
                PropertyDefinition propertyDefinition = new PropertyDefinition();
                propertyDefinition.setType(NormativeCredentialConstant.DATA_TYPE);
                propertyValueChecker.checkProperty("credential", credentialNode, repositoryDefinition.getCredential(), propertyDefinition, repositoryDefinition.getId());
            }
        });
    }
}
Also used : Node(org.yaml.snakeyaml.nodes.Node) DataType(org.alien4cloud.tosca.model.types.DataType) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 20 with PropertyValue

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

the class PreconfiguredInputsModifier method process.

@Override
public void process(Topology topology, FlowExecutionContext context) {
    EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Preconfigured input modifier requires an environment context."));
    ApplicationEnvironment environment = environmentContext.getEnvironment();
    Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
    AlienContextVariables alienContextVariables = new AlienContextVariables();
    alienContextVariables.setApplicationEnvironment(environment);
    alienContextVariables.setLocation(locations.values().stream().findFirst().get());
    alienContextVariables.setApplication(environmentContext.getApplication());
    // TODO: avoid reloading every time - find a way to know the last update on files (git hash ?)
    Properties appVarProps = quickFileStorageService.loadApplicationVariables(environmentContext.getApplication().getId());
    Properties envTypeVarProps = quickFileStorageService.loadEnvironmentTypeVariables(topology.getId(), environment.getEnvironmentType());
    Properties envVarProps = quickFileStorageService.loadEnvironmentVariables(topology.getId(), environment.getId());
    Map<String, Object> inputsMappingsMap = quickFileStorageService.loadInputsMappingFile(topology.getId());
    InputsMappingFileVariableResolver.InputsResolvingResult inputsResolvingResult = InputsMappingFileVariableResolver.configure(appVarProps, envTypeVarProps, envVarProps, alienContextVariables).resolve(inputsMappingsMap, topology.getInputs());
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getMissingVariables())) {
        context.log().error(new MissingVariablesTask(inputsResolvingResult.getMissingVariables()));
    }
    if (CollectionUtils.isNotEmpty(inputsResolvingResult.getUnresolved())) {
        context.log().error(new UnresolvablePredefinedInputsTask(inputsResolvingResult.getUnresolved()));
    }
    // checking constraints
    Map<String, ConstraintUtil.ConstraintInformation> violations = Maps.newHashMap();
    Map<String, ConstraintUtil.ConstraintInformation> typesViolations = Maps.newHashMap();
    for (Map.Entry<String, PropertyValue> entry : safe(inputsResolvingResult.getResolved()).entrySet()) {
        try {
            ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), entry.getValue(), topology.getInputs().get(entry.getKey()));
        } catch (ConstraintViolationException e) {
            violations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
            typesViolations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
        }
    }
    if (MapUtils.isNotEmpty(violations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(violations, TaskCode.PREDEFINED_INPUTS_CONSTRAINT_VIOLATION));
    }
    if (MapUtils.isNotEmpty(typesViolations)) {
        context.log().error(new PredefinedInputsConstraintViolationTask(typesViolations, TaskCode.PREDEFINED_INPUTS_TYPE_VIOLATION));
    }
    PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = new PreconfiguredInputsConfiguration(environment.getTopologyVersion(), environment.getId());
    preconfiguredInputsConfiguration.setInputs(inputsResolvingResult.getResolved());
    // add unresolved so that they are not considered as deployer input
    inputsResolvingResult.getUnresolved().forEach(unresolved -> preconfiguredInputsConfiguration.getInputs().put(unresolved, null));
    // TODO: improve me
    preconfiguredInputsConfiguration.setLastUpdateDate(new Date());
    preconfiguredInputsConfiguration.setCreationDate(new Date());
    context.saveConfiguration(preconfiguredInputsConfiguration);
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PredefinedInputsConstraintViolationTask(alien4cloud.topology.task.PredefinedInputsConstraintViolationTask) PreconfiguredInputsConfiguration(org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) Properties(java.util.Properties) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Date(java.util.Date) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) AlienContextVariables(org.alien4cloud.tosca.variable.AlienContextVariables) InputsMappingFileVariableResolver(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver) UnresolvablePredefinedInputsTask(alien4cloud.topology.task.UnresolvablePredefinedInputsTask) MissingVariablesTask(alien4cloud.topology.task.MissingVariablesTask) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Aggregations

PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)28 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)20 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)17 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)16 Test (org.junit.Test)15 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)13 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)11 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)10 Map (java.util.Map)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)9 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)5 DataType (org.alien4cloud.tosca.model.types.DataType)5 Application (alien4cloud.model.application.Application)3 Location (alien4cloud.model.orchestrators.locations.Location)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Maps (com.google.common.collect.Maps)3 Arrays (java.util.Arrays)3 PreconfiguredInputsConfiguration (org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration)3 ToscaTypes (org.alien4cloud.tosca.normative.types.ToscaTypes)3 PropertiesYamlParser (org.alien4cloud.tosca.utils.PropertiesYamlParser)3