use of bio.terra.workspace.model.ClonedControlledGcpGcsBucket in project terra-workspace-manager by DataBiosphere.
the class ControlledGcsBucketLifecycle method testCloneBucket.
private void testCloneBucket(GcpGcsBucketResource sourceBucket, TestUserSpecification cloningUser, ControlledGcpResourceApi resourceApi) throws Exception {
final String destinationBucketName = "clone-" + UUID.randomUUID().toString();
// clone the bucket
final String clonedBucketDescription = "A cloned bucket";
final CloneControlledGcpGcsBucketRequest cloneRequest = new CloneControlledGcpGcsBucketRequest().bucketName(destinationBucketName).destinationWorkspaceId(getDestinationWorkspaceId()).name(sourceBucket.getMetadata().getName()).description(clonedBucketDescription).location(// use same as src
null).cloningInstructions(CloningInstructionsEnum.RESOURCE).jobControl(new JobControl().id(UUID.randomUUID().toString()));
logger.info("Cloning bucket\n\tname: {}\n\tresource ID: {}\n\tworkspace: {}\n\t" + "projectID: {}\ninto destination bucket\n\tname: {}\n\tworkspace: {}\n\tprojectID: {}", sourceBucket.getMetadata().getName(), sourceBucket.getMetadata().getResourceId(), sourceBucket.getMetadata().getWorkspaceId(), getSourceProjectId(), destinationBucketName, getDestinationWorkspaceId(), getDestinationProjectId());
CloneControlledGcpGcsBucketResult cloneResult = resourceApi.cloneGcsBucket(cloneRequest, sourceBucket.getMetadata().getWorkspaceId(), sourceBucket.getMetadata().getResourceId());
cloneResult = ClientTestUtils.pollWhileRunning(cloneResult, () -> resourceApi.getCloneGcsBucketResult(cloneRequest.getDestinationWorkspaceId(), cloneRequest.getJobControl().getId()), CloneControlledGcpGcsBucketResult::getJobReport, Duration.ofSeconds(5));
ClientTestUtils.assertJobSuccess("cloned bucket", cloneResult.getJobReport(), cloneResult.getErrorReport());
final ClonedControlledGcpGcsBucket clonedBucket = cloneResult.getBucket();
assertEquals(getWorkspaceId(), clonedBucket.getSourceWorkspaceId());
assertEquals(sourceBucket.getMetadata().getResourceId(), clonedBucket.getSourceResourceId());
final CreatedControlledGcpGcsBucket createdBucket = clonedBucket.getBucket();
final GcpGcsBucketResource clonedResource = createdBucket.getGcpBucket();
assertEquals(destinationBucketName, clonedResource.getAttributes().getBucketName());
final ResourceMetadata clonedResourceMetadata = clonedResource.getMetadata();
assertEquals(getDestinationWorkspaceId(), clonedResourceMetadata.getWorkspaceId());
assertEquals(sourceBucket.getMetadata().getName(), clonedResourceMetadata.getName());
assertEquals(clonedBucketDescription, clonedResourceMetadata.getDescription());
final ResourceMetadata sourceMetadata = sourceBucket.getMetadata();
assertEquals(CloningInstructionsEnum.NOTHING, clonedResourceMetadata.getCloningInstructions());
assertEquals(sourceMetadata.getCloudPlatform(), clonedResourceMetadata.getCloudPlatform());
assertEquals(ResourceType.GCS_BUCKET, clonedResourceMetadata.getResourceType());
assertEquals(StewardshipType.CONTROLLED, clonedResourceMetadata.getStewardshipType());
assertEquals(sourceMetadata.getControlledResourceMetadata().getAccessScope(), clonedResourceMetadata.getControlledResourceMetadata().getAccessScope());
assertEquals(sourceMetadata.getControlledResourceMetadata().getManagedBy(), clonedResourceMetadata.getControlledResourceMetadata().getManagedBy());
assertEquals(sourceMetadata.getControlledResourceMetadata().getPrivateResourceUser(), clonedResourceMetadata.getControlledResourceMetadata().getPrivateResourceUser());
assertEquals(CloudPlatform.GCP, clonedResourceMetadata.getCloudPlatform());
final Storage destinationProjectStorageClient = ClientTestUtils.getGcpStorageClient(cloningUser, getDestinationProjectId());
final Bucket destinationGcsBucket = destinationProjectStorageClient.get(destinationBucketName);
// Location, storage class, and lifecycle rules should match values from createBucketAttempt
assertEquals(StorageClass.STANDARD, destinationGcsBucket.getStorageClass());
assertEquals(BUCKET_LOCATION, // default since not specified
destinationGcsBucket.getLocation());
assertEquals(2, destinationGcsBucket.getLifecycleRules().size());
verifyClonedLifecycleRules(destinationGcsBucket);
assertEquals(CloningInstructionsEnum.RESOURCE, clonedBucket.getEffectiveCloningInstructions());
// test retrieving file from destination bucket
Storage cloningUserStorageClient = ClientTestUtils.getGcpStorageClient(cloningUser, getDestinationProjectId());
BlobId blobId = BlobId.of(destinationBucketName, GCS_BLOB_NAME);
assertNotNull(blobId);
final Blob retrievedFile = cloningUserStorageClient.get(blobId);
assertNotNull(retrievedFile);
assertEquals(blobId.getName(), retrievedFile.getBlobId().getName());
}
Aggregations