Search in sources :

Example 36 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class NodeMatchingSubstitutionService method onCopyConfiguration.

// FIXME fix this, synch with org.alien4cloud.alm.deployment.configuration.services.PolicyMatchingSubstitutionService#onCopyConfiguration
@EventListener
// Process this after location matching copy (first element).
@Order(30)
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
    DeploymentMatchingConfiguration sourceConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    DeploymentMatchingConfiguration targetConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(target.getTopologyVersion(), target.getId()));
    if (sourceConfiguration == null || MapUtils.isEmpty(sourceConfiguration.getLocationGroups()) || targetConfiguration == null || MapUtils.isEmpty(targetConfiguration.getLocationGroups())) {
        // Nothing to copy
        return;
    }
    // We have to execute a piece of the deployment flow to find out matching candidates so we copy only required inputs
    Topology topology = topologyServiceCore.getOrFail(Csar.createId(target.getApplicationId(), target.getTopologyVersion()));
    if (MapUtils.isNotEmpty(topology.getNodeTemplates())) {
        Application application = applicationService.getOrFail(target.getApplicationId());
        FlowExecutionContext executionContext = new FlowExecutionContext(deploymentConfigurationDao, topology, new EnvironmentContext(application, target));
        flowExecutor.execute(topology, getMatchingFlow(), executionContext);
        Map<String, Set<String>> locResTemplateIdsPerNodeIds = (Map<String, Set<String>>) executionContext.getExecutionCache().get(FlowExecutionContext.SELECTED_MATCH_NODE_LOCATION_TEMPLATE_BY_NODE_ID_MAP);
        // Update the substitution on the target if available substitution is always compatible
        Map<String, String> validOnNewEnvSubstitutedNodes = safe(sourceConfiguration.getMatchedLocationResources()).entrySet().stream().filter(entry -> locResTemplateIdsPerNodeIds.containsKey(entry.getKey()) && locResTemplateIdsPerNodeIds.get(entry.getKey()).contains(entry.getValue())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        if (MapUtils.isNotEmpty(validOnNewEnvSubstitutedNodes)) {
            if (targetConfiguration.getMatchedLocationResources() == null) {
                targetConfiguration.setMatchedLocationResources(Maps.newHashMap());
            }
            validOnNewEnvSubstitutedNodes.forEach((key, value) -> {
                targetConfiguration.getMatchedLocationResources().put(key, value);
                // Copy properties set on the node to the new one
                targetConfiguration.getMatchedNodesConfiguration().put(key, safe(sourceConfiguration.getMatchedNodesConfiguration()).get(key));
            });
            deploymentConfigurationDao.save(targetConfiguration);
        }
    }
}
Also used : EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) TopologyServiceCore(alien4cloud.topology.TopologyServiceCore) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Inject(javax.inject.Inject) Service(org.springframework.stereotype.Service) Map(java.util.Map) Application(alien4cloud.model.application.Application) ApplicationService(alien4cloud.application.ApplicationService) OnDeploymentConfigCopyEvent(org.alien4cloud.alm.deployment.configuration.events.OnDeploymentConfigCopyEvent) ITopologyModifier(org.alien4cloud.alm.deployment.configuration.flow.ITopologyModifier) MapUtils(org.apache.commons.collections4.MapUtils) Order(org.springframework.core.annotation.Order) NodeMatchingConfigAutoSelectModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.NodeMatchingConfigAutoSelectModifier) Csar(org.alien4cloud.tosca.model.Csar) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Set(java.util.Set) SetMatchedNodeModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.action.SetMatchedNodeModifier) EventListener(org.springframework.context.event.EventListener) AbstractDeploymentConfig(org.alien4cloud.alm.deployment.configuration.model.AbstractDeploymentConfig) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) NotFoundException(alien4cloud.exception.NotFoundException) List(java.util.List) EnvironmentContext(org.alien4cloud.alm.deployment.configuration.flow.EnvironmentContext) FlowExecutor(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutor) AbstractComposedModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.AbstractComposedModifier) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) NodeMatchingCompositeModifier(org.alien4cloud.alm.deployment.configuration.flow.modifiers.matching.NodeMatchingCompositeModifier) Topology(org.alien4cloud.tosca.model.templates.Topology) Set(java.util.Set) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Application(alien4cloud.model.application.Application) Map(java.util.Map) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 37 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class OrchestratorPropertiesService method onCopyConfiguration.

