Search in sources :

Example 1 with ApiCreatedControlledGcpGcsBucket

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

the class ControlledGcpResourceApiController method createBucket.

@Override
public ResponseEntity<ApiCreatedControlledGcpGcsBucket> createBucket(UUID workspaceId, @Valid ApiCreateControlledGcpGcsBucketRequestBody body) {
    final AuthenticatedUserRequest userRequest = getAuthenticatedInfo();
    ControlledResourceFields commonFields = toCommonFields(workspaceId, body.getCommon(), userRequest);
    ControlledGcsBucketResource resource = ControlledGcsBucketResource.builder().bucketName(body.getGcsBucket().getName()).common(commonFields).build();
    final ControlledGcsBucketResource createdBucket = controlledResourceService.createControlledResourceSync(resource, commonFields.getIamRole(), userRequest, body.getGcsBucket()).castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
    var response = new ApiCreatedControlledGcpGcsBucket().resourceId(createdBucket.getResourceId()).gcpBucket(createdBucket.toApiResource());
    return new ResponseEntity<>(response, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ControlledResourceFields(bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) ApiCreatedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket)

Example 2 with ApiCreatedControlledGcpGcsBucket

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

the class CopyGcsBucketDefinitionStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
    final FlightMap inputParameters = flightContext.getInputParameters();
    final FlightMap workingMap = flightContext.getWorkingMap();
    final CloningInstructions cloningInstructions = Optional.ofNullable(inputParameters.get(ControlledResourceKeys.CLONING_INSTRUCTIONS, CloningInstructions.class)).orElse(sourceBucket.getCloningInstructions());
    // future steps need the resolved cloning instructions
    workingMap.put(ControlledResourceKeys.CLONING_INSTRUCTIONS, cloningInstructions);
    if (CloningInstructions.COPY_NOTHING.equals(cloningInstructions)) {
        final ApiClonedControlledGcpGcsBucket noOpResult = new ApiClonedControlledGcpGcsBucket().effectiveCloningInstructions(cloningInstructions.toApiModel()).bucket(null).sourceWorkspaceId(sourceBucket.getWorkspaceId()).sourceResourceId(sourceBucket.getResourceId());
        FlightUtils.setResponse(flightContext, noOpResult, HttpStatus.OK);
        return StepResult.getStepResultSuccess();
    }
    // todo: handle COPY_REFERENCE PF-811, PF-812
    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 bucketName = Optional.ofNullable(inputParameters.get(ControlledResourceKeys.DESTINATION_BUCKET_NAME, String.class)).orElseGet(this::randomBucketName);
    final PrivateResourceState privateResourceState = sourceBucket.getAccessScope() == AccessScopeType.ACCESS_SCOPE_PRIVATE ? PrivateResourceState.INITIALIZING : PrivateResourceState.NOT_APPLICABLE;
    // Store effective bucket name for destination
    workingMap.put(ControlledResourceKeys.DESTINATION_BUCKET_NAME, bucketName);
    final UUID destinationWorkspaceId = inputParameters.get(ControlledResourceKeys.DESTINATION_WORKSPACE_ID, UUID.class);
    // bucket resource for create flight
    ControlledResourceFields commonFields = ControlledResourceFields.builder().workspaceId(destinationWorkspaceId).resourceId(// random ID for new resource
    UUID.randomUUID()).name(resourceName).description(description).cloningInstructions(sourceBucket.getCloningInstructions()).assignedUser(sourceBucket.getAssignedUser().orElse(null)).accessScope(sourceBucket.getAccessScope()).managedBy(sourceBucket.getManagedBy()).applicationId(sourceBucket.getApplicationId()).privateResourceState(privateResourceState).build();
    ControlledGcsBucketResource destinationBucketResource = ControlledGcsBucketResource.builder().bucketName(bucketName).common(commonFields).build();
    final ApiGcpGcsBucketCreationParameters destinationCreationParameters = getDestinationCreationParameters(inputParameters, workingMap);
    final ControlledResourceIamRole iamRole = IamRoleUtils.getIamRoleForAccessScope(sourceBucket.getAccessScope());
    // Launch a CreateControlledResourcesFlight to make the destination bucket
    final ControlledGcsBucketResource clonedBucket = controlledResourceService.createControlledResourceSync(destinationBucketResource, iamRole, userRequest, destinationCreationParameters).castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
    workingMap.put(ControlledResourceKeys.CLONED_RESOURCE_DEFINITION, clonedBucket);
    final ApiCreatedControlledGcpGcsBucket apiCreatedBucket = new ApiCreatedControlledGcpGcsBucket().gcpBucket(clonedBucket.toApiResource()).resourceId(destinationBucketResource.getResourceId());
    final ApiClonedControlledGcpGcsBucket apiBucketResult = new ApiClonedControlledGcpGcsBucket().effectiveCloningInstructions(cloningInstructions.toApiModel()).bucket(apiCreatedBucket).sourceWorkspaceId(sourceBucket.getWorkspaceId()).sourceResourceId(sourceBucket.getResourceId());
    workingMap.put(ControlledResourceKeys.CLONE_DEFINITION_RESULT, apiBucketResult);
    if (cloningInstructions.equals(CloningInstructions.COPY_DEFINITION)) {
        FlightUtils.setResponse(flightContext, apiBucketResult, HttpStatus.OK);
    }
    return StepResult.getStepResultSuccess();
}
Also used : CloningInstructions(bio.terra.workspace.service.resource.model.CloningInstructions) FlightMap(bio.terra.stairway.FlightMap) ApiClonedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiClonedControlledGcpGcsBucket) ControlledResourceIamRole(bio.terra.workspace.service.iam.model.ControlledResourceIamRole) UUID(java.util.UUID) ControlledResourceFields(bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields) ApiGcpGcsBucketCreationParameters(bio.terra.workspace.generated.model.ApiGcpGcsBucketCreationParameters) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) ApiCreatedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket) PrivateResourceState(bio.terra.workspace.service.resource.controlled.model.PrivateResourceState)

