Search in sources :

Example 1 with ApiClient

use of bio.terra.workspace.client.ApiClient in project terra-workspace-manager by DataBiosphere.

the class ControlledApplicationSharedGcsBucketLifecycle method doUserJourney.

@Override
public void doUserJourney(TestUserSpecification unused, WorkspaceApi workspaceApi) throws Exception {
    ApiClient ownerApiClient = ClientTestUtils.getClientForTestUser(owner, server);
    ApiClient wsmappApiClient = ClientTestUtils.getClientForTestUser(wsmapp, server);
    WorkspaceApplicationApi ownerWsmAppApi = new WorkspaceApplicationApi(ownerApiClient);
    ControlledGcpResourceApi wsmappResourceApi = new ControlledGcpResourceApi(wsmappApiClient);
    // Owner adds a reader and a writer to the workspace
    workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(reader.userEmail), getWorkspaceId(), IamRole.READER);
    logger.info("Added {} as a reader to workspace {}", reader.userEmail, getWorkspaceId());
    workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(writer.userEmail), getWorkspaceId(), IamRole.WRITER);
    logger.info("Added {} as a writer to workspace {}", writer.userEmail, getWorkspaceId());
    // Create the cloud context
    String projectId = CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
    assertNotNull(projectId);
    logger.info("Created project {}", projectId);
    // Create the bucket - should fail because application is not enabled
    String bucketResourceName = RandomStringUtils.random(6, true, false);
    ApiException createBucketFails = assertThrows(ApiException.class, () -> GcsBucketUtils.makeControlledGcsBucketAppShared(wsmappResourceApi, getWorkspaceId(), bucketResourceName, CloningInstructionsEnum.NOTHING));
    // TODO: [PF-1208] this should be FORBIDDEN (403), but we are throwing the wrong thing
    assertEquals(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED, createBucketFails.getCode());
    logger.info("Failed to create bucket, as expected");
    // Enable the application in the workspace
    WorkspaceApplicationDescription applicationDescription = ownerWsmAppApi.enableWorkspaceApplication(getWorkspaceId(), TEST_WSM_APP);
    assertThat(applicationDescription.getApplicationState(), equalTo(ApplicationState.OPERATING));
    logger.info("Enabled application in the workspace");
    // Validate that it is enabled
    WorkspaceApplicationDescription retrievedDescription = ownerWsmAppApi.getWorkspaceApplication(getWorkspaceId(), TEST_WSM_APP);
    assertThat(applicationDescription, equalTo(retrievedDescription));
    assertThat(applicationDescription.getWorkspaceApplicationState(), equalTo(WorkspaceApplicationState.ENABLED));
    // Create the bucket - should work this time
    CreatedControlledGcpGcsBucket createdBucket = GcsBucketUtils.makeControlledGcsBucketAppShared(wsmappResourceApi, getWorkspaceId(), bucketResourceName, CloningInstructionsEnum.NOTHING);
    bucketName = createdBucket.getGcpBucket().getAttributes().getBucketName();
    assertNotNull(bucketName);
    logger.info("Created bucket {}", bucketName);
    // Try to disable; should error because you cannot disable an app if it owns resources
    // in the workspace.
    ApiException disableAppFails = assertThrows(ApiException.class, () -> ownerWsmAppApi.disableWorkspaceApplication(getWorkspaceId(), TEST_WSM_APP));
    assertEquals(HttpStatusCodes.STATUS_CODE_BAD_REQUEST, disableAppFails.getCode());
    logger.info("Failed to disable app, as expected");
    try (GcsBucketAccessTester tester = new GcsBucketAccessTester(wsmapp, bucketName, projectId)) {
        tester.checkAccess(wsmapp, ControlledResourceIamRole.EDITOR);
        tester.checkAccess(owner, ControlledResourceIamRole.WRITER);
        tester.checkAccess(writer, ControlledResourceIamRole.WRITER);
        tester.checkAccess(reader, ControlledResourceIamRole.READER);
    }
    // The reader should be able to enumerate the bucket.
    ResourceApi readerResourceApi = ClientTestUtils.getResourceClient(reader, server);
    ResourceList bucketList = readerResourceApi.enumerateResources(getWorkspaceId(), 0, 5, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED);
    assertEquals(1, bucketList.getResources().size());
    MultiResourcesUtils.assertResourceType(ResourceType.GCS_BUCKET, bucketList);
    // Owner cannot delete the bucket through WSM
    ControlledGcpResourceApi ownerResourceApi = new ControlledGcpResourceApi(ownerApiClient);
    ApiException cannotDelete = assertThrows(ApiException.class, () -> GcsBucketUtils.deleteControlledGcsBucket(createdBucket.getResourceId(), getWorkspaceId(), ownerResourceApi));
    // TODO: [PF-1208] this should be FORBIDDEN (403), but we are throwing the wrong thing
    assertEquals(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED, cannotDelete.getCode());
    logger.info("Owner delete failed as expected");
    // Application can delete the bucket through WSM
    GcsBucketUtils.deleteControlledGcsBucket(createdBucket.getResourceId(), getWorkspaceId(), wsmappResourceApi);
    logger.info("Application delete succeeded");
}
Also used : ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ResourceApi(bio.terra.workspace.api.ResourceApi) ResourceList(bio.terra.workspace.model.ResourceList) GrantRoleRequestBody(bio.terra.workspace.model.GrantRoleRequestBody) WorkspaceApplicationApi(bio.terra.workspace.api.WorkspaceApplicationApi) WorkspaceApplicationDescription(bio.terra.workspace.model.WorkspaceApplicationDescription) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ApiClient(bio.terra.workspace.client.ApiClient) ApiException(bio.terra.workspace.client.ApiException) CreatedControlledGcpGcsBucket(bio.terra.workspace.model.CreatedControlledGcpGcsBucket) GcsBucketAccessTester(scripts.utils.GcsBucketAccessTester)

