use of bio.terra.workspace.model.JobReport.StatusEnum in project terra-cli by DataBiosphere.
the class WorkspaceManagerService method createWorkspace.
/**
* Call the Workspace Manager POST "/api/workspaces/v1" endpoint to create a new workspace, then
* call the POST "/api/workspaces/v1/{workspaceId}/cloudcontexts" endpoint to create a new backing
* Google context. Poll the "/api/workspaces/v1/{workspaceId}/cloudcontexts/results/{jobId}"
* endpoint to wait for the job to finish.
*
* @param displayName optional display name
* @param description optional description
* @return the Workspace Manager workspace description object
* @throws SystemException if the job to create the workspace cloud context fails
* @throws UserActionableException if the CLI times out waiting for the job to complete
*/
public WorkspaceDescription createWorkspace(@Nullable String displayName, @Nullable String description) {
return handleClientExceptions(() -> {
// create the Terra workspace object
UUID workspaceId = UUID.randomUUID();
CreateWorkspaceRequestBody workspaceRequestBody = new CreateWorkspaceRequestBody();
workspaceRequestBody.setId(workspaceId);
workspaceRequestBody.setStage(WorkspaceStageModel.MC_WORKSPACE);
workspaceRequestBody.setSpendProfile(Context.getServer().getWsmDefaultSpendProfile());
workspaceRequestBody.setDisplayName(displayName);
workspaceRequestBody.setDescription(description);
// make the create workspace request
WorkspaceApi workspaceApi = new WorkspaceApi(apiClient);
HttpUtils.callWithRetries(() -> workspaceApi.createWorkspace(workspaceRequestBody), WorkspaceManagerService::isRetryable);
// create the Google project that backs the Terra workspace object
UUID jobId = UUID.randomUUID();
CreateCloudContextRequest cloudContextRequest = new CreateCloudContextRequest();
cloudContextRequest.setCloudPlatform(CloudPlatform.GCP);
cloudContextRequest.setJobControl(new JobControl().id(jobId.toString()));
// make the initial create context request
HttpUtils.callWithRetries(() -> workspaceApi.createCloudContext(cloudContextRequest, workspaceId), WorkspaceManagerService::isRetryable);
// poll the result endpoint until the job is no longer RUNNING
CreateCloudContextResult createContextResult = HttpUtils.pollWithRetries(() -> workspaceApi.getCreateCloudContextResult(workspaceId, jobId.toString()), (result) -> isDone(result.getJobReport()), WorkspaceManagerService::isRetryable, CREATE_WORKSPACE_MAXIMUM_RETRIES, CREATE_WORKSPACE_DURATION_SLEEP_FOR_RETRY);
logger.debug("create workspace context result: {}", createContextResult);
StatusEnum status = createContextResult.getJobReport().getStatus();
if (StatusEnum.FAILED == status) {
// need to delete the empty workspace before continuing
boolean workspaceSuccessfullyDeleted = false;
try {
deleteWorkspace(workspaceId);
workspaceSuccessfullyDeleted = true;
} catch (RuntimeException ex) {
logger.error("Failed to delete workspace {} when cleaning up failed creation of cloud context. ", workspaceId, ex);
}
// message
if (createContextResult.getErrorReport().getMessage().contains("spend profile") && createContextResult.getErrorReport().getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
final String errorMessage;
if (workspaceSuccessfullyDeleted) {
errorMessage = "Accessing the spend profile failed. Ask an administrator to grant you access.";
} else {
errorMessage = String.format("Accessing the spend profile failed. Ask an administrator to grant you access. " + "There was a problem cleaning up the partially created workspace ID %s", workspaceId);
}
throw new UserActionableException(errorMessage);
}
}
// handle non-spend-profile-related failures
throwIfJobNotCompleted(createContextResult.getJobReport(), createContextResult.getErrorReport());
// call the get workspace endpoint to get the full description object
return HttpUtils.callWithRetries(() -> workspaceApi.getWorkspace(workspaceId), WorkspaceManagerService::isRetryable);
}, "Error creating a new workspace");
}
Aggregations