Search in sources :

Example 1 with AlreadyExistException

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

the class AddCapabilitySubstitutionTypeProcessor method process.

@Override
public void process(Csar csar, Topology topology, AddCapabilitySubstitutionTypeOperation 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.getCapabilities() == null || !nodeTemplate.getCapabilities().containsKey(operation.getCapabilityId())) {
        throw new NotFoundException("Capability " + operation.getCapabilityId() + " 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> substitutionCapabilities = topology.getSubstitutionMapping().getCapabilities();
    if (substitutionCapabilities == null) {
        substitutionCapabilities = Maps.newHashMap();
        topology.getSubstitutionMapping().setCapabilities(substitutionCapabilities);
    } else if (substitutionCapabilities.containsKey(operation.getSubstitutionCapabilityId())) {
        // ensure name unicity
        throw new AlreadyExistException(String.format("A substitution with capability id <%s> already exists", operation.getSubstitutionCapabilityId()));
    }
    substitutionCapabilities.put(operation.getSubstitutionCapabilityId(), new SubstitutionTarget(operation.getNodeTemplateName(), operation.getCapabilityId()));
}
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 2 with AlreadyExistException

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

the class UpdateCapabilitySubstitutionTypeProcessor method process.

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

Example 3 with AlreadyExistException

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

the class LocationService method autoConfigure.

/**
 * Auto-configure locations using the given location auto-configurer.
 *
 * @param orchestrator The id of the orchestrator that own the locations.
 * @param locationAutoConfigurer The auto-configurer to use for getting locations.
 */
public void autoConfigure(Orchestrator orchestrator, ILocationAutoConfigurer locationAutoConfigurer) {
    List<Location> locations = locationAutoConfigurer.getLocations();
    for (Location location : locations) {
        // 
        location.setId(UUID.randomUUID().toString());
        location.setOrchestratorId(orchestrator.getId());
        try {
            createLocation(orchestrator, location, location.getInfrastructureType());
        } catch (AlreadyExistException e) {
            log.debug("Location <" + location.getName() + "> is already configured for this location - skipping", e);
        }
    }
}
Also used : AlreadyExistException(alien4cloud.exception.AlreadyExistException) Location(alien4cloud.model.orchestrators.locations.Location)

Example 4 with AlreadyExistException

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

the class PluginManager method uploadPlugin.

/**
 * Upload a plugin from a given path.
 *
 * @param uploadedPluginPath The path of the plugin to upload.<br>
 *            The state of the new uploaded plugin will be determined as follow:
 *            <ul>
 *            <li>plugin doesn't exists: load and enable</li>
 *            <li>plugin exists: keep the state (reload if enabled)</li>
 *            </ul>
 * @return the uploaded plugin
 * @throws IOException In case there is an issue with the access to the plugin file.
 * @throws PluginLoadingException
 * @throws AlreadyExistException if a plugin with the same id already exists in the repository
 * @throws MissingPlugingDescriptorFileException
 */
public Plugin uploadPlugin(Path uploadedPluginPath) throws PluginLoadingException, IOException, MissingPlugingDescriptorFileException {
    // load the plugin descriptor
    FileSystem fs = FileSystems.newFileSystem(uploadedPluginPath, null);
    PluginDescriptor descriptor;
    try {
        try {
            descriptor = YamlParserUtil.parseFromUTF8File(fs.getPath(PLUGIN_DESCRIPTOR_FILE), PluginDescriptor.class);
        } catch (IOException e) {
            if (e instanceof NoSuchFileException) {
                throw new MissingPlugingDescriptorFileException();
            } else {
                throw e;
            }
        }
        String pluginPathId = getPluginPathId();
        Plugin plugin = new Plugin(descriptor, pluginPathId);
        // check plugin already exists and is loaded
        if (pluginContexts.get(plugin.getId()) != null) {
            log.warn("Uploading Plugin [ {} ] impossible (already exists and enabled)", plugin.getId());
            throw new AlreadyExistException("A plugin with the given id already exists and is enabled.");
        }
        Plugin oldPlugin = alienDAO.findById(Plugin.class, plugin.getId());
        if (oldPlugin != null) {
            // remove all files for the old plugin but keep configuration.
            removePlugin(plugin.getId(), false);
        }
        Path pluginPath = getPluginPath(pluginPathId);
        FileUtil.unzip(uploadedPluginPath, pluginPath);
        // copy ui directory in case it exists
        Path pluginUiSourcePath = pluginPath.resolve(UI_DIRECTORY);
        Path pluginUiPath = getPluginUiPath(pluginPathId);
        if (Files.exists(pluginUiSourcePath)) {
            FileUtil.copy(pluginUiSourcePath, pluginUiPath);
        }
        alienDAO.save(plugin);
        if (oldPlugin == null || oldPlugin.isEnabled()) {
            enablePlugin(plugin);
        }
        return plugin;
    } finally {
        fs.close();
    }
}
Also used : Path(java.nio.file.Path) PluginDescriptor(alien4cloud.plugin.model.PluginDescriptor) FileSystem(java.nio.file.FileSystem) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) MissingPlugingDescriptorFileException(alien4cloud.plugin.exception.MissingPlugingDescriptorFileException) ManagedPlugin(alien4cloud.plugin.model.ManagedPlugin)

