Search in sources :

Example 1 with PrivateResourceState

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();
}
Also used : CloningInstructions(bio.terra.workspace.service.resource.model.CloningInstructions) FlightMap(bio.terra.stairway.FlightMap) ApiClonedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiClonedControlledGcpGcsBucket) ControlledResourceIamRole(bio.terra.workspace.service.iam.model.ControlledResourceIamRole) UUID(java.util.UUID) ControlledResourceFields(bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields) ApiGcpGcsBucketCreationParameters(bio.terra.workspace.generated.model.ApiGcpGcsBucketCreationParameters) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) ApiCreatedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket) PrivateResourceState(bio.terra.workspace.service.resource.controlled.model.PrivateResourceState)

Example 2 with PrivateResourceState

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();
}
Also used : ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) FlightMap(bio.terra.stairway.FlightMap) InconsistentFieldsException(bio.terra.common.exception.InconsistentFieldsException) PrivateResourceState(bio.terra.workspace.service.resource.controlled.model.PrivateResourceState)

Example 3 with PrivateResourceState

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()));
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateResourceException(bio.terra.workspace.service.resource.exception.DuplicateResourceException) PrivateResourceState(bio.terra.workspace.service.resource.controlled.model.PrivateResourceState)

Aggregations

PrivateResourceState (bio.terra.workspace.service.resource.controlled.model.PrivateResourceState)3 FlightMap (bio.terra.stairway.FlightMap)2 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)2 InconsistentFieldsException (bio.terra.common.exception.InconsistentFieldsException)1 ApiClonedControlledGcpGcsBucket (bio.terra.workspace.generated.model.ApiClonedControlledGcpGcsBucket)1 ApiCreatedControlledGcpGcsBucket (bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket)1 ApiGcpGcsBucketCreationParameters (bio.terra.workspace.generated.model.ApiGcpGcsBucketCreationParameters)1 ControlledResourceIamRole (bio.terra.workspace.service.iam.model.ControlledResourceIamRole)1 ControlledGcsBucketResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource)1 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)1 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)1 CloningInstructions (bio.terra.workspace.service.resource.model.CloningInstructions)1 UUID (java.util.UUID)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1