Search in sources :

Example 1 with CreateCloudContextRequest

use of bio.terra.workspace.model.CreateCloudContextRequest in project terra-workspace-manager by DataBiosphere.

the class Jobs method doUserJourney.

@Override
public void doUserJourney(TestUserSpecification testUser, WorkspaceApi workspaceApi) throws Exception {
    JobsApi jobsApi = ClientTestUtils.getJobsClient(testUser, server);
    // The purpose of this test is to exercise the jobsApi so we
    // create a cloud context - something that will run async
    String contextJobId = UUID.randomUUID().toString();
    var createContext = new CreateCloudContextRequest().cloudPlatform(CloudPlatform.GCP).jobControl(new JobControl().id(contextJobId));
    logger.info("Creating GCP cloud context");
    CreateCloudContextResult contextResult = workspaceApi.createCloudContext(createContext, getWorkspaceId());
    JobReport jobReport = contextResult.getJobReport();
    while (ClientTestUtils.jobIsRunning(jobReport)) {
        TimeUnit.SECONDS.sleep(10);
        jobReport = jobsApi.retrieveJob(contextJobId);
    }
    logger.info("Create GCP context status is {}", jobReport.getStatus().toString());
}
Also used : JobsApi(bio.terra.workspace.api.JobsApi) CreateCloudContextResult(bio.terra.workspace.model.CreateCloudContextResult) JobControl(bio.terra.workspace.model.JobControl) CreateCloudContextRequest(bio.terra.workspace.model.CreateCloudContextRequest) JobReport(bio.terra.workspace.model.JobReport)

Example 2 with CreateCloudContextRequest

use of bio.terra.workspace.model.CreateCloudContextRequest 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");
}
Also used : StatusEnum(bio.terra.workspace.model.JobReport.StatusEnum) WorkspaceApi(bio.terra.workspace.api.WorkspaceApi) CreateCloudContextResult(bio.terra.workspace.model.CreateCloudContextResult) UserActionableException(bio.terra.cli.exception.UserActionableException) JobControl(bio.terra.workspace.model.JobControl) CreateWorkspaceRequestBody(bio.terra.workspace.model.CreateWorkspaceRequestBody) UUID(java.util.UUID) CreateCloudContextRequest(bio.terra.workspace.model.CreateCloudContextRequest)

Example 3 with CreateCloudContextRequest

use of bio.terra.workspace.model.CreateCloudContextRequest in project terra-workspace-manager by DataBiosphere.

the class CloudContextMaker method createGcpCloudContext.

/**
 * Creates a GCP cloud context for a given workspace. Returns the GCP project ID as a string.
 */
public static String createGcpCloudContext(UUID workspaceUuid, WorkspaceApi workspaceApi) throws Exception {
    String contextJobId = UUID.randomUUID().toString();
    var createContext = new CreateCloudContextRequest().cloudPlatform(CloudPlatform.GCP).jobControl(new JobControl().id(contextJobId));
    logger.info("Creating GCP cloud context");
    CreateCloudContextResult contextResult = workspaceApi.createCloudContext(createContext, workspaceUuid);
    while (ClientTestUtils.jobIsRunning(contextResult.getJobReport())) {
        Thread.sleep(CREATE_CONTEXT_POLL_INTERVAL.toMillis());
        contextResult = workspaceApi.getCreateCloudContextResult(workspaceUuid, contextJobId);
    }
    logger.info("Create GCP context status is {}", contextResult.getJobReport().getStatus().toString());
    if (contextResult.getJobReport().getStatus() == StatusEnum.FAILED) {
        if (contextResult.getErrorReport() == null) {
            logger.info("Create failed, but no error report found!");
        } else {
            ErrorReport report = contextResult.getErrorReport();
            logger.info("Error report ({}) {}", report.getStatusCode(), report.getMessage());
            for (String cause : report.getCauses()) {
                logger.info("  cause: {}", cause);
            }
        }
    }
    ClientTestUtils.assertJobSuccess("Create Cloud Context", contextResult.getJobReport(), contextResult.getErrorReport());
    return contextResult.getGcpContext().getProjectId();
}
Also used : ErrorReport(bio.terra.workspace.model.ErrorReport) CreateCloudContextResult(bio.terra.workspace.model.CreateCloudContextResult) JobControl(bio.terra.workspace.model.JobControl) CreateCloudContextRequest(bio.terra.workspace.model.CreateCloudContextRequest)

Aggregations

CreateCloudContextRequest (bio.terra.workspace.model.CreateCloudContextRequest)3 CreateCloudContextResult (bio.terra.workspace.model.CreateCloudContextResult)3 JobControl (bio.terra.workspace.model.JobControl)3 UserActionableException (bio.terra.cli.exception.UserActionableException)1 JobsApi (bio.terra.workspace.api.JobsApi)1 WorkspaceApi (bio.terra.workspace.api.WorkspaceApi)1 CreateWorkspaceRequestBody (bio.terra.workspace.model.CreateWorkspaceRequestBody)1 ErrorReport (bio.terra.workspace.model.ErrorReport)1 JobReport (bio.terra.workspace.model.JobReport)1 StatusEnum (bio.terra.workspace.model.JobReport.StatusEnum)1 UUID (java.util.UUID)1