Example 2 with ApiClient

use of bio.terra.workspace.client.ApiClient in project terra-workspace-manager by DataBiosphere.

the class EnumerateResources method doSetup.

@Override
public void doSetup(List<TestUserSpecification> testUsers, WorkspaceApi workspaceApi) throws Exception {
    // initialize workspace
    super.doSetup(testUsers, workspaceApi);
    assertThat("There must be two test users defined for this test.", testUsers != null && testUsers.size() == 2);
    TestUserSpecification workspaceOwner = testUsers.get(0);
    workspaceReader = testUsers.get(1);
    // static assumptions
    assertThat(PAGE_SIZE * 2, lessThan(RESOURCE_COUNT));
    assertThat(PAGE_SIZE * 3, greaterThan(RESOURCE_COUNT));
    ApiClient ownerApiClient = ClientTestUtils.getClientForTestUser(workspaceOwner, server);
    ownerControlledGcpResourceApi = new ControlledGcpResourceApi(ownerApiClient);
    ownerReferencedGcpResourceApi = new ReferencedGcpResourceApi(ownerApiClient);
    ownerResourceApi = new ResourceApi(ownerApiClient);
    ApiClient readerApiClient = ClientTestUtils.getClientForTestUser(workspaceReader, server);
    readerResourceApi = new ResourceApi(readerApiClient);
    // Create a cloud context for the workspace
    CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
    // create the resources for the test
    logger.info("Creating {} resources", RESOURCE_COUNT);
    resourceList = MultiResourcesUtils.makeResources(ownerReferencedGcpResourceApi, ownerControlledGcpResourceApi, getWorkspaceId());
    logger.info("Created {} resources", resourceList.size());
}
Also used : ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) ResourceApi(bio.terra.workspace.api.ResourceApi) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) TestUserSpecification(bio.terra.testrunner.runner.config.TestUserSpecification) ApiClient(bio.terra.workspace.client.ApiClient)

Example 3 with ApiClient

use of bio.terra.workspace.client.ApiClient in project terra-workspace-manager by DataBiosphere.

the class EnumerateJobs method doSetup.

@Override
public void doSetup(List<TestUserSpecification> testUsers, WorkspaceApi workspaceApi) throws Exception {
    // initialize workspace
    super.doSetup(testUsers, workspaceApi);
    TestUserSpecification workspaceOwner = testUsers.get(0);
    // If we like the alpha1 API for job enumeration, then we can maybe piggyback on
    // the EnumerateResources test instead of creating our own set.
    ApiClient ownerApiClient = ClientTestUtils.getClientForTestUser(workspaceOwner, server);
    ownerControlledGcpResourceApi = new ControlledGcpResourceApi(ownerApiClient);
    ownerReferencedGcpResourceApi = new ReferencedGcpResourceApi(ownerApiClient);
    alpha1Api = new Alpha1Api(ownerApiClient);
    // Create a cloud context for the workspace
    CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
    // create the resources for the test
    logger.info("Creating {} resources", RESOURCE_COUNT);
    resourceList = MultiResourcesUtils.makeResources(ownerReferencedGcpResourceApi, ownerControlledGcpResourceApi, getWorkspaceId());
    logger.info("Created {} resources", resourceList.size());
    logger.info("Cleaning up {} resources", resourceList.size());
    MultiResourcesUtils.cleanupResources(resourceList, ownerControlledGcpResourceApi, getWorkspaceId());
    logger.info("Cleaned up {} resources", resourceList.size());
}
Also used : ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) Alpha1Api(bio.terra.workspace.api.Alpha1Api) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) TestUserSpecification(bio.terra.testrunner.runner.config.TestUserSpecification) ApiClient(bio.terra.workspace.client.ApiClient)

Example 4 with ApiClient

use of bio.terra.workspace.client.ApiClient in project terra-workspace-manager by DataBiosphere.

the class ControlledApplicationPrivateGcsBucketLifecycle method doUserJourney.

