Search in sources :

Example 1 with GcpBigQueryDataTableResource

use of bio.terra.workspace.model.GcpBigQueryDataTableResource in project terra-workspace-manager by DataBiosphere.

the class ReferencedBigQueryResourceLifecycle method testUpdateReferences.

private void testUpdateReferences(GcpBigQueryDatasetResource dataset, GcpBigQueryDataTableResource table, ReferencedGcpResourceApi fullAccessApi) throws Exception {
    ReferencedGcpResourceApi partialAccessApi = ClientTestUtils.getReferencedGcpResourceClient(partialAccessUser, server);
    ResourceApi partialAccessResourceApi = ClientTestUtils.getResourceClient(partialAccessUser, server);
    UUID bqDatasetResourceId = dataset.getMetadata().getResourceId();
    UUID bqTableResourceId = table.getMetadata().getResourceId();
    // Update BQ dataset's name and description
    String newDatasetName = "newDatasetName";
    String newDatasetDescription = "newDescription";
    BqDatasetUtils.updateBigQueryDatasetReference(fullAccessApi, getWorkspaceId(), bqDatasetResourceId, newDatasetName, newDatasetDescription, /*projectId=*/
    null, /*datasetId=*/
    null);
    GcpBigQueryDatasetResource datasetReferenceFirstUpdate = fullAccessApi.getBigQueryDatasetReference(getWorkspaceId(), bqDatasetResourceId);
    assertEquals(newDatasetName, datasetReferenceFirstUpdate.getMetadata().getName());
    assertEquals(newDatasetDescription, datasetReferenceFirstUpdate.getMetadata().getDescription());
    assertEquals(referencedBqTableAttributes.getDatasetId(), datasetReferenceFirstUpdate.getAttributes().getDatasetId());
    assertEquals(referencedBqTableAttributes.getProjectId(), datasetReferenceFirstUpdate.getAttributes().getProjectId());
    // {@code userWithPartialAccess} does not have access to the original dataset.
    assertFalse(partialAccessResourceApi.checkReferenceAccess(getWorkspaceId(), bqDatasetResourceId));
    // Update BQ dataset's referencing target
    // Attempt to update the referencing target but {@code userWithPartialAccess} does not have
    // access to the original dataset.
    assertThrows(ApiException.class, () -> BqDatasetUtils.updateBigQueryDatasetReference(partialAccessApi, getWorkspaceId(), bqDatasetResourceId, /*name=*/
    null, /*description=*/
    null, /*projectId=*/
    null, bqTableFromAlternateDatasetAttributes.getDatasetId()));
    BqDatasetUtils.updateBigQueryDatasetReference(fullAccessApi, getWorkspaceId(), bqDatasetResourceId, /*name=*/
    null, /*description=*/
    null, /*projectId=*/
    null, bqTableFromAlternateDatasetAttributes.getDatasetId());
    GcpBigQueryDatasetResource datasetReferenceSecondUpdate = fullAccessApi.getBigQueryDatasetReference(getWorkspaceId(), bqDatasetResourceId);
    assertEquals(newDatasetName, datasetReferenceSecondUpdate.getMetadata().getName());
    assertEquals(newDatasetDescription, datasetReferenceSecondUpdate.getMetadata().getDescription());
    assertEquals(table.getAttributes().getProjectId(), datasetReferenceSecondUpdate.getAttributes().getProjectId());
    assertEquals(bqTableFromAlternateDatasetAttributes.getDatasetId(), datasetReferenceSecondUpdate.getAttributes().getDatasetId());
    // {@code userWithPartialAccess} have access to dataset 2. Now since the reference is pointing
    // to dataset 2, the user have access to this reference now.
    assertTrue(partialAccessResourceApi.checkReferenceAccess(getWorkspaceId(), bqDatasetResourceId));
    // Update BQ data table's name and description.
    String newDataTableName = "newDataTableName";
    String newDataTableDescription = "a new description to the new data table reference";
    BqDataTableUtils.updateBigQueryDataTableReference(fullAccessApi, getWorkspaceId(), bqTableResourceId, newDataTableName, newDataTableDescription, /*projectId=*/
    null, /*datasetId=*/
    null, /*tableId=*/
    null);
    GcpBigQueryDataTableResource dataTableReferenceFirstUpdate = fullAccessApi.getBigQueryDataTableReference(getWorkspaceId(), bqTableResourceId);
    assertEquals(newDataTableName, dataTableReferenceFirstUpdate.getMetadata().getName());
    assertEquals(newDataTableDescription, dataTableReferenceFirstUpdate.getMetadata().getDescription());
    assertEquals(table.getAttributes().getProjectId(), dataTableReferenceFirstUpdate.getAttributes().getProjectId());
    assertEquals(table.getAttributes().getDatasetId(), dataTableReferenceFirstUpdate.getAttributes().getDatasetId());
    assertEquals(table.getAttributes().getDataTableId(), dataTableReferenceFirstUpdate.getAttributes().getDataTableId());
    // Update bq data table target
    // Attempt to update bq data table reference but {@code userWithPartialAccess} does not have
    // access to the bq table 2.
    assertThrows(ApiException.class, () -> BqDataTableUtils.updateBigQueryDataTableReference(partialAccessApi, getWorkspaceId(), bqTableResourceId, /*name=*/
    null, /*description=*/
    null, /*projectId=*/
    null, bqTableFromAlternateDatasetAttributes.getDatasetId(), bqTableFromAlternateDatasetAttributes.getDataTableId()));
    // Successfully update the referencing target because the {@code userWithFullAccess} has
    // access to the bq table 2.
    BqDataTableUtils.updateBigQueryDataTableReference(fullAccessApi, getWorkspaceId(), bqTableResourceId, /*name=*/
    null, /*description=*/
    null, /*projectId=*/
    null, bqTableFromAlternateDatasetAttributes.getDatasetId(), bqTableFromAlternateDatasetAttributes.getDataTableId());
    GcpBigQueryDataTableResource dataTableReferenceSecondUpdate = fullAccessApi.getBigQueryDataTableReference(getWorkspaceId(), bqTableResourceId);
    assertEquals(newDataTableName, dataTableReferenceSecondUpdate.getMetadata().getName());
    assertEquals(newDataTableDescription, dataTableReferenceSecondUpdate.getMetadata().getDescription());
    assertEquals(table.getAttributes().getProjectId(), dataTableReferenceSecondUpdate.getAttributes().getProjectId());
    assertEquals(bqTableFromAlternateDatasetAttributes.getDatasetId(), dataTableReferenceSecondUpdate.getAttributes().getDatasetId());
    assertEquals(bqTableFromAlternateDatasetAttributes.getDataTableId(), dataTableReferenceSecondUpdate.getAttributes().getDataTableId());
    BqDataTableUtils.updateBigQueryDataTableReference(fullAccessApi, getWorkspaceId(), bqTableResourceId, /*name=*/
    null, /*description=*/
    null, /*projectId=*/
    null, table.getAttributes().getDatasetId(), /*tableId=*/
    null);
    GcpBigQueryDataTableResource dataTableReferenceThirdUpdate = fullAccessApi.getBigQueryDataTableReference(getWorkspaceId(), bqTableResourceId);
    assertEquals(newDataTableName, dataTableReferenceThirdUpdate.getMetadata().getName());
    assertEquals(newDataTableDescription, dataTableReferenceThirdUpdate.getMetadata().getDescription());
    assertEquals(table.getAttributes().getProjectId(), dataTableReferenceThirdUpdate.getAttributes().getProjectId());
    assertEquals(table.getAttributes().getDatasetId(), dataTableReferenceThirdUpdate.getAttributes().getDatasetId());
    assertEquals(bqTableFromAlternateDatasetAttributes.getDataTableId(), dataTableReferenceThirdUpdate.getAttributes().getDataTableId());
    BqDataTableUtils.updateBigQueryDataTableReference(fullAccessApi, getWorkspaceId(), bqTableResourceId, /*name=*/
    null, /*description=*/
    null, /*projectId=*/
    null, /*datasetId=*/
    null, table.getAttributes().getDataTableId());
    GcpBigQueryDataTableResource dataTableReferenceFourthUpdate = fullAccessApi.getBigQueryDataTableReference(getWorkspaceId(), bqTableResourceId);
    assertEquals(newDataTableName, dataTableReferenceFourthUpdate.getMetadata().getName());
    assertEquals(newDataTableDescription, dataTableReferenceFourthUpdate.getMetadata().getDescription());
    assertEquals(table.getAttributes().getProjectId(), dataTableReferenceFourthUpdate.getAttributes().getProjectId());
    assertEquals(table.getAttributes().getDatasetId(), dataTableReferenceFourthUpdate.getAttributes().getDatasetId());
    assertEquals(table.getAttributes().getDataTableId(), dataTableReferenceFourthUpdate.getAttributes().getDataTableId());
}
Also used : ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) ResourceApi(bio.terra.workspace.api.ResourceApi) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) GcpBigQueryDataTableResource(bio.terra.workspace.model.GcpBigQueryDataTableResource) UUID(java.util.UUID) GcpBigQueryDatasetResource(bio.terra.workspace.model.GcpBigQueryDatasetResource)

