Search in sources :

Example 1 with ApiClonedWorkspace

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

the class AwaitCloneAllResourcesFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    validateRequiredEntries(context.getInputParameters(), ControlledResourceKeys.SOURCE_WORKSPACE_ID, JobMapKeys.REQUEST.getKeyName());
    validateRequiredEntries(context.getWorkingMap(), ControlledResourceKeys.CLONE_ALL_RESOURCES_FLIGHT_ID);
    final var cloneAllResourcesFlightId = context.getWorkingMap().get(ControlledResourceKeys.CLONE_ALL_RESOURCES_FLIGHT_ID, String.class);
    final var destinationWorkspace = context.getInputParameters().get(JobMapKeys.REQUEST.getKeyName(), Workspace.class);
    try {
        // noinspection deprecation
        final FlightState subflightState = context.getStairway().waitForFlight(cloneAllResourcesFlightId, FLIGHT_POLL_SECONDS, FLIGHT_POLL_CYCLES);
        if (FlightStatus.SUCCESS != subflightState.getFlightStatus()) {
            // no point in retrying the await step
            return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, subflightState.getException().orElseGet(() -> new RuntimeException(String.format("Subflight had unexpected status %s. No exception for subflight found.", subflightState.getFlightStatus()))));
        }
        final FlightMap subflightResultMap = FlightUtils.getResultMapRequired(subflightState);
        // Build the response object from the resource ID to details map. The map won't have been
        // instantiated if there are no resources in the workspace, so just use an empty map in that
        // case.
        final var resourceIdToDetails = Optional.ofNullable(subflightResultMap.get(ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT, new TypeReference<Map<UUID, WsmResourceCloneDetails>>() {
        })).orElse(Collections.emptyMap());
        final var apiClonedWorkspace = new ApiClonedWorkspace();
        apiClonedWorkspace.setDestinationWorkspaceId(destinationWorkspace.getWorkspaceId());
        final var sourceWorkspaceId = context.getInputParameters().get(ControlledResourceKeys.SOURCE_WORKSPACE_ID, UUID.class);
        apiClonedWorkspace.setSourceWorkspaceId(sourceWorkspaceId);
        final List<ApiResourceCloneDetails> resources = resourceIdToDetails.values().stream().map(WsmResourceCloneDetails::toApiModel).collect(Collectors.toList());
        apiClonedWorkspace.setResources(resources);
        // Set overall response for workspace clone flights
        FlightUtils.setResponse(context, apiClonedWorkspace, HttpStatus.OK);
    } catch (DatabaseOperationException | FlightWaitTimedOutException e) {
        // Retry for database issues or expired wait loop
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : ApiResourceCloneDetails(bio.terra.workspace.generated.model.ApiResourceCloneDetails) FlightWaitTimedOutException(bio.terra.stairway.exception.FlightWaitTimedOutException) FlightState(bio.terra.stairway.FlightState) ApiClonedWorkspace(bio.terra.workspace.generated.model.ApiClonedWorkspace) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) FlightMap(bio.terra.stairway.FlightMap) StepResult(bio.terra.stairway.StepResult) Map(java.util.Map) FlightMap(bio.terra.stairway.FlightMap)

Aggregations

FlightMap (bio.terra.stairway.FlightMap)1 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 ApiClonedWorkspace (bio.terra.workspace.generated.model.ApiClonedWorkspace)1 ApiResourceCloneDetails (bio.terra.workspace.generated.model.ApiResourceCloneDetails)1 Map (java.util.Map)1