Search in sources :

Example 1 with ApiClonedControlledGcpBigQueryDataset

use of bio.terra.workspace.generated.model.ApiClonedControlledGcpBigQueryDataset in project terra-workspace-manager by DataBiosphere.

the class CopyBigQueryDatasetDefinitionStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
    final FlightMap inputParameters = flightContext.getInputParameters();
    final FlightMap workingMap = flightContext.getWorkingMap();
    final CloningInstructions effectiveCloningInstructions = inputParameters.get(ControlledResourceKeys.CLONING_INSTRUCTIONS, CloningInstructions.class);
    // TODO: handle cloning a controlled resource with REFERENCE option, PF-812
    if (CloningInstructions.COPY_NOTHING.equals(effectiveCloningInstructions) || CloningInstructions.COPY_REFERENCE.equals(effectiveCloningInstructions)) {
        // nothing further to do here or on following steps
        // Build an empty response object
        final ApiClonedControlledGcpBigQueryDataset result = new ApiClonedControlledGcpBigQueryDataset().dataset(null).sourceWorkspaceId(sourceDataset.getWorkspaceId()).sourceResourceId(sourceDataset.getResourceId()).effectiveCloningInstructions(effectiveCloningInstructions.toApiModel());
        FlightUtils.setResponse(flightContext, result, HttpStatus.OK);
        return StepResult.getStepResultSuccess();
    }
    final String resourceName = FlightUtils.getInputParameterOrWorkingValue(flightContext, ResourceKeys.RESOURCE_NAME, ResourceKeys.PREVIOUS_RESOURCE_NAME, String.class);
    final String description = FlightUtils.getInputParameterOrWorkingValue(flightContext, ResourceKeys.RESOURCE_DESCRIPTION, ResourceKeys.PREVIOUS_RESOURCE_DESCRIPTION, String.class);
    final String datasetName = Optional.ofNullable(inputParameters.get(ControlledResourceKeys.DESTINATION_DATASET_NAME, String.class)).orElse(sourceDataset.getDatasetName());
    workingMap.put(ControlledResourceKeys.DESTINATION_DATASET_NAME, datasetName);
    final UUID destinationWorkspaceId = inputParameters.get(ControlledResourceKeys.DESTINATION_WORKSPACE_ID, UUID.class);
    final String location = FlightUtils.getInputParameterOrWorkingValue(flightContext, ControlledResourceKeys.LOCATION, ControlledResourceKeys.LOCATION, String.class);
    final String destinationProjectId = gcpCloudContextService.getRequiredGcpProject(destinationWorkspaceId);
    final ControlledResourceFields commonFields = ControlledResourceFields.builder().accessScope(sourceDataset.getAccessScope()).assignedUser(sourceDataset.getAssignedUser().orElse(null)).cloningInstructions(sourceDataset.getCloningInstructions()).description(description).managedBy(sourceDataset.getManagedBy()).name(resourceName).resourceId(UUID.randomUUID()).workspaceId(destinationWorkspaceId).build();
    final ControlledBigQueryDatasetResource destinationResource = ControlledBigQueryDatasetResource.builder().projectId(destinationProjectId).datasetName(datasetName).common(commonFields).build();
    final ApiGcpBigQueryDatasetCreationParameters creationParameters = new ApiGcpBigQueryDatasetCreationParameters().datasetId(datasetName).location(location);
    final ControlledResourceIamRole iamRole = IamRoleUtils.getIamRoleForAccessScope(destinationResource.getAccessScope());
    final ControlledBigQueryDatasetResource clonedResource = controlledResourceService.createControlledResourceSync(destinationResource, iamRole, userRequest, creationParameters).castByEnum(WsmResourceType.CONTROLLED_GCP_BIG_QUERY_DATASET);
    workingMap.put(ControlledResourceKeys.CLONED_RESOURCE_DEFINITION, clonedResource);
    final ApiClonedControlledGcpBigQueryDataset apiResult = new ApiClonedControlledGcpBigQueryDataset().dataset(clonedResource.toApiResource()).effectiveCloningInstructions(effectiveCloningInstructions.toApiModel()).sourceWorkspaceId(sourceDataset.getWorkspaceId()).sourceResourceId(sourceDataset.getResourceId());
    workingMap.put(ControlledResourceKeys.CLONE_DEFINITION_RESULT, apiResult);
    if (CloningInstructions.COPY_DEFINITION.equals(effectiveCloningInstructions) || CloningInstructions.COPY_RESOURCE.equals(effectiveCloningInstructions)) {
        // Later steps, if any, don't change the success response, since they only affect
        // internal tables and rows in the dataset.
        FlightUtils.setResponse(flightContext, apiResult, HttpStatus.OK);
    }
    return StepResult.getStepResultSuccess();
}
Also used : CloningInstructions(bio.terra.workspace.service.resource.model.CloningInstructions) FlightMap(bio.terra.stairway.FlightMap) ControlledResourceIamRole(bio.terra.workspace.service.iam.model.ControlledResourceIamRole) ApiGcpBigQueryDatasetCreationParameters(bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetCreationParameters) UUID(java.util.UUID) ControlledResourceFields(bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields) ControlledBigQueryDatasetResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource) ApiClonedControlledGcpBigQueryDataset(bio.terra.workspace.generated.model.ApiClonedControlledGcpBigQueryDataset)