Example 2 with GcpBigQueryDataTableResource

use of bio.terra.workspace.model.GcpBigQueryDataTableResource in project terra-workspace-manager by DataBiosphere.

the class ReferencedBigQueryResourceLifecycle method testGetReferences.

private void testGetReferences(GcpBigQueryDatasetResource referencedDataset, GcpBigQueryDataTableResource referencedDataTable, ReferencedGcpResourceApi referencedGcpResourceApi) throws Exception {
    // Get the references
    GcpBigQueryDatasetResource fetchedDataset = referencedGcpResourceApi.getBigQueryDatasetReference(getWorkspaceId(), bqDatasetResourceId);
    assertEquals(referencedDataset, fetchedDataset);
    GcpBigQueryDataTableResource fetchedDataTable = referencedGcpResourceApi.getBigQueryDataTableReference(getWorkspaceId(), bqDataTableResourceId);
    assertEquals(referencedDataTable, fetchedDataTable);
    // Enumerate the references
    // Any workspace member can view references in WSM, even if they can't view the underlying cloud
    // resource or contents.
    ResourceApi noAccessApi = ClientTestUtils.getResourceClient(noAccessUser, server);
    ResourceList referenceList = noAccessApi.enumerateResources(getWorkspaceId(), 0, 5, /*referenceType=*/
    null, StewardshipType.REFERENCED);
    assertEquals(2, referenceList.getResources().size());
    ResourceList datasetList = noAccessApi.enumerateResources(getWorkspaceId(), 0, 5, /*referenceType=*/
    ResourceType.BIG_QUERY_DATASET, StewardshipType.REFERENCED);
    assertEquals(1, datasetList.getResources().size());
    MultiResourcesUtils.assertResourceType(ResourceType.BIG_QUERY_DATASET, datasetList);
    ResourceList tableList = noAccessApi.enumerateResources(getWorkspaceId(), 0, 5, /*referenceType=*/
    ResourceType.BIG_QUERY_DATA_TABLE, StewardshipType.REFERENCED);
    assertEquals(1, tableList.getResources().size());
    MultiResourcesUtils.assertResourceType(ResourceType.BIG_QUERY_DATA_TABLE, tableList);
}
Also used : ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) ResourceApi(bio.terra.workspace.api.ResourceApi) ResourceList(bio.terra.workspace.model.ResourceList) GcpBigQueryDataTableResource(bio.terra.workspace.model.GcpBigQueryDataTableResource) GcpBigQueryDatasetResource(bio.terra.workspace.model.GcpBigQueryDatasetResource)

