Search in sources :

Example 11 with FlowExecutionContext

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() + ".");
            }
        }
    }
}
Also used : LocationModifierReference(alien4cloud.model.orchestrators.locations.LocationModifierReference) LocationService(alien4cloud.orchestrators.locations.services.LocationService) PluginManager(alien4cloud.plugin.PluginManager) MetaPropertiesService(alien4cloud.common.MetaPropertiesService) LocationPolicyValidationService(alien4cloud.topology.validation.LocationPolicyValidationService) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) SecretCredentialInfo(org.alien4cloud.alm.deployment.configuration.model.SecretCredentialInfo) LocationMatchingService(alien4cloud.deployment.matching.services.location.LocationMatchingService) Lists(com.google.common.collect.Lists) Map(java.util.Map) SecretProviderService(org.alien4cloud.secret.services.SecretProviderService) LinkedList(java.util.LinkedList) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) ILocationMatch(alien4cloud.model.deployment.matching.ILocationMatch) ToscaContext(alien4cloud.tosca.context.ToscaContext) MapUtils(org.apache.commons.collections4.MapUtils) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MissingPluginException(alien4cloud.plugin.exception.MissingPluginException) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) List(java.util.List) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Optional(java.util.Optional) Topology(org.alien4cloud.tosca.model.templates.Topology) SecretCredentialInfo(org.alien4cloud.alm.deployment.configuration.model.SecretCredentialInfo) LinkedList(java.util.LinkedList) MissingPluginException(alien4cloud.plugin.exception.MissingPluginException) LocationModifierReference(alien4cloud.model.orchestrators.locations.LocationModifierReference) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) Map(java.util.Map) Location(alien4cloud.model.orchestrators.locations.Location)

Example 12 with FlowExecutionContext

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();
    }
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) FlowExecutionLog(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionLog) Test(org.junit.Test)

Aggregations

FlowExecutionContext (org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext)12 ITopologyModifier (org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier)8 Topology (org.alien4cloud.tosca.model.templates.Topology)7 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)6 Map (java.util.Map)6 EnvironmentContext (org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext)6 DeploymentMatchingConfiguration (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration)6 Set (java.util.Set)5 NotFoundException (alien4cloud.exception.NotFoundException)4 List (java.util.List)4 Inject (javax.inject.Inject)4 Csar (org.alien4cloud.tosca.model.Csar)4 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)3 ToscaContext (alien4cloud.tosca.context.ToscaContext)3 Maps (com.google.common.collect.Maps)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 Component (org.springframework.stereotype.Component)3 ApplicationService (alien4cloud.application.ApplicationService)2 Application (alien4cloud.model.application.Application)2