Search in sources :

Example 21 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class RenameInputProcessor method processInputOperation.

@Override
protected void processInputOperation(Csar csar, Topology topology, RenameInputOperation operation, Map<String, PropertyDefinition> inputs) {
    if (!inputs.containsKey(operation.getInputName())) {
        throw new NotFoundException("Input " + operation.getInputName() + " not found");
    }
    if (operation.getInputName().equals(operation.getNewInputName())) {
        // nothing has changed.
        return;
    }
    if (operation.getNewInputName() == null || operation.getNewInputName().isEmpty() || !operation.getNewInputName().matches("\\w+")) {
        throw new InvalidNameException("newInputName", operation.getNewInputName(), "\\w+");
    }
    if (inputs.containsKey(operation.getNewInputName())) {
        throw new AlreadyExistException("Input " + operation.getNewInputName() + " already existed");
    }
    PropertyDefinition propertyDefinition = inputs.remove(operation.getInputName());
    inputs.put(operation.getNewInputName(), propertyDefinition);
    Map<String, NodeTemplate> nodeTemplates = topology.getNodeTemplates();
    for (NodeTemplate nodeTemp : safe(nodeTemplates).values()) {
        renameInputInProperties(nodeTemp.getProperties(), operation.getInputName(), operation.getNewInputName());
        if (nodeTemp.getRelationships() != null) {
            for (RelationshipTemplate relationshipTemplate : nodeTemp.getRelationships().values()) {
                renameInputInProperties(relationshipTemplate.getProperties(), operation.getInputName(), operation.getNewInputName());
            }
        }
        if (nodeTemp.getCapabilities() != null) {
            for (Capability capability : nodeTemp.getCapabilities().values()) {
                renameInputInProperties(capability.getProperties(), operation.getInputName(), operation.getNewInputName());
            }
        }
    }
    // rename preconfigured input
    renamePreconfiguredInput(csar, topology, operation);
    log.debug("Change the name of an input parameter [ {} ] to [ {} ] for the topology ", operation.getInputName(), operation.getNewInputName(), topology.getId());
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) InvalidNameException(alien4cloud.exception.InvalidNameException) Capability(org.alien4cloud.tosca.model.templates.Capability) NotFoundException(alien4cloud.exception.NotFoundException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 22 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class AddRequirementSubstitutionTypeProcessor method process.

@Override
public void process(Csar csar, Topology topology, AddRequirementSubstitutionTypeOperation operation) {
    if (topology.getNodeTemplates() == null || !topology.getNodeTemplates().containsKey(operation.getNodeTemplateName())) {
        throw new NotFoundException("Node " + operation.getNodeTemplateName() + " do not exist");
    }
    NodeTemplate nodeTemplate = topology.getNodeTemplates().get(operation.getNodeTemplateName());
    if (nodeTemplate.getRequirements() == null || !nodeTemplate.getRequirements().containsKey(operation.getRequirementId())) {
        throw new NotFoundException("Requirement " + operation.getRequirementId() + " do not exist for node " + operation.getNodeTemplateName());
    }
    if (topology.getSubstitutionMapping() == null || topology.getSubstitutionMapping().getSubstitutionType() == null) {
        throw new NotFoundException("No substitution type has been found");
    }
    Map<String, SubstitutionTarget> substitutionRequirements = topology.getSubstitutionMapping().getRequirements();
    if (substitutionRequirements == null) {
        substitutionRequirements = Maps.newHashMap();
        topology.getSubstitutionMapping().setRequirements(substitutionRequirements);
    } else if (substitutionRequirements.containsKey(operation.getSubstitutionRequirementId())) {
        // ensure name unicity
        throw new AlreadyExistException(String.format("The substitution requirement <%s> already exists", operation.getSubstitutionRequirementId()));
    }
    substitutionRequirements.put(operation.getSubstitutionRequirementId(), new SubstitutionTarget(operation.getNodeTemplateName(), operation.getRequirementId()));
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NotFoundException(alien4cloud.exception.NotFoundException) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 23 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class UpdateRequirementSubstitutionTypeProcessor method process.

@Override
public void process(Csar csar, Topology topology, UpdateRequirementSubstitutionTypeOperation operation) {
    if (topology.getSubstitutionMapping() == null || topology.getSubstitutionMapping().getSubstitutionType() == null) {
        throw new NotFoundException("No substitution type has been found");
    }
    Map<String, SubstitutionTarget> substitutionRequirements = topology.getSubstitutionMapping().getRequirements();
    if (substitutionRequirements == null) {
        throw new NotFoundException("No substitution requirement has been found");
    }
    SubstitutionTarget target = substitutionRequirements.remove(operation.getSubstitutionRequirementId());
    if (target == null) {
        throw new NotFoundException("No substitution requirement has been found for key " + operation.getSubstitutionRequirementId());
    }
    if (substitutionRequirements.containsKey(operation.getNewRequirementId())) {
        throw new AlreadyExistException(String.format("Can not rename from <%s> to <%s> since requirement <%s> already exists", operation.getSubstitutionRequirementId(), operation.getNewRequirementId(), operation.getNewRequirementId()));
    }
    substitutionRequirements.put(operation.getNewRequirementId(), target);
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) SubstitutionTarget(org.alien4cloud.tosca.model.templates.SubstitutionTarget) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 24 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class ApplicationController method create.

/**
 * Create a new application in the system.
 *
 * @param request The new application to create.
 */
@ApiOperation(value = "Create a new application in the system.", notes = "If successfull returns a rest response with the id of the created application in data. If not successful a rest response with an error content is returned. Role required [ APPLICATIONS_MANAGER ]. " + "By default the application creator will have application roles [APPLICATION_MANAGER, DEPLOYMENT_MANAGER]")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.CREATED)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<String> create(@Valid @RequestBody CreateApplicationRequest request) {
    AuthorizationUtil.checkHasOneRoleIn(Role.APPLICATIONS_MANAGER);
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    // check the topology template id to recover the related topology id
    String topologyId = request.getTopologyTemplateVersionId();
    if (topologyId != null) {
        applicationVersionService.getTemplateTopology(topologyId);
    }
    // check unity of archive name
    try {
        archiveIndexer.ensureUniqueness(request.getArchiveName(), VersionUtil.DEFAULT_VERSION_NAME);
    } catch (AlreadyExistException e) {
        return RestResponseBuilder.<String>builder().error(RestErrorBuilder.builder(RestErrorCode.APPLICATION_CSAR_VERSION_ALREADY_EXIST).message("CSAR: " + request.getArchiveName() + ", Version: " + VersionUtil.DEFAULT_VERSION_NAME + " already exists in the repository.").build()).build();
    }
    // create the application with default environment and version
    String applicationId = applicationService.create(auth.getName(), request.getArchiveName(), request.getName(), request.getDescription());
    ApplicationVersion version = applicationVersionService.createInitialVersion(applicationId, topologyId);
    applicationEnvironmentService.createApplicationEnvironment(auth.getName(), applicationId, version.getTopologyVersions().keySet().iterator().next());
    return RestResponseBuilder.<String>builder().data(applicationId).build();
}
Also used : ApplicationVersion(alien4cloud.model.application.ApplicationVersion) Authentication(org.springframework.security.core.Authentication) AlreadyExistException(alien4cloud.exception.AlreadyExistException) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 25 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class ApplicationDeploymentController method deploy.

/**
 * Trigger deployment of the application on the current configured PaaS.
 *
 * @param deployApplicationRequest application details for deployment (applicationId + deploymentProperties)
 * @return An empty rest response.
 */
@ApiOperation(value = "Deploys the application on the configured Cloud.", notes = "Application role required [ APPLICATION_MANAGER | APPLICATION_DEVOPS ] and Application environment role required [ DEPLOYMENT_MANAGER ]")
@RequestMapping(value = "/deployment", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit(bodyHiddenFields = { "secretProviderCredentials" })
public RestResponse<?> deploy(@Valid @RequestBody DeployApplicationRequest deployApplicationRequest) {
    String applicationId = deployApplicationRequest.getApplicationId();
    String environmentId = deployApplicationRequest.getApplicationEnvironmentId();
    Application application = applicationService.checkAndGetApplication(applicationId);
    ApplicationEnvironment environment = applicationEnvironmentService.getEnvironmentByIdOrDefault(applicationId, environmentId);
    if (!environment.getApplicationId().equals(applicationId)) {
        throw new NotFoundException("Unable to find environment with id <" + environmentId + "> for application <" + applicationId + ">");
    }
    // Security check user must be authorized to deploy the environment (or be application manager)
    AuthorizationUtil.checkAuthorizationForEnvironment(application, environment);
    // ensure deployment status is sync with underlying orchestrator
    applicationEnvironmentService.getStatus(environment);
    // check that the environment is not already deployed
    boolean isEnvironmentDeployed = applicationEnvironmentService.isDeployed(environment.getId());
    if (isEnvironmentDeployed) {
        throw new AlreadyExistException("Environment with id <" + environmentId + "> for application <" + applicationId + "> is already deployed");
    }
    // prepare the deployment
    ApplicationTopologyVersion topologyVersion = applicationVersionService.getOrFail(Csar.createId(environment.getApplicationId(), environment.getVersion()), environment.getTopologyVersion());
    Topology topology = topologyServiceCore.getOrFail(topologyVersion.getArchiveId());
    return toscaContextualAspect.execInToscaContext(() -> doDeploy(deployApplicationRequest, application, environment, topology), false, topology);
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) AlreadyExistException(alien4cloud.exception.AlreadyExistException) Application(alien4cloud.model.application.Application) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) ApplicationTopologyVersion(alien4cloud.model.application.ApplicationTopologyVersion) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AlreadyExistException (alien4cloud.exception.AlreadyExistException)26 NotFoundException (alien4cloud.exception.NotFoundException)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)7 CSARUsedInActiveDeployment (alien4cloud.component.repository.exception.CSARUsedInActiveDeployment)6 ToscaTypeAlreadyDefinedInOtherCSAR (alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR)6 IOException (java.io.IOException)5 Path (java.nio.file.Path)5 InvalidNameException (alien4cloud.exception.InvalidNameException)4 SubstitutionTarget (org.alien4cloud.tosca.model.templates.SubstitutionTarget)4 Audit (alien4cloud.audit.annotation.Audit)3 ParsingException (alien4cloud.tosca.parser.ParsingException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)2 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)2 PluginConfigurationException (alien4cloud.paas.exception.PluginConfigurationException)2 ParsingError (alien4cloud.tosca.parser.ParsingError)2 Csar (org.alien4cloud.tosca.model.Csar)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 Application (alien4cloud.model.application.Application)1