Example 3 with GcpBigQueryDataTableResource

use of bio.terra.workspace.model.GcpBigQueryDataTableResource in project terra-workspace-manager by DataBiosphere.

the class ReferencedBigQueryResourceLifecycle method doUserJourney.

@Override
protected void doUserJourney(TestUserSpecification testUser, WorkspaceApi workspaceApi) throws Exception {
    ReferencedGcpResourceApi referencedGcpResourceApi = ClientTestUtils.getReferencedGcpResourceClient(testUser, server);
    // Grant secondary users READER permission in the workspace.
    workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(partialAccessUser.userEmail), getWorkspaceId(), IamRole.READER);
    workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(noAccessUser.userEmail), getWorkspaceId(), IamRole.READER);
    // Create the references
    GcpBigQueryDatasetResource referencedDataset = BqDatasetUtils.makeBigQueryDatasetReference(referencedBqDatasetAttributes, referencedGcpResourceApi, getWorkspaceId(), MultiResourcesUtils.makeName(), CloningInstructionsEnum.REFERENCE);
    bqDatasetResourceId = referencedDataset.getMetadata().getResourceId();
    GcpBigQueryDataTableResource referencedDataTable = BqDatasetUtils.makeBigQueryDataTableReference(referencedBqTableAttributes, referencedGcpResourceApi, getWorkspaceId(), MultiResourcesUtils.makeName(), CloningInstructionsEnum.REFERENCE);
    bqDataTableResourceId = referencedDataTable.getMetadata().getResourceId();
    // Get references
    testGetReferences(referencedDataset, referencedDataTable, referencedGcpResourceApi);
    // Clone references
    testCloneReferences(referencedDataset, referencedDataTable, referencedGcpResourceApi, workspaceApi);
    // Validate reference access
    testValidateReferences(testUser);
    // Update the references
    testUpdateReferences(referencedDataset, referencedDataTable, referencedGcpResourceApi);
    // Delete the references
    referencedGcpResourceApi.deleteBigQueryDatasetReference(getWorkspaceId(), bqDatasetResourceId);
    referencedGcpResourceApi.deleteBigQueryDataTableReference(getWorkspaceId(), bqDataTableResourceId);
    // Enumerating all resources with no filters should be empty
    ResourceApi resourceApi = ClientTestUtils.getResourceClient(testUser, server);
    ResourceList enumerateResult = resourceApi.enumerateResources(getWorkspaceId(), 0, 100, null, null);
    assertTrue(enumerateResult.getResources().isEmpty());
}
Also used : ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) ResourceApi(bio.terra.workspace.api.ResourceApi) ResourceList(bio.terra.workspace.model.ResourceList) GrantRoleRequestBody(bio.terra.workspace.model.GrantRoleRequestBody) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) GcpBigQueryDataTableResource(bio.terra.workspace.model.GcpBigQueryDataTableResource) GcpBigQueryDatasetResource(bio.terra.workspace.model.GcpBigQueryDatasetResource)

