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();
}
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();
}
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()));
}
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();
}
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();
}
Aggregations