@Override
public void doUserJourney(TestUserSpecification testUser, WorkspaceApi workspaceApi) throws Exception {
    ApiClient ownerApiClient = ClientTestUtils.getClientForTestUser(owner, server);
    ApiClient wsmappApiClient = ClientTestUtils.getClientForTestUser(wsmapp, server);
    WorkspaceApplicationApi ownerWsmAppApi = new WorkspaceApplicationApi(ownerApiClient);
    ControlledGcpResourceApi wsmappResourceApi = new ControlledGcpResourceApi(wsmappApiClient);
    // Owner adds a reader and a writer to the workspace
    workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(reader.userEmail), getWorkspaceId(), IamRole.READER);
    logger.info("Added {} as a reader to workspace {}", reader.userEmail, getWorkspaceId());
    workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(writer.userEmail), getWorkspaceId(), IamRole.WRITER);
    logger.info("Added {} as a writer to workspace {}", writer.userEmail, getWorkspaceId());
    // Create the cloud context
    String projectId = CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
    assertNotNull(projectId);
    logger.info("Created project {}", projectId);
    // Enable the application in the workspace
    WorkspaceApplicationDescription applicationDescription = ownerWsmAppApi.enableWorkspaceApplication(getWorkspaceId(), TEST_WSM_APP);
    assertThat(applicationDescription.getApplicationState(), equalTo(ApplicationState.OPERATING));
    logger.info("Enabled application {} in the workspace {}", TEST_WSM_APP, getWorkspaceId());
    // CASE 1: Create a bucket with no assigned user
    testNoAssignedUser(wsmappResourceApi, projectId);
    // CASE 2: Create a bucket with workspace writer as READER
    testAssignedReader(wsmappResourceApi, projectId);
    // CASE 3: Create a bucket with workspace reader as WRITER
    testAssignedWriter(wsmappResourceApi, projectId);
    // All buckets should be visible to enumeration
    ResourceApi ownerResourceApi = ClientTestUtils.getResourceClient(owner, server);
    ResourceList bucketList = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, 5, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED);
    assertEquals(3, bucketList.getResources().size());
    MultiResourcesUtils.assertResourceType(ResourceType.GCS_BUCKET, bucketList);
}
Also used : ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ResourceApi(bio.terra.workspace.api.ResourceApi) ResourceList(bio.terra.workspace.model.ResourceList) GrantRoleRequestBody(bio.terra.workspace.model.GrantRoleRequestBody) WorkspaceApplicationApi(bio.terra.workspace.api.WorkspaceApplicationApi) WorkspaceApplicationDescription(bio.terra.workspace.model.WorkspaceApplicationDescription) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ApiClient(bio.terra.workspace.client.ApiClient)

Example 5 with ApiClient

use of bio.terra.workspace.client.ApiClient in project terra-workspace-manager by DataBiosphere.

the class ServiceStatus method userJourney.

@Override
public void userJourney(TestUserSpecification testUser) throws Exception {
    if (delay.getSeconds() > 0)
        TimeUnit.SECONDS.sleep(delay.getSeconds());
    logger.info("Checking service status endpoint now.");
    ApiClient apiClient = ClientTestUtils.getClientWithoutAccessToken(server);
    UnauthenticatedApi unauthenticatedApi = new UnauthenticatedApi(apiClient);
    unauthenticatedApi.serviceStatus();
    int httpCode = unauthenticatedApi.getApiClient().getStatusCode();
    logger.info("Service status return code: {}", httpCode);
}
Also used : ApiClient(bio.terra.workspace.client.ApiClient) UnauthenticatedApi(bio.terra.workspace.api.UnauthenticatedApi)

Aggregations

ApiClient (bio.terra.workspace.client.ApiClient)7 ControlledGcpResourceApi (bio.terra.workspace.api.ControlledGcpResourceApi)5 ResourceApi (bio.terra.workspace.api.ResourceApi)4 ReferencedGcpResourceApi (bio.terra.workspace.api.ReferencedGcpResourceApi)3 TestUserSpecification (bio.terra.testrunner.runner.config.TestUserSpecification)2 UnauthenticatedApi (bio.terra.workspace.api.UnauthenticatedApi)2 WorkspaceApplicationApi (bio.terra.workspace.api.WorkspaceApplicationApi)2 ApiException (bio.terra.workspace.client.ApiException)2 GrantRoleRequestBody (bio.terra.workspace.model.GrantRoleRequestBody)2 ResourceList (bio.terra.workspace.model.ResourceList)2 WorkspaceApplicationDescription (bio.terra.workspace.model.WorkspaceApplicationDescription)2 Context (bio.terra.cli.businessobject.Context)1 Server (bio.terra.cli.businessobject.Server)1 SystemException (bio.terra.cli.exception.SystemException)1 UserActionableException (bio.terra.cli.exception.UserActionableException)1 AddBqTableParams (bio.terra.cli.serialization.userfacing.input.AddBqTableParams)1 AddGcsObjectParams (bio.terra.cli.serialization.userfacing.input.AddGcsObjectParams)1 AddGitRepoParams (bio.terra.cli.serialization.userfacing.input.AddGitRepoParams)1 CreateBqDatasetParams (bio.terra.cli.serialization.userfacing.input.CreateBqDatasetParams)1 CreateGcpNotebookParams (bio.terra.cli.serialization.userfacing.input.CreateGcpNotebookParams)1