Search in sources :

Example 26 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class WorkspaceServiceImpl method getWorkspaceAccessLevel.

@Override
public WorkspaceAccessLevel getWorkspaceAccessLevel(String workspaceNamespace, String workspaceId) {
    String userAccess;
    try {
        userAccess = fireCloudService.getWorkspace(workspaceNamespace, workspaceId).getAccessLevel();
    } catch (org.pmiops.workbench.firecloud.ApiException e) {
        if (e.getCode() == 404) {
            throw new NotFoundException(String.format("Workspace %s/%s not found", workspaceNamespace, workspaceId));
        } else {
            throw new ServerErrorException(e.getResponseBody());
        }
    }
    if (userAccess.equals(PROJECT_OWNER_ACCESS_LEVEL)) {
        return WorkspaceAccessLevel.OWNER;
    }
    WorkspaceAccessLevel result = WorkspaceAccessLevel.fromValue(userAccess);
    if (result == null) {
        throw new ServerErrorException("Unrecognized access level: " + userAccess);
    }
    return result;
}
Also used : NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) ApiException(org.pmiops.workbench.firecloud.ApiException) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException) WorkspaceAccessLevel(org.pmiops.workbench.model.WorkspaceAccessLevel)

Example 27 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class ClusterController method listClusters.

@Override
public ResponseEntity<ClusterListResponse> listClusters() {
    String project = userProvider.get().getFreeTierBillingProjectName();
    org.pmiops.workbench.notebooks.model.Cluster fcCluster;
    try {
        fcCluster = this.notebooksService.getCluster(project, DEFAULT_CLUSTER_NAME);
    } catch (NotFoundException e) {
        fcCluster = this.notebooksService.createCluster(project, DEFAULT_CLUSTER_NAME, createFirecloudClusterRequest());
    }
    ClusterListResponse resp = new ClusterListResponse();
    resp.setDefaultCluster(TO_ALL_OF_US_CLUSTER.apply(fcCluster));
    return ResponseEntity.ok(resp);
}
Also used : ClusterListResponse(org.pmiops.workbench.model.ClusterListResponse) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException)

Example 28 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class CohortReviewController method getParticipantCohortStatuses.

/**
 * Get all participants for the specified cohortId and cdrVersionId. This endpoint does pagination
 * based on page, pageSize, sortOrder and sortColumn.
 */
@Override
public ResponseEntity<org.pmiops.workbench.model.CohortReview> getParticipantCohortStatuses(String workspaceNamespace, String workspaceId, Long cohortId, Long cdrVersionId, PageFilterRequest request) {
    CohortReview cohortReview = null;
    Cohort cohort = cohortReviewService.findCohort(cohortId);
    Workspace workspace = cohortReviewService.validateMatchingWorkspace(workspaceNamespace, workspaceId, cohort.getWorkspaceId(), WorkspaceAccessLevel.READER);
    CdrVersionContext.setCdrVersion(workspace.getCdrVersion());
    try {
        cohortReview = cohortReviewService.findCohortReview(cohortId, cdrVersionId);
    } catch (NotFoundException nfe) {
        cohortReview = initializeCohortReview(cdrVersionId, cohort);
    }
    PageRequest pageRequest = createPageRequest(request);
    List<Filter> filters = request.getFilters() == null ? Collections.<Filter>emptyList() : request.getFilters().getItems();
    List<ParticipantCohortStatus> participantCohortStatuses = cohortReviewService.findAll(cohortReview.getCohortReviewId(), filters, pageRequest);
    org.pmiops.workbench.model.CohortReview responseReview = TO_CLIENT_COHORTREVIEW.apply(cohortReview, pageRequest);
    responseReview.setParticipantCohortStatuses(participantCohortStatuses.stream().map(TO_CLIENT_PARTICIPANT).collect(Collectors.toList()));
    return ResponseEntity.ok(responseReview);
}
Also used : PageRequest(org.pmiops.workbench.cohortreview.util.PageRequest) Cohort(org.pmiops.workbench.db.model.Cohort) ParticipantCohortStatus(org.pmiops.workbench.db.model.ParticipantCohortStatus) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) org.pmiops.workbench.model(org.pmiops.workbench.model) CohortReview(org.pmiops.workbench.db.model.CohortReview) Workspace(org.pmiops.workbench.db.model.Workspace)

Example 29 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class WorkspacesController method cloneWorkspace.

