use of alien4cloud.topology.task.PredefinedInputsConstraintViolationTask 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);
}
Aggregations