use of org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext in project alien4cloud by alien4cloud.
the class LocationMatchingModifier method process.
@Override
public void process(Topology topology, FlowExecutionContext context) {
// first process
processLocationMatching(topology, context);
Optional<DeploymentMatchingConfiguration> configurationOptional = context.getConfiguration(DeploymentMatchingConfiguration.class, LocationMatchingModifier.class.getSimpleName());
// perform validation
locationPolicyValidationService.validateLocationPolicies(configurationOptional.orElse(new DeploymentMatchingConfiguration())).forEach(locationPolicyTask -> context.log().error(locationPolicyTask));
// No errors from validation let's inject location topology modifiers if any.
if (context.log().isValid()) {
Map<String, Location> selectedLocations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
List<Location> locationsWithVault = selectedLocations.values().stream().filter(location -> location.getSecretProviderConfiguration() != null && location.getSecretProviderConfiguration().getConfiguration() != null).collect(Collectors.toList());
boolean needVaultCredential = locationsWithVault.size() > 0;
if (needVaultCredential) {
List<SecretCredentialInfo> secretCredentialInfos = new LinkedList<>();
for (Location location : locationsWithVault) {
try {
SecretCredentialInfo info = new SecretCredentialInfo();
String pluginName = location.getSecretProviderConfiguration().getPluginName();
Object rawSecretConfiguration = location.getSecretProviderConfiguration().getConfiguration();
secretCredentialInfos.add(secretProviderService.getSecretCredentialInfo(pluginName, rawSecretConfiguration));
} catch (Exception e) {
log.error("Cannot process secret provider configuration", e);
}
}
context.getExecutionCache().put(FlowExecutionContext.SECRET_CREDENTIAL, secretCredentialInfos);
} else {
context.getExecutionCache().remove(FlowExecutionContext.SECRET_CREDENTIAL);
}
for (LocationModifierReference modifierReference : safe(selectedLocations.values().iterator().next().getModifiers())) {
if (pluginManager.getPluginOrFail(modifierReference.getPluginId()).isEnabled()) {
injectLocationTopologyModfier(context, selectedLocations.values().iterator().next().getName(), modifierReference);
} else {
log.info("The plugin " + modifierReference.getPluginId() + " is not activated. Ignoring " + modifierReference.getBeanName() + ".");
}
}
}
}
use of org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext in project alien4cloud by alien4cloud.
the class PostMatchingNodeSetupModifierTest method testPropertiesCleanup.
@Test
public void testPropertiesCleanup() throws ParsingException {
Mockito.reset(csarRepositorySearchService);
Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-SNAPSHOT")).thenReturn(Mockito.mock(Csar.class));
NodeType mockType = Mockito.mock(NodeType.class);
Mockito.when(mockType.isAbstract()).thenReturn(true);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockType);
CapabilityType mockCapaType = Mockito.mock(CapabilityType.class);
Mockito.when(mockCapaType.getElementId()).thenReturn("tosca.capabilities.Root");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Root"), Mockito.any(Set.class))).thenReturn(mockCapaType);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get("../alien4cloud-test-common/src/test/resources/data/csars/matching-change-cleanup/tosca.yml"));
for (Entry<String, CapabilityType> capabilityTypeEntry : parsingResult.getResult().getCapabilityTypes().entrySet()) {
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(capabilityTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(capabilityTypeEntry.getValue());
}
for (Entry<String, NodeType> nodeTypeEntry : parsingResult.getResult().getNodeTypes().entrySet()) {
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(nodeTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(nodeTypeEntry.getValue());
}
try {
ToscaContext.init(Sets.newHashSet());
PostMatchingNodeSetupModifier postMatchingNodeSetupModifier = new PostMatchingNodeSetupModifier();
Topology topology = new Topology();
topology.setNodeTemplates(Maps.newHashMap());
topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplOne")));
// Configure the deployment config
DeploymentMatchingConfiguration matchingConfiguration = new DeploymentMatchingConfiguration();
matchingConfiguration.setMatchedLocationResources(Maps.newHashMap());
matchingConfiguration.getMatchedLocationResources().put("my_node", "a_location_resource");
NodePropsOverride nodePropsOverride = new NodePropsOverride();
nodePropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
nodePropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
nodePropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
nodePropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = new NodeCapabilitiesPropsOverride();
nodeCapabilitiesPropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
nodeCapabilitiesPropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
nodeCapabilitiesPropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
nodeCapabilitiesPropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
nodePropsOverride.getCapabilities().put("my_capability", nodeCapabilitiesPropsOverride);
matchingConfiguration.setMatchedNodesConfiguration(Maps.newHashMap());
matchingConfiguration.getMatchedNodesConfiguration().put("my_node", nodePropsOverride);
FlowExecutionContext mockContext = Mockito.mock(FlowExecutionContext.class);
Mockito.when(mockContext.log()).thenReturn(Mockito.mock(FlowExecutionLog.class));
Mockito.when(mockContext.getConfiguration(DeploymentMatchingConfiguration.class, AbstractPostMatchingSetupModifier.class.getSimpleName())).thenReturn(Optional.of(matchingConfiguration));
// All properties resources should be remain as matched type is compliant
postMatchingNodeSetupModifier.process(topology, mockContext);
Assert.assertEquals(4, nodePropsOverride.getProperties().size());
Assert.assertEquals(4, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
// Change the type and check that properties are cleared
topology.getNodeTemplates().clear();
topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplTwo")));
postMatchingNodeSetupModifier.process(topology, mockContext);
Assert.assertEquals(1, nodePropsOverride.getProperties().size());
Assert.assertEquals(1, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
} finally {
ToscaContext.destroy();
}
}
Aggregations