Example 3 with ApiCreatedControlledGcpGcsBucket

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

the class AwaitCloneGcsBucketResourceFlightStep 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 FlightStatus subflightStatus = subflightState.getFlightStatus();
        final WsmCloneResourceResult cloneResult = WorkspaceCloneUtils.flightStatusToCloneResult(subflightStatus, resource);
        final var cloneDetails = new WsmResourceCloneDetails();
        cloneDetails.setResult(cloneResult);
        final FlightMap resultMap = FlightUtils.getResultMapRequired(subflightState);
        final var clonedBucket = resultMap.get(JobMapKeys.RESPONSE.getKeyName(), ApiClonedControlledGcpGcsBucket.class);
        cloneDetails.setStewardshipType(StewardshipType.CONTROLLED);
        cloneDetails.setResourceType(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
        cloneDetails.setCloningInstructions(resource.getCloningInstructions());
        cloneDetails.setSourceResourceId(resource.getResourceId());
        cloneDetails.setDestinationResourceId(Optional.ofNullable(clonedBucket).map(ApiClonedControlledGcpGcsBucket::getBucket).map(ApiCreatedControlledGcpGcsBucket::getResourceId).orElse(null));
        cloneDetails.setErrorMessage(FlightUtils.getFlightErrorMessage(subflightState));
        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) WsmCloneResourceResult(bio.terra.workspace.service.workspace.model.WsmCloneResourceResult) HashMap(java.util.HashMap) FlightWaitTimedOutException(bio.terra.stairway.exception.FlightWaitTimedOutException) ApiCreatedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket) FlightState(bio.terra.stairway.FlightState) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) FlightStatus(bio.terra.stairway.FlightStatus) FlightMap(bio.terra.stairway.FlightMap) StepResult(bio.terra.stairway.StepResult) HashMap(java.util.HashMap) Map(java.util.Map) FlightMap(bio.terra.stairway.FlightMap)

Aggregations

ApiCreatedControlledGcpGcsBucket (bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket)3 FlightMap (bio.terra.stairway.FlightMap)2 ControlledGcsBucketResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource)2 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)2 FlightState (bio.terra.stairway.FlightState)1 FlightStatus (bio.terra.stairway.FlightStatus)1 StepResult (bio.terra.stairway.StepResult)1 DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)1 FlightWaitTimedOutException (bio.terra.stairway.exception.FlightWaitTimedOutException)1 ApiClonedControlledGcpGcsBucket (bio.terra.workspace.generated.model.ApiClonedControlledGcpGcsBucket)1 ApiGcpGcsBucketCreationParameters (bio.terra.workspace.generated.model.ApiGcpGcsBucketCreationParameters)1 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)1 ControlledResourceIamRole (bio.terra.workspace.service.iam.model.ControlledResourceIamRole)1 PrivateResourceState (bio.terra.workspace.service.resource.controlled.model.PrivateResourceState)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