Search in sources :

Example 16 with StepStatus

use of bio.terra.stairway.StepStatus in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDeleteFlightTest method deleteMcWorkspaceWithResource.

@Test
@DisabledIfEnvironmentVariable(named = "TEST_ENV", matches = BUFFER_SERVICE_DISABLED_ENVS_REG_EX)
void deleteMcWorkspaceWithResource() throws Exception {
    // Create a workspace with a controlled resource
    AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
    Workspace workspace = connectedTestUtils.createWorkspaceWithGcpContext(userRequest);
    ControlledBigQueryDatasetResource dataset = ControlledResourceFixtures.makeDefaultControlledBigQueryBuilder(workspace.getWorkspaceId()).build();
    var creationParameters = ControlledResourceFixtures.defaultBigQueryDatasetCreationParameters().datasetId(dataset.getDatasetName());
    controlledResourceService.createControlledResourceSync(dataset, null, userRequest, creationParameters).castByEnum(WsmResourceType.CONTROLLED_GCP_BIG_QUERY_DATASET);
    ControlledBigQueryDatasetResource gotResource = controlledResourceService.getControlledResource(workspace.getWorkspaceId(), dataset.getResourceId(), userRequest).castByEnum(WsmResourceType.CONTROLLED_GCP_BIG_QUERY_DATASET);
    assertEquals(dataset, gotResource);
    // Run the delete flight, retrying every step once
    FlightMap deleteParameters = new FlightMap();
    deleteParameters.put(WorkspaceFlightMapKeys.WORKSPACE_ID, workspace.getWorkspaceId().toString());
    deleteParameters.put(WorkspaceFlightMapKeys.WORKSPACE_STAGE, workspace.getWorkspaceStage().name());
    deleteParameters.put(JobMapKeys.AUTH_USER_INFO.getKeyName(), userRequest);
    Map<String, StepStatus> doFailures = new HashMap<>();
    doFailures.put(DeleteControlledSamResourcesStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    doFailures.put(DeleteGcpProjectStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    doFailures.put(DeleteWorkspaceAuthzStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    doFailures.put(DeleteWorkspaceStateStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    FlightDebugInfo debugInfo = FlightDebugInfo.newBuilder().doStepFailures(doFailures).build();
    FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), WorkspaceDeleteFlight.class, deleteParameters, DELETION_FLIGHT_TIMEOUT, debugInfo);
    assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
    // Verify the resource and workspace are not in WSM DB
    assertThrows(WorkspaceNotFoundException.class, () -> controlledResourceService.getControlledResource(dataset.getWorkspaceId(), dataset.getResourceId(), userRequest));
    assertThrows(WorkspaceNotFoundException.class, () -> workspaceService.getWorkspace(workspace.getWorkspaceId(), userRequest));
}
Also used : FlightDebugInfo(bio.terra.stairway.FlightDebugInfo) HashMap(java.util.HashMap) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) StepStatus(bio.terra.stairway.StepStatus) FlightState(bio.terra.stairway.FlightState) FlightMap(bio.terra.stairway.FlightMap) ControlledBigQueryDatasetResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource) Workspace(bio.terra.workspace.service.workspace.model.Workspace) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) Test(org.junit.jupiter.api.Test) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Example 17 with StepStatus

use of bio.terra.stairway.StepStatus in project terra-workspace-manager by DataBiosphere.

the class ControlledResourceServiceTest method deleteBqDatasetDo.

