Search in sources :

Example 1 with WriteTransaction

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

the class RawDaoTestFixture method storeResource.

// Write a resource into the database from bare parts
@WriteTransaction
public void storeResource(String workspaceId, String cloudPlatform, String resourceId, String name, String description, String stewardshipType, String resourceType, String resourceFamily, String cloningInstructions, String attributes, String accessScope, String managedBy, String associatedApp, String assignedUser, String privateResourceState) {
    final String sql = "INSERT INTO resource (workspace_id, cloud_platform, resource_id, name, description, stewardship_type," + " exact_resource_type, resource_type, cloning_instructions, attributes," + " access_scope, managed_by, associated_app, assigned_user, private_resource_state)" + " VALUES (:workspace_id, :cloud_platform, :resource_id, :name, :description, :stewardship_type," + " :exact_resource_type, :resource_type, :cloning_instructions, cast(:attributes AS jsonb)," + " :access_scope, :managed_by, :associated_app, :assigned_user, :private_resource_state)";
    final var params = new MapSqlParameterSource().addValue("workspace_id", workspaceId).addValue("cloud_platform", cloudPlatform).addValue("resource_id", resourceId).addValue("name", name).addValue("description", description).addValue("stewardship_type", stewardshipType).addValue("exact_resource_type", resourceType).addValue("resource_type", resourceFamily).addValue("cloning_instructions", cloningInstructions).addValue("attributes", attributes).addValue("access_scope", accessScope).addValue("managed_by", managedBy).addValue("associated_app", associatedApp).addValue("assigned_user", assignedUser).addValue("private_resource_state", privateResourceState);
    try {
        jdbcTemplate.update(sql, params);
        logger.info("Inserted record for resource {} for workspace {}", resourceId, workspaceId);
    } catch (DuplicateKeyException e) {
        throw new DuplicateResourceException(String.format("A resource already exists in the workspace that has the same name (%s) or the same id (%s)", name, resourceId));
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateResourceException(bio.terra.workspace.service.resource.exception.DuplicateResourceException) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 2 with WriteTransaction

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

the class ResourceDao method claimCleanupForWorkspacePrivateResources.

/**
 * Reads all private controlled resources assigned to a given user in a given workspace which are
 * not being cleaned up by other flights and marks them as being cleaned up by the current flight.
 *
 * @return A list of all controlled resources assigned to the given user in the given workspace
 *     which were not locked by another flight. Every item in this list will have
 *     cleanup_flight_id set to the provided flight id.
 */
@WriteTransaction
public List<ControlledResource> claimCleanupForWorkspacePrivateResources(UUID workspaceId, String userEmail, String flightId) {
    String filterClause = " AND stewardship_type = :controlled_resource" + " AND access_scope = :access_scope" + " AND assigned_user = :user_email" + " AND (cleanup_flight_id IS NULL" + " OR cleanup_flight_id = :flight_id)";
    String readSql = RESOURCE_SELECT_SQL + filterClause;
    String writeSql = "UPDATE resource SET cleanup_flight_id = :flight_id WHERE workspace_id = :workspace_id" + filterClause;
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("controlled_resource", CONTROLLED.toSql()).addValue("access_scope", AccessScopeType.ACCESS_SCOPE_PRIVATE.toSql()).addValue("user_email", userEmail).addValue("flight_id", flightId);
    List<DbResource> dbResources = jdbcTemplate.query(readSql, params, DB_RESOURCE_ROW_MAPPER);
    jdbcTemplate.update(writeSql, params);
    return dbResources.stream().map(this::constructResource).map(WsmResource::castToControlledResource).collect(Collectors.toList());
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DbResource(bio.terra.workspace.db.model.DbResource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 3 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 workspaceId, 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", workspaceId.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, workspaceId);
    return deleted;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 4 with WriteTransaction

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

the class ResourceDao method setPrivateResourceState.

/**
 * Set the private_resource_state of a single private controlled resource. To set the state for
 * all a user's private resources in a workspace, use {@link
 * #setPrivateResourcesStateForWorkspaceUser(UUID, String, PrivateResourceState)}
 */
@WriteTransaction
public void setPrivateResourceState(ControlledResource resource, PrivateResourceState privateResourceState) {
    final String sql = "UPDATE resource SET private_resource_state = :private_resource_state WHERE workspace_id = :workspace_id AND resource_id = :resource_id AND access_scope = :private_access_scope";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("private_resource_state", privateResourceState.toSql()).addValue("workspace_id", resource.getWorkspaceId().toString()).addValue("resource_id", resource.getResourceId().toString()).addValue("private_access_scope", AccessScopeType.ACCESS_SCOPE_PRIVATE.toSql());
    jdbcTemplate.update(sql, params);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 5 with WriteTransaction

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

the class ResourceDao method deleteAllControlledResources.

/**
 * Deletes all controlled resources on a specified cloud platform in a workspace.
 *
 * @param workspaceId ID of the workspace to return resources from.
 * @param cloudPlatform The cloud platform to delete resources from. Unlike
 *     listControlledResources, this is not optional.
 * @return True if at least one resource was deleted, false if no resources were deleted.
 */
@WriteTransaction
public boolean deleteAllControlledResources(UUID workspaceId, CloudPlatform cloudPlatform) {
    String sql = "DELETE FROM resource WHERE workspace_id = :workspace_id AND cloud_platform = :cloud_platform AND stewardship_type = :controlled_resource";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("cloud_platform", cloudPlatform.toSql()).addValue("controlled_resource", CONTROLLED.toSql());
    int rowsDeleted = jdbcTemplate.update(sql, params);
    return rowsDeleted > 0;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Aggregations

WriteTransaction (bio.terra.common.db.WriteTransaction)16 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)16 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)4 DuplicateCloudContextException (bio.terra.workspace.service.workspace.exceptions.DuplicateCloudContextException)2 ApplicationInUseException (bio.terra.workspace.db.exception.ApplicationInUseException)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