Search in sources :

Example 26 with ControlledResource

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());
}
Also used : ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) UUID(java.util.UUID) ControlledResourceFields(bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields) ControlledAiNotebookInstanceResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.ainotebook.ControlledAiNotebookInstanceResource) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 27 with ControlledResource

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();
}
Also used : ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) FlightMap(bio.terra.stairway.FlightMap) ConcurrentFlightModificationException(bio.terra.workspace.service.workspace.exceptions.ConcurrentFlightModificationException)

Aggregations

ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)27 Test (org.junit.jupiter.api.Test)13 UUID (java.util.UUID)9 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)8 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)6 FlightMap (bio.terra.stairway.FlightMap)5 FlightState (bio.terra.stairway.FlightState)5 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)5 ManagementException (com.azure.core.management.exception.ManagementException)5 ControlledGcsBucketResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource)4 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)4 WsmResource (bio.terra.workspace.service.resource.model.WsmResource)3 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)2 ApiGcpGcsBucketCreationParameters (bio.terra.workspace.generated.model.ApiGcpGcsBucketCreationParameters)2 ControlledResourceIamRole (bio.terra.workspace.service.iam.model.ControlledResourceIamRole)2 JobBuilder (bio.terra.workspace.service.job.JobBuilder)2 ControlledAzureDiskResource (bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource)2 ControlledAzureIpResource (bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource)2 ControlledAzureNetworkResource (bio.terra.workspace.service.resource.controlled.cloud.azure.network.ControlledAzureNetworkResource)2 ControlledBigQueryDatasetResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource)2