Search in sources :

Example 11 with ApiGcpBigQueryDatasetCreationParameters

use of bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetCreationParameters 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 12 with ApiGcpBigQueryDatasetCreationParameters

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

the class CreateBigQueryDatasetStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
    FlightMap inputMap = flightContext.getInputParameters();
    ApiGcpBigQueryDatasetCreationParameters creationParameters = inputMap.get(CREATION_PARAMETERS, ApiGcpBigQueryDatasetCreationParameters.class);
    FlightMap workingMap = flightContext.getWorkingMap();
    FlightUtils.validateRequiredEntries(workingMap, ControlledResourceKeys.GCP_CLOUD_CONTEXT);
    GcpCloudContext cloudContext = workingMap.get(ControlledResourceKeys.GCP_CLOUD_CONTEXT, GcpCloudContext.class);
    String projectId = cloudContext.getGcpProjectId();
    List<Access> accessConfiguration = buildDatasetAccessConfiguration(cloudContext);
    DatasetReference datasetId = new DatasetReference().setProjectId(projectId).setDatasetId(resource.getDatasetName() == null ? resource.getName() : resource.getDatasetName());
    Dataset datasetToCreate = new Dataset().setDatasetReference(datasetId).setLocation(Optional.ofNullable(creationParameters.getLocation()).orElse(DEFAULT_REGION)).setDefaultTableExpirationMs(BigQueryApiConversions.toBqExpirationTime(creationParameters.getDefaultTableLifetime())).setDefaultPartitionExpirationMs(BigQueryApiConversions.toBqExpirationTime(creationParameters.getDefaultPartitionLifetime())).setAccess(accessConfiguration);
    BigQueryCow bqCow = crlService.createWsmSaBigQueryCow();
    try {
        bqCow.datasets().insert(projectId, datasetToCreate).execute();
    } catch (GoogleJsonResponseException e) {
        // other cases, surface the exception and attempt to retry.
        if (e.getStatusCode() == HttpStatus.SC_CONFLICT) {
            logger.info("BQ dataset {} in project {} already exists", resource.getDatasetName(), projectId);
            return StepResult.getStepResultSuccess();
        }
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    } catch (IOException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : DatasetReference(com.google.api.services.bigquery.model.DatasetReference) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Dataset(com.google.api.services.bigquery.model.Dataset) Access(com.google.api.services.bigquery.model.Dataset.Access) FlightMap(bio.terra.stairway.FlightMap) ApiGcpBigQueryDatasetCreationParameters(bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetCreationParameters) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult) BigQueryCow(bio.terra.cloudres.google.bigquery.BigQueryCow) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Aggregations

ApiGcpBigQueryDatasetCreationParameters (bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetCreationParameters)12 ControlledBigQueryDatasetResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource)11 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)9 Test (org.junit.jupiter.api.Test)9 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)9 BigQueryCow (bio.terra.cloudres.google.bigquery.BigQueryCow)5 StepStatus (bio.terra.stairway.StepStatus)5 ApiGcpBigQueryDatasetUpdateParameters (bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetUpdateParameters)5 HashMap (java.util.HashMap)5 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)4 Dataset (com.google.api.services.bigquery.model.Dataset)3 FlightMap (bio.terra.stairway.FlightMap)2 CreateBigQueryDatasetStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.CreateBigQueryDatasetStep)2 RetrieveBigQueryDatasetCloudAttributesStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.RetrieveBigQueryDatasetCloudAttributesStep)2 UpdateBigQueryDatasetStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.UpdateBigQueryDatasetStep)2 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)2 StepResult (bio.terra.stairway.StepResult)1 ApiClonedControlledGcpBigQueryDataset (bio.terra.workspace.generated.model.ApiClonedControlledGcpBigQueryDataset)1 ControlledResourceIamRole (bio.terra.workspace.service.iam.model.ControlledResourceIamRole)1 DeleteBigQueryDatasetStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.DeleteBigQueryDatasetStep)1