From be1d504b9d859e7f3cbd7fa0411f7fa05c0428eb Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 8 Oct 2015 09:03:59 +0200 Subject: [PATCH 1/2] Overload RemoteGcsHelper.create to take projectId and key path --- .../gcloud/storage/RemoteGcsHelper.java | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java index f4c9b22a47b5..9940ad57af27 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java @@ -92,46 +92,32 @@ public static String generateBucketName() { } /** - * Creates a {@code RemoteGcsHelper} object. + * Creates a {@code RemoteGcsHelper} object for the given project id and JSON key path. * + * @param projectId id of the project to be used for running the tests + * @param keyPath path to the JSON key to be used for running the tests * @param options creation options * @return A {@code RemoteGcsHelper} object for the provided options. - * @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables - * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file - * pointed by {@code GCLOUD_TESTS_KEY} does not exist + * @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if the file pointed by + * {@code keyPath} does not exist */ - public static RemoteGcsHelper create(Option... options) throws GcsHelperException { + public static RemoteGcsHelper create(String projectId, String keyPath, Option... options) + throws GcsHelperException { boolean keyFromClassPath = false; Map, Option> optionsMap = Option.asImmutableMap(options); if (optionsMap.containsKey(KeyFromClasspath.class)) { keyFromClassPath = ((KeyFromClasspath) optionsMap.get(KeyFromClasspath.class)).keyFromClasspath(); } - String projectId = System.getenv(PROJECT_ID_ENV_VAR); - String stringKeyPath = System.getenv(PRIVATE_KEY_ENV_VAR); - if (projectId == null) { - String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set"; - if (log.isLoggable(Level.WARNING)) { - log.log(Level.WARNING, message); - } - throw new GcsHelperException(message); - } - if (stringKeyPath == null) { - String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set"; - if (log.isLoggable(Level.WARNING)) { - log.log(Level.WARNING, message); - } - throw new GcsHelperException(message); - } try { InputStream keyFileStream; if (keyFromClassPath) { - keyFileStream = RemoteGcsHelper.class.getResourceAsStream(stringKeyPath); + keyFileStream = RemoteGcsHelper.class.getResourceAsStream(keyPath); if (keyFileStream == null) { - throw new FileNotFoundException(stringKeyPath + " not found in classpath"); + throw new FileNotFoundException(keyPath + " not found in classpath"); } } else { - keyFileStream = new FileInputStream(stringKeyPath); + keyFileStream = new FileInputStream(keyPath); } StorageOptions storageOptions = StorageOptions.builder() .authCredentials(AuthCredentials.createForJson(keyFileStream)) @@ -151,6 +137,36 @@ public static RemoteGcsHelper create(Option... options) throws GcsHelperExceptio } } + /** + * Creates a {@code RemoteGcsHelper} object. Project id and path to JSON key are read from two + * environment variables: {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY}. + * + * @param options creation options + * @return A {@code RemoteGcsHelper} object for the provided options. + * @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables + * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file + * pointed by {@code GCLOUD_TESTS_KEY} does not exist + */ + public static RemoteGcsHelper create(Option... options) throws GcsHelperException { + String projectId = System.getenv(PROJECT_ID_ENV_VAR); + String keyPath = System.getenv(PRIVATE_KEY_ENV_VAR); + if (projectId == null) { + String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set"; + if (log.isLoggable(Level.WARNING)) { + log.log(Level.WARNING, message); + } + throw new GcsHelperException(message); + } + if (keyPath == null) { + String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set"; + if (log.isLoggable(Level.WARNING)) { + log.log(Level.WARNING, message); + } + throw new GcsHelperException(message); + } + return create(projectId, keyPath, options); + } + private static class DeleteBucketTask implements Callable { private Storage storage; From 385dd4efdd1a0d5a3be81bf4a5d89d87ca167449 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 8 Oct 2015 09:35:32 +0200 Subject: [PATCH 2/2] Add default RetryParams to storage options built from RemoteGcsHelper --- .../test/java/com/google/gcloud/storage/RemoteGcsHelper.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java index 9940ad57af27..b6e8daeec445 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.RetryParams; import com.google.gcloud.storage.RemoteGcsHelper.Option.KeyFromClasspath; import java.io.FileInputStream; @@ -122,6 +123,7 @@ public static RemoteGcsHelper create(String projectId, String keyPath, Option... StorageOptions storageOptions = StorageOptions.builder() .authCredentials(AuthCredentials.createForJson(keyFileStream)) .projectId(projectId) + .retryParams(RetryParams.getDefaultInstance()) .build(); return new RemoteGcsHelper(storageOptions); } catch (FileNotFoundException ex) {