Search in sources :

Example 46 with Workspace

use of bio.terra.workspace.service.workspace.model.Workspace in project terra-workspace-manager by DataBiosphere.

the class WorkspaceApiController method cloneWorkspace.

/**
 * Clone an entire workspace by creating a new workspace and cloning the workspace's resources
 * into it.
 *
 * @param workspaceUuid - ID of source workspace
 * @param body - request body
 * @return - result structure for the overall clone operation with details for each resource
 */
@Override
public ResponseEntity<ApiCloneWorkspaceResult> cloneWorkspace(UUID workspaceUuid, @Valid ApiCloneWorkspaceRequest body) {
    final AuthenticatedUserRequest petRequest = getCloningCredentials(workspaceUuid);
    Optional<SpendProfileId> spendProfileId = Optional.ofNullable(body.getSpendProfile()).map(SpendProfileId::new);
    final UUID destinationWorkspaceId = UUID.randomUUID();
    // ET uses userFacingId; CWB doesn't. Schema enforces that userFacingId must be set. CWB doesn't
    // pass userFacingId in request, so use id. Prefix with "a" because userFacingId must start with
    // letter.
    String destinationUserFacingId = Optional.ofNullable(body.getUserFacingId()).orElse("a-" + destinationWorkspaceId);
    ControllerValidationUtils.validateUserFacingId(destinationUserFacingId);
    // Construct the target workspace object from the inputs
    final Workspace destinationWorkspace = Workspace.builder().workspaceId(destinationWorkspaceId).userFacingId(destinationUserFacingId).spendProfileId(spendProfileId.orElse(null)).workspaceStage(WorkspaceStage.MC_WORKSPACE).displayName(body.getDisplayName()).description(body.getDescription()).properties(propertyMapFromApi(body.getProperties())).build();
    final String jobId = workspaceService.cloneWorkspace(workspaceUuid, petRequest, body.getLocation(), destinationWorkspace);
    final ApiCloneWorkspaceResult result = fetchCloneWorkspaceResult(jobId, getAuthenticatedInfo());
    final ApiClonedWorkspace clonedWorkspaceStub = new ApiClonedWorkspace().destinationWorkspaceId(destinationWorkspaceId).destinationUserFacingId(destinationUserFacingId).sourceWorkspaceId(workspaceUuid);
    result.setWorkspace(clonedWorkspaceStub);
    return new ResponseEntity<>(result, getAsyncResponseCode(result.getJobReport()));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ApiClonedWorkspace(bio.terra.workspace.generated.model.ApiClonedWorkspace) ApiCloneWorkspaceResult(bio.terra.workspace.generated.model.ApiCloneWorkspaceResult) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) UUID(java.util.UUID) SpendProfileId(bio.terra.workspace.service.spendprofile.SpendProfileId) ApiCreatedWorkspace(bio.terra.workspace.generated.model.ApiCreatedWorkspace) Workspace(bio.terra.workspace.service.workspace.model.Workspace) ApiClonedWorkspace(bio.terra.workspace.generated.model.ApiClonedWorkspace)

Example 47 with Workspace

use of bio.terra.workspace.service.workspace.model.Workspace in project terra-workspace-manager by DataBiosphere.

the class WorkspaceApiController method createWorkspace.

@Override
public ResponseEntity<ApiCreatedWorkspace> createWorkspace(@RequestBody ApiCreateWorkspaceRequestBody body) {
    AuthenticatedUserRequest userRequest = getAuthenticatedInfo();
    logger.info("Creating workspace {} for {} subject {}", body.getId(), userRequest.getEmail(), userRequest.getSubjectId());
    // Existing client libraries should not need to know about the stage, as they won't use any of
    // the features it gates. If stage isn't specified in a create request, we default to
    // RAWLS_WORKSPACE.
    ApiWorkspaceStageModel requestStage = body.getStage();
    requestStage = (requestStage == null ? ApiWorkspaceStageModel.RAWLS_WORKSPACE : requestStage);
    WorkspaceStage internalStage = WorkspaceStage.fromApiModel(requestStage);
    Optional<SpendProfileId> spendProfileId = Optional.ofNullable(body.getSpendProfile()).map(SpendProfileId::new);
    // ET uses userFacingId; CWB doesn't. Schema enforces that userFacingId must be set. CWB doesn't
    // pass userFacingId in request, so use id. Prefix with "a" because userFacingId must start with
    // a letter.
    String userFacingId = Optional.ofNullable(body.getUserFacingId()).orElse("a-" + body.getId());
    ControllerValidationUtils.validateUserFacingId(userFacingId);
    Workspace workspace = Workspace.builder().workspaceId(body.getId()).userFacingId(userFacingId).displayName(body.getDisplayName()).description(body.getDescription()).spendProfileId(spendProfileId.orElse(null)).workspaceStage(internalStage).properties(propertyMapFromApi(body.getProperties())).build();
    UUID createdId = workspaceService.createWorkspace(workspace, userRequest);
    ApiCreatedWorkspace responseWorkspace = new ApiCreatedWorkspace().id(createdId);
    logger.info("Created workspace {} for {}", responseWorkspace, userRequest.getEmail());
    return new ResponseEntity<>(responseWorkspace, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) WorkspaceStage(bio.terra.workspace.service.workspace.model.WorkspaceStage) ApiWorkspaceStageModel(bio.terra.workspace.generated.model.ApiWorkspaceStageModel) ApiCreatedWorkspace(bio.terra.workspace.generated.model.ApiCreatedWorkspace) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) UUID(java.util.UUID) SpendProfileId(bio.terra.workspace.service.spendprofile.SpendProfileId) ApiCreatedWorkspace(bio.terra.workspace.generated.model.ApiCreatedWorkspace) Workspace(bio.terra.workspace.service.workspace.model.Workspace) ApiClonedWorkspace(bio.terra.workspace.generated.model.ApiClonedWorkspace)

