Search in sources :

Example 6 with ControlledGcsBucketResource

use of bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource in project terra-workspace-manager by DataBiosphere.

the class ControlledGcsBucketResourceTest method testSerialization.

@Test
public void testSerialization() throws JsonProcessingException {
    ControlledGcsBucketResource gcsBucketResource = ControlledResourceFixtures.makeDefaultControlledGcsBucketBuilder(null).build();
    final ObjectMapper objectMapper = StairwayMapper.getObjectMapper();
    final String serialized = objectMapper.writeValueAsString(gcsBucketResource);
    final ControlledGcsBucketResource deserialized = objectMapper.readValue(serialized, ControlledGcsBucketResource.class);
    assertThat(deserialized, equalTo(gcsBucketResource));
}
Also used : ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 7 with ControlledGcsBucketResource

use of bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource in project terra-workspace-manager by DataBiosphere.

the class ResourceDaoTest method listAndDeleteControlledResourceInContext.

@Test
public void listAndDeleteControlledResourceInContext() {
    UUID workspaceId = createGcpWorkspace();
    ControlledGcsBucketResource bucket = ControlledResourceFixtures.makeDefaultControlledGcsBucketBuilder(workspaceId).build();
    ControlledBigQueryDatasetResource dataset = ControlledResourceFixtures.makeDefaultControlledBigQueryBuilder(workspaceId).build();
    resourceDao.createControlledResource(bucket);
    resourceDao.createControlledResource(dataset);
    List<ControlledResource> gcpList = resourceDao.listControlledResources(workspaceId, CloudPlatform.GCP);
    List<ControlledResource> azureList = resourceDao.listControlledResources(workspaceId, CloudPlatform.AZURE);
    List<ControlledResource> allCloudList = resourceDao.listControlledResources(workspaceId, null);
    assertTrue(azureList.isEmpty());
    assertThat(gcpList, containsInAnyOrder(bucket, dataset));
    assertThat(allCloudList, containsInAnyOrder(bucket, dataset));
    assertTrue(resourceDao.deleteAllControlledResources(workspaceId, CloudPlatform.GCP));
    assertFalse(resourceDao.deleteAllControlledResources(workspaceId, CloudPlatform.AZURE));
    List<ControlledResource> listAfterDeletion = resourceDao.listControlledResources(workspaceId, CloudPlatform.GCP);
    assertTrue(listAfterDeletion.isEmpty());
}
Also used : ControlledResource(bio.terra.workspace.service.resource.controlled.model.ControlledResource) UUID(java.util.UUID) ControlledBigQueryDatasetResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 8 with ControlledGcsBucketResource

use of bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource in project terra-workspace-manager by DataBiosphere.

the class ResourceDaoTest method duplicateControlledBucketNameRejected.

@Test
public void duplicateControlledBucketNameRejected() {
    final String clashingBucketName = "not-a-pail";
    final UUID workspaceId1 = createGcpWorkspace();
    final ControlledGcsBucketResource initialResource = ControlledResourceFixtures.makeDefaultControlledGcsBucketBuilder(workspaceId1).bucketName(clashingBucketName).build();
    resourceDao.createControlledResource(initialResource);
    final UUID workspaceId2 = createGcpWorkspace();
    final ControlledGcsBucketResource duplicatingResource = ControlledResourceFixtures.makeDefaultControlledGcsBucketBuilder(workspaceId2).bucketName(clashingBucketName).build();
    assertThrows(DuplicateResourceException.class, () -> resourceDao.createControlledResource(duplicatingResource));
    // clean up
    resourceDao.deleteResource(initialResource.getWorkspaceId(), initialResource.getResourceId());
    resourceDao.deleteResource(duplicatingResource.getWorkspaceId(), duplicatingResource.getResourceId());
}
Also used : UUID(java.util.UUID) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) Test(org.junit.jupiter.api.Test) BaseUnitTest(bio.terra.workspace.common.BaseUnitTest)

Example 9 with ControlledGcsBucketResource

use of bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource in project terra-workspace-manager by DataBiosphere.

the class ControlledGcpResourceApiController method createBucket.

@Override
public ResponseEntity<ApiCreatedControlledGcpGcsBucket> createBucket(UUID workspaceId, @Valid ApiCreateControlledGcpGcsBucketRequestBody body) {
    final AuthenticatedUserRequest userRequest = getAuthenticatedInfo();
    ControlledResourceFields commonFields = toCommonFields(workspaceId, body.getCommon(), userRequest);
    ControlledGcsBucketResource resource = ControlledGcsBucketResource.builder().bucketName(body.getGcsBucket().getName()).common(commonFields).build();
    final ControlledGcsBucketResource createdBucket = controlledResourceService.createControlledResourceSync(resource, commonFields.getIamRole(), userRequest, body.getGcsBucket()).castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
    var response = new ApiCreatedControlledGcpGcsBucket().resourceId(createdBucket.getResourceId()).gcpBucket(createdBucket.toApiResource());
    return new ResponseEntity<>(response, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) ControlledResourceFields(bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields) ControlledGcsBucketResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource) ApiCreatedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket)

Example 10 with ControlledGcsBucketResource

use of bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource 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)

Aggregations

ControlledGcsBucketResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.ControlledGcsBucketResource)19 Test (org.junit.jupiter.api.Test)13 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)8 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)8 UUID (java.util.UUID)6 StepStatus (bio.terra.stairway.StepStatus)5 BaseUnitTest (bio.terra.workspace.common.BaseUnitTest)5 HashMap (java.util.HashMap)5 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)4 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)4 ControlledResourceFields (bio.terra.workspace.service.resource.controlled.model.ControlledResourceFields)4 StorageCow (bio.terra.cloudres.google.storage.StorageCow)3 FlightMap (bio.terra.stairway.FlightMap)3 ApiGcpGcsBucketCreationParameters (bio.terra.workspace.generated.model.ApiGcpGcsBucketCreationParameters)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ApiCreatedControlledGcpGcsBucket (bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket)2 CreateGcsBucketStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.CreateGcsBucketStep)2 GcsBucketCloudSyncStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.GcsBucketCloudSyncStep)2 RetrieveGcsBucketCloudAttributesStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.RetrieveGcsBucketCloudAttributesStep)2 UpdateGcsBucketStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.gcsbucket.UpdateGcsBucketStep)2