Search in sources :

Example 1 with DuplicateWorkspaceException

use of bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDao method createWorkspace.

/**
 * Persists a workspace to DB. Returns ID of persisted workspace on success.
 *
 * @param workspace all properties of the workspace to create
 * @return workspace id
 */
@WriteTransaction
public UUID createWorkspace(Workspace workspace) {
    final String sql = "INSERT INTO workspace (workspace_id, display_name, description, spend_profile, properties, workspace_stage) " + "values (:workspace_id, :display_name, :description, :spend_profile," + " cast(:properties AS jsonb), :workspace_stage)";
    final String workspaceId = workspace.getWorkspaceId().toString();
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId).addValue("display_name", workspace.getDisplayName().orElse(null)).addValue("description", workspace.getDescription().orElse(null)).addValue("spend_profile", workspace.getSpendProfileId().map(SpendProfileId::getId).orElse(null)).addValue("properties", DbSerDes.propertiesToJson(workspace.getProperties())).addValue("workspace_stage", workspace.getWorkspaceStage().toString());
    try {
        jdbcTemplate.update(sql, params);
        logger.info("Inserted record for workspace {}", workspaceId);
    } catch (DuplicateKeyException e) {
        throw new DuplicateWorkspaceException(String.format("Workspace with id %s already exists - display name %s stage %s", workspaceId, workspace.getDisplayName().toString(), workspace.getWorkspaceStage().toString()), e);
    }
    return workspace.getWorkspaceId();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DuplicateWorkspaceException(bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException) SpendProfileId(bio.terra.workspace.service.spendprofile.SpendProfileId) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 2 with DuplicateWorkspaceException

use of bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException in project terra-workspace-manager by DataBiosphere.

the class CreateWorkspaceStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws RetryException, InterruptedException {
    UUID workspaceId = workspace.getWorkspaceId();
    try {
        workspaceDao.createWorkspace(workspace);
    } catch (DuplicateWorkspaceException ex) {
        // This might be the result of a step re-running, or it might be an ID conflict. We can ignore
        // this if the existing workspace matches the one we were about to create, otherwise rethrow.
        Workspace existingWorkspace = workspaceDao.getWorkspace(workspaceId);
        if (!workspace.equals(existingWorkspace)) {
            throw ex;
        }
    }
    FlightUtils.setResponse(flightContext, workspaceId, HttpStatus.OK);
    logger.info("Workspace created with id {}", workspaceId);
    return StepResult.getStepResultSuccess();
}
Also used : DuplicateWorkspaceException(bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException) UUID(java.util.UUID) Workspace(bio.terra.workspace.service.workspace.model.Workspace)

Aggregations

DuplicateWorkspaceException (bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException)2 WriteTransaction (bio.terra.common.db.WriteTransaction)1 SpendProfileId (bio.terra.workspace.service.spendprofile.SpendProfileId)1 Workspace (bio.terra.workspace.service.workspace.model.Workspace)1 UUID (java.util.UUID)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1