Search in sources :

Example 16 with ControlledResource

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();
}
Also used : CloningInstructions(bio.terra.workspace.service.resource.model.CloningInstructions) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) JobBuilder(bio.terra.workspace.service.job.JobBuilder)

Example 17 with ControlledResource

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);
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) ControlledAzureDiskResource(bio.terra.workspace.service.resource.controlled.cloud.azure.disk.ControlledAzureDiskResource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) UUID(java.util.UUID) ApiAzureDiskCreationParameters(bio.terra.workspace.generated.model.ApiAzureDiskCreationParameters) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 18 with ControlledResource

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"));
}
Also used : FlightState(bio.terra.stairway.FlightState) ApiAzureRelayNamespaceCreationParameters(bio.terra.workspace.generated.model.ApiAzureRelayNamespaceCreationParameters) ManagementException(com.azure.core.management.exception.ManagementException) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) RelayManager(com.azure.resourcemanager.relay.RelayManager) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) UUID(java.util.UUID) ControlledAzureRelayNamespaceResource(bio.terra.workspace.service.resource.controlled.cloud.azure.relayNamespace.ControlledAzureRelayNamespaceResource) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 19 with ControlledResource

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);
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) ApiAzureIpCreationParameters(bio.terra.workspace.generated.model.ApiAzureIpCreationParameters) ControlledAzureIpResource(bio.terra.workspace.service.resource.controlled.cloud.azure.ip.ControlledAzureIpResource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) UUID(java.util.UUID) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.jupiter.api.Test) BaseAzureTest(bio.terra.workspace.common.BaseAzureTest)

Example 20 with ControlledResource

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) InvalidControlledResourceException(bio.terra.workspace.service.resource.controlled.exception.InvalidControlledResourceException) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource)

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