Search in sources :

Example 1 with Workspace

use of org.pmiops.workbench.model.Workspace in project workbench by all-of-us.

the class WorkspacesController method getWorkspacesForReview.

// Note we do not paginate the workspaces list, since we expect few workspaces
// to require review.
// 
// We can add pagination in the DAO by returning Slice<Workspace> if we want the method to return
// pagination information (e.g. are there more workspaces to get), and Page<Workspace> if we
// want the method to return both pagination information and a total count.
@Override
@AuthorityRequired({ Authority.REVIEW_RESEARCH_PURPOSE })
public ResponseEntity<WorkspaceListResponse> getWorkspacesForReview() {
    WorkspaceListResponse response = new WorkspaceListResponse();
    List<org.pmiops.workbench.db.model.Workspace> workspaces = workspaceService.findForReview();
    response.setItems(workspaces.stream().map(TO_CLIENT_WORKSPACE).collect(Collectors.toList()));
    return ResponseEntity.ok(response);
}
Also used : WorkspaceListResponse(org.pmiops.workbench.model.WorkspaceListResponse) Workspace(org.pmiops.workbench.model.Workspace) AuthorityRequired(org.pmiops.workbench.annotations.AuthorityRequired)

Example 2 with Workspace

use of org.pmiops.workbench.model.Workspace in project workbench by all-of-us.

the class WorkspacesController method createWorkspace.

@Override
public ResponseEntity<Workspace> createWorkspace(Workspace workspace) {
    if (Strings.isNullOrEmpty(workspace.getNamespace())) {
        throw new BadRequestException("missing required field 'namespace'");
    } else if (Strings.isNullOrEmpty(workspace.getName())) {
        throw new BadRequestException("missing required field 'name'");
    } else if (workspace.getResearchPurpose() == null) {
        throw new BadRequestException("missing required field 'researchPurpose'");
    } else if (workspace.getDataAccessLevel() == null) {
        throw new BadRequestException("missing required field 'dataAccessLevel'");
    }
    User user = userProvider.get();
    org.pmiops.workbench.db.model.Workspace existingWorkspace = workspaceService.getByName(workspace.getNamespace(), workspace.getName());
    if (existingWorkspace != null) {
        throw new ConflictException(String.format("Workspace %s/%s already exists", workspace.getNamespace(), workspace.getName()));
    }
    // Note: please keep any initialization logic here in sync with CloneWorkspace().
    FirecloudWorkspaceId workspaceId = generateFirecloudWorkspaceId(workspace.getNamespace(), workspace.getName());
    FirecloudWorkspaceId fcWorkspaceId = workspaceId;
    org.pmiops.workbench.firecloud.model.Workspace fcWorkspace = null;
    for (int attemptValue = 0; attemptValue < MAX_FC_CREATION_ATTEMPT_VALUES; attemptValue++) {
        try {
            fcWorkspace = attemptFirecloudWorkspaceCreation(fcWorkspaceId);
            break;
        } catch (ConflictException e) {
            if (attemptValue >= 5) {
                throw e;
            } else {
                fcWorkspaceId = new FirecloudWorkspaceId(workspaceId.getWorkspaceNamespace(), workspaceId.getWorkspaceName() + Integer.toString(attemptValue));
            }
        }
    }
    Timestamp now = new Timestamp(clock.instant().toEpochMilli());
    org.pmiops.workbench.db.model.Workspace dbWorkspace = new org.pmiops.workbench.db.model.Workspace();
    dbWorkspace.setFirecloudName(fcWorkspaceId.getWorkspaceName());
    dbWorkspace.setWorkspaceNamespace(fcWorkspaceId.getWorkspaceNamespace());
    dbWorkspace.setCreator(user);
    dbWorkspace.setCreationTime(now);
    dbWorkspace.setLastModifiedTime(now);
    dbWorkspace.setVersion(1);
    setCdrVersionId(dbWorkspace, workspace.getCdrVersionId());
    writeWorkspaceConfigFile(fcWorkspace, dbWorkspace.getCdrVersion());
    org.pmiops.workbench.db.model.Workspace reqWorkspace = FROM_CLIENT_WORKSPACE.apply(workspace);
    // TODO: enforce data access level authorization
    dbWorkspace.setDataAccessLevel(reqWorkspace.getDataAccessLevel());
    dbWorkspace.setName(reqWorkspace.getName());
    dbWorkspace.setDescription(reqWorkspace.getDescription());
    // Ignore incoming fields pertaining to review status; clients can only request a review.
    setResearchPurposeDetails(dbWorkspace, workspace.getResearchPurpose());
    if (reqWorkspace.getReviewRequested()) {
        // Use a consistent timestamp.
        dbWorkspace.setTimeRequested(now);
    }
    dbWorkspace.setReviewRequested(reqWorkspace.getReviewRequested());
    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.getDao().save(dbWorkspace);
    return ResponseEntity.ok(TO_SINGLE_CLIENT_WORKSPACE_FROM_FC_AND_DB.apply(dbWorkspace, fcWorkspace));
}
Also used : User(org.pmiops.workbench.db.model.User) ConflictException(org.pmiops.workbench.exceptions.ConflictException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) Timestamp(java.sql.Timestamp) FirecloudWorkspaceId(org.pmiops.workbench.db.model.Workspace.FirecloudWorkspaceId) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) Workspace(org.pmiops.workbench.model.Workspace)

