Search in sources :

Example 31 with ApplicationEnvironment

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

the class ApplicationVersionService method update.

/**
 * Update an application version and all it's topology versions.
 *
 * @param applicationVersionId The application version for which to update all versions.
 * @param newVersion The updated version number.
 * @param newDescription The new description.
 */
public void update(String applicationId, String applicationVersionId, String newVersion, String newDescription) {
    ApplicationVersion applicationVersion = getOrFail(applicationVersionId);
    if (!applicationId.equals(applicationVersion.getApplicationId())) {
        throw new AuthorizationServiceException("It is not authorize to change an application with a wrong application id: request application id [" + applicationId + "] version application id: [" + applicationVersion.getApplicationId() + "]");
    }
    if (newDescription != null) {
        applicationVersion.setDescription(newDescription);
    }
    if (newVersion != null && !applicationVersion.getVersion().equals(newVersion)) {
        // if the version is a release it is not possible to change it's version number
        if (applicationVersion.isReleased()) {
            throw new UpdateApplicationVersionException("The application version " + applicationVersion.getId() + " is released and cannot be update.");
        }
        if (applicationVersionNameExists(applicationVersion.getApplicationId(), newVersion)) {
            throw new AlreadyExistException("An application version already exist for this application with the version :" + newVersion);
        }
        ApplicationEnvironment[] relatedEnvironments = findAllApplicationVersionUsage(applicationVersion.getApplicationId(), applicationVersion.getVersion());
        if (ArrayUtils.isNotEmpty(relatedEnvironments)) {
            // should fal the update if linked to deployed environment
            failIfAnyEnvironmentDeployed(relatedEnvironments);
            // Should fail the update if exposed as a service
            failIfAnyEnvironmentExposedAsService(applicationId, relatedEnvironments);
        }
        // When changing the version number we actually perform a full version creation and then delete the previous one.
        ApplicationVersion newApplicationVersion = new ApplicationVersion();
        newApplicationVersion.setVersion(newVersion);
        newApplicationVersion.setNestedVersion(VersionUtil.parseVersion(newVersion));
        newApplicationVersion.setDescription(applicationVersion.getDescription());
        newApplicationVersion.setApplicationId(applicationVersion.getApplicationId());
        newApplicationVersion.setReleased(!VersionUtil.isSnapshot(newVersion));
        newApplicationVersion.setTopologyVersions(Maps.newHashMap());
        importTopologiesFromPreviousVersion(newApplicationVersion, applicationVersion);
        // save the new version
        alienDAO.save(newApplicationVersion);
        resourceUpdateInterceptor.runOnTopologyVersionReleased(new TopologyVersionUpdated(applicationVersion, newApplicationVersion));
        // update topology versions on related objects: (environments, deploymentTopologies)
        updateTopologyVersion(relatedEnvironments, applicationVersion, newApplicationVersion);
        // delete the previous version
        deleteVersion(applicationVersion);
    } else {
        // save the new version
        alienDAO.save(applicationVersion);
    }
}
Also used : ApplicationVersion(alien4cloud.model.application.ApplicationVersion) TopologyVersionUpdated(alien4cloud.common.ResourceUpdateInterceptor.TopologyVersionUpdated) AuthorizationServiceException(org.springframework.security.access.AuthorizationServiceException) UpdateApplicationVersionException(alien4cloud.utils.version.UpdateApplicationVersionException) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment)

Example 32 with ApplicationEnvironment

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

the class ManagedServiceResourceService method checkAndGetApplicationEnvironment.

/**
 * Get application environment, checks for DEPLOYMENT_MANAGEMENT rights on it.
 *
 * @param environmentId
 * @return the environment if the current user has the proper rights on it
 * @throws java.nio.file.AccessDeniedException if the current user doesn't have proper rights on the requested environment
 */
private ApplicationEnvironment checkAndGetApplicationEnvironment(String environmentId) {
    ApplicationEnvironment environment = environmentService.getOrFail(environmentId);
    Application application = applicationService.getOrFail(environment.getApplicationId());
    // Only a user with deployment rĂ´le on the environment can create an associated service.
    AuthorizationUtil.checkAuthorizationForEnvironment(application, environment);
    return environment;
}
Also used : ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Application(alien4cloud.model.application.Application)

