use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.
the class ResourceDaoTest method duplicateNotebookIsRejected.
// AI Notebooks are unique on the tuple {instanceId, location, projectId } in addition
// to the underlying requirement that resource ID and resource names are unique within a
// workspace.
@Test
public void duplicateNotebookIsRejected() {
final UUID workspaceId1 = createGcpWorkspace();
ControlledResourceFields commonFields1 = ControlledResourceFixtures.makeNotebookCommonFieldsBuilder().workspaceId(workspaceId1).build();
ControlledAiNotebookInstanceResource initialResource = ControlledResourceFixtures.makeDefaultAiNotebookInstance().common(commonFields1).build();
resourceDao.createControlledResource(initialResource);
assertEquals(initialResource, resourceDao.getResource(initialResource.getWorkspaceId(), initialResource.getResourceId()));
ControlledResourceFields commonFields2 = ControlledResourceFixtures.makeNotebookCommonFieldsBuilder().workspaceId(workspaceId1).name("resource-2").build();
final ControlledResource duplicatingResource = ControlledResourceFixtures.makeDefaultAiNotebookInstance().common(commonFields2).build();
assertThrows(DuplicateResourceException.class, () -> resourceDao.createControlledResource(duplicatingResource));
ControlledResourceFields commonFields3 = ControlledResourceFixtures.makeNotebookCommonFieldsBuilder().workspaceId(createGcpWorkspace()).name("resource-3").build();
final ControlledResource resourceWithDifferentWorkspaceId = ControlledResourceFixtures.makeDefaultAiNotebookInstance().common(commonFields3).build();
// should be fine: separate workspaces implies separate gcp projects
resourceDao.createControlledResource(resourceWithDifferentWorkspaceId);
assertEquals(resourceWithDifferentWorkspaceId, resourceDao.getResource(resourceWithDifferentWorkspaceId.getWorkspaceId(), resourceWithDifferentWorkspaceId.getResourceId()));
ControlledResourceFields commonFields4 = ControlledResourceFixtures.makeNotebookCommonFieldsBuilder().workspaceId(workspaceId1).name("resource-4").build();
final ControlledResource resourceWithDifferentLocation = ControlledResourceFixtures.makeDefaultAiNotebookInstance().common(commonFields4).location("somewhere-else").build();
// same project & instance ID but different location from resource1
resourceDao.createControlledResource(resourceWithDifferentLocation);
assertEquals(resourceWithDifferentLocation, resourceDao.getResource(resourceWithDifferentLocation.getWorkspaceId(), resourceWithDifferentLocation.getResourceId()));
ControlledResourceFields commonFields5 = ControlledResourceFixtures.makeNotebookCommonFieldsBuilder().workspaceId(workspaceId1).name("resource-5").build();
final ControlledAiNotebookInstanceResource resourceWithDefaultLocation = ControlledResourceFixtures.makeDefaultAiNotebookInstance().common(commonFields5).location(null).build();
resourceDao.createControlledResource(resourceWithDefaultLocation);
assertEquals(resourceWithDefaultLocation, resourceDao.getResource(resourceWithDefaultLocation.getWorkspaceId(), resourceWithDefaultLocation.getResourceId()));
assertEquals(DEFAULT_ZONE, resourceWithDefaultLocation.getLocation());
// clean up
resourceDao.deleteResource(initialResource.getWorkspaceId(), initialResource.getResourceId());
// resource2 never got created
resourceDao.deleteResource(resourceWithDifferentWorkspaceId.getWorkspaceId(), resourceWithDifferentWorkspaceId.getResourceId());
resourceDao.deleteResource(resourceWithDifferentLocation.getWorkspaceId(), resourceWithDifferentLocation.getResourceId());
resourceDao.deleteResource(resourceWithDefaultLocation.getWorkspaceId(), resourceWithDefaultLocation.getResourceId());
}
use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.
the class ReleasePrivateResourceCleanupClaimsStep method undoStep.
@Override
public StepResult undoStep(FlightContext context) throws InterruptedException {
FlightMap workingMap = context.getWorkingMap();
// We release the lock on resources in the do step, so we need to attempt to re-acquire it
// here before undoing. If another flight has claimed any of the resources in the meantime, we
// cannot undo them further without clobbering, so this is a dismal failure.
List<ControlledResource> relockedResources = resourceDao.claimCleanupForWorkspacePrivateResources(workspaceId, userEmail, context.getFlightId());
List<ResourceRolePair> originalResourceRolePairs = workingMap.get(ControlledResourceKeys.RESOURCE_ROLES_TO_REMOVE, new TypeReference<>() {
});
List<ControlledResource> originalResources = originalResourceRolePairs.stream().map(ResourceRolePair::getResource).distinct().collect(Collectors.toList());
if (!relockedResources.containsAll(originalResources)) {
logger.error("Unable to re-acquire cleanup_flight_id lock on all private resources being cleaned up for flight {}.", context.getFlightId());
throw new ConcurrentFlightModificationException("Unable to re-acquire cleanup_flight_id lock on all private resources being cleaned up");
}
// UNDO steps.
return StepResult.getStepResultSuccess();
}
Aggregations