use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.
the class ControlledResourceService method cloneBigQueryDataset.
/**
* Make a clone of a BigQuery dataset
*
* @param sourceWorkspaceId - workspace ID of original dataset
* @param sourceResourceId - resource ID of original dataset
* @param destinationWorkspaceId - destination (sink) workspace ID
* @param jobControl - job control structure (should already have ID)
* @param userRequest - request object for this call
* @param destinationResourceName - resource name. Uses source name if null
* @param destinationDescription - description string for cloned dataset. Source description if
* null.
* @param destinationDatasetName - name for new resource. Can equal source name. If null, a random
* name will be generated
* @param destinationLocation - location override. Uses source location if null
* @param cloningInstructionsOverride - Cloning instructions for this clone operation, overriding
* any existing instructions. Existing instructions are used if null.
*/
public String cloneBigQueryDataset(UUID sourceWorkspaceId, UUID sourceResourceId, UUID destinationWorkspaceId, ApiJobControl jobControl, AuthenticatedUserRequest userRequest, @Nullable String destinationResourceName, @Nullable String destinationDescription, @Nullable String destinationDatasetName, @Nullable String destinationLocation, @Nullable ApiCloningInstructionsEnum cloningInstructionsOverride) {
stageService.assertMcWorkspace(destinationWorkspaceId, "cloneGcpBigQueryDataset");
final ControlledResource sourceDatasetResource = getControlledResource(sourceWorkspaceId, sourceResourceId, userRequest);
// Authorization check is handled as the first flight step rather than before the flight, as
// this flight is re-used for cloneWorkspace.
// Write access to the target workspace will be established in the create flight
final String jobDescription = String.format("Clone controlled resource %s; id %s; name %s", sourceDatasetResource.getResourceType(), sourceDatasetResource.getResourceId(), sourceDatasetResource.getName());
final JobBuilder jobBuilder = jobService.newJob().description(jobDescription).jobId(jobControl.getId()).flightClass(CloneControlledGcpBigQueryDatasetResourceFlight.class).resource(sourceDatasetResource).userRequest(userRequest).operationType(OperationType.CLONE).resourceType(sourceDatasetResource.getResourceType()).stewardshipType(sourceDatasetResource.getStewardshipType()).workspaceId(sourceWorkspaceId.toString()).addParameter(ControlledResourceKeys.DESTINATION_WORKSPACE_ID, destinationWorkspaceId).addParameter(ResourceKeys.RESOURCE_NAME, destinationResourceName).addParameter(ResourceKeys.RESOURCE_DESCRIPTION, destinationDescription).addParameter(ControlledResourceKeys.LOCATION, destinationLocation).addParameter(ControlledResourceKeys.DESTINATION_DATASET_NAME, destinationDatasetName).addParameter(ControlledResourceKeys.CLONING_INSTRUCTIONS, // compute effective cloning instructions
Optional.ofNullable(cloningInstructionsOverride).map(CloningInstructions::fromApiModel).orElse(sourceDatasetResource.getCloningInstructions()));
return jobBuilder.submit();
}
use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.
the class CreateAndDeleteAzureControlledResourceFlightTest method createAzureDiskControlledResource.
@Test
public void createAzureDiskControlledResource() throws InterruptedException {
UUID workspaceId = azureTestUtils.createWorkspace(workspaceService);
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
// Cloud context needs to be created first
FlightState createAzureContextFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateAzureContextFlight.class, azureTestUtils.createAzureContextInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, null);
assertEquals(FlightStatus.SUCCESS, createAzureContextFlightState.getFlightStatus());
assertTrue(workspaceService.getAuthorizedAzureCloudContext(workspaceId, userRequest).isPresent());
final ApiAzureDiskCreationParameters creationParameters = ControlledResourceFixtures.getAzureDiskCreationParameters();
// TODO: make this application-private resource once the POC supports it
final UUID resourceId = UUID.randomUUID();
ControlledAzureDiskResource resource = ControlledAzureDiskResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name(getAzureName("disk")).description(getAzureName("disk-desc")).cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).diskName(creationParameters.getName()).region(creationParameters.getRegion()).size(creationParameters.getSize()).build();
// Submit a Disk creation flight.
FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
// Verify controlled resource exists in the workspace.
ControlledResource res = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
try {
ControlledAzureDiskResource azureDiskResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_DISK);
assertEquals(resource, azureDiskResource);
} catch (Exception e) {
fail("Failed to cast resource to ControlledAzureDiskResource", e);
}
}
use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.
the class CreateAndDeleteAzureControlledResourceFlightTest method createAndDeleteAzureRelayNamespaceControlledResource.
@Test
public void createAndDeleteAzureRelayNamespaceControlledResource() throws InterruptedException {
UUID workspaceId = azureTestUtils.createWorkspace(workspaceService);
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
// Cloud context needs to be created first
FlightState createAzureContextFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateAzureContextFlight.class, azureTestUtils.createAzureContextInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, null);
assertEquals(FlightStatus.SUCCESS, createAzureContextFlightState.getFlightStatus());
assertTrue(workspaceService.getAuthorizedAzureCloudContext(workspaceId, userRequest).isPresent());
final ApiAzureRelayNamespaceCreationParameters creationParameters = ControlledResourceFixtures.getAzureRelayNamespaceCreationParameters();
final UUID resourceId = UUID.randomUUID();
ControlledAzureRelayNamespaceResource resource = ControlledAzureRelayNamespaceResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name(getAzureName("ip")).description(getAzureName("ip-desc")).cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).namespaceName(creationParameters.getNamespaceName()).region(creationParameters.getRegion()).build();
// Submit an IP creation flight.
FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
// Verify controlled resource exists in the workspace.
ControlledResource res = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
var relayNamespaceResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_RELAY_NAMESPACE);
assertEquals(resource, relayNamespaceResource);
// Submit a VM deletion flight.
FlightState deleteFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), DeleteControlledResourceFlight.class, azureTestUtils.deleteControlledResourceInputParameters(workspaceId, resourceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
assertEquals(FlightStatus.SUCCESS, deleteFlightState.getFlightStatus());
TimeUnit.SECONDS.sleep(5);
RelayManager manager = azureTestUtils.getRelayManager();
ManagementException exception = assertThrows(ManagementException.class, () -> manager.namespaces().getByResourceGroup(azureTestUtils.getAzureCloudContext().getAzureResourceGroupId(), resource.getNamespaceName()));
// We see both ResourceNotFound and NotFound in the code field
assertTrue(exception.getValue().getCode().contains("NotFound"));
}
use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.
the class CreateAndDeleteAzureControlledResourceFlightTest method createAzureIpControlledResource.
@Test
public void createAzureIpControlledResource() throws InterruptedException {
UUID workspaceId = azureTestUtils.createWorkspace(workspaceService);
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
// Cloud context needs to be created first
FlightState createAzureContextFlightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateAzureContextFlight.class, azureTestUtils.createAzureContextInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, null);
assertEquals(FlightStatus.SUCCESS, createAzureContextFlightState.getFlightStatus());
assertTrue(workspaceService.getAuthorizedAzureCloudContext(workspaceId, userRequest).isPresent());
final ApiAzureIpCreationParameters creationParameters = ControlledResourceFixtures.getAzureIpCreationParameters();
// TODO: make this application-private resource once the POC supports it
final UUID resourceId = UUID.randomUUID();
ControlledAzureIpResource resource = ControlledAzureIpResource.builder().common(ControlledResourceFields.builder().workspaceId(workspaceId).resourceId(resourceId).name(getAzureName("ip")).description(getAzureName("ip-desc")).cloningInstructions(CloningInstructions.COPY_RESOURCE).accessScope(AccessScopeType.fromApi(ApiAccessScope.SHARED_ACCESS)).managedBy(ManagedByType.fromApi(ApiManagedBy.USER)).build()).ipName(creationParameters.getName()).region(creationParameters.getRegion()).build();
// Submit an IP creation flight.
FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateControlledResourceFlight.class, azureTestUtils.createControlledResourceInputParameters(workspaceId, userRequest, resource), STAIRWAY_FLIGHT_TIMEOUT, null);
assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
// Verify controlled resource exists in the workspace.
ControlledResource res = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
try {
var azureIpResource = res.castByEnum(WsmResourceType.CONTROLLED_AZURE_IP);
assertEquals(resource, azureIpResource);
} catch (Exception e) {
fail("Failed to cast resource to ControlledAzureIpResource", e);
}
}
use of bio.terra.workspace.service.resource.controlled.model.ControlledResource in project terra-workspace-manager by DataBiosphere.
the class ControlledGcpResourceApiController method updateGcsBucket.
@Override
public ResponseEntity<ApiGcpGcsBucketResource> updateGcsBucket(UUID workspaceId, UUID resourceId, @Valid ApiUpdateControlledGcpGcsBucketRequestBody body) {
logger.info("Updating bucket resourceId {} workspaceId {}", resourceId, workspaceId);
final AuthenticatedUserRequest userRequest = getAuthenticatedInfo();
final ControlledResource resource = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest);
if (resource.getResourceType() != WsmResourceType.CONTROLLED_GCP_GCS_BUCKET) {
throw new InvalidControlledResourceException(String.format("Resource %s is not a GCS Bucket", resourceId));
}
final ControlledGcsBucketResource bucketResource = resource.castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
controlledResourceService.updateGcsBucket(bucketResource, body.getUpdateParameters(), userRequest, body.getName(), body.getDescription());
// Retrieve and cast response to ApiGcpGcsBucketResource
final ControlledGcsBucketResource updatedResource = controlledResourceService.getControlledResource(workspaceId, resourceId, userRequest).castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
return new ResponseEntity<>(updatedResource.toApiResource(), HttpStatus.OK);
}
Aggregations