@EventListener
// This is one of the last elements to process to place it's order quite far, after location match copy anyway.
@Order(40)
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
    DeploymentMatchingConfiguration sourceMatchingConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    if (sourceMatchingConfiguration == null || MapUtils.isEmpty(sourceMatchingConfiguration.getLocationIds())) {
        return;
    }
    DeploymentMatchingConfiguration targetMatchingConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    if (targetMatchingConfiguration == null || MapUtils.isEmpty(targetMatchingConfiguration.getLocationIds()) || !sourceMatchingConfiguration.getOrchestratorId().equals(targetMatchingConfiguration.getOrchestratorId())) {
        // If the target does not have the same orchestrator as the source then don't copy the inputs
        return;
    }
    OrchestratorDeploymentProperties sourceProperties = deploymentConfigurationDao.findById(OrchestratorDeploymentProperties.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    if (sourceProperties == null || MapUtils.isEmpty(sourceProperties.getProviderDeploymentProperties())) {
        return;
    }
    OrchestratorDeploymentProperties targetProperties = new OrchestratorDeploymentProperties(target.getTopologyVersion(), target.getId(), sourceProperties.getOrchestratorId());
    targetProperties.setProviderDeploymentProperties(sourceProperties.getProviderDeploymentProperties());
    deploymentConfigurationDao.save(targetProperties);
}
Also used : DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) OrchestratorDeploymentProperties(org.alien4cloud.alm.deployment.configuration.model.OrchestratorDeploymentProperties) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 38 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class DeployService method generateOrchestratorDeploymentId.

/**
 * Generate the human readable deployment id for the orchestrator.
 *
 * @param envId Id of the deployed environment.
 * @param orchestratorId Id of the orchestrator on which the deployment is performed.
 * @return The orchestrator deployment id.
 * @throws alien4cloud.paas.exception.OrchestratorDeploymentIdConflictException
 */
private String generateOrchestratorDeploymentId(String envId, String orchestratorId) throws OrchestratorDeploymentIdConflictException {
    log.debug("Generating deployment paaS Id...");
    log.debug("All spaces will be replaced by an \"_\" charactr. You might consider it while naming your applications.");
    ApplicationEnvironment env = applicationEnvironmentService.getOrFail(envId);
    Orchestrator orchestrator = orchestratorService.getOrFail(orchestratorId);
    String namePattern = orchestrator.getDeploymentNamePattern();
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(namePattern);
    String orchestratorDeploymentId = (String) exp.getValue(new OrchestratorIdContext(env, applicationService.getOrFail(env.getApplicationId()), namePattern.contains("metaProperties[")));
    // ensure that the id is not used by another deployment.
    if (deploymentService.isActiveDeployment(orchestratorId, orchestratorDeploymentId)) {
        throw new OrchestratorDeploymentIdConflictException("Conflict detected with the generated paasId <" + orchestratorDeploymentId + ">.");
    }
    return orchestratorDeploymentId;
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) OrchestratorDeploymentIdConflictException(alien4cloud.paas.exception.OrchestratorDeploymentIdConflictException) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Orchestrator(alien4cloud.model.orchestrators.Orchestrator)

Example 39 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class ApplicationEnvironmentService method getEnvironmentByIdOrDefault.

/**
 * Get a environment for and application
 *
 * @param applicationId
 * @param applicationEnvironmentId
 * @return
 */
public ApplicationEnvironment getEnvironmentByIdOrDefault(String applicationId, String applicationEnvironmentId) {
    ApplicationEnvironment environment = null;
    if (applicationEnvironmentId == null) {
        ApplicationEnvironment[] applicationEnvironments = getByApplicationId(applicationId);
        environment = applicationEnvironments[0];
    } else {
        environment = getOrFail(applicationEnvironmentId);
    }
    return environment;
}
Also used : ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment)

Example 40 with ApplicationEnvironment

use of alien4cloud.model.application.ApplicationEnvironment in project alien4cloud by alien4cloud.

the class ApplicationEnvironmentService method checkAndGetApplicationEnvironment.

/**
 * Check rights on the related application and get the application environment
 * If no roles mentioned, all {@link ApplicationRole} values will be used
 *
 * @param applicationEnvironmentId application's environment id
 * @param roles {@link ApplicationRole} to check right on the underlying application
 * @return the corresponding application environment
 */
public ApplicationEnvironment checkAndGetApplicationEnvironment(String applicationEnvironmentId, IResourceRoles... roles) {
    ApplicationEnvironment applicationEnvironment = getOrFail(applicationEnvironmentId);
    // Is the user allowed access (application, environment) level ?
    if (AuthorizationUtil.hasAuthorization(applicationEnvironment, Role.ADMIN, roles)) {
        return applicationEnvironment;
    }
    // is the user allowed to access at the application level
    applicationService.checkAndGetApplication(applicationEnvironment.getApplicationId(), roles);
    return applicationEnvironment;
}
Also used : ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment)

Aggregations

ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)82 ApiOperation (io.swagger.annotations.ApiOperation)42 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)42 Application (alien4cloud.model.application.Application)40 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)35 Audit (alien4cloud.audit.annotation.Audit)27 List (java.util.List)17 Collectors (java.util.stream.Collectors)16 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)15 RestResponse (alien4cloud.rest.model.RestResponse)15 Topology (org.alien4cloud.tosca.model.templates.Topology)15 Set (java.util.Set)14 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)13 NotFoundException (alien4cloud.exception.NotFoundException)13 Map (java.util.Map)13 Resource (javax.annotation.Resource)12 ApplicationTopologyVersion (alien4cloud.model.application.ApplicationTopologyVersion)11 Deployment (alien4cloud.model.deployment.Deployment)11 Arrays (java.util.Arrays)11 Location (alien4cloud.model.orchestrators.locations.Location)10