use of bio.terra.cli.serialization.userfacing.UFClonedResource in project terra-cli by DataBiosphere.
the class CloneWorkspace method cloneWorkspace.
@Test
public void cloneWorkspace(TestInfo testInfo) throws Exception {
workspaceCreator.login();
// create a workspace
// `terra workspace create --format=json`
sourceWorkspace = TestCommand.runAndParseCommandExpectSuccess(UFWorkspace.class, "workspace", "create");
// Add a bucket resource
UFGcsBucket sourceBucket = TestCommand.runAndParseCommandExpectSuccess(UFGcsBucket.class, "resource", "create", "gcs-bucket", "--name=" + "bucket_1", "--bucket-name=" + UUID.randomUUID(), "--cloning=COPY_RESOURCE");
// Add another bucket resource with COPY_NOTHING
UFGcsBucket copyNothingBucket = TestCommand.runAndParseCommandExpectSuccess(UFGcsBucket.class, "resource", "create", "gcs-bucket", "--name=" + "bucket_2", "--bucket-name=" + UUID.randomUUID(), "--cloning=COPY_NOTHING");
// Add a dataset resource
UFBqDataset sourceDataset = TestCommand.runAndParseCommandExpectSuccess(UFBqDataset.class, "resource", "create", "bq-dataset", "--name=dataset_1", "--dataset-id=dataset_1", "--description=The first dataset.", "--cloning=COPY_RESOURCE");
UFBqDataset datasetReference = TestCommand.runAndParseCommandExpectSuccess(UFBqDataset.class, "resource", "add-ref", "bq-dataset", "--name=dataset_ref", "--project-id=" + externalDataset.getProjectId(), "--dataset-id=" + externalDataset.getDatasetId(), "--cloning=COPY_REFERENCE");
UFGitRepo gitRepositoryReference = TestCommand.runAndParseCommandExpectSuccess(UFGitRepo.class, "resource", "add-ref", "git-repo", "--name=" + GIT_REPO_REF_NAME, "--repo-url=" + GIT_REPO_HTTPS_URL, "--cloning=COPY_REFERENCE");
// Clone the workspace
UFClonedWorkspace clonedWorkspace = TestCommand.runAndParseCommandExpectSuccess(UFClonedWorkspace.class, "workspace", "clone", "--name=cloned_workspace", "--description=A clone.");
assertEquals(sourceWorkspace.id, clonedWorkspace.sourceWorkspace.id, "Correct source workspace ID for clone.");
destinationWorkspace = clonedWorkspace.destinationWorkspace;
assertThat("There are 5 cloned resources", clonedWorkspace.resources, hasSize(SOURCE_RESOURCE_NUM));
UFClonedResource bucketClonedResource = getOrFail(clonedWorkspace.resources.stream().filter(cr -> sourceBucket.id.equals(cr.sourceResource.id)).findFirst());
assertEquals(CloneResourceResult.SUCCEEDED, bucketClonedResource.result, "bucket clone succeeded");
assertNotNull(bucketClonedResource.destinationResource, "Destination bucket resource was created");
UFClonedResource copyNothingBucketClonedResource = getOrFail(clonedWorkspace.resources.stream().filter(cr -> copyNothingBucket.id.equals(cr.sourceResource.id)).findFirst());
assertEquals(CloneResourceResult.SKIPPED, copyNothingBucketClonedResource.result, "COPY_NOTHING resource was skipped.");
assertNull(copyNothingBucketClonedResource.destinationResource, "Skipped resource has no destination resource.");
UFClonedResource datasetRefClonedResource = getOrFail(clonedWorkspace.resources.stream().filter(cr -> datasetReference.id.equals(cr.sourceResource.id)).findFirst());
assertEquals(CloneResourceResult.SUCCEEDED, datasetRefClonedResource.result, "Dataset reference clone succeeded.");
assertEquals(StewardshipType.REFERENCED, datasetRefClonedResource.destinationResource.stewardshipType, "Dataset reference has correct stewardship type.");
UFClonedResource datasetClonedResource = getOrFail(clonedWorkspace.resources.stream().filter(cr -> sourceDataset.id.equals(cr.sourceResource.id)).findFirst());
assertEquals(CloneResourceResult.SUCCEEDED, datasetClonedResource.result, "Dataset clone succeeded.");
assertEquals("The first dataset.", datasetClonedResource.destinationResource.description, "Dataset description matches.");
UFClonedResource gitRepoClonedResource = getOrFail(clonedWorkspace.resources.stream().filter(cr -> gitRepositoryReference.id.equals(cr.sourceResource.id)).findFirst());
assertEquals(CloneResourceResult.SUCCEEDED, gitRepoClonedResource.result, "Git repo clone succeeded");
assertEquals(GIT_REPO_REF_NAME, gitRepoClonedResource.destinationResource.name, "Resource type matches GIT_REPO");
// Switch to the new workspace from the clone
TestCommand.runCommandExpectSuccess("workspace", "set", "--id=" + clonedWorkspace.destinationWorkspace.id);
// Validate resources
List<UFResource> resources = TestCommand.runAndParseCommandExpectSuccess(new TypeReference<>() {
}, "resource", "list");
assertThat("Destination workspace has three resources.", resources, hasSize(DESTINATION_RESOURCE_NUM));
}
use of bio.terra.cli.serialization.userfacing.UFClonedResource in project terra-cli by DataBiosphere.
the class Clone method buildUfClonedResource.
private UFClonedResource buildUfClonedResource(Workspace sourceWorkspace, Workspace destinationWorkspace, ResourceCloneDetails resourceCloneDetails) {
Resource sourceResource = sourceWorkspace.getResource(resourceCloneDetails.getName());
final Resource destinationResource;
if (CloneResourceResult.SUCCEEDED == resourceCloneDetails.getResult()) {
destinationResource = destinationWorkspace.getResource(resourceCloneDetails.getName());
} else {
destinationResource = null;
}
return new UFClonedResource(resourceCloneDetails, sourceResource.serializeToCommand(), Optional.ofNullable(destinationResource).map(Resource::serializeToCommand).orElse(null));
}
use of bio.terra.cli.serialization.userfacing.UFClonedResource in project terra-cli by DataBiosphere.
the class Clone method execute.
@Override
protected void execute() {
workspaceOption.overrideIfSpecified();
Workspace sourceWorkspace = Context.requireWorkspace();
ClonedWorkspace clonedWorkspace = sourceWorkspace.clone(workspaceNameAndDescription.displayName, workspaceNameAndDescription.description);
Workspace destinationWorkspaceHydrated = Workspace.get(clonedWorkspace.getDestinationWorkspaceId());
// Get a list of UFClonedResource objects based on the resources returned in the ClonedWorkspace
java.util.List<UFClonedResource> ufClonedResources = clonedWorkspace.getResources().stream().map(r -> buildUfClonedResource(sourceWorkspace, destinationWorkspaceHydrated, r)).collect(Collectors.toList());
// print results
formatOption.printReturnValue(new UFClonedWorkspace(new UFWorkspace(sourceWorkspace), new UFWorkspace(destinationWorkspaceHydrated), ufClonedResources), this::printText);
}
Aggregations