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;
}
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();
}
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);
}
}
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;
}
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;
}
Aggregations