Example 4 with GcpBigQueryDataTableResource

use of bio.terra.workspace.model.GcpBigQueryDataTableResource in project terra-cli by DataBiosphere.

the class BqTable method addReferenced.

/**
 * Add a BigQuery data table as a referenced resource in the workspace.
 *
 * @return the resource that was added
 */
public static BqTable addReferenced(AddBqTableParams createParams) {
    validateEnvironmentVariableName(createParams.resourceFields.name);
    GcpBigQueryDataTableResource addedResource = WorkspaceManagerService.fromContext().createReferencedBigQueryDataTable(Context.requireWorkspace().getId(), createParams);
    logger.info("Created BQ data table: {}", addedResource);
    // convert the WSM object to a CLI object
    Context.requireWorkspace().listResourcesAndSync();
    return new BqTable(addedResource);
}
Also used : PDBqTable(bio.terra.cli.serialization.persisted.resource.PDBqTable) UFBqTable(bio.terra.cli.serialization.userfacing.resource.UFBqTable) GcpBigQueryDataTableResource(bio.terra.workspace.model.GcpBigQueryDataTableResource)

Aggregations

GcpBigQueryDataTableResource (bio.terra.workspace.model.GcpBigQueryDataTableResource)4 ReferencedGcpResourceApi (bio.terra.workspace.api.ReferencedGcpResourceApi)3 ResourceApi (bio.terra.workspace.api.ResourceApi)3 GcpBigQueryDatasetResource (bio.terra.workspace.model.GcpBigQueryDatasetResource)3 ResourceList (bio.terra.workspace.model.ResourceList)2 PDBqTable (bio.terra.cli.serialization.persisted.resource.PDBqTable)1 UFBqTable (bio.terra.cli.serialization.userfacing.resource.UFBqTable)1 GrantRoleRequestBody (bio.terra.workspace.model.GrantRoleRequestBody)1 UUID (java.util.UUID)1