Example 33 with ApplicationEnvironment

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

the class ManagedServiceResourceService method create.

/**
 * Create a Service resource associated with the given environment.
 *
 * @param environmentId The environment to create a service for, the service version will be the one of the environment current associated version.
 * @param serviceName The name of the service as it should appears.
 * @param fromRuntime If we should try to create the service from the runtime topology related to the environment.
 * @return the id of the created service
 *
 * @throws AlreadyExistException if a service with the given name, or related to the given environment already exists
 * @throws alien4cloud.exception.NotFoundException if <b>fromRuntime</b> is set to true, but the environment is not deployed
 * @throws MissingSubstitutionException if topology related to the environment doesn't define a substitution type
 */
public synchronized String create(String serviceName, String environmentId, boolean fromRuntime) {
    ApplicationEnvironment environment = checkAndGetApplicationEnvironment(environmentId);
    // check that the service does not exists already for this environment
    if (alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("environmentId", environmentId)).count() > 0) {
        throw new AlreadyExistException("A service resource for environment <" + environmentId + "> and version <" + environment.getTopologyVersion() + "> already exists.");
    }
    Topology topology;
    String state = ToscaNodeLifecycleConstants.INITIAL;
    Deployment deployment = null;
    if (fromRuntime) {
        deployment = deploymentService.getActiveDeploymentOrFail(environmentId);
        topology = deploymentRuntimeStateService.getRuntimeTopology(deployment.getId());
        DeploymentStatus currentStatus = deploymentRuntimeStateService.getDeploymentStatus(deployment);
        state = managedServiceResourceEventService.getInstanceStateFromDeploymentStatus(currentStatus);
        if (state == null) {
            // We need a valid deployment state to expose as service.
            throw new InvalidDeploymentStatusException("Creating a service out of a running deployment is possible only when it's status is one of [DEPLOYED, FAILURE, UNDEPLOYED] current was <" + currentStatus + ">", currentStatus);
        }
    } else {
        topology = topologyServiceCore.getOrFail(Csar.createId(environment.getApplicationId(), environment.getTopologyVersion()));
    }
    if (topology.getSubstitutionMapping() == null) {
        throw new MissingSubstitutionException("Substitution is required to expose a topology.");
    }
    // The elementId of the type created out of the substitution is currently the archive name.
    String serviceId = serviceResourceService.create(serviceName, environment.getTopologyVersion(), topology.getArchiveName(), environment.getTopologyVersion(), environmentId);
    // Update the service relationships definition from the topology substitution
    updateServiceRelationship(serviceId, topology);
    if (fromRuntime) {
        managedServiceResourceEventService.updateRunningService((DeploymentTopology) topology, serviceResourceService.getOrFail(serviceId), deployment, state);
    }
    ServiceResource serviceResource = serviceResourceService.getOrFail(serviceId);
    // trigger a ManagedServiceCreatedEvent
    publisher.publishEvent(new ManagedServiceCreatedEvent(this, serviceResource));
    return serviceId;
}
Also used : MissingSubstitutionException(org.alien4cloud.alm.service.exceptions.MissingSubstitutionException) InvalidDeploymentStatusException(org.alien4cloud.alm.service.exceptions.InvalidDeploymentStatusException) Deployment(alien4cloud.model.deployment.Deployment) ServiceResource(alien4cloud.model.service.ServiceResource) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) AlreadyExistException(alien4cloud.exception.AlreadyExistException) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) ManagedServiceCreatedEvent(org.alien4cloud.alm.events.ManagedServiceCreatedEvent) DeploymentStatus(alien4cloud.paas.model.DeploymentStatus)

Example 34 with ApplicationEnvironment

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

the class InputService method onCopyConfiguration.

