Search in sources :

Example 6 with WriteTransaction

use of bio.terra.common.db.WriteTransaction in project terra-workspace-manager by DataBiosphere.

the class ResourceDao method deleteResourceForResourceType.

@WriteTransaction
public boolean deleteResourceForResourceType(UUID workspaceUuid, UUID resourceId, WsmResourceType resourceType) {
    final String sql = "DELETE FROM resource WHERE workspace_id = :workspace_id AND resource_id = :resource_id" + " AND exact_resource_type = :exact_resource_type";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid.toString()).addValue("resource_id", resourceId.toString()).addValue("exact_resource_type", resourceType.toSql());
    int rowsAffected = jdbcTemplate.update(sql, params);
    boolean deleted = rowsAffected > 0;
    logger.info("{} record for resource {} of resource type {} in workspace {}", (deleted ? "Deleted" : "No Delete - did not find"), resourceId, resourceType, workspaceUuid);
    return deleted;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 7 with WriteTransaction

use of bio.terra.common.db.WriteTransaction 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, user_facing_id, display_name, description, spend_profile, properties, workspace_stage) " + "values (:workspace_id, :user_facing_id, :display_name, :description, :spend_profile," + " cast(:properties AS jsonb), :workspace_stage)";
    final String workspaceUuid = workspace.getWorkspaceId().toString();
    // validateUserFacingId() is called in controller. Also call here to be safe (eg see bug
    // PF-1616).
    ControllerValidationUtils.validateUserFacingId(workspace.getUserFacingId());
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid).addValue("user_facing_id", workspace.getUserFacingId()).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 {}", workspaceUuid);
    } catch (DuplicateKeyException e) {
        if (e.getMessage().contains("duplicate key value violates unique constraint \"workspace_pkey\"")) {
            // Workspace with workspace_id already exists.
            throw new DuplicateWorkspaceException(String.format("Workspace with id %s already exists - display name %s stage %s", workspaceUuid, workspace.getDisplayName().toString(), workspace.getWorkspaceStage().toString()), e);
        } else if (e.getMessage().contains("duplicate key value violates unique constraint \"workspace_user_facing_id_key\"")) {
            // workspace_id is new, but workspace with user_facing_id already exists.
            throw new DuplicateUserFacingIdException(String.format(// "ID" instead of "userFacingId" because end user sees this.
            "Workspace with ID %s already exists", workspace.getUserFacingId()), e);
        } else {
            throw e;
        }
    }
    return workspace.getWorkspaceId();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DuplicateUserFacingIdException(bio.terra.workspace.service.workspace.exceptions.DuplicateUserFacingIdException) 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 8 with WriteTransaction

use of bio.terra.common.db.WriteTransaction in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDao method createCloudContextStart.

/**
 * Create cloud context - this is used as part of CreateGcpContextFlightV2 to insert the context
 * row at the start of the create context operation.
 */
@WriteTransaction
public void createCloudContextStart(UUID workspaceUuid, CloudPlatform cloudPlatform, String flightId) {
    final String platform = cloudPlatform.toSql();
    final String sql = "INSERT INTO cloud_context (workspace_id, cloud_platform, creating_flight)" + " VALUES (:workspace_id, :cloud_platform, :creating_flight)";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid.toString()).addValue("cloud_platform", platform).addValue("creating_flight", flightId);
    try {
        jdbcTemplate.update(sql, params);
        logger.info("Inserted record for {} cloud context for workspace {}", platform, workspaceUuid);
    } catch (DuplicateKeyException e) {
        throw new DuplicateCloudContextException(String.format("Workspace with id %s already has context for %s cloud type", workspaceUuid, platform), e);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DuplicateCloudContextException(bio.terra.workspace.service.workspace.exceptions.DuplicateCloudContextException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 9 with WriteTransaction

use of bio.terra.common.db.WriteTransaction in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDao method deleteWorkspace.

/**
 * @param workspaceUuid unique identifier of the workspace
 * @return true on successful delete, false if there's nothing to delete
 */
@WriteTransaction
public boolean deleteWorkspace(UUID workspaceUuid) {
    final String sql = "DELETE FROM workspace WHERE workspace_id = :id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("id", workspaceUuid.toString());
    int rowsAffected = jdbcTemplate.update(sql, params);
    boolean deleted = rowsAffected > 0;
    if (deleted) {
        logger.info("Deleted record for workspace {}", workspaceUuid);
    } else {
        logger.info("No record found for delete workspace {}", workspaceUuid);
    }
    return deleted;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 10 with WriteTransaction

use of bio.terra.common.db.WriteTransaction in project terra-workspace-manager by DataBiosphere.

the class ResourceDao method deleteResource.

@WriteTransaction
public boolean deleteResource(UUID workspaceUuid, UUID resourceId) {
    final String sql = "DELETE FROM resource WHERE workspace_id = :workspace_id AND resource_id = :resource_id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid.toString()).addValue("resource_id", resourceId.toString());
    int rowsAffected = jdbcTemplate.update(sql, params);
    boolean deleted = rowsAffected > 0;
    logger.info("{} record for resource {} in workspace {}", (deleted ? "Deleted" : "No Delete - did not find"), resourceId, workspaceUuid);
    return deleted;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Aggregations

WriteTransaction (bio.terra.common.db.WriteTransaction)18 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)18 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)5 ApplicationInUseException (bio.terra.workspace.db.exception.ApplicationInUseException)2 DuplicateCloudContextException (bio.terra.workspace.service.workspace.exceptions.DuplicateCloudContextException)2 DuplicateUserFacingIdException (bio.terra.workspace.service.workspace.exceptions.DuplicateUserFacingIdException)2 MissingRequiredFieldException (bio.terra.common.exception.MissingRequiredFieldException)1 DbResource (bio.terra.workspace.db.model.DbResource)1 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)1 SpendProfileId (bio.terra.workspace.service.spendprofile.SpendProfileId)1 DuplicateWorkspaceException (bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException)1 Timestamp (java.sql.Timestamp)1 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)1