Search in sources :

Example 1 with AIPlatformNotebooksCow

use of bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow in project terra-workspace-manager by DataBiosphere.

the class NotebookCloudSyncStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
    FlightMap workingMap = flightContext.getWorkingMap();
    FlightUtils.validateRequiredEntries(workingMap, ControlledResourceKeys.GCP_CLOUD_CONTEXT);
    GcpCloudContext cloudContext = workingMap.get(ControlledResourceKeys.GCP_CLOUD_CONTEXT, GcpCloudContext.class);
    List<Binding> newBindings = createBindings(cloudContext, flightContext.getWorkingMap());
    AIPlatformNotebooksCow notebooks = crlService.getAIPlatformNotebooksCow();
    InstanceName instanceName = resource.toInstanceName(cloudContext.getGcpProjectId());
    try {
        Policy policy = notebooks.instances().getIamPolicy(instanceName).execute();
        // Duplicating bindings is harmless (e.g. on retry). GCP de-duplicates.
        Optional.ofNullable(policy.getBindings()).ifPresent(newBindings::addAll);
        policy.setBindings(newBindings);
        notebooks.instances().setIamPolicy(instanceName, new SetIamPolicyRequest().setPolicy(policy)).execute();
    } catch (IOException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : Binding(com.google.api.services.notebooks.v1.model.Binding) InstanceName(bio.terra.cloudres.google.notebooks.InstanceName) Policy(com.google.api.services.notebooks.v1.model.Policy) AIPlatformNotebooksCow(bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow) SetIamPolicyRequest(com.google.api.services.notebooks.v1.model.SetIamPolicyRequest) FlightMap(bio.terra.stairway.FlightMap) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Example 2 with AIPlatformNotebooksCow

use of bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow in project terra-workspace-manager by DataBiosphere.

the class DeleteAiNotebookInstanceStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
    final GcpCloudContext gcpCloudContext = flightContext.getWorkingMap().get(ControlledResourceKeys.GCP_CLOUD_CONTEXT, GcpCloudContext.class);
    InstanceName instanceName = resource.toInstanceName(gcpCloudContext.getGcpProjectId());
    AIPlatformNotebooksCow notebooks = crlService.getAIPlatformNotebooksCow();
    try {
        Optional<Operation> rawOperation = deleteIfFound(instanceName, notebooks);
        if (rawOperation.isEmpty()) {
            logger.info("Notebook instance {} already deleted", instanceName.formatName());
            return StepResult.getStepResultSuccess();
        }
        GcpUtils.pollUntilSuccess(notebooks.operations().operationCow(rawOperation.get()), Duration.ofSeconds(20), Duration.ofMinutes(10));
    } catch (IOException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : InstanceName(bio.terra.cloudres.google.notebooks.InstanceName) AIPlatformNotebooksCow(bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow) Operation(com.google.api.services.notebooks.v1.model.Operation) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Example 3 with AIPlatformNotebooksCow

use of bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow 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());
    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) HashMap(java.util.HashMap) StepStatus(bio.terra.stairway.StepStatus) ControlledAiNotebookInstanceResource(bio.terra.workspace.service.resource.controlled.cloud.gcp.ainotebook.ControlledAiNotebookInstanceResource) DeleteAiNotebookInstanceStep(bio.terra.workspace.service.resource.controlled.cloud.gcp.ainotebook.DeleteAiNotebookInstanceStep) Test(org.junit.jupiter.api.Test) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Example 4 with AIPlatformNotebooksCow

use of bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow in project terra-workspace-manager by DataBiosphere.

the class CreateAiNotebookInstanceStep method undoStep.