Example 5 with AlreadyExistException

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

the class CloudServiceArchiveController method uploadCSAR.

@ApiOperation(value = "Upload a csar zip file.")
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<CsarUploadResult> uploadCSAR(@RequestParam(required = false) String workspace, @RequestParam("file") MultipartFile csar) throws IOException {
    Path csarPath = null;
    try {
        if (workspace == null) {
            workspace = AlienConstants.GLOBAL_WORKSPACE_ID;
        }
        // Perform check that the user has one of ARCHITECT, COMPONENT_MANAGER or ADMIN role
        archiveIndexerAuthorizationFilter.preCheckAuthorization(workspace);
        log.info("Serving file upload with name [" + csar.getOriginalFilename() + "]");
        csarPath = Files.createTempFile(tempDirPath, null, '.' + CsarFileRepository.CSAR_EXTENSION);
        // save the archive in the temp directory
        FileUploadUtil.safeTransferTo(csarPath, csar);
        // load, parse the archive definitions and save on disk
        ParsingResult<Csar> result = csarUploadService.upload(csarPath, CSARSource.UPLOAD, workspace);
        RestError error = null;
        if (result.hasError(ParsingErrorLevel.ERROR)) {
            error = RestErrorBuilder.builder(RestErrorCode.CSAR_PARSING_ERROR).build();
        }
        return RestResponseBuilder.<CsarUploadResult>builder().error(error).data(CsarUploadUtil.toUploadResult(result)).build();
    } catch (ParsingException e) {
        log.error("Error happened while parsing csar file <" + e.getFileName() + ">", e);
        String fileName = e.getFileName() == null ? csar.getOriginalFilename() : e.getFileName();
        CsarUploadResult uploadResult = new CsarUploadResult();
        uploadResult.getErrors().put(fileName, e.getParsingErrors());
        return RestResponseBuilder.<CsarUploadResult>builder().error(RestErrorBuilder.builder(RestErrorCode.CSAR_INVALID_ERROR).build()).data(uploadResult).build();
    } catch (AlreadyExistException | CSARUsedInActiveDeployment | ToscaTypeAlreadyDefinedInOtherCSAR e) {
        CsarUploadResult uploadResult = new CsarUploadResult();
        uploadResult.getErrors().put(csar.getOriginalFilename(), Lists.newArrayList(UploadExceptionUtil.parsingErrorFromException(e)));
        return RestResponseBuilder.<CsarUploadResult>builder().error(RestErrorBuilder.builder(RestErrorCode.ALREADY_EXIST_ERROR).build()).data(uploadResult).build();
    } finally {
        if (csarPath != null) {
            // Clean up
            try {
                FileUtil.delete(csarPath);
            } catch (IOException e) {
            // The repository might just move the file instead of copying to save IO disk access
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) RestError(alien4cloud.rest.model.RestError) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) 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