Search in sources :

Example 1 with SystemException

use of bio.terra.cli.exception.SystemException in project terra-cli by DataBiosphere.

the class DataRepoService method getVersion.

/**
 * Call the Data Repo "/configuration" endpoint to get the version of the server that is currently
 * running.
 *
 * @return the Data Repo configuration object
 */
public RepositoryConfigurationModel getVersion() {
    UnauthenticatedApi unauthenticatedApi = new UnauthenticatedApi(apiClient);
    RepositoryConfigurationModel repositoryConfig = null;
    try {
        repositoryConfig = unauthenticatedApi.retrieveRepositoryConfig();
    } catch (ApiException ex) {
        throw new SystemException("Error getting Data Repo version", ex);
    }
    return repositoryConfig;
}
Also used : SystemException(bio.terra.cli.exception.SystemException) RepositoryConfigurationModel(bio.terra.datarepo.model.RepositoryConfigurationModel) UnauthenticatedApi(bio.terra.datarepo.api.UnauthenticatedApi) ApiException(bio.terra.datarepo.client.ApiException)

Example 2 with SystemException

use of bio.terra.cli.exception.SystemException in project terra-cli by DataBiosphere.

the class GoogleNotebooks method stop.

public void stop(InstanceName instanceName) {
    try {
        Operation stopOperation = notebooks.instances().stop(instanceName).execute();
        pollForSuccess(stopOperation, "Error stopping notebook instance: ");
    } catch (InterruptedException | IOException e) {
        checkFor409BadState(e);
        throw new SystemException("Error stopping notebook instance", e);
    }
}
Also used : SystemException(bio.terra.cli.exception.SystemException) Operation(com.google.api.services.notebooks.v1.model.Operation) IOException(java.io.IOException)

Example 3 with SystemException

use of bio.terra.cli.exception.SystemException in project terra-cli by DataBiosphere.

the class GoogleOauth method revokeToken.

/**
 * Revoke token (https://developers.google.com/identity/protocols/oauth2/web-server#tokenrevoke).
 *
 * <p>Google Java OAuth library doesn't support revoking tokens
 * (https://github.com/googleapis/google-oauth-java-client/issues/250), so make the call ourself.
 *
 * @param credential credentials object
 */
public static void revokeToken(Optional<GoogleCredentials> credential) {
    if (credential.isPresent() && credential.get().getAccessToken() != null) {
        String endpoint = "https://oauth2.googleapis.com/revoke";
        Map<String, String> headers = ImmutableMap.of("Content-type", "application/x-www-form-urlencoded");
        Map<String, String> params = ImmutableMap.of("token", credential.get().getAccessToken().getTokenValue());
        try {
            HttpUtils.sendHttpRequest(endpoint, "POST", headers, params);
        } catch (IOException ioEx) {
            throw new SystemException("Unable to revoke token", ioEx);
        }
    }
}
Also used : SystemException(bio.terra.cli.exception.SystemException) IOException(java.io.IOException)

Example 4 with SystemException

use of bio.terra.cli.exception.SystemException in project terra-cli by DataBiosphere.

the class WorkspaceManagerService method enumerateAllResources.

/**
 * Call the Workspace Manager GET "/api/workspaces/v1/{workspaceId}/resources" endpoint, possibly
 * multiple times, to get a list of all resources (controlled and referenced) in the workspace.
 * Throw an exception if the number of resources in the workspace is greater than the specified
 * limit.
 *
 * @param workspaceId the workspace to query
 * @param limit the maximum number of resources to return
 * @return a list of resources
 * @throws SystemException if the number of resources in the workspace > the specified limit
 */
public List<ResourceDescription> enumerateAllResources(UUID workspaceId, int limit) {
    return handleClientExceptions(() -> {
        // poll the enumerate endpoint until no results are returned, or we hit the limit
        List<ResourceDescription> allResources = new ArrayList<>();
        int numResultsReturned = 0;
        do {
            int offset = allResources.size();
            ResourceList result = HttpUtils.callWithRetries(() -> new ResourceApi(apiClient).enumerateResources(workspaceId, offset, MAX_RESOURCES_PER_ENUMERATE_REQUEST, null, null), WorkspaceManagerService::isRetryable);
            // add all fetched resources to the running list
            numResultsReturned = result.getResources().size();
            logger.debug("Called enumerate endpoints, fetched {} resources", numResultsReturned);
            allResources.addAll(result.getResources());
            // if we have fetched more than the limit, then throw an exception
            if (allResources.size() > limit) {
                throw new SystemException("Total number of resources (" + allResources.size() + ") exceeds the CLI limit (" + limit + ")");
            }
        // if this fetch returned less than the maximum allowed per request, then that indicates
        // there are no more
        } while (numResultsReturned >= MAX_RESOURCES_PER_ENUMERATE_REQUEST);
        logger.debug("Fetched total number of resources: {}", allResources.size());
        return allResources;
    }, "Error enumerating resources in the workspace.");
}
Also used : ResourceList(bio.terra.workspace.model.ResourceList) ResourceApi(bio.terra.workspace.api.ResourceApi) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) SystemException(bio.terra.cli.exception.SystemException) ResourceDescription(bio.terra.workspace.model.ResourceDescription) ArrayList(java.util.ArrayList)

Example 5 with SystemException

use of bio.terra.cli.exception.SystemException in project terra-cli by DataBiosphere.

the class User method doOauthLoginFlow.

/**
 * Do the OAuth login flow. If there is an existing non-expired credential stored on disk, then we
 * load that. If not, then we prompt the user for the requested user scopes.
 */
private void doOauthLoginFlow() {
    try (InputStream inputStream = User.class.getClassLoader().getResourceAsStream(CLIENT_SECRET_FILENAME)) {
        // log the user in and get their consent to the requested scopes
        boolean launchBrowserAutomatically = Context.getConfig().getBrowserLaunchOption().equals(Config.BrowserLaunchOption.AUTO);
        googleCredentials = GoogleOauth.doLoginAndConsent(USER_SCOPES, inputStream, Context.getContextDir().toFile(), launchBrowserAutomatically, LOGIN_LANDING_PAGE);
    } catch (IOException | GeneralSecurityException ex) {
        throw new SystemException("Error fetching user credentials.", ex);
    }
}
Also used : PDUser(bio.terra.cli.serialization.persisted.PDUser) SystemException(bio.terra.cli.exception.SystemException) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Aggregations

SystemException (bio.terra.cli.exception.SystemException)18 IOException (java.io.IOException)13 InputStream (java.io.InputStream)4 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 PDServer (bio.terra.cli.serialization.persisted.PDServer)2 Operation (com.google.api.services.notebooks.v1.model.Operation)2 File (java.io.File)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 LocalProcessLauncher (bio.terra.cli.app.utils.LocalProcessLauncher)1 PDContext (bio.terra.cli.serialization.persisted.PDContext)1 PDUser (bio.terra.cli.serialization.persisted.PDUser)1 CloudResourceManagerCow (bio.terra.cloudres.google.cloudresourcemanager.CloudResourceManagerCow)1 UnauthenticatedApi (bio.terra.datarepo.api.UnauthenticatedApi)1 ApiException (bio.terra.datarepo.client.ApiException)1 RepositoryConfigurationModel (bio.terra.datarepo.model.RepositoryConfigurationModel)1 ControlledGcpResourceApi (bio.terra.workspace.api.ControlledGcpResourceApi)1 ReferencedGcpResourceApi (bio.terra.workspace.api.ReferencedGcpResourceApi)1