@EventListener
@Order(10)
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    ApplicationEnvironment target = onDeploymentConfigCopyEvent.getTargetEnvironment();
    DeploymentInputs deploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    if (deploymentInputs == null || MapUtils.isEmpty(deploymentInputs.getInputs())) {
        // Nothing to copy
        return;
    }
    Topology topology = topologyServiceCore.getOrFail(Csar.createId(target.getApplicationId(), target.getTopologyVersion()));
    if (MapUtils.isNotEmpty(topology.getInputs())) {
        Map<String, PropertyDefinition> inputsDefinitions = topology.getInputs();
        Map<String, AbstractPropertyValue> inputsToCopy = deploymentInputs.getInputs().entrySet().stream().filter(inputEntry -> inputsDefinitions.containsKey(inputEntry.getKey())).filter(inputEntry -> {
            // Copy only inputs which satisfy the new input definition
            try {
                if (!(inputEntry.getValue() instanceof FunctionPropertyValue)) {
                    ConstraintPropertyService.checkPropertyConstraint(inputEntry.getKey(), PropertyService.asPropertyValue(inputEntry.getValue()), inputsDefinitions.get(inputEntry.getKey()));
                }
                return true;
            } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException e) {
                return false;
            }
        }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        if (MapUtils.isNotEmpty(inputsToCopy)) {
            DeploymentInputs targetDeploymentInputs = deploymentConfigurationDao.findById(DeploymentInputs.class, AbstractDeploymentConfig.generateId(target.getTopologyVersion(), target.getId()));
            if (targetDeploymentInputs == null) {
                targetDeploymentInputs = new DeploymentInputs(target.getTopologyVersion(), target.getId());
            }
            // Copy inputs from original topology
            targetDeploymentInputs.setInputs(inputsToCopy);
            deploymentConfigurationDao.save(targetDeploymentInputs);
        }
    }
}
Also used : ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) TagUtil(alien4cloud.utils.TagUtil) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) TopologyServiceCore(alien4cloud.topology.TopologyServiceCore) MetaPropConfiguration(alien4cloud.model.common.MetaPropConfiguration) MetaPropertiesService(alien4cloud.common.MetaPropertiesService) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Service(org.springframework.stereotype.Service) Application(alien4cloud.model.application.Application) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Map(java.util.Map) ConstraintPropertyService(alien4cloud.utils.services.ConstraintPropertyService) OnDeploymentConfigCopyEvent(org.alien4cloud.alm.deployment.configuration.events.OnDeploymentConfigCopyEvent) MapUtils(org.apache.commons.collections4.MapUtils) Order(org.springframework.core.annotation.Order) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Csar(org.alien4cloud.tosca.model.Csar) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) EventListener(org.springframework.context.event.EventListener) PropertyService(alien4cloud.utils.services.PropertyService) AbstractDeploymentConfig(org.alien4cloud.alm.deployment.configuration.model.AbstractDeploymentConfig) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NotFoundException(alien4cloud.exception.NotFoundException) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) Topology(org.alien4cloud.tosca.model.templates.Topology) DeploymentInputs(org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Map(java.util.Map) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 35 with ApplicationEnvironment

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

the class LocationMatchService method onCopyConfiguration.

@EventListener
// We must process location copy before copy of elements that depends from the location.
@Order(20)
public void onCopyConfiguration(OnDeploymentConfigCopyEvent onDeploymentConfigCopyEvent) {
    ApplicationEnvironment source = onDeploymentConfigCopyEvent.getSourceEnvironment();
    DeploymentMatchingConfiguration sourceConfiguration = deploymentConfigurationDao.findById(DeploymentMatchingConfiguration.class, AbstractDeploymentConfig.generateId(source.getTopologyVersion(), source.getId()));
    if (sourceConfiguration == null || MapUtils.isEmpty(sourceConfiguration.getLocationGroups())) {
        // Nothing to copy
        return;
    }
    // Set the location policy to the target environment.
    setLocationPolicy(onDeploymentConfigCopyEvent.getTargetEnvironment(), sourceConfiguration.getOrchestratorId(), sourceConfiguration.getLocationIds());
}
Also used : DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

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