Search in sources :

Example 1 with ConflictException

use of org.pmiops.workbench.exceptions.ConflictException 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 2 with ConflictException

use of org.pmiops.workbench.exceptions.ConflictException 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 3 with ConflictException

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

the class CohortAnnotationDefinitionControllerTest method createCohortAnnotationDefinition_NameConflict.

@Test
public void createCohortAnnotationDefinition_NameConflict() throws Exception {
    String namespace = "aou-test";
    String name = "test";
    long cohortId = 1;
    long workspaceId = 1;
    long annotationDefinitionId = 1;
    final String columnName = "testing";
    Cohort cohort = createCohort(workspaceId);
    Workspace workspace = createWorkspace(namespace, name, workspaceId);
    CohortAnnotationDefinition request = createClientCohortAnnotationDefinition(annotationDefinitionId, cohortId, columnName, AnnotationType.STRING);
    org.pmiops.workbench.db.model.CohortAnnotationDefinition existingDefinition = createDBCohortAnnotationDefinition(cohortId, annotationDefinitionId, request.getAnnotationType(), request.getColumnName());
    WorkspaceAccessLevel owner = WorkspaceAccessLevel.OWNER;
    when(workspaceService.enforceWorkspaceAccessLevel(namespace, name, WorkspaceAccessLevel.WRITER)).thenReturn(owner);
    when(cohortDao.findOne(cohortId)).thenReturn(cohort);
    when(workspaceService.getRequired(namespace, name)).thenReturn(workspace);
    when(cohortAnnotationDefinitionDao.findByCohortIdAndColumnName(cohortId, columnName)).thenReturn(existingDefinition);
    CohortAnnotationDefinition expectedResponse = createClientCohortAnnotationDefinition(annotationDefinitionId, cohortId, columnName, AnnotationType.STRING);
    try {
        cohortAnnotationDefinitionController.createCohortAnnotationDefinition(namespace, name, cohortId, request);
        fail("Should have thrown a ConflictException!");
    } catch (ConflictException e) {
        assertEquals("Conflict: Cohort Annotation Definition name exists for: " + columnName, e.getMessage());
    }
    verify(cohortDao, times(1)).findOne(cohortId);
    verify(workspaceService, times(1)).getRequired(namespace, name);
    verify(cohortAnnotationDefinitionDao, times(1)).findByCohortIdAndColumnName(cohortId, columnName);
    verify(workspaceService).enforceWorkspaceAccessLevel(namespace, name, WorkspaceAccessLevel.WRITER);
    verifyNoMoreMockInteractions();
}
Also used : CohortAnnotationDefinition(org.pmiops.workbench.model.CohortAnnotationDefinition) Cohort(org.pmiops.workbench.db.model.Cohort) ConflictException(org.pmiops.workbench.exceptions.ConflictException) WorkspaceAccessLevel(org.pmiops.workbench.model.WorkspaceAccessLevel) Workspace(org.pmiops.workbench.db.model.Workspace) Test(org.junit.Test)

Example 4 with ConflictException

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

the class UserService method updateWithRetries.

private User updateWithRetries(Function<User, User> userModifier, User user) {
    int numAttempts = 0;
    while (true) {
        user = userModifier.apply(user);
        updateDataAccessLevel(user);
        try {
            user = userDao.save(user);
            return user;
        } catch (ObjectOptimisticLockingFailureException e) {
            if (numAttempts < MAX_RETRIES) {
                user = userDao.findOne(user.getUserId());
                numAttempts++;
            } else {
                throw new ConflictException(String.format("Could not update user %s", user.getUserId()));
            }
        }
    }
}
Also used : ConflictException(org.pmiops.workbench.exceptions.ConflictException) ObjectOptimisticLockingFailureException(org.springframework.orm.ObjectOptimisticLockingFailureException)

Example 5 with ConflictException

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

the class WorkspacesControllerTest method testStaleShareWorkspace.

@Test
public void testStaleShareWorkspace() throws Exception {
    Workspace workspace = createDefaultWorkspace();
    workspace = workspacesController.createWorkspace(workspace).getBody();
    ShareWorkspaceRequest shareWorkspaceRequest = new ShareWorkspaceRequest();
    shareWorkspaceRequest.setWorkspaceEtag(workspace.getEtag());
    UserRole creator = new UserRole();
    creator.setEmail(LOGGED_IN_USER_EMAIL);
    creator.setRole(WorkspaceAccessLevel.OWNER);
    shareWorkspaceRequest.addItemsItem(creator);
    // Simulate time between API calls to trigger last-modified/@Version changes.
    CLOCK.increment(1000);
    WorkspaceACLUpdateResponseList responseValue = new WorkspaceACLUpdateResponseList();
    when(fireCloudService.updateWorkspaceACL(anyString(), anyString(), anyListOf(WorkspaceACLUpdate.class))).thenReturn(responseValue);
    workspacesController.shareWorkspace(workspace.getNamespace(), workspace.getName(), shareWorkspaceRequest);
    // Simulate time between API calls to trigger last-modified/@Version changes.
    CLOCK.increment(1000);
    shareWorkspaceRequest = new ShareWorkspaceRequest();
    // Use the initial etag, not the updated value from shareWorkspace.
    shareWorkspaceRequest.setWorkspaceEtag(workspace.getEtag());
    try {
        workspacesController.shareWorkspace(workspace.getNamespace(), workspace.getName(), shareWorkspaceRequest);
        fail("expected conflict exception when sharing with stale etag");
    } catch (ConflictException e) {
    // Expected
    }
}
Also used : ConflictException(org.pmiops.workbench.exceptions.ConflictException) UserRole(org.pmiops.workbench.model.UserRole) WorkspaceACLUpdateResponseList(org.pmiops.workbench.firecloud.model.WorkspaceACLUpdateResponseList) ShareWorkspaceRequest(org.pmiops.workbench.model.ShareWorkspaceRequest) Workspace(org.pmiops.workbench.model.Workspace) WorkspaceACLUpdate(org.pmiops.workbench.firecloud.model.WorkspaceACLUpdate) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.Test)

Aggregations

ConflictException (org.pmiops.workbench.exceptions.ConflictException)13 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)6 Workspace (org.pmiops.workbench.model.Workspace)5 Timestamp (java.sql.Timestamp)4 Test (org.junit.Test)4 Cohort (org.pmiops.workbench.db.model.Cohort)4 User (org.pmiops.workbench.db.model.User)4 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)3 ServerErrorException (org.pmiops.workbench.exceptions.ServerErrorException)2 ApiException (org.pmiops.workbench.firecloud.ApiException)2 UserRole (org.pmiops.workbench.model.UserRole)2 Address (com.blockscore.models.Address)1 Person (com.blockscore.models.Person)1 Blob (com.google.cloud.storage.Blob)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1