Search in sources :

Example 46 with Audit

use of alien4cloud.audit.annotation.Audit 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 47 with Audit

use of alien4cloud.audit.annotation.Audit 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)

Example 48 with Audit

use of alien4cloud.audit.annotation.Audit in project alien4cloud by alien4cloud.

the class ApplicationEnvironmentController method updateTopologyVersion.

/**
 * Use new topology version for environment
 *
 * @param applicationEnvironmentId environment's id
 * @param applicationId application's id
 * @param request request for new topology's version
 */
@ApiOperation(value = "Use new topology version for the given application environment", notes = "The logged-in user must have the application manager role for this application. Application role required [ APPLICATION_MANAGER ]")
@RequestMapping(value = "/{applicationEnvironmentId:.+}/topology-version", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<Void> updateTopologyVersion(@PathVariable String applicationId, @PathVariable String applicationEnvironmentId, @RequestBody UpdateTopologyVersionForEnvironmentRequest request) throws OrchestratorDisabledException {
    // Only APPLICATION_MANAGER & DEPLOYMENT_MANAGER on the underlying application can update an application environment
    ApplicationEnvironment applicationEnvironment = applicationEnvironmentService.checkAndGetApplicationEnvironment(applicationEnvironmentId, ApplicationRole.APPLICATION_MANAGER, ApplicationEnvironmentRole.DEPLOYMENT_MANAGER);
    if (applicationEnvironment == null) {
        return RestResponseBuilder.<Void>builder().data(null).error(RestErrorBuilder.builder(RestErrorCode.APPLICATION_ENVIRONMENT_ERROR).message("Application environment with id <" + applicationEnvironmentId + "> does not exist").build()).build();
    }
    // update the version of the environment
    ApplicationVersion newApplicationVersion = applicationVersionService.getOrFailByArchiveId(Csar.createId(applicationEnvironment.getApplicationId(), request.getNewTopologyVersion()));
    String oldVersion = applicationEnvironment.getVersion();
    String oldTopologyVersion = applicationEnvironment.getTopologyVersion();
    String newVersion = newApplicationVersion.getVersion();
    String newTopologyVersion = request.getNewTopologyVersion();
    if (!Objects.equals(newVersion, oldVersion) || !Objects.equals(newTopologyVersion, oldTopologyVersion)) {
        // Only process if something has changed
        applicationEnvironmentService.updateTopologyVersion(applicationEnvironment, oldTopologyVersion, newVersion, newTopologyVersion, request.getEnvironmentToCopyInput());
    }
    return RestResponseBuilder.<Void>builder().build();
}
Also used : ApplicationVersion(alien4cloud.model.application.ApplicationVersion) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 49 with Audit

use of alien4cloud.audit.annotation.Audit in project alien4cloud by alien4cloud.

the class ApplicationEnvironmentController method delete.

/**
 * Delete an application environment based on it's id.
 *
 * @param applicationEnvironmentId
 * @return
 */
@ApiOperation(value = "Delete an application environment from its id", notes = "The logged-in user must have the application manager role for this application. Application role required [ APPLICATION_MANAGER ]")
@RequestMapping(value = "/{applicationEnvironmentId:.+}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<Boolean> delete(@PathVariable String applicationId, @PathVariable String applicationEnvironmentId) {
    // Only APPLICATION_MANAGER on the underlying application can delete an application environment
    ApplicationEnvironment environmentToDelete = applicationEnvironmentService.checkAndGetApplicationEnvironment(applicationEnvironmentId, ApplicationRole.APPLICATION_MANAGER);
    int countEnvironment = applicationEnvironmentService.getByApplicationId(environmentToDelete.getApplicationId()).length;
    if (countEnvironment == 1) {
        throw new DeleteLastApplicationEnvironmentException("Application environment with id <" + applicationEnvironmentId + "> cannot be deleted as it's the last one for the application id <" + environmentToDelete.getApplicationId() + ">");
    }
    applicationEnvironmentService.delete(applicationEnvironmentId);
    return RestResponseBuilder.<Boolean>builder().data(true).build();
}
Also used : ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) DeleteLastApplicationEnvironmentException(alien4cloud.exception.DeleteLastApplicationEnvironmentException) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 50 with Audit

use of alien4cloud.audit.annotation.Audit in project alien4cloud by alien4cloud.

the class ApplicationEnvironmentController method update.

/**
 * Update application environment
 *
 * @param applicationEnvironmentId
 * @param request
 * @return
 */
@ApiOperation(value = "Updates by merging the given request into the given application environment", notes = "The logged-in user must have the application manager role for this application. Application role required [ APPLICATION_MANAGER ]")
@RequestMapping(value = "/{applicationEnvironmentId:.+}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<Void> update(@PathVariable String applicationId, @PathVariable String applicationEnvironmentId, @RequestBody UpdateApplicationEnvironmentRequest request) throws OrchestratorDisabledException {
    // Only APPLICATION_MANAGER on the underlying application can update an application environment
    ApplicationEnvironment applicationEnvironment = applicationEnvironmentService.checkAndGetApplicationEnvironment(applicationEnvironmentId, ApplicationRole.APPLICATION_MANAGER);
    if (applicationEnvironment == null) {
        return RestResponseBuilder.<Void>builder().data(null).error(RestErrorBuilder.builder(RestErrorCode.APPLICATION_ENVIRONMENT_ERROR).message("Application environment with id <" + applicationEnvironmentId + "> does not exist").build()).build();
    }
    applicationEnvironmentService.ensureNameUnicity(applicationEnvironment.getApplicationId(), request.getName());
    ReflectionUtil.mergeObject(request, applicationEnvironment);
    if (applicationEnvironment.getName() == null || applicationEnvironment.getName().isEmpty()) {
        throw new UnsupportedOperationException("Application environment name cannot be set to null or empty");
    }
    if (request.getCurrentVersionId() != null) {
        // update the version of the environment
        ApplicationVersion applicationVersion = applicationVersionService.getOrFailByArchiveId(Csar.createId(applicationEnvironment.getApplicationId(), request.getCurrentVersionId()));
        applicationEnvironment.setVersion(applicationVersion.getVersion());
        applicationEnvironment.setTopologyVersion(request.getCurrentVersionId());
    }
    alienDAO.save(applicationEnvironment);
    return RestResponseBuilder.<Void>builder().build();
}
Also used : ApplicationVersion(alien4cloud.model.application.ApplicationVersion) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

Audit (alien4cloud.audit.annotation.Audit)71 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)69 ApiOperation (io.swagger.annotations.ApiOperation)67 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)63 Application (alien4cloud.model.application.Application)27 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)24 Location (alien4cloud.model.orchestrators.locations.Location)15 List (java.util.List)15 GroupDTO (alien4cloud.rest.orchestrator.model.GroupDTO)11 UserDTO (alien4cloud.rest.orchestrator.model.UserDTO)11 RestResponse (alien4cloud.rest.model.RestResponse)10 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)9 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)7 ResourcePermissionService (alien4cloud.authorization.ResourcePermissionService)7 IGenericSearchDAO (alien4cloud.dao.IGenericSearchDAO)7 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)7 RestResponseBuilder (alien4cloud.rest.model.RestResponseBuilder)7 ApplicationEnvironmentAuthorizationDTO (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO)7 ApplicationEnvironmentAuthorizationUpdateRequest (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest)7 Subject (alien4cloud.security.Subject)7