use of org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration in project alien4cloud by alien4cloud.
the class InputsModifier method process.
@Override
public void process(Topology topology, FlowExecutionContext context) {
EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Input modifier requires an environment context."));
ApplicationEnvironment environment = environmentContext.getEnvironment();
DeploymentInputs deploymentInputs = context.getConfiguration(DeploymentInputs.class, InputsModifier.class.getSimpleName()).orElse(new DeploymentInputs(environment.getTopologyVersion(), environment.getId()));
if (deploymentInputs.getInputs() == null) {
deploymentInputs.setInputs(Maps.newHashMap());
}
Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
Map<String, PropertyValue> applicationInputs = inputService.getAppContextualInputs(context.getEnvironmentContext().get().getApplication(), topology.getInputs());
Map<String, PropertyValue> locationInputs = inputService.getLocationContextualInputs(locations, topology.getInputs());
// If the initial topology or any modifier configuration has changed since last inputs update then we refresh inputs.
if (deploymentInputs.getLastUpdateDate() == null || deploymentInputs.getLastUpdateDate().before(context.getLastFlowParamUpdate())) {
// FIXME exclude the application and location provided inputs from this method as it process them...
boolean updated = deploymentInputService.synchronizeInputs(topology.getInputs(), deploymentInputs.getInputs());
if (updated) {
// save the config if changed. This is for ex, if an input has been deleted from the topology
context.saveConfiguration(deploymentInputs);
}
}
PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputsModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
Map<String, AbstractPropertyValue> inputValues = Maps.newHashMap(deploymentInputs.getInputs());
inputValues.putAll(applicationInputs);
inputValues.putAll(locationInputs);
inputValues.putAll(preconfiguredInputsConfiguration.getInputs());
// Now that we have inputs ready let's process get_input functions in the topology to actually replace values.
if (topology.getNodeTemplates() != null) {
FunctionEvaluatorContext evaluatorContext = new FunctionEvaluatorContext(topology, inputValues);
for (Entry<String, NodeTemplate> entry : topology.getNodeTemplates().entrySet()) {
NodeTemplate nodeTemplate = entry.getValue();
processGetInput(evaluatorContext, nodeTemplate, nodeTemplate.getProperties());
if (nodeTemplate.getRelationships() != null) {
for (Entry<String, RelationshipTemplate> relEntry : nodeTemplate.getRelationships().entrySet()) {
RelationshipTemplate relationshipTemplate = relEntry.getValue();
processGetInput(evaluatorContext, relationshipTemplate, relationshipTemplate.getProperties());
}
}
if (nodeTemplate.getCapabilities() != null) {
for (Entry<String, Capability> capaEntry : nodeTemplate.getCapabilities().entrySet()) {
Capability capability = capaEntry.getValue();
processGetInput(evaluatorContext, nodeTemplate, capability.getProperties());
}
}
if (nodeTemplate.getRequirements() != null) {
for (Entry<String, Requirement> requirementEntry : nodeTemplate.getRequirements().entrySet()) {
Requirement requirement = requirementEntry.getValue();
processGetInput(evaluatorContext, nodeTemplate, requirement.getProperties());
}
}
}
}
}
use of org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration in project alien4cloud by alien4cloud.
the class DeploymentTopologyDTOBuilder method build.
/**
* Create a deployment topology dto from the context of the execution of a deployment flow.
*
* @param executionContext The deployment flow execution context.
* @return The deployment topology.
*/
private DeploymentTopologyDTO build(FlowExecutionContext executionContext) {
// re-create the deployment topology object for api compatibility purpose
DeploymentTopology deploymentTopology = new DeploymentTopology();
ReflectionUtil.mergeObject(executionContext.getTopology(), deploymentTopology);
deploymentTopology.setInitialTopologyId(executionContext.getTopology().getId());
deploymentTopology.setEnvironmentId(executionContext.getEnvironmentContext().get().getEnvironment().getId());
deploymentTopology.setVersionId(executionContext.getEnvironmentContext().get().getEnvironment().getTopologyVersion());
DeploymentTopologyDTO deploymentTopologyDTO = new DeploymentTopologyDTO();
topologyDTOBuilder.initTopologyDTO(deploymentTopology, deploymentTopologyDTO);
// Convert log result to validation result.
TopologyValidationResult validationResult = new TopologyValidationResult();
for (AbstractTask task : executionContext.getLog().getInfos()) {
validationResult.addInfo(task);
}
for (AbstractTask task : executionContext.getLog().getWarnings()) {
validationResult.addWarning(task);
}
for (AbstractTask task : executionContext.getLog().getErrors()) {
validationResult.addTask(task);
}
validationResult.setValid(validationResult.getTaskList() == null || validationResult.getTaskList().isEmpty());
deploymentTopologyDTO.setValidation(validationResult);
Optional<PreconfiguredInputsConfiguration> preconfiguredInputsConfiguration = executionContext.getConfiguration(PreconfiguredInputsConfiguration.class, DeploymentTopologyDTOBuilder.class.getSimpleName());
if (!preconfiguredInputsConfiguration.isPresent()) {
deploymentTopology.setPreconfiguredInputProperties(Maps.newHashMap());
} else {
deploymentTopology.setPreconfiguredInputProperties(preconfiguredInputsConfiguration.get().getInputs());
}
Optional<DeploymentInputs> inputsOptional = executionContext.getConfiguration(DeploymentInputs.class, DeploymentTopologyDTOBuilder.class.getSimpleName());
if (!inputsOptional.isPresent()) {
deploymentTopology.setDeployerInputProperties(Maps.newHashMap());
deploymentTopology.setUploadedInputArtifacts(Maps.newHashMap());
} else {
deploymentTopology.setDeployerInputProperties(inputsOptional.get().getInputs());
deploymentTopology.setUploadedInputArtifacts(inputsOptional.get().getInputArtifacts());
}
Optional<DeploymentMatchingConfiguration> matchingConfigurationOptional = executionContext.getConfiguration(DeploymentMatchingConfiguration.class, DeploymentTopologyDTOBuilder.class.getSimpleName());
if (!matchingConfigurationOptional.isPresent()) {
return deploymentTopologyDTO;
}
DeploymentMatchingConfiguration matchingConfiguration = matchingConfigurationOptional.get();
deploymentTopology.setOrchestratorId(matchingConfiguration.getOrchestratorId());
deploymentTopology.setLocationGroups(matchingConfiguration.getLocationGroups());
deploymentTopologyDTO.setLocationPolicies(matchingConfiguration.getLocationIds());
// Good enough approximation as it doesn't contains just the location dependencies.
deploymentTopology.setLocationDependencies(executionContext.getTopology().getDependencies());
DeploymentSubstitutionConfiguration substitutionConfiguration = new DeploymentSubstitutionConfiguration();
substitutionConfiguration.setSubstitutionTypes(new LocationResourceTypes());
// fill DTO with policies substitution stuffs
fillDTOWithPoliciesSubstitutionConfiguration(executionContext, deploymentTopology, deploymentTopologyDTO, matchingConfiguration, substitutionConfiguration);
// fill DTO with nodes substitution stuffs
fillDTOWithNodesSubstitutionConfiguration(executionContext, deploymentTopology, deploymentTopologyDTO, matchingConfiguration, substitutionConfiguration);
deploymentTopologyDTO.setAvailableSubstitutions(substitutionConfiguration);
ApplicationEnvironment environment = executionContext.getEnvironmentContext().get().getEnvironment();
OrchestratorDeploymentProperties orchestratorDeploymentProperties = executionContext.getConfiguration(OrchestratorDeploymentProperties.class, this.getClass().getSimpleName()).orElse(new OrchestratorDeploymentProperties(environment.getTopologyVersion(), environment.getId(), matchingConfiguration.getOrchestratorId()));
deploymentTopology.setProviderDeploymentProperties(orchestratorDeploymentProperties.getProviderDeploymentProperties());
deploymentTopologyDTO.setSecretCredentialInfos((List<SecretCredentialInfo>) executionContext.getExecutionCache().get(FlowExecutionContext.SECRET_CREDENTIAL));
return deploymentTopologyDTO;
}
use of org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration 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);
}
use of org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration in project alien4cloud by alien4cloud.
the class InputValidationModifier method process.
/**
* Validate all required input is provided with a non null value.
*
* @param topology The topology to process.
* @param context The object that stores warnings and errors (tasks) associated with the execution flow. Note that the flow will end-up if an error
*/
@Override
public void process(Topology topology, FlowExecutionContext context) {
Optional<DeploymentInputs> inputsOptional = context.getConfiguration(DeploymentInputs.class, InputValidationModifier.class.getSimpleName());
PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputValidationModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
// Define a task regarding properties
PropertiesTask task = new PropertiesTask();
task.setCode(TaskCode.INPUT_PROPERTY);
task.setProperties(Maps.newHashMap());
task.getProperties().put(TaskLevel.REQUIRED, Lists.newArrayList());
Map<String, AbstractPropertyValue> inputValues = safe(inputsOptional.orElse(new DeploymentInputs()).getInputs());
Map<String, PropertyValue> predefinedInputValues = safe(preconfiguredInputsConfiguration.getInputs());
// override deployer inputValues with predefinedInputValues
inputValues = Maps.newHashMap(inputValues);
inputValues.putAll(predefinedInputValues);
for (Entry<String, PropertyDefinition> propDef : safe(topology.getInputs()).entrySet()) {
if (propDef.getValue().isRequired() && inputValues.get(propDef.getKey()) == null) {
task.getProperties().get(TaskLevel.REQUIRED).add(propDef.getKey());
}
}
if (CollectionUtils.isNotEmpty(task.getProperties().get(TaskLevel.REQUIRED))) {
context.log().error(task);
}
// Check input artifacts
deploymentInputArtifactValidationService.validate(topology, inputsOptional.orElse(new DeploymentInputs())).forEach(inputArtifactTask -> context.log().error(inputArtifactTask));
}
Aggregations