Search in sources :

Example 21 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration 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)

Example 22 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration in project alien4cloud by alien4cloud.

the class DeploymentConfigurationDaoTest method migrate_data_previously_store_in_elastic_search.

@Test
public void migrate_data_previously_store_in_elastic_search() throws Exception {
    String existingId = AbstractDeploymentConfig.generateId("versionOK", "envId");
    // given a configuration store in ES
    when(alienDao.findById(DeploymentMatchingConfiguration.class, existingId)).thenReturn(new DeploymentMatchingConfiguration("versionOK", "envId"));
    // given the location of the existing config is defined
    Path configLocalPath = Paths.get("target/deployment_config_dao_test/config");
    Files.deleteIfExists(configLocalPath);
    when(localGitRepositoryPathResolver.resolve(eq(DeploymentMatchingConfiguration.class), eq(existingId))).thenReturn(configLocalPath);
    // when looking for a config that not existing in Git yet
    DeploymentMatchingConfiguration config = dao.findById(DeploymentMatchingConfiguration.class, existingId);
    // then we get data from ES
    assertThat(config).isNotNull();
    assertThat(config.getId()).isEqualTo(existingId);
    // ES data has been deleted
    verify(alienDao).delete(DeploymentMatchingConfiguration.class, existingId);
    // check data has been migrated into git
    assertThat(Files.exists(configLocalPath)).isTrue();
}
Also used : Path(java.nio.file.Path) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 23 with DeploymentMatchingConfiguration

use of org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration in project alien4cloud by alien4cloud.

the class BlockStorageEventHandler method updateApplicationTopology.

private void updateApplicationTopology(PaaSInstancePersistentResourceMonitorEvent persistentResourceEvent, final Map<String, Object> persistentProperties) {
    Deployment deployment = deploymentService.get(persistentResourceEvent.getDeploymentId());
    String topologyId = deployment.getSourceId() + ":" + deployment.getVersionId();
    Topology topology = topoServiceCore.getOrFail(topologyId);
    // The deployment topology may have changed and the node removed, in such situations there is nothing to update as the block won't be reused.
    NodeTemplate nodeTemplate;
    try {
        nodeTemplate = TopologyUtils.getNodeTemplate(topology, persistentResourceEvent.getNodeTemplateId());
    } catch (NotFoundException e) {
        log.warn("Fail to update persistent resource for node {}", persistentResourceEvent.getNodeTemplateId(), e);
        return;
    }
    for (String propertyName : persistentProperties.keySet()) {
        Object propertyValue = persistentProperties.get(propertyName);
        AbstractPropertyValue abstractPropertyValue = nodeTemplate.getProperties().get(propertyName);
        if (abstractPropertyValue != null && abstractPropertyValue instanceof FunctionPropertyValue) {
            // the value is set in the topology
            FunctionPropertyValue function = (FunctionPropertyValue) abstractPropertyValue;
            if (function.getFunction().equals(ToscaFunctionConstants.GET_INPUT) && propertyValue instanceof String) {
                DeploymentInputs deploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(deployment.getVersionId(), deployment.getEnvironmentId()));
                // the value is set in the input (deployment setup)
                log.info("Updating deploymentsetup [ {} ] input properties [ {} ] to add a new VolumeId", deploymentInputs.getId(), function.getTemplateName());
                log.debug("Property [ {} ] to update: [ {} ]. New value is [ {} ]", propertyName, persistentResourceEvent.getPersistentProperties().get(propertyName), propertyValue);
                deploymentInputs.getInputs().put(function.getTemplateName(), new ScalarPropertyValue((String) propertyValue));
                deploymentConfigurationDao.save(deploymentInputs);
            } else {
                // this is not supported / print a warning
                log.warn("Failed to store the id of the created block storage [ {} ] for deployment [ {} ] application [ {} ] environment [ {} ]");
                return;
            }
        } else {
            DeploymentMatchingConfiguration matchingConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(deployment.getVersionId(), deployment.getEnvironmentId()));
            log.info("Updating deployment topology: Persistent resource property [ {} ] for node template <{}.{}> to add a value", propertyName, matchingConfiguration.getId(), persistentResourceEvent.getNodeTemplateId());
            log.debug("Value to add: [ {} ]. New value is [ {} ]", persistentResourceEvent.getPersistentProperties().get(propertyName), propertyValue);
            matchingConfiguration.getMatchedNodesConfiguration().get(persistentResourceEvent.getNodeTemplateId()).getProperties().put(propertyName, getPropertyValue(propertyValue));
            deploymentConfigurationDao.save(matchingConfiguration);
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) Deployment(alien4cloud.model.deployment.Deployment) NotFoundException(alien4cloud.exception.NotFoundException) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology)

Aggregations

DeploymentMatchingConfiguration (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration)23 LocationPolicyTask (alien4cloud.topology.task.LocationPolicyTask)10 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)8 Map (java.util.Map)8 List (java.util.List)7 NotFoundException (alien4cloud.exception.NotFoundException)6 Location (alien4cloud.model.orchestrators.locations.Location)5 Set (java.util.Set)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 FlowExecutionContext (org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext)4 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)3 Maps (com.google.common.collect.Maps)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 ITopologyModifier (org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier)3 NodeMatchingConfigAutoSelectModifier (org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.NodeMatchingConfigAutoSelectModifier)3 NodePropsOverride (org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride)3 OrchestratorDeploymentProperties (org.alien4cloud.alm.deployment.configuration.model.OrchestratorDeploymentProperties)3 Csar (org.alien4cloud.tosca.model.Csar)3 MapUtils (org.apache.commons.collections4.MapUtils)3