@Override
public StepResult undoStep(FlightContext flightContext) throws InterruptedException {
    final GcpCloudContext gcpCloudContext = flightContext.getWorkingMap().get(ControlledResourceKeys.GCP_CLOUD_CONTEXT, GcpCloudContext.class);
    InstanceName instanceName = resource.toInstanceName(gcpCloudContext.getGcpProjectId());
    AIPlatformNotebooksCow notebooks = crlService.getAIPlatformNotebooksCow();
    try {
        OperationCow<Operation> deletionOperation;
        try {
            deletionOperation = notebooks.operations().operationCow(notebooks.instances().delete(instanceName).execute());
        } catch (GoogleJsonResponseException e) {
            // The AI notebook instance may never have been created or have already been deleted.
            if (e.getStatusCode() == HttpStatus.NOT_FOUND.value()) {
                logger.debug("No notebook instance {} to delete.", instanceName.formatName());
                return StepResult.getStepResultSuccess();
            }
            return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
        }
        GcpUtils.pollUntilSuccess(deletionOperation, Duration.ofSeconds(20), Duration.ofMinutes(12));
    } catch (IOException | RetryException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : InstanceName(bio.terra.cloudres.google.notebooks.InstanceName) AIPlatformNotebooksCow(bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.notebooks.v1.model.Operation) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult) RetryException(bio.terra.stairway.exception.RetryException) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Example 5 with AIPlatformNotebooksCow

use of bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow in project terra-workspace-manager by DataBiosphere.

the class CreateAiNotebookInstanceStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
    final GcpCloudContext gcpCloudContext = flightContext.getWorkingMap().get(ControlledResourceKeys.GCP_CLOUD_CONTEXT, GcpCloudContext.class);
    String projectId = gcpCloudContext.getGcpProjectId();
    InstanceName instanceName = resource.toInstanceName(projectId);
    Instance instance = createInstanceModel(flightContext, projectId, petEmail);
    AIPlatformNotebooksCow notebooks = crlService.getAIPlatformNotebooksCow();
    try {
        OperationCow<Operation> creationOperation;
        try {
            creationOperation = notebooks.operations().operationCow(notebooks.instances().create(instanceName, instance).execute());
        } catch (GoogleJsonResponseException e) {
            // retry.
            if (HttpStatus.CONFLICT.value() == e.getStatusCode()) {
                logger.debug("Notebook instance {} already created.", instanceName.formatName());
                return StepResult.getStepResultSuccess();
            } else if (HttpStatus.BAD_REQUEST.value() == e.getStatusCode()) {
                // Don't retry bad requests, which won't change. Instead fail faster.
                return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, e);
            }
            return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
        }
        GcpUtils.pollUntilSuccess(creationOperation, Duration.ofSeconds(20), Duration.ofMinutes(12));
    } catch (IOException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : InstanceName(bio.terra.cloudres.google.notebooks.InstanceName) AIPlatformNotebooksCow(bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Instance(com.google.api.services.notebooks.v1.model.Instance) Operation(com.google.api.services.notebooks.v1.model.Operation) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext)

Aggregations

AIPlatformNotebooksCow (bio.terra.cloudres.google.notebooks.AIPlatformNotebooksCow)5 InstanceName (bio.terra.cloudres.google.notebooks.InstanceName)5 StepResult (bio.terra.stairway.StepResult)4 GcpCloudContext (bio.terra.workspace.service.workspace.model.GcpCloudContext)4 IOException (java.io.IOException)4 Operation (com.google.api.services.notebooks.v1.model.Operation)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 FlightMap (bio.terra.stairway.FlightMap)1 StepStatus (bio.terra.stairway.StepStatus)1 RetryException (bio.terra.stairway.exception.RetryException)1 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)1 ControlledAiNotebookInstanceResource (bio.terra.workspace.service.resource.controlled.cloud.gcp.ainotebook.ControlledAiNotebookInstanceResource)1 DeleteAiNotebookInstanceStep (bio.terra.workspace.service.resource.controlled.cloud.gcp.ainotebook.DeleteAiNotebookInstanceStep)1 Binding (com.google.api.services.notebooks.v1.model.Binding)1 Instance (com.google.api.services.notebooks.v1.model.Instance)1 Policy (com.google.api.services.notebooks.v1.model.Policy)1 SetIamPolicyRequest (com.google.api.services.notebooks.v1.model.SetIamPolicyRequest)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)1