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));
}
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');
}
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();
}
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()));
}
}
}
}
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
}
}
Aggregations