Search in sources :

Example 1 with ApiException

use of bio.terra.buffer.client.ApiException in project terra-resource-buffer by DataBiosphere.

the class UnauthorizedAccess method userJourney.

@Override
public void userJourney(TestUserSpecification testUser) throws Exception {
    ApiClient apiClient = BufferServiceUtils.getClient(server);
    BufferApi bufferApi = new BufferApi(apiClient);
    try {
        retryHandout(bufferApi, UUID.randomUUID().toString());
        assertThat("Invalid SA account access not throwing exception", false);
    } catch (ApiException apiEx) {
        assertThat(apiEx.getCode(), equalTo(401));
    }
}
Also used : BufferApi(bio.terra.buffer.api.BufferApi) ApiClient(bio.terra.buffer.client.ApiClient) ApiException(bio.terra.buffer.client.ApiException)

Example 2 with ApiException

use of bio.terra.buffer.client.ApiException in project terra-workspace-manager by DataBiosphere.

the class BufferService method handoutResource.

/**
 * Retrieve a single resource from the Buffer Service. The instance and pool are already
 * configured.
 *
 * @param requestBody
 * @return ResourceInfo
 */
public ResourceInfo handoutResource(HandoutRequestBody requestBody) {
    try {
        BufferApi bufferApi = bufferApi(bufferServiceConfiguration.getInstanceUrl());
        ResourceInfo info = bufferApi.handoutResource(requestBody, bufferServiceConfiguration.getPoolId());
        logger.info("Retrieved resource from pool {} on Buffer Service instance {}", bufferServiceConfiguration.getPoolId(), bufferServiceConfiguration.getInstanceUrl());
        return info;
    } catch (IOException e) {
        throw new BufferServiceAuthorizationException(String.format("Error reading or parsing credentials file at %s", bufferServiceConfiguration.getClientCredentialFilePath()), e.getCause());
    } catch (ApiException e) {
        if (e.getCode() == HttpStatus.UNAUTHORIZED.value()) {
            throw new BufferServiceAuthorizationException("Not authorized to access Buffer Service", e.getCause());
        } else {
            throw new BufferServiceAPIException(e);
        }
    }
}
Also used : ResourceInfo(bio.terra.buffer.model.ResourceInfo) BufferServiceAPIException(bio.terra.workspace.service.buffer.exception.BufferServiceAPIException) BufferApi(bio.terra.buffer.api.BufferApi) BufferServiceAuthorizationException(bio.terra.workspace.service.buffer.exception.BufferServiceAuthorizationException) IOException(java.io.IOException) ApiException(bio.terra.buffer.client.ApiException)

Example 3 with ApiException

use of bio.terra.buffer.client.ApiException in project terra-workspace-manager by DataBiosphere.

the class BufferService method getPoolInfo.

/**
 * Return the PoolInfo object for the ResourceBuffer pool that we are using to create Google Cloud
 * projects. Note that this is configured once per Workspace Manager instance (both the instance
 * of RBS to use and which pool) so no configuration happens here.
 *
 * @return PoolInfo
 */
@Traced
public PoolInfo getPoolInfo() {
    try {
        BufferApi bufferApi = bufferApi(bufferServiceConfiguration.getInstanceUrl());
        PoolInfo info = bufferApi.getPoolInfo(bufferServiceConfiguration.getPoolId());
        logger.info("Retrieved pool {} on Buffer Service instance {}", bufferServiceConfiguration.getPoolId(), bufferServiceConfiguration.getInstanceUrl());
        return info;
    } catch (IOException e) {
        throw new BufferServiceAuthorizationException("Error reading or parsing credentials file", e.getCause());
    } catch (ApiException e) {
        if (e.getCode() == HttpStatus.UNAUTHORIZED.value()) {
            throw new BufferServiceAuthorizationException("Not authorized to access Buffer Service", e.getCause());
        } else {
            throw new BufferServiceAPIException(e);
        }
    }
}
Also used : BufferServiceAPIException(bio.terra.workspace.service.buffer.exception.BufferServiceAPIException) BufferApi(bio.terra.buffer.api.BufferApi) BufferServiceAuthorizationException(bio.terra.workspace.service.buffer.exception.BufferServiceAuthorizationException) PoolInfo(bio.terra.buffer.model.PoolInfo) IOException(java.io.IOException) ApiException(bio.terra.buffer.client.ApiException) Traced(io.opencensus.contrib.spring.aop.Traced)

Example 4 with ApiException

use of bio.terra.buffer.client.ApiException in project terra-resource-buffer by DataBiosphere.

the class GetPoolInfo method userJourney.

@Override
public void userJourney(TestUserSpecification testUser) throws Exception {
    ApiClient apiClient = BufferServiceUtils.getClient(server);
    BufferApi bufferApi = new BufferApi(apiClient);
    // TODO: is there a valid pool id that I could use here instead of expecting failure?
    try {
        PoolInfo poolInfo = bufferApi.getPoolInfo("123");
        logger.debug("poolInfo: {}", poolInfo);
        assertThat("GET pool info did not throw not found exception", false);
    } catch (ApiException apiEx) {
        logger.debug("Caught exception fetching pool info", apiEx);
        assertThat("Exception text reason", apiEx.getResponseBody().contains("Pool 123 not found"));
    }
    int httpCode = bufferApi.getApiClient().getStatusCode();
    logger.info("GET pool info HTTP code: {}", httpCode);
    assertThat(httpCode, equalTo(404));
}
Also used : BufferApi(bio.terra.buffer.api.BufferApi) PoolInfo(bio.terra.buffer.model.PoolInfo) ApiClient(bio.terra.buffer.client.ApiClient) ApiException(bio.terra.buffer.client.ApiException)

Example 5 with ApiException

use of bio.terra.buffer.client.ApiException in project terra-resource-buffer by DataBiosphere.

the class BufferServiceUtils method retryHandout.

/**
 * Retries Handout resource API if resource is no resource is available.
 */
public static String retryHandout(BufferApi bufferApi, String handoutRequestId) throws InterruptedException, ApiException {
    int numAttempts = 1;
    int maxNumAttempts = 5;
    while (numAttempts <= maxNumAttempts) {
        try {
            return bufferApi.handoutResource(new HandoutRequestBody().handoutRequestId(handoutRequestId), POOL_ID).getCloudResourceUid().getGoogleProjectUid().getProjectId();
        } catch (ApiException e) {
            // Only retry when resource is not available (404).
            if (e.getCode() != 404) {
                throw e;
            }
            logger.info("No resource available, retrying... Attempts so far: {}", numAttempts, e);
        }
        ++numAttempts;
        TimeUnit.SECONDS.sleep(5);
    }
    throw new InterruptedException("Exceeds maximum number of retries.");
}
Also used : HandoutRequestBody(bio.terra.buffer.model.HandoutRequestBody) ApiException(bio.terra.buffer.client.ApiException)

Aggregations

ApiException (bio.terra.buffer.client.ApiException)5 BufferApi (bio.terra.buffer.api.BufferApi)4 ApiClient (bio.terra.buffer.client.ApiClient)2 PoolInfo (bio.terra.buffer.model.PoolInfo)2 BufferServiceAPIException (bio.terra.workspace.service.buffer.exception.BufferServiceAPIException)2 BufferServiceAuthorizationException (bio.terra.workspace.service.buffer.exception.BufferServiceAuthorizationException)2 IOException (java.io.IOException)2 HandoutRequestBody (bio.terra.buffer.model.HandoutRequestBody)1 ResourceInfo (bio.terra.buffer.model.ResourceInfo)1 Traced (io.opencensus.contrib.spring.aop.Traced)1