@Override
public ResponseEntity<CloneWorkspaceResponse> cloneWorkspace(String workspaceNamespace, String workspaceId, CloneWorkspaceRequest body) {
    Workspace workspace = body.getWorkspace();
    if (Strings.isNullOrEmpty(workspace.getNamespace())) {
        throw new BadRequestException("missing required field 'workspace.namespace'");
    } else if (Strings.isNullOrEmpty(workspace.getName())) {
        throw new BadRequestException("missing required field 'workspace.name'");
    } else if (workspace.getResearchPurpose() == null) {
        throw new BadRequestException("missing required field 'workspace.researchPurpose'");
    }
    User user = userProvider.get();
    if (workspaceService.getByName(workspace.getNamespace(), workspace.getName()) != null) {
        throw new ConflictException(String.format("Workspace %s/%s already exists", workspace.getNamespace(), workspace.getName()));
    }
    // Retrieving the workspace is done first, which acts as an access check.
    String fromBucket = null;
    try {
        fromBucket = fireCloudService.getWorkspace(workspaceNamespace, workspaceId).getWorkspace().getBucketName();
    } catch (ApiException e) {
        if (e.getCode() == 404) {
            log.log(Level.INFO, "Firecloud workspace not found", e);
            throw new NotFoundException(String.format("workspace %s/%s not found or not accessible", workspaceNamespace, workspaceId));
        }
        log.log(Level.SEVERE, "Firecloud server error", e);
        throw new ServerErrorException();
    }
    org.pmiops.workbench.db.model.Workspace fromWorkspace = workspaceService.getRequiredWithCohorts(workspaceNamespace, workspaceId);
    if (fromWorkspace == null) {
        throw new NotFoundException(String.format("Workspace %s/%s not found", workspaceNamespace, workspaceId));
    }
    FirecloudWorkspaceId fcWorkspaceId = generateFirecloudWorkspaceId(workspace.getNamespace(), workspace.getName());
    fireCloudService.cloneWorkspace(workspaceNamespace, workspaceId, fcWorkspaceId.getWorkspaceNamespace(), fcWorkspaceId.getWorkspaceName());
    org.pmiops.workbench.firecloud.model.Workspace toFcWorkspace = null;
    try {
        toFcWorkspace = fireCloudService.getWorkspace(fcWorkspaceId.getWorkspaceNamespace(), fcWorkspaceId.getWorkspaceName()).getWorkspace();
    } catch (ApiException e) {
        log.log(Level.SEVERE, "Firecloud error retrieving newly cloned workspace", e);
        throw new ServerErrorException();
    }
    // feasibly copy within a single API request.
    for (Blob b : cloudStorageService.getBlobList(fromBucket, NOTEBOOKS_WORKSPACE_DIRECTORY)) {
        if (!NOTEBOOK_PATTERN.matcher(b.getName()).matches()) {
            continue;
        }
        if (b.getSize() != null && b.getSize() / 1e6 > MAX_NOTEBOOK_SIZE_MB) {
            throw new FailedPreconditionException(String.format("workspace %s/%s contains a notebook larger than %dMB: '%s'; cannot clone - please " + "remove this notebook, reduce its size, or contact the workspace owner", workspaceNamespace, workspaceId, MAX_NOTEBOOK_SIZE_MB, b.getName()));
        }
        cloudStorageService.copyBlob(b.getBlobId(), BlobId.of(toFcWorkspace.getBucketName(), b.getName()));
    }
    // The final step in the process is to clone the AoU representation of the
    // workspace. The implication here is that we may generate orphaned
    // Firecloud workspaces / buckets, but a user should not be able to see
    // half-way cloned workspaces via AoU - so it will just appear as a
    // transient failure.
    org.pmiops.workbench.db.model.Workspace toWorkspace = FROM_CLIENT_WORKSPACE.apply(body.getWorkspace());
    org.pmiops.workbench.db.model.Workspace dbWorkspace = new org.pmiops.workbench.db.model.Workspace();
    Timestamp now = new Timestamp(clock.instant().toEpochMilli());
    dbWorkspace.setFirecloudName(fcWorkspaceId.getWorkspaceName());
    dbWorkspace.setWorkspaceNamespace(fcWorkspaceId.getWorkspaceNamespace());
    dbWorkspace.setCreator(user);
    dbWorkspace.setCreationTime(now);
    dbWorkspace.setLastModifiedTime(now);
    dbWorkspace.setVersion(1);
    dbWorkspace.setName(toWorkspace.getName());
    ResearchPurpose researchPurpose = body.getWorkspace().getResearchPurpose();
    setResearchPurposeDetails(dbWorkspace, researchPurpose);
    if (researchPurpose.getReviewRequested()) {
        // Use a consistent timestamp.
        dbWorkspace.setTimeRequested(now);
    }
    dbWorkspace.setReviewRequested(researchPurpose.getReviewRequested());
    // Clone the previous description, by default.
    if (Strings.isNullOrEmpty(toWorkspace.getDescription())) {
        dbWorkspace.setDescription(fromWorkspace.getDescription());
    } else {
        dbWorkspace.setDescription(toWorkspace.getDescription());
    }
    dbWorkspace.setCdrVersion(fromWorkspace.getCdrVersion());
    dbWorkspace.setDataAccessLevel(fromWorkspace.getDataAccessLevel());
    writeWorkspaceConfigFile(toFcWorkspace, dbWorkspace.getCdrVersion());
    org.pmiops.workbench.db.model.WorkspaceUserRole permissions = new org.pmiops.workbench.db.model.WorkspaceUserRole();
    permissions.setRole(WorkspaceAccessLevel.OWNER);
    permissions.setWorkspace(dbWorkspace);
    permissions.setUser(user);
    dbWorkspace.addWorkspaceUserRole(permissions);
    dbWorkspace = workspaceService.saveAndCloneCohorts(fromWorkspace, dbWorkspace);
    CloneWorkspaceResponse resp = new CloneWorkspaceResponse();
    resp.setWorkspace(TO_SINGLE_CLIENT_WORKSPACE_FROM_FC_AND_DB.apply(dbWorkspace, toFcWorkspace));
    return ResponseEntity.ok(resp);
}
Also used : Blob(com.google.cloud.storage.Blob) User(org.pmiops.workbench.db.model.User) ConflictException(org.pmiops.workbench.exceptions.ConflictException) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) Timestamp(java.sql.Timestamp) FirecloudWorkspaceId(org.pmiops.workbench.db.model.Workspace.FirecloudWorkspaceId) CloneWorkspaceResponse(org.pmiops.workbench.model.CloneWorkspaceResponse) FailedPreconditionException(org.pmiops.workbench.exceptions.FailedPreconditionException) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) Workspace(org.pmiops.workbench.model.Workspace) ApiException(org.pmiops.workbench.firecloud.ApiException) ResearchPurpose(org.pmiops.workbench.model.ResearchPurpose)