Example 3 with Workspace

use of org.pmiops.workbench.model.Workspace in project workbench by all-of-us.

the class WorkspacesControllerTest method testCreateWorkspace.

@Test
public void testCreateWorkspace() throws Exception {
    Workspace workspace = createDefaultWorkspace();
    workspacesController.createWorkspace(workspace);
    verify(fireCloudService).createWorkspace(workspace.getNamespace(), workspace.getName());
    stubGetWorkspace(workspace.getNamespace(), workspace.getName(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
    Workspace workspace2 = workspacesController.getWorkspace(workspace.getNamespace(), workspace.getId()).getBody().getWorkspace();
    assertThat(workspace2.getCreationTime()).isEqualTo(NOW_TIME);
    assertThat(workspace2.getLastModifiedTime()).isEqualTo(NOW_TIME);
    assertThat(workspace2.getCdrVersionId()).isEqualTo(cdrVersionId);
    assertThat(workspace2.getCreator()).isEqualTo(LOGGED_IN_USER_EMAIL);
    assertThat(workspace2.getDataAccessLevel()).isEqualTo(DataAccessLevel.PROTECTED);
    assertThat(workspace2.getDescription()).isEqualTo("description");
    assertThat(workspace2.getId()).isEqualTo("name");
    assertThat(workspace2.getName()).isEqualTo("name");
    assertThat(workspace2.getResearchPurpose().getDiseaseFocusedResearch()).isTrue();
    assertThat(workspace2.getResearchPurpose().getDiseaseOfFocus()).isEqualTo("cancer");
    assertThat(workspace2.getResearchPurpose().getMethodsDevelopment()).isTrue();
    assertThat(workspace2.getResearchPurpose().getControlSet()).isTrue();
    assertThat(workspace2.getResearchPurpose().getAggregateAnalysis()).isTrue();
    assertThat(workspace2.getResearchPurpose().getAncestry()).isTrue();
    assertThat(workspace2.getResearchPurpose().getCommercialPurpose()).isTrue();
    assertThat(workspace2.getResearchPurpose().getPopulation()).isTrue();
    assertThat(workspace2.getResearchPurpose().getPopulationOfFocus()).isEqualTo("population");
    assertThat(workspace2.getResearchPurpose().getAdditionalNotes()).isEqualTo("additional notes");
    assertThat(workspace2.getNamespace()).isEqualTo("namespace");
    assertThat(workspace2.getResearchPurpose().getReviewRequested()).isTrue();
    assertThat(workspace2.getResearchPurpose().getTimeRequested()).isEqualTo(NOW_TIME);
    // Test that the correct owner is added.
    assertThat(workspace2.getUserRoles().size()).isEqualTo(1);
    assertThat(workspace2.getUserRoles().get(0).getRole()).isEqualTo(WorkspaceAccessLevel.OWNER);
}
Also used : Workspace(org.pmiops.workbench.model.Workspace) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.Test)

Example 4 with Workspace

use of org.pmiops.workbench.model.Workspace in project workbench by all-of-us.

the class WorkspacesControllerTest method testCreateMultipleFirecloudSameName.

@Test
public void testCreateMultipleFirecloudSameName() throws Exception {
    Workspace workspace = createDefaultWorkspace();
    workspacesController.createWorkspace(workspace);
    Workspace workspace2 = createDefaultWorkspace();
    workspace2.setName(workspace2.getName() + ' ');
    doThrow(new ConflictException("Conflict")).when(fireCloudService).createWorkspace(workspace2.getNamespace(), workspace2.getId());
    stubGetWorkspace(workspace2.getNamespace(), workspace2.getId() + '0', LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
    Workspace workspaceCreated = workspacesController.createWorkspace(workspace2).getBody();
    assertThat(workspaceCreated.getId()).isEqualTo(workspace2.getId() + '0');
}
Also used : ConflictException(org.pmiops.workbench.exceptions.ConflictException) Workspace(org.pmiops.workbench.model.Workspace) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.Test)

Example 5 with Workspace

use of org.pmiops.workbench.model.Workspace in project workbench by all-of-us.

the class WorkspacesControllerTest method testListForApproval.

@Test
public void testListForApproval() throws Exception {
    List<Workspace> forApproval = workspacesController.getWorkspacesForReview().getBody().getItems();
    assertThat(forApproval).isEmpty();
    Workspace ws;
    ResearchPurpose researchPurpose;
    String nameForRequested = "requestedButNotApprovedYet";
    // requested approval, but not approved
    ws = createDefaultWorkspace();
    ws.setName(nameForRequested);
    researchPurpose = ws.getResearchPurpose();
    researchPurpose.setApproved(null);
    researchPurpose.setTimeReviewed(null);
    stubGetWorkspace(ws.getNamespace(), ws.getName().toLowerCase(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
    workspacesController.createWorkspace(ws);
    // already approved
    ws = createDefaultWorkspace();
    ws.setName("alreadyApproved");
    stubGetWorkspace(ws.getNamespace(), ws.getName().toLowerCase(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
    researchPurpose = ws.getResearchPurpose();
    ws = workspacesController.createWorkspace(ws).getBody();
    ResearchPurposeReviewRequest request = new ResearchPurposeReviewRequest();
    request.setApproved(true);
    workspacesController.reviewWorkspace(ws.getNamespace(), ws.getId(), request);
    // no approval requested
    ws = createDefaultWorkspace();
    ws.setName("noApprovalRequested");
    researchPurpose = ws.getResearchPurpose();
    researchPurpose.setReviewRequested(false);
    researchPurpose.setTimeRequested(null);
    researchPurpose.setApproved(null);
    researchPurpose.setTimeReviewed(null);
    stubGetWorkspace(ws.getNamespace(), ws.getName().toLowerCase(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
    ws = workspacesController.createWorkspace(ws).getBody();
    forApproval = workspacesController.getWorkspacesForReview().getBody().getItems();
    assertThat(forApproval.size()).isEqualTo(1);
    ws = forApproval.get(0);
    assertThat(ws.getName()).isEqualTo(nameForRequested);
}
Also used : ResearchPurposeReviewRequest(org.pmiops.workbench.model.ResearchPurposeReviewRequest) Matchers.anyString(org.mockito.Matchers.anyString) Workspace(org.pmiops.workbench.model.Workspace) ResearchPurpose(org.pmiops.workbench.model.ResearchPurpose) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.Test)

Aggregations

Workspace (org.pmiops.workbench.model.Workspace)31 Test (org.junit.Test)23 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)23 ResearchPurpose (org.pmiops.workbench.model.ResearchPurpose)13 User (org.pmiops.workbench.db.model.User)9 CloneWorkspaceRequest (org.pmiops.workbench.model.CloneWorkspaceRequest)8 UserRole (org.pmiops.workbench.model.UserRole)7 ConflictException (org.pmiops.workbench.exceptions.ConflictException)6 ShareWorkspaceRequest (org.pmiops.workbench.model.ShareWorkspaceRequest)6 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)5 WorkspaceACLUpdate (org.pmiops.workbench.firecloud.model.WorkspaceACLUpdate)5 WorkspaceACLUpdateResponseList (org.pmiops.workbench.firecloud.model.WorkspaceACLUpdateResponseList)5 UpdateWorkspaceRequest (org.pmiops.workbench.model.UpdateWorkspaceRequest)5 Blob (com.google.cloud.storage.Blob)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ApiException (org.pmiops.workbench.firecloud.ApiException)4 ResearchPurposeReviewRequest (org.pmiops.workbench.model.ResearchPurposeReviewRequest)4 Timestamp (java.sql.Timestamp)3 FirecloudWorkspaceId (org.pmiops.workbench.db.model.Workspace.FirecloudWorkspaceId)3 ShareWorkspaceResponse (org.pmiops.workbench.model.ShareWorkspaceResponse)3