Example 48 with Workspace

use of bio.terra.workspace.service.workspace.model.Workspace in project terra-workspace-manager by DataBiosphere.

the class WorkspaceApiController method getWorkspaceByUserFacingId.

@Override
public ResponseEntity<ApiWorkspaceDescription> getWorkspaceByUserFacingId(@PathVariable("workspaceUserFacingId") String userFacingId) {
    AuthenticatedUserRequest userRequest = getAuthenticatedInfo();
    logger.info("Getting workspace {} for {}", userFacingId, userRequest.getEmail());
    Workspace workspace = workspaceService.getWorkspaceByUserFacingId(userFacingId, userRequest);
    ApiWorkspaceDescription desc = buildWorkspaceDescription(workspace);
    logger.info("Got workspace {} for {}", desc, userRequest.getEmail());
    return new ResponseEntity<>(desc, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ApiWorkspaceDescription(bio.terra.workspace.generated.model.ApiWorkspaceDescription) ApiCreatedWorkspace(bio.terra.workspace.generated.model.ApiCreatedWorkspace) Workspace(bio.terra.workspace.service.workspace.model.Workspace) ApiClonedWorkspace(bio.terra.workspace.generated.model.ApiClonedWorkspace)

Example 49 with Workspace

use of bio.terra.workspace.service.workspace.model.Workspace in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDao method getWorkspaceIfExists.

@ReadTransaction
public Optional<Workspace> getWorkspaceIfExists(UUID uuid) {
    if (uuid == null) {
        throw new MissingRequiredFieldException("Valid workspace id is required");
    }
    String sql = WORKSPACE_SELECT_SQL + " WHERE workspace_id = :id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("id", uuid.toString());
    try {
        Workspace result = DataAccessUtils.requiredSingleResult(jdbcTemplate.query(sql, params, WORKSPACE_ROW_MAPPER));
        logger.info("Retrieved workspace record {}", result);
        return Optional.of(result);
    } catch (EmptyResultDataAccessException e) {
        return Optional.empty();
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) MissingRequiredFieldException(bio.terra.common.exception.MissingRequiredFieldException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Workspace(bio.terra.workspace.service.workspace.model.Workspace) ReadTransaction(bio.terra.common.db.ReadTransaction)

Example 50 with Workspace

use of bio.terra.workspace.service.workspace.model.Workspace in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDao method getWorkspaceByUserFacingId.

/**
 * Retrieves a workspace from database by userFacingId.
 */
public Workspace getWorkspaceByUserFacingId(String userFacingId) {
    if (userFacingId == null || userFacingId.isEmpty()) {
        throw new MissingRequiredFieldException("userFacingId is required");
    }
    String sql = WORKSPACE_SELECT_SQL + " WHERE user_facing_id = :user_facing_id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("user_facing_id", userFacingId);
    Workspace result;
    try {
        result = DataAccessUtils.requiredSingleResult(jdbcTemplate.query(sql, params, WORKSPACE_ROW_MAPPER));
        logger.info("Retrieved workspace record {}", result);
        return result;
    } catch (EmptyResultDataAccessException e) {
        throw new WorkspaceNotFoundException(String.format("Workspace %s not found.", userFacingId));
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) MissingRequiredFieldException(bio.terra.common.exception.MissingRequiredFieldException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) WorkspaceNotFoundException(bio.terra.workspace.db.exception.WorkspaceNotFoundException) Workspace(bio.terra.workspace.service.workspace.model.Workspace)

Aggregations

Workspace (bio.terra.workspace.service.workspace.model.Workspace)74 Test (org.junit.jupiter.api.Test)40 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)30 ApiClonedWorkspace (bio.terra.workspace.generated.model.ApiClonedWorkspace)27 UUID (java.util.UUID)27 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)11 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)8 HashMap (java.util.HashMap)7 FlightDebugInfo (bio.terra.stairway.FlightDebugInfo)6 ApiCreatedWorkspace (bio.terra.workspace.generated.model.ApiCreatedWorkspace)6 SpendProfileId (bio.terra.workspace.service.spendprofile.SpendProfileId)6 ResponseEntity (org.springframework.http.ResponseEntity)6 FlightMap (bio.terra.stairway.FlightMap)5 StepStatus (bio.terra.stairway.StepStatus)5 Traced (io.opencensus.contrib.spring.aop.Traced)5 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)5 ForbiddenException (bio.terra.common.exception.ForbiddenException)4 FlightState (bio.terra.stairway.FlightState)3 ApiWorkspaceDescription (bio.terra.workspace.generated.model.ApiWorkspaceDescription)3 JobBuilder (bio.terra.workspace.service.job.JobBuilder)3