Example 30 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class CohortReviewServiceImpl method validateMatchingWorkspace.

@Override
public Workspace validateMatchingWorkspace(String workspaceNamespace, String workspaceName, long workspaceId, WorkspaceAccessLevel accessRequired) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceName, accessRequired);
    Workspace workspace = workspaceService.getRequired(workspaceNamespace, workspaceName);
    if (workspace.getWorkspaceId() != workspaceId) {
        throw new NotFoundException(String.format("Not Found: No workspace matching workspaceNamespace: %s, workspaceId: %s", workspaceNamespace, workspaceName));
    }
    return workspace;
}
Also used : NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) Workspace(org.pmiops.workbench.db.model.Workspace)

Aggregations

NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)32 Test (org.junit.Test)18 WorkspaceAccessLevel (org.pmiops.workbench.model.WorkspaceAccessLevel)16 Workspace (org.pmiops.workbench.db.model.Workspace)14 Cohort (org.pmiops.workbench.db.model.Cohort)10 ApiException (org.pmiops.workbench.firecloud.ApiException)5 ParticipantCohortAnnotation (org.pmiops.workbench.db.model.ParticipantCohortAnnotation)4 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)4 ServerErrorException (org.pmiops.workbench.exceptions.ServerErrorException)4 CohortReview (org.pmiops.workbench.db.model.CohortReview)3 Blob (com.google.cloud.storage.Blob)2 Gson (com.google.gson.Gson)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ParticipantCohortStatus (org.pmiops.workbench.db.model.ParticipantCohortStatus)2 User (org.pmiops.workbench.db.model.User)2 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)2 org.pmiops.workbench.model (org.pmiops.workbench.model)2 ClusterListResponse (org.pmiops.workbench.model.ClusterListResponse)2 ModifyCohortAnnotationDefinitionRequest (org.pmiops.workbench.model.ModifyCohortAnnotationDefinitionRequest)2