Search in sources :

Example 6 with Application

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

the class ApplicationController method updateImage.

/**
 * Update application's image.
 *
 * @param applicationId The application id.
 * @param image new image of the application
 * @return nothing if success, error will be handled in global exception strategy
 */
@ApiOperation(value = "Updates the image for the application.", notes = "The logged-in user must have the application manager role for this application. Application role required [ APPLICATION_MANAGER ]")
@RequestMapping(value = "/{applicationId:.+}/image", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<String> updateImage(@PathVariable String applicationId, @RequestParam("file") MultipartFile image) {
    Application application = applicationService.checkAndGetApplication(applicationId, ApplicationRole.APPLICATION_MANAGER);
    String imageId;
    try {
        imageId = imageDAO.writeImage(image.getBytes());
    } catch (IOException e) {
        throw new ImageUploadException("Unable to read image from file upload [" + image.getOriginalFilename() + "] to update application [" + applicationId + "]", e);
    }
    application.setImageId(imageId);
    alienDAO.save(application);
    return RestResponseBuilder.<String>builder().data(imageId).build();
}
Also used : ImageUploadException(alien4cloud.images.exception.ImageUploadException) IOException(java.io.IOException) Application(alien4cloud.model.application.Application) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with Application

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

the class ApplicationController method search.

/**
 * Search for an application.
 *
 * @param searchRequest The element that contains criterias for search operation.
 * @return A rest response that contains a {@link FacetedSearchResult} containing applications.
 */
@ApiOperation(value = "Search for applications", notes = "Returns a search result with that contains applications matching the request. A application is returned only if the connected user has at least one application role in [ APPLICATION_MANAGER | APPLICATION_USER | APPLICATION_DEVOPS | DEPLOYMENT_MANAGER ]")
@RequestMapping(value = "/search", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public RestResponse<FacetedSearchResult> search(@RequestBody FilteredSearchRequest searchRequest) {
    FilterBuilder authorizationFilter = AuthorizationUtil.getResourceAuthorizationFilters();
    // We want to sort applications by deployed/undeployed and then application name.
    // Query all application ids and name.
    QueryBuilder queryBuilder = alienDAO.buildSearchQuery(Application.class, searchRequest.getQuery()).setFilters(searchRequest.getFilters(), authorizationFilter).queryBuilder();
    SearchResponse response = alienDAO.getClient().prepareSearch(alienDAO.getIndexForType(Application.class)).setQuery(queryBuilder).setFetchSource(new String[] { "name" }, null).setSize(Integer.MAX_VALUE).get();
    // Get their status (deployed vs undeployed)
    List<DeployedAppHolder> appHolders = Lists.newLinkedList();
    for (SearchHit hit : response.getHits().getHits()) {
        String id = hit.getId();
        String appName = (String) hit.getSource().get("name");
        boolean isDeployed = alienDAO.buildQuery(Deployment.class).setFilters(fromKeyValueCouples("sourceId", id, "endDate", null)).count() > 0;
        appHolders.add(new DeployedAppHolder(id, appName, isDeployed));
    }
    // Sort to have first all deployed apps sorted by name and then all undeployed apps sorted by name.
    Collections.sort(appHolders);
    // Compute the list of app ids to fetch based on the query pagination parameters
    List<String> appIdsToFetch = Lists.newArrayList();
    int to = searchRequest.getFrom() + searchRequest.getSize();
    for (int i = searchRequest.getFrom(); i < appHolders.size() && i < to; i++) {
        appIdsToFetch.add(appHolders.get(i).appId);
    }
    List<Application> applications;
    if (appIdsToFetch.size() == 0) {
        applications = Lists.newArrayList();
    } else {
        applications = alienDAO.findByIds(Application.class, appIdsToFetch.toArray(new String[appIdsToFetch.size()]));
    }
    return RestResponseBuilder.<FacetedSearchResult>builder().data(new FacetedSearchResult<>(searchRequest.getFrom(), to, response.getTookInMillis(), appHolders.size(), new String[] { Application.class.getSimpleName() }, applications.toArray(new Application[applications.size()]), Maps.newHashMap())).build();
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Deployment(alien4cloud.model.deployment.Deployment) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) Application(alien4cloud.model.application.Application) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 8 with Application

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

the class ApplicationDeploymentController method getInstanceInformation.

/**
 * Get detailed information for every instances of every node of the application on the PaaS.
 *
 * @param applicationId The id of the application to be deployed.
 * @return A {@link RestResponse} that contains detailed informations (See {@link InstanceInformation}) of the application on the PaaS it is deployed.
 */
@ApiOperation(value = "Get detailed informations for every instances of every node of the application on the PaaS.", notes = "Application role required [ APPLICATION_MANAGER | APPLICATION_DEVOPS ] and Application environment role required [ APPLICATION_USER | DEPLOYMENT_MANAGER ]")
@RequestMapping(value = "/{applicationId:.+}/environments/{applicationEnvironmentId}/deployment/informations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public DeferredResult<RestResponse<Map<String, Map<String, InstanceInformation>>>> getInstanceInformation(@PathVariable String applicationId, @PathVariable String applicationEnvironmentId) {
    Application application = applicationService.checkAndGetApplication(applicationId);
    ApplicationEnvironment environment = applicationEnvironmentService.getEnvironmentByIdOrDefault(application.getId(), applicationEnvironmentId);
    AuthorizationUtil.checkAuthorizationForEnvironment(application, environment, ApplicationEnvironmentRole.values());
    Deployment deployment = applicationEnvironmentService.getActiveDeployment(environment.getId());
    final DeferredResult<RestResponse<Map<String, Map<String, InstanceInformation>>>> instancesDeferredResult = new DeferredResult<>(5L * 60L * 1000L);
    if (deployment == null) {
        // if there is no topology associated with the version it could not have been deployed.
        instancesDeferredResult.setResult(RestResponseBuilder.<Map<String, Map<String, InstanceInformation>>>builder().build());
    } else {
        try {
            deploymentRuntimeStateService.getInstancesInformation(deployment, new IPaaSCallback<Map<String, Map<String, InstanceInformation>>>() {

                @Override
                public void onSuccess(Map<String, Map<String, InstanceInformation>> data) {
                    instancesDeferredResult.setResult(RestResponseBuilder.<Map<String, Map<String, InstanceInformation>>>builder().data(data).build());
                }

                @Override
                public void onFailure(Throwable throwable) {
                    instancesDeferredResult.setErrorResult(throwable);
                }
            });
        } catch (OrchestratorDisabledException e) {
            log.error("Cannot get instance informations as topology plugin cannot be found.", e);
            instancesDeferredResult.setResult(RestResponseBuilder.<Map<String, Map<String, InstanceInformation>>>builder().build());
        }
    }
    return instancesDeferredResult;
}
Also used : RestResponse(alien4cloud.rest.model.RestResponse) Deployment(alien4cloud.model.deployment.Deployment) InstanceInformation(alien4cloud.paas.model.InstanceInformation) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) OrchestratorDisabledException(alien4cloud.paas.exception.OrchestratorDisabledException) Application(alien4cloud.model.application.Application) Map(java.util.Map) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with Application

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

the class ApplicationDeploymentController method updateDeployment.

@ApiOperation(value = "Update the active deployment for the given application on the given cloud.", notes = "Application role required [ APPLICATION_MANAGER | APPLICATION_DEVOPS ] and Application environment role required [ DEPLOYMENT_MANAGER ]")
@RequestMapping(value = "/{applicationId:.+}/environments/{applicationEnvironmentId}/update-deployment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public DeferredResult<RestResponse<Void>> updateDeployment(@PathVariable String applicationId, @PathVariable String applicationEnvironmentId, @ApiParam(value = "The secret provider configuration and credentials.") @RequestBody SecretProviderCredentials secretProviderCredentials) {
    final DeferredResult<RestResponse<Void>> result = new DeferredResult<>(15L * 60L * 1000L);
    Application application = applicationService.checkAndGetApplication(applicationId);
    // get the topology from the version and the cloud from the environment
    ApplicationEnvironment environment = applicationEnvironmentService.getEnvironmentByIdOrDefault(application.getId(), applicationEnvironmentId);
    if (!environment.getApplicationId().equals(applicationId)) {
        throw new NotFoundException("Unable to find environment with id <" + applicationEnvironmentId + "> for application <" + applicationId + ">");
    }
    AuthorizationUtil.checkAuthorizationForEnvironment(application, environment, ApplicationEnvironmentRole.APPLICATION_USER);
    // check that the environment is not already deployed
    boolean isEnvironmentDeployed = applicationEnvironmentService.isDeployed(environment.getId());
    if (!isEnvironmentDeployed) {
        // the topology must be deployed in order to update it
        throw new NotFoundException("Application <" + applicationId + "> is not deployed for environment with id <" + applicationEnvironmentId + ">, can't update it");
    }
    Deployment deployment = deploymentService.getActiveDeployment(environment.getId());
    if (deployment == null) {
        throw new NotFoundException("Unable to find deployment for environment with id <" + applicationEnvironmentId + "> application <" + applicationId + ">, can't update it");
    }
    ApplicationTopologyVersion topologyVersion = applicationVersionService.getOrFail(Csar.createId(environment.getApplicationId(), environment.getVersion()), environment.getTopologyVersion());
    Topology topology = topologyServiceCore.getOrFail(topologyVersion.getArchiveId());
    DeploymentTopologyDTO deploymentTopologyDTO = deploymentTopologyDTOBuilder.prepareDeployment(topology, application, environment);
    TopologyValidationResult validation = deploymentTopologyDTO.getValidation();
    deploymentService.checkDeploymentUpdateFeasibility(deployment, deploymentTopologyDTO.getTopology());
    // if not valid, then return validation errors
    if (!validation.isValid()) {
        result.setErrorResult(RestResponseBuilder.<Void>builder().error(new RestError(RestErrorCode.INVALID_DEPLOYMENT_TOPOLOGY.getCode(), "The deployment topology for the application <" + application.getName() + "> on the environment <" + environment.getName() + "> is not valid.")).build());
    }
    // process with the deployment
    deployService.update(secretProviderCredentials, deploymentTopologyDTO.getTopology(), application, deployment, new IPaaSCallback<Object>() {

        @Override
        public void onSuccess(Object data) {
            result.setResult(RestResponseBuilder.<Void>builder().build());
        }

        @Override
        public void onFailure(Throwable e) {
            result.setErrorResult(RestResponseBuilder.<Void>builder().error(new RestError(RestErrorCode.UNCATEGORIZED_ERROR.getCode(), e.getMessage())).build());
        }
    });
    return result;
}
Also used : RestResponse(alien4cloud.rest.model.RestResponse) NotFoundException(alien4cloud.exception.NotFoundException) Deployment(alien4cloud.model.deployment.Deployment) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) DeploymentTopologyDTO(alien4cloud.deployment.DeploymentTopologyDTO) ApplicationTopologyVersion(alien4cloud.model.application.ApplicationTopologyVersion) TopologyValidationResult(alien4cloud.topology.TopologyValidationResult) RestError(alien4cloud.rest.model.RestError) Application(alien4cloud.model.application.Application) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with Application

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