Example 2 with ApiClonedControlledGcpBigQueryDataset

use of bio.terra.workspace.generated.model.ApiClonedControlledGcpBigQueryDataset in project terra-workspace-manager by DataBiosphere.

the class AwaitCloneControlledGcpBigQueryDatasetResourceFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    // wait for the flight
    try {
        final FlightState subflightState = context.getStairway().waitForFlight(subflightId, FLIGHT_POLL_SECONDS, FLIGHT_POLL_CYCLES);
        final WsmResourceCloneDetails cloneDetails = new WsmResourceCloneDetails();
        final WsmCloneResourceResult cloneResult = WorkspaceCloneUtils.flightStatusToCloneResult(subflightState.getFlightStatus(), resource);
        cloneDetails.setResult(cloneResult);
        final FlightMap resultMap = FlightUtils.getResultMapRequired(subflightState);
        final var clonedDataset = resultMap.get(JobMapKeys.RESPONSE.getKeyName(), ApiClonedControlledGcpBigQueryDataset.class);
        cloneDetails.setStewardshipType(StewardshipType.CONTROLLED);
        cloneDetails.setResourceType(WsmResourceType.CONTROLLED_GCP_BIG_QUERY_DATASET);
        cloneDetails.setCloningInstructions(resource.getCloningInstructions());
        cloneDetails.setSourceResourceId(resource.getResourceId());
        cloneDetails.setDestinationResourceId(Optional.ofNullable(clonedDataset).map(ApiClonedControlledGcpBigQueryDataset::getDataset).map(ApiGcpBigQueryDatasetResource::getMetadata).map(ApiResourceMetadata::getResourceId).orElse(null));
        String errorMessage = FlightUtils.getFlightErrorMessage(subflightState);
        cloneDetails.setErrorMessage(errorMessage);
        cloneDetails.setName(resource.getName());
        cloneDetails.setDescription(resource.getDescription());
        // add to the map
        final var resourceIdToResult = Optional.ofNullable(context.getWorkingMap().get(ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT, new TypeReference<Map<UUID, WsmResourceCloneDetails>>() {
        })).orElseGet(HashMap::new);
        resourceIdToResult.put(resource.getResourceId(), cloneDetails);
        context.getWorkingMap().put(ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT, resourceIdToResult);
    } catch (DatabaseOperationException | FlightWaitTimedOutException e) {
        // Retry for database issues or expired wait loop
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    validateRequiredEntries(context.getWorkingMap(), ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT);
    return StepResult.getStepResultSuccess();
}
Also used : WsmResourceCloneDetails(bio.terra.workspace.service.workspace.model.WsmResourceCloneDetails) ApiResourceMetadata(bio.terra.workspace.generated.model.ApiResourceMetadata) WsmCloneResourceResult(bio.terra.workspace.service.workspace.model.WsmCloneResourceResult) HashMap(java.util.HashMap) FlightWaitTimedOutException(bio.terra.stairway.exception.FlightWaitTimedOutException) ApiClonedControlledGcpBigQueryDataset(bio.terra.workspace.generated.model.ApiClonedControlledGcpBigQueryDataset) FlightState(bio.terra.stairway.FlightState) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) FlightMap(bio.terra.stairway.FlightMap) StepResult(bio.terra.stairway.StepResult) HashMap(java.util.HashMap) Map(java.util.Map) FlightMap(bio.terra.stairway.FlightMap)

Aggregations

FlightMap (bio.terra.stairway.FlightMap)2 ApiClonedControlledGcpBigQueryDataset (bio.terra.workspace.generated.model.ApiClonedControlledGcpBigQueryDataset)2 FlightState (bio.terra.stairway.FlightState)1 StepResult (bio.terra.stairway.StepResult)1 DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)1 FlightWaitTimedOutException (bio.terra.stairway.exception.FlightWaitTimedOutException)1 ApiGcpBigQueryDatasetCreationParameters (bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetCreationParameters)1 ApiResourceMetadata (bio.terra.workspace.generated.model.ApiResourceMetadata)1 ControlledResourceIamRole (bio.terra.workspace.service.iam.model.ControlledResourceIamRole)1 ControlledBigQueryDatasetResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource)1 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)1 CloningInstructions (bio.terra.workspace.service.resource.model.CloningInstructions)1 WsmCloneResourceResult (bio.terra.workspace.service.workspace.model.WsmCloneResourceResult)1 WsmResourceCloneDetails (bio.terra.workspace.service.workspace.model.WsmResourceCloneDetails)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1