@Test
@DisabledIfEnvironmentVariable(named = "TEST_ENV", matches = BUFFER_SERVICE_DISABLED_ENVS_REG_EX)
void deleteBqDatasetDo() throws Exception {
    String datasetId = ControlledResourceFixtures.uniqueDatasetId();
    String location = "us-central1";
    ApiGcpBigQueryDatasetCreationParameters creationParameters = new ApiGcpBigQueryDatasetCreationParameters().datasetId(datasetId).location(location);
    ControlledBigQueryDatasetResource resource = ControlledResourceFixtures.makeDefaultControlledBigQueryBuilder(workspace.getWorkspaceId()).datasetName(datasetId).build();
    ControlledBigQueryDatasetResource createdDataset = controlledResourceService.createControlledResourceSync(resource, null, user.getAuthenticatedRequest(), creationParameters).castByEnum(WsmResourceType.CONTROLLED_GCP_BIG_QUERY_DATASET);
    assertEquals(resource, createdDataset);
    // Test idempotency of delete by retrying steps once.
    Map<String, StepStatus> retrySteps = new HashMap<>();
    retrySteps.put(DeleteMetadataStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    retrySteps.put(DeleteBigQueryDatasetStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    // Do not test lastStepFailure, as this flight has no undo steps, only dismal failure.
    jobService.setFlightDebugInfoForTest(FlightDebugInfo.newBuilder().doStepFailures(retrySteps).build());
    controlledResourceService.deleteControlledResourceSync(resource.getWorkspaceId(), resource.getResourceId(), user.getAuthenticatedRequest(), true);
    BigQueryCow bqCow = crlService.createWsmSaBigQueryCow();
    GoogleJsonResponseException getException = assertThrows(GoogleJsonResponseException.class, () -> bqCow.datasets().get(projectId, resource.getDatasetName()).execute());
    assertEquals(HttpStatus.NOT_FOUND.value(), getException.getStatusCode());
    assertThrows(ResourceNotFoundException.class, () -> controlledResourceService.getControlledResource(workspace.getWorkspaceId(), resource.getResourceId(), user.getAuthenticatedRequest()));
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) DeleteMetadataStep(bio.terra.workspace.service.resource.controlled.flight.delete.DeleteMetadataStep) StepStatus(bio.terra.stairway.StepStatus) BigQueryCow(bio.terra.cloudres.google.bigquery.BigQueryCow) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Example 18 with StepStatus

use of bio.terra.stairway.StepStatus in project terra-workspace-manager by DataBiosphere.

the class ControlledResourceServiceTest method updateGcsBucketUndo.

@Test
@DisabledIfEnvironmentVariable(named = "TEST_ENV", matches = BUFFER_SERVICE_DISABLED_ENVS_REG_EX)
void updateGcsBucketUndo() throws Exception {
    ControlledGcsBucketResource createdBucket = createDefaultSharedGcsBucket(workspace, user);
    Map<String, StepStatus> retrySteps = new HashMap<>();
    retrySteps.put(RetrieveControlledResourceMetadataStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    retrySteps.put(UpdateControlledResourceMetadataStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    retrySteps.put(RetrieveGcsBucketCloudAttributesStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    retrySteps.put(UpdateGcsBucketStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    jobService.setFlightDebugInfoForTest(FlightDebugInfo.newBuilder().undoStepFailures(retrySteps).lastStepFailure(true).build());
    // update the bucket
    String newName = "NEW_bucketname";
    String newDescription = "new resource description";
    // Service methods which wait for a flight to complete will throw an
    // InvalidResultStateException when that flight fails without a cause, which occurs when a
    // flight fails via debugInfo.
    assertThrows(InvalidResultStateException.class, () -> controlledResourceService.updateGcsBucket(createdBucket, ControlledResourceFixtures.BUCKET_UPDATE_PARAMETERS_2, user.getAuthenticatedRequest(), newName, newDescription));
    // check the properties stored on the cloud were not updated
    BucketInfo updatedBucket = crlService.createStorageCow(projectId).get(createdBucket.getBucketName()).getBucketInfo();
    ApiGcpGcsBucketUpdateParameters cloudParameters = GcsApiConversions.toUpdateParameters(updatedBucket);
    assertNotEquals(ControlledResourceFixtures.BUCKET_UPDATE_PARAMETERS_2, cloudParameters);
    // check the properties stored in WSM were not updated
    ControlledGcsBucketResource fetchedResource = controlledResourceService.getControlledResource(workspace.getWorkspaceId(), createdBucket.getResourceId(), user.getAuthenticatedRequest()).castByEnum(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
    assertEquals(createdBucket.getName(), fetchedResource.getName());
    assertEquals(createdBucket.getDescription(), fetchedResource.getDescription());
}
Also used : UpdateControlledResourceMetadataStep(bio.terra.workspace.service.resource.controlled.flight.update.UpdateControlledResourceMetadataStep) StepStatus(bio.terra.stairway.StepStatus) RetrieveControlledResourceMetadataStep(bio.terra.workspace.service.resource.controlled.flight.update.RetrieveControlledResourceMetadataStep) BucketInfo(com.google.cloud.storage.BucketInfo) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Example 19 with StepStatus

use of bio.terra.stairway.StepStatus in project terra-workspace-manager by DataBiosphere.

the class ControlledResourceServiceTest method deleteAiNotebookInstanceDo.

@Test
@DisabledIfEnvironmentVariable(named = "TEST_ENV", matches = BUFFER_SERVICE_DISABLED_ENVS_REG_EX)
void deleteAiNotebookInstanceDo() throws Exception {
    ControlledAiNotebookInstanceResource resource = createDefaultPrivateAiNotebookInstance("delete-ai-notebook-instance-do", user);
    InstanceName instanceName = resource.toInstanceName(projectId);
    AIPlatformNotebooksCow notebooks = crlService.getAIPlatformNotebooksCow();
    // Test idempotency of steps by retrying them once.
    Map<String, StepStatus> retrySteps = new HashMap<>();
    retrySteps.put(DeleteAiNotebookInstanceStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    jobService.setFlightDebugInfoForTest(FlightDebugInfo.newBuilder().doStepFailures(retrySteps).build());
    controlledResourceService.deleteControlledResourceSync(resource.getWorkspaceId(), resource.getResourceId(), user.getAuthenticatedRequest(), true);
    assertNotFound(instanceName, notebooks);
    assertThrows(ResourceNotFoundException.class, () -> controlledResourceService.getControlledResource(resource.getWorkspaceId(), resource.getResourceId(), user.getAuthenticatedRequest()));
}
Also used : InstanceName(bio.terra.cloudres.google.notebooks.InstanceName) AIPlatformNotebooksCow(bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow) StepStatus(bio.terra.stairway.StepStatus) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Example 20 with StepStatus

use of bio.terra.stairway.StepStatus in project terra-workspace-manager by DataBiosphere.

the class ControlledResourceServiceTest method createAiNotebookInstanceDo.

@Test
@DisabledIfEnvironmentVariable(named = "TEST_ENV", matches = BUFFER_SERVICE_DISABLED_ENVS_REG_EX)
void createAiNotebookInstanceDo() throws Exception {
    UUID workspaceUuid = workspace.getWorkspaceId();
    String workspaceUserFacingId = workspace.getUserFacingId();
    var instanceId = "create-ai-notebook-instance-do";
    var serverName = "verily-autopush";
    cliConfiguration.setServerName(serverName);
    ApiGcpAiNotebookInstanceCreationParameters creationParameters = ControlledResourceFixtures.defaultNotebookCreationParameters().instanceId(instanceId).location(DEFAULT_NOTEBOOK_LOCATION);
    ControlledAiNotebookInstanceResource resource = makeNotebookTestResource(workspaceUuid, "initial-notebook-name", instanceId);
    // Test idempotency of steps by retrying them once.
    Map<String, StepStatus> retrySteps = new HashMap<>();
    retrySteps.put(RetrieveNetworkNameStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    retrySteps.put(GrantPetUsagePermissionStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    retrySteps.put(CreateAiNotebookInstanceStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    retrySteps.put(NotebookCloudSyncStep.class.getName(), StepStatus.STEP_RESULT_FAILURE_RETRY);
    jobService.setFlightDebugInfoForTest(FlightDebugInfo.newBuilder().doStepFailures(retrySteps).build());
    String jobId = controlledResourceService.createAiNotebookInstance(resource, creationParameters, DEFAULT_ROLE, new ApiJobControl().id(UUID.randomUUID().toString()), "fakeResultPath", user.getAuthenticatedRequest());
    jobService.waitForJob(jobId);
    assertEquals(FlightStatus.SUCCESS, stairwayComponent.get().getFlightState(jobId).getFlightStatus());
    assertEquals(resource, controlledResourceService.getControlledResource(workspaceUuid, resource.getResourceId(), user.getAuthenticatedRequest()));
    InstanceName instanceName = resource.toInstanceName(workspaceService.getAuthorizedRequiredGcpProject(workspaceUuid, user.getAuthenticatedRequest()));
    Instance instance = crlService.getAIPlatformNotebooksCow().instances().get(instanceName).execute();
    // Test that the user has permissions from WRITER roles on the notebooks instance. Only notebook
    // instance level permissions can be checked on the notebook instance test IAM permissions
    // endpoint, so no "notebooks.instances.list" permission as that's project level.
    List<String> expectedWriterPermissions = ImmutableList.of("notebooks.instances.get", "notebooks.instances.reset", "notebooks.instances.setAccelerator", "notebooks.instances.setMachineType", "notebooks.instances.start", "notebooks.instances.stop", "notebooks.instances.use");
    assertThat(AIPlatformNotebooksCow.create(crlService.getClientConfig(), user.getGoogleCredentials()).instances().testIamPermissions(instanceName, new com.google.api.services.notebooks.v1.model.TestIamPermissionsRequest().setPermissions(expectedWriterPermissions)).execute().getPermissions(), containsInAnyOrder(expectedWriterPermissions.toArray()));
    // Test that the user has access to the notebook with a service account through proxy mode.
    // git secrets gets a false positive if 'service_account' is double quoted.
    assertThat(instance.getMetadata(), Matchers.hasEntry("proxy-mode", "service_" + "account"));
    assertThat(instance.getMetadata(), Matchers.hasEntry("terra-cli-server", serverName));
    assertThat(instance.getMetadata(), Matchers.hasEntry("terra-workspace-id", workspaceUserFacingId));
    ServiceAccountName serviceAccountName = ServiceAccountName.builder().projectId(instanceName.projectId()).email(instance.getServiceAccount()).build();
    // The user needs to have the actAs permission on the service account.
    String actAsPermission = "iam.serviceAccounts.actAs";
    assertThat(IamCow.create(crlService.getClientConfig(), user.getGoogleCredentials()).projects().serviceAccounts().testIamPermissions(serviceAccountName, new TestIamPermissionsRequest().setPermissions(List.of(actAsPermission))).execute().getPermissions(), Matchers.contains(actAsPermission));
    // Creating a controlled resource with a duplicate underlying notebook instance is not allowed.
    ControlledAiNotebookInstanceResource duplicateResource = makeNotebookTestResource(workspaceUuid, "new-name-same-notebook-instance", instanceId);
    String duplicateResourceJobId = controlledResourceService.createAiNotebookInstance(duplicateResource, creationParameters, DEFAULT_ROLE, new ApiJobControl().id(UUID.randomUUID().toString()), "fakeResultPath", user.getAuthenticatedRequest());
    jobService.waitForJob(duplicateResourceJobId);
    JobService.JobResultOrException<ControlledAiNotebookInstanceResource> duplicateJobResult = jobService.retrieveJobResult(duplicateResourceJobId, ControlledAiNotebookInstanceResource.class, user.getAuthenticatedRequest());
    assertEquals(DuplicateResourceException.class, duplicateJobResult.getException().getClass());
}
Also used : Instance(com.google.api.services.notebooks.v1.model.Instance) JobService(bio.terra.workspace.service.job.JobService) bio.terra.workspace.generated.model(bio.terra.workspace.generated.model) org.junit.jupiter.api(org.junit.jupiter.api) TestIamPermissionsRequest(com.google.api.services.iam.v1.model.TestIamPermissionsRequest) ServiceAccountName(bio.terra.cloudres.google.iam.ServiceAccountName) StepStatus(bio.terra.stairway.StepStatus) InstanceName(bio.terra.cloudres.google.notebooks.InstanceName) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Aggregations

StepStatus (bio.terra.stairway.StepStatus)23 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)23 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)20 Test (org.junit.jupiter.api.Test)13 FlightDebugInfo (bio.terra.stairway.FlightDebugInfo)10 HashMap (java.util.HashMap)9 FlightState (bio.terra.stairway.FlightState)7 UUID (java.util.UUID)7 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)6 Workspace (bio.terra.workspace.service.workspace.model.Workspace)5 Project (com.google.api.services.cloudresourcemanager.v3.model.Project)5 ControlledBigQueryDatasetResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.bqdataset.ControlledBigQueryDatasetResource)4 BigQueryCow (bio.terra.cloudres.google.bigquery.BigQueryCow)3 StorageCow (bio.terra.cloudres.google.storage.StorageCow)3 FlightMap (bio.terra.stairway.FlightMap)3 ApiClonedWorkspace (bio.terra.workspace.generated.model.ApiClonedWorkspace)3 CreateWorkspaceStep (bio.terra.workspace.service.workspace.flight.CreateWorkspaceStep)3 InstanceName (bio.terra.cloudres.google.notebooks.InstanceName)2 ApiGcpBigQueryDatasetCreationParameters (bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetCreationParameters)2 ApiGcpBigQueryDatasetUpdateParameters (bio.terra.workspace.generated.model.ApiGcpBigQueryDatasetUpdateParameters)2