Search in sources :

Example 1 with UFClonedWorkspace

use of bio.terra.cli.serialization.userfacing.UFClonedWorkspace 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));
}
Also used : UFClonedResource(bio.terra.cli.serialization.userfacing.UFClonedResource) UFResource(bio.terra.cli.serialization.userfacing.UFResource) UFBqDataset(bio.terra.cli.serialization.userfacing.resource.UFBqDataset) UFGitRepo(bio.terra.cli.serialization.userfacing.resource.UFGitRepo) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) UFClonedWorkspace(bio.terra.cli.serialization.userfacing.UFClonedWorkspace) UFGcsBucket(bio.terra.cli.serialization.userfacing.resource.UFGcsBucket) Test(org.junit.jupiter.api.Test)

Example 2 with UFClonedWorkspace

use of bio.terra.cli.serialization.userfacing.UFClonedWorkspace 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);
}
Also used : UFClonedResource(bio.terra.cli.serialization.userfacing.UFClonedResource) Context(bio.terra.cli.businessobject.Context) ResourceCloneDetails(bio.terra.workspace.model.ResourceCloneDetails) Workspace(bio.terra.cli.businessobject.Workspace) Format(bio.terra.cli.command.shared.options.Format) CloneResourceResult(bio.terra.workspace.model.CloneResourceResult) Resource(bio.terra.cli.businessobject.Resource) Collectors(java.util.stream.Collectors) BaseCommand(bio.terra.cli.command.shared.BaseCommand) WorkspaceNameAndDescription(bio.terra.cli.command.shared.options.WorkspaceNameAndDescription) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride) UFClonedResource(bio.terra.cli.serialization.userfacing.UFClonedResource) UFClonedWorkspace(bio.terra.cli.serialization.userfacing.UFClonedWorkspace) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) Optional(java.util.Optional) Command(picocli.CommandLine.Command) ClonedWorkspace(bio.terra.workspace.model.ClonedWorkspace) CommandLine(picocli.CommandLine) UFClonedWorkspace(bio.terra.cli.serialization.userfacing.UFClonedWorkspace) ClonedWorkspace(bio.terra.workspace.model.ClonedWorkspace) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) UFClonedWorkspace(bio.terra.cli.serialization.userfacing.UFClonedWorkspace) Workspace(bio.terra.cli.businessobject.Workspace) UFClonedWorkspace(bio.terra.cli.serialization.userfacing.UFClonedWorkspace) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) ClonedWorkspace(bio.terra.workspace.model.ClonedWorkspace) WorkspaceOverride(bio.terra.cli.command.shared.options.WorkspaceOverride)

Aggregations

UFClonedResource (bio.terra.cli.serialization.userfacing.UFClonedResource)2 UFClonedWorkspace (bio.terra.cli.serialization.userfacing.UFClonedWorkspace)2 UFWorkspace (bio.terra.cli.serialization.userfacing.UFWorkspace)2 Context (bio.terra.cli.businessobject.Context)1 Resource (bio.terra.cli.businessobject.Resource)1 Workspace (bio.terra.cli.businessobject.Workspace)1 BaseCommand (bio.terra.cli.command.shared.BaseCommand)1 Format (bio.terra.cli.command.shared.options.Format)1 WorkspaceNameAndDescription (bio.terra.cli.command.shared.options.WorkspaceNameAndDescription)1 WorkspaceOverride (bio.terra.cli.command.shared.options.WorkspaceOverride)1 UFResource (bio.terra.cli.serialization.userfacing.UFResource)1 UFBqDataset (bio.terra.cli.serialization.userfacing.resource.UFBqDataset)1 UFGcsBucket (bio.terra.cli.serialization.userfacing.resource.UFGcsBucket)1 UFGitRepo (bio.terra.cli.serialization.userfacing.resource.UFGitRepo)1 CloneResourceResult (bio.terra.workspace.model.CloneResourceResult)1 ClonedWorkspace (bio.terra.workspace.model.ClonedWorkspace)1 ResourceCloneDetails (bio.terra.workspace.model.ResourceCloneDetails)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.jupiter.api.Test)1