use of bio.terra.workspace.service.resource.controlled.model.PrivateResourceState in project terra-workspace-manager by DataBiosphere.
the class CopyGcsBucketDefinitionStep method doStep.
@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
final FlightMap inputParameters = flightContext.getInputParameters();
final FlightMap workingMap = flightContext.getWorkingMap();
final CloningInstructions cloningInstructions = Optional.ofNullable(inputParameters.get(ControlledResourceKeys.CLONING_INSTRUCTIONS, CloningInstructions.class)).orElse(sourceBucket.getCloningInstructions());
// future steps need the resolved cloning instructions
workingMap.put(ControlledResourceKeys.CLONING_INSTRUCTIONS, cloningInstructions);
if (CloningInstructions.COPY_NOTHING.equals(cloningInstructions)) {
final ApiClonedControlledGcpGcsBucket noOpResult = new ApiClonedControlledGcpGcsBucket().effectiveCloningInstructions(cloningInstructions.toApiModel()).bucket(null).sourceWorkspaceId(sourceBucket.getWorkspaceId()).sourceResourceId(sourceBucket.getResourceId());
FlightUtils.setResponse(flightContext, noOpResult, HttpStatus.OK);
return StepResult.getStepResultSuccess();
}
// todo: handle COPY_REFERENCE PF-811, PF-812
final String resourceName = FlightUtils.getInputParameterOrWorkingValue(flightContext, ResourceKeys.RESOURCE_NAME, ResourceKeys.PREVIOUS_RESOURCE_NAME, String.class);
final String description = FlightUtils.getInputParameterOrWorkingValue(flightContext, ResourceKeys.RESOURCE_DESCRIPTION, ResourceKeys.PREVIOUS_RESOURCE_DESCRIPTION, String.class);
final String bucketName = Optional.ofNullable(inputParameters.get(ControlledResourceKeys.DESTINATION_BUCKET_NAME, String.class)).orElseGet(this::randomBucketName);
final PrivateResourceState privateResourceState = sourceBucket.getAccessScope() == AccessScopeType.ACCESS_SCOPE_PRIVATE ? PrivateResourceState.INITIALIZING : PrivateResourceState.NOT_APPLICABLE;
// Store effective bucket name for destination
workingMap.put(ControlledResourceKeys.DESTINATION_BUCKET_NAME, bucketName);
final UUID destinationWorkspaceId = inputParameters.get(ControlledResourceKeys.DESTINATION_WORKSPACE_ID, UUID.class);
// bucket resource for create flight
ControlledResourceFields commonFields = ControlledResourceFields.builder().workspaceId(destinationWorkspaceId).resourceId(// random ID for new resource
UUID.randomUUID()).name(resourceName).description(description).cloningInstructions(sourceBucket.getCloningInstructions()).assignedUser(sourceBucket.getAssignedUser().orElse(null)).accessScope(sourceBucket.getAccessScope()).managedBy(sourceBucket.getManagedBy()).applicationId(sourceBucket.getApplicationId()).privateResourceState(privateResourceState).build();
ControlledGcsBucketResource destinationBucketResource = ControlledGcsBucketResource.builder().bucketName(bucketName).common(commonFields).build();
final ApiGcpGcsBucketCreationParameters destinationCreationParameters = getDestinationCreationParameters(inputParameters, workingMap);
final ControlledResourceIamRole iamRole = IamRoleUtils.getIamRoleForAccessScope(sourceBucket.getAccessScope());
// Launch a CreateControlledResourcesFlight to make the destination bucket
final ControlledGcsBucketResource clonedBucket = controlledResourceService.createControlledResourceSync(destinationBucketResource, iamRole, userRequest, destinationCreationParameters).castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
workingMap.put(ControlledResourceKeys.CLONED_RESOURCE_DEFINITION, clonedBucket);
final ApiCreatedControlledGcpGcsBucket apiCreatedBucket = new ApiCreatedControlledGcpGcsBucket().gcpBucket(clonedBucket.toApiResource()).resourceId(destinationBucketResource.getResourceId());
final ApiClonedControlledGcpGcsBucket apiBucketResult = new ApiClonedControlledGcpGcsBucket().effectiveCloningInstructions(cloningInstructions.toApiModel()).bucket(apiCreatedBucket).sourceWorkspaceId(sourceBucket.getWorkspaceId()).sourceResourceId(sourceBucket.getResourceId());
workingMap.put(ControlledResourceKeys.CLONE_DEFINITION_RESULT, apiBucketResult);
if (cloningInstructions.equals(CloningInstructions.COPY_DEFINITION)) {
FlightUtils.setResponse(flightContext, apiBucketResult, HttpStatus.OK);
}
return StepResult.getStepResultSuccess();
}
use of bio.terra.workspace.service.resource.controlled.model.PrivateResourceState in project terra-workspace-manager by DataBiosphere.
the class MarkPrivateResourcesAbandonedStep method undoStep.
@Override
public StepResult undoStep(FlightContext context) throws InterruptedException {
FlightMap workingMap = context.getWorkingMap();
boolean userStillInWorkspace = workingMap.get(ControlledResourceKeys.REMOVED_USER_IS_WORKSPACE_MEMBER, Boolean.class);
// to private resources.
if (userStillInWorkspace) {
return StepResult.getStepResultSuccess();
}
List<ResourceRolePair> resourceRolePairs = workingMap.get(ControlledResourceKeys.RESOURCE_ROLES_TO_REMOVE, new TypeReference<>() {
});
List<ControlledResource> uniqueControlledResources = resourceRolePairs.stream().map(ResourceRolePair::getResource).distinct().collect(Collectors.toList());
for (ControlledResource resource : uniqueControlledResources) {
PrivateResourceState privateResourceState = resource.getPrivateResourceState().orElseThrow(() -> new InconsistentFieldsException("Received private resource without private resource state set"));
resourceDao.setPrivateResourceState(resource, privateResourceState);
}
return StepResult.getStepResultSuccess();
}
use of bio.terra.workspace.service.resource.controlled.model.PrivateResourceState in project terra-workspace-manager by DataBiosphere.
the class ResourceDao method storeResource.
private void storeResource(WsmResource resource) {
// TODO: add resource locking to fix this
// We create resources in flights, so we have steps that call resource creation that may
// get run more than once. The safe solution is to "lock" the resource by writing the flight id
// into the row at creation. Then it is possible on a re-insert to know whether the error is
// because this flight step is re-running or because some other flight used the same resource
// id. The small risk we have here is that a duplicate resource id of will appear to be
// successfully created, but in fact will be silently rejected.
final String countSql = "SELECT COUNT(*) FROM resource WHERE resource_id = :resource_id";
MapSqlParameterSource countParams = new MapSqlParameterSource().addValue("resource_id", resource.getResourceId().toString());
Integer count = jdbcTemplate.queryForObject(countSql, countParams, Integer.class);
if (count != null && count == 1) {
return;
}
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", resource.getWorkspaceId().toString()).addValue("cloud_platform", resource.getResourceType().getCloudPlatform().toString()).addValue("resource_id", resource.getResourceId().toString()).addValue("name", resource.getName()).addValue("description", resource.getDescription()).addValue("stewardship_type", resource.getStewardshipType().toSql()).addValue("exact_resource_type", resource.getResourceType().toSql()).addValue("resource_type", resource.getResourceFamily().toSql()).addValue("cloning_instructions", resource.getCloningInstructions().toSql()).addValue("attributes", resource.attributesToJson());
if (resource.getStewardshipType().equals(CONTROLLED)) {
ControlledResource controlledResource = resource.castToControlledResource();
// noinspection deprecation
params.addValue("access_scope", controlledResource.getAccessScope().toSql()).addValue("managed_by", controlledResource.getManagedBy().toSql()).addValue("associated_app", controlledResource.getApplicationId()).addValue("assigned_user", controlledResource.getAssignedUser().orElse(null)).addValue("private_resource_state", controlledResource.getPrivateResourceState().map(PrivateResourceState::toSql).orElse(null));
} else {
params.addValue("access_scope", null).addValue("managed_by", null).addValue("associated_app", null).addValue("assigned_user", null).addValue("private_resource_state", null);
}
try {
jdbcTemplate.update(sql, params);
logger.info("Inserted record for resource {} for workspace {}", resource.getResourceId(), resource.getWorkspaceId());
} 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)", resource.getName(), resource.getResourceId().toString()));
}
}
Aggregations