the class ApplicationDeploymentController method getAppEnvironmentAndCheckAuthorization.

private ApplicationEnvironment getAppEnvironmentAndCheckAuthorization(String applicationId, String applicationEnvironmentId) {
    Application application = applicationService.checkAndGetApplication(applicationId);
    ApplicationEnvironment environment = applicationEnvironmentService.getEnvironmentByIdOrDefault(application.getId(), applicationEnvironmentId);
    AuthorizationUtil.checkAuthorizationForEnvironment(application, environment);
    return environment;
}
Also used : Application(alien4cloud.model.application.Application) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment)

Aggregations

Application (alien4cloud.model.application.Application)103 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)45 ApiOperation (io.swagger.annotations.ApiOperation)43 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)39 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)38 Audit (alien4cloud.audit.annotation.Audit)28 List (java.util.List)14 Topology (org.alien4cloud.tosca.model.templates.Topology)14 Set (java.util.Set)12 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)11 RestResponse (alien4cloud.rest.model.RestResponse)11 Collectors (java.util.stream.Collectors)11 Map (java.util.Map)10 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)9 ApplicationTopologyVersion (alien4cloud.model.application.ApplicationTopologyVersion)9 Arrays (java.util.Arrays)9 When (cucumber.api.java.en.When)8 Deployment (alien4cloud.model.deployment.Deployment)7 RestResponseBuilder (alien4cloud.rest.model.RestResponseBuilder)7 ApplicationEnvironmentAuthorizationDTO (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO)7