From a73af3e68eab8624e0d6dd97eac9a68fd9010fc5 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Tue, 31 Dec 2019 15:04:09 +0530 Subject: [PATCH 01/21] feat: integrate disabling Lifecycle rule api for to removing lifecyclerules from bucket --- google-cloud-storage/pom.xml | 8 +++ .../java/com/google/cloud/storage/Bucket.java | 10 ++++ .../com/google/cloud/storage/Storage.java | 8 +++ .../com/google/cloud/storage/StorageImpl.java | 19 +++++++ .../cloud/storage/spi/v1/HttpStorageRpc.java | 53 ++++++++++++++++++- .../storage/spi/v1/HttpStorageRpcSpans.java | 3 +- .../cloud/storage/spi/v1/StorageRpc.java | 8 +++ .../cloud/storage/it/ITStorageTest.java | 48 ++++++++++++++++- pom.xml | 11 ++++ 9 files changed, 165 insertions(+), 3 deletions(-) diff --git a/google-cloud-storage/pom.xml b/google-cloud-storage/pom.xml index b2fd42b33b..086cd4d530 100644 --- a/google-cloud-storage/pom.xml +++ b/google-cloud-storage/pom.xml @@ -72,6 +72,10 @@ io.opencensus opencensus-api + + com.google.cloud + google-cloud-iamcredentials + com.google.api.grpc proto-google-iam-v1 @@ -190,6 +194,10 @@ httpcore test + + com.google.protobuf + protobuf-java + diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 7aa847c9b6..4610b8868c 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1114,6 +1114,16 @@ public boolean deleteDefaultAcl(Entity entity) { return storage.deleteDefaultAcl(getName(), entity); } + /** + * Disable or delete lifecycle rules of the requested bucket. + * + * @return {@code true} if the bucket lifecycle rules was deleted. + * @throws StorageException upon failure + */ + public boolean disableLifeCycleRule(Bucket bucket, String serviceAccount) { + return storage.disableLifeCycleRule(bucket, serviceAccount); + } + /** * Creates a new default blob ACL entry on this bucket. * diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index e28a941088..889f85f712 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -3076,6 +3076,14 @@ PostPolicyV4 generateSignedPostPolicyV4( */ boolean deleteDefaultAcl(String bucket, Entity entity); + /** + * Disable or delete lifecycle rules of the requested bucket. + * + * @return {@code true} if the bucket lifecycle rules was deleted. + * @throws StorageException upon failure + */ + boolean disableLifeCycleRule(BucketInfo bucketInfo, String serviceAccount); + /** * Creates a new default blob ACL entry on the specified bucket. * diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 0e24521eb6..3ae730940d 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -1158,6 +1158,25 @@ public Boolean call() { } } + @Override + public boolean disableLifeCycleRule(final BucketInfo bucketInfo, final String serviceAccount) { + final com.google.api.services.storage.model.Bucket bucketPb = bucketInfo.toPb(); + try { + return runWithRetries( + new Callable() { + @Override + public Boolean call() { + return storageRpc.disableLifeCycleRule(bucketPb, serviceAccount); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock()); + } catch (RetryHelperException e) { + throw StorageException.translateAndThrow(e); + } + } + @Override public boolean deleteAcl(final String bucket, final Entity entity) { return deleteAcl(bucket, entity, new BucketSourceOption[0]); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index e211c0277e..85356ccd7c 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -25,6 +25,7 @@ import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.http.ByteArrayContent; import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpContent; import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestFactory; @@ -33,6 +34,7 @@ import com.google.api.client.http.HttpResponseException; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.InputStreamContent; +import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.http.json.JsonHttpContent; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; @@ -57,6 +59,9 @@ import com.google.cloud.Tuple; import com.google.cloud.http.CensusHttpModule; import com.google.cloud.http.HttpTransportOptions; +import com.google.cloud.iam.credentials.v1.GenerateAccessTokenRequest; +import com.google.cloud.iam.credentials.v1.GenerateAccessTokenResponse; +import com.google.cloud.iam.credentials.v1.IamCredentialsClient; import com.google.cloud.storage.StorageException; import com.google.cloud.storage.StorageOptions; import com.google.common.base.Function; @@ -66,6 +71,7 @@ import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; +import com.google.protobuf.Duration; import io.opencensus.common.Scope; import io.opencensus.trace.AttributeValue; import io.opencensus.trace.Span; @@ -87,7 +93,11 @@ public class HttpStorageRpc implements StorageRpc { public static final String NO_ACL_PROJECTION = "noAcl"; private static final String ENCRYPTION_KEY_PREFIX = "x-goog-encryption-"; private static final String SOURCE_ENCRYPTION_KEY_PREFIX = "x-goog-copy-source-encryption-"; - + private static final String GOOGLE_STORAGE_API_ENDPOINT = + "https://storage.googleapis.com/storage/v1/b/"; + private static final String GOOGLE_API_CLOUD_SCOPE = + "https://www.googleapis.com/auth/cloud-platform"; + private static final Duration LIFE_TIME = Duration.newBuilder().setSeconds(3600).build(); // declare this HttpStatus code here as it's not included in java.net.HttpURLConnection private static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416; @@ -1000,6 +1010,47 @@ public boolean deleteAcl(String bucket, String entity, Map options) { } } + @Override + public boolean disableLifeCycleRule(Bucket bucket, String serviceAccount) { + Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE); + Scope scope = tracer.withSpan(span); + try { + IamCredentialsClient client = IamCredentialsClient.create(); + GenerateAccessTokenRequest request = + GenerateAccessTokenRequest.newBuilder() + .setName(serviceAccount) + .addScope(GOOGLE_API_CLOUD_SCOPE) + .setLifetime(LIFE_TIME) + .build(); + GenerateAccessTokenResponse accessTokenResponse = client.generateAccessToken(request); + HttpHeaders headers = new HttpHeaders(); + headers.setAuthorization("Bearer " + accessTokenResponse.getAccessToken()); + headers.setContentType("application/json"); + + HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(); + HttpContent httpContent = new JsonHttpContent(new JacksonFactory(), ""); + HttpRequest httpRequest = + requestFactory + .buildPutRequest( + new GenericUrl( + GOOGLE_STORAGE_API_ENDPOINT + bucket.getName() + "?fields=lifecycle"), + httpContent) + .setHeaders(headers); + httpRequest.execute(); + return true; + } catch (IOException ex) { + span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); + StorageException serviceException = translate(ex); + if (serviceException.getCode() == HTTP_NOT_FOUND) { + return false; + } + throw serviceException; + } finally { + scope.close(); + span.end(); + } + } + @Override public BucketAccessControl createAcl(BucketAccessControl acl, Map options) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_CREATE_BUCKET_ACL); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java index a5e4daf7da..6428adb46d 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java @@ -33,6 +33,8 @@ class HttpStorageRpcSpans { static final String SPAN_NAME_PATCH_BUCKET = getTraceSpanName("patch(Bucket,Map)"); static final String SPAN_NAME_PATCH_OBJECT = getTraceSpanName("patch(StorageObject,Map)"); static final String SPAN_NAME_DELETE_BUCKET = getTraceSpanName("delete(Bucket,Map)"); + static final String SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE = + getTraceSpanName("delete(Bucket,String)"); static final String SPAN_NAME_DELETE_OBJECT = getTraceSpanName("delete(StorageObject,Map)"); static final String SPAN_NAME_CREATE_BATCH = getTraceSpanName("createBatch()"); static final String SPAN_NAME_COMPOSE = getTraceSpanName("compose(Iterable,StorageObject,Map)"); @@ -96,7 +98,6 @@ class HttpStorageRpcSpans { getTraceSpanName(RpcBatch.class.getName() + ".submit()"); static final EndSpanOptions END_SPAN_OPTIONS = EndSpanOptions.builder().setSampleToLocalSpanStore(true).build(); - static String getTraceSpanName(String methodDescriptor) { return String.format( "%s.%s.%s", SPAN_NAME_CLIENT_PREFIX, HttpStorageRpc.class.getName(), methodDescriptor); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index 36c7a5ff1b..321089ba7a 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -256,6 +256,14 @@ public int hashCode() { */ boolean delete(Bucket bucket, Map options); + /** + * Disable or delete lifecycle rules of the requested bucket. + * + * @return {@code true} if the bucket lifecycle rule was disable. + * @throws StorageException upon failure + */ + boolean disableLifeCycleRule(Bucket bucket, String serviceAccount); + /** * Deletes the requested storage object. * diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 2bbb20ce70..cb16351b2b 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -89,6 +89,8 @@ import com.google.common.collect.Lists; import com.google.common.io.BaseEncoding; import com.google.common.io.ByteStreams; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import com.google.iam.v1.Binding; import com.google.iam.v1.IAMPolicyGrpc; import com.google.iam.v1.SetIamPolicyRequest; @@ -181,6 +183,10 @@ public class ITStorageTest { && System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC").equalsIgnoreCase("true"); private static final List LOCATION_TYPES = ImmutableList.of("multi-region", "region", "dual-region"); + private static final String GOOGLE_API_CLIENT_EMAIL = "clientEmail"; + private static final String GOOGLE_API_CLOUD_SCOPE = + "https://www.googleapis.com/auth/cloud-platform"; + private static final String REGEXP = "^\"|\"$"; @BeforeClass public static void beforeClass() throws IOException { @@ -3250,7 +3256,7 @@ public void testBlobReload() throws Exception { String blobName = "test-blob-reload"; BlobId blobId = BlobId.of(BUCKET, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); - Blob blob = storage.create(blobInfo, new byte[] {0, 1, 2}); + Blob blob = storage.create(blobInfo, new byte[]{0, 1, 2}); Blob blobUnchanged = blob.reload(); assertEquals(blob, blobUnchanged); @@ -3273,4 +3279,44 @@ public void testBlobReload() throws Exception { updated.delete(); assertNull(updated.reload()); } + + @Test + public void testDisableLifeCycleRule() throws Exception { + String lifeCycleRuleBucket = RemoteStorageHelper.generateBucketName(); + List lifecycleRules = + ImmutableList.of( + new LifecycleRule( + LifecycleAction.newDeleteAction(), + LifecycleCondition.newBuilder().setAge(2).build())); + try { + Bucket bucket = + storage.create( + BucketInfo.newBuilder(lifeCycleRuleBucket) + .setStorageClass(StorageClass.COLDLINE) + .setLocation("us-central1") + .setLifecycleRules(lifecycleRules) + .build()); + assertEquals(lifeCycleRuleBucket, bucket.getName()); + assertEquals(StorageClass.COLDLINE, bucket.getStorageClass()); + assertEquals(lifecycleRules, bucket.getLifecycleRules()); + boolean isDisable = + bucket.disableLifeCycleRule(bucket, getFromCredential(GOOGLE_API_CLIENT_EMAIL)); + if (isDisable) { + bucket = + storage.get( + lifeCycleRuleBucket, Storage.BucketGetOption.fields(Storage.BucketField.values())); + assertNull(bucket.getLifecycleRules()); + } + } finally { + RemoteStorageHelper.forceDelete(storage, lifeCycleRuleBucket, 5, TimeUnit.SECONDS); + } + } + + private static String getFromCredential(String key) throws Exception { + Gson gson = new Gson(); + GoogleCredentials credentials = + GoogleCredentials.getApplicationDefault().createScoped(GOOGLE_API_CLOUD_SCOPE); + JsonObject jsonObject = gson.fromJson(gson.toJson(credentials), JsonObject.class); + return jsonObject.get(key).toString().replaceAll(REGEXP, ""); + } } diff --git a/pom.xml b/pom.xml index 9f50e1abaa..39f239b3b4 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ google-cloud-storage-parent 1.93.4 1.9.0 + 3.11.1 4.13 1.4.4 1.3.2 @@ -146,6 +147,16 @@ api-common ${google.api-common.version} + + com.google.cloud + google-cloud-iamcredentials + 0.43.0-alpha + + + com.google.protobuf + protobuf-java + ${protobuf.version} + com.google.api.grpc proto-google-iam-v1 From 22351d81f52953423e92f781fc5f1d8cbe4f17f0 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Fri, 3 Jan 2020 15:50:45 +0530 Subject: [PATCH 02/21] feat: code refactor and added method level java doc with example --- .../java/com/google/cloud/storage/Bucket.java | 23 ++++++++++++++++++- .../com/google/cloud/storage/Storage.java | 23 ++++++++++++++++++- .../com/google/cloud/storage/StorageImpl.java | 4 ++-- .../cloud/storage/spi/v1/HttpStorageRpc.java | 3 +-- .../cloud/storage/it/ITStorageTest.java | 8 ++++--- pom.xml | 3 ++- 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 4610b8868c..490aa7c99b 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1117,10 +1117,31 @@ public boolean deleteDefaultAcl(Entity entity) { /** * Disable or delete lifecycle rules of the requested bucket. * + *

Example of disable or delete lifecycle rules of the bucket. + * + *

{@code
+   * String bucketName = "my-unique-bucket";
+   * String serviceAccount = "client-email";
+   * Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
+   *     .setStorageClass(StorageClass.COLDLINE)
+   *     .setLocation("us-central1")
+   *     .setLifecycleRules(ImmutableList.of(
+   *             new LifecycleRule(
+   *                 LifecycleAction.newDeleteAction(),
+   *                 LifecycleCondition.newBuilder().setAge(2).build())))
+   *     .build());
+   * boolean isDisable  = bucket.disableLifeCycleRule(bucketName,serviceAccount);
+   * if (isDisable) {
+   *   // the lifecycle rules was disabled or deleted
+   * }
+   * }
+ * + * @param bucket name of the bucket. + * @param serviceAccount client_email which is present in credential.json file. * @return {@code true} if the bucket lifecycle rules was deleted. * @throws StorageException upon failure */ - public boolean disableLifeCycleRule(Bucket bucket, String serviceAccount) { + public boolean disableLifeCycleRule(String bucket, String serviceAccount) { return storage.disableLifeCycleRule(bucket, serviceAccount); } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 889f85f712..7a63d06358 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -3079,10 +3079,31 @@ PostPolicyV4 generateSignedPostPolicyV4( /** * Disable or delete lifecycle rules of the requested bucket. * + *

Example of disable or delete lifecycle rules of the bucket. + * + *

{@code
+   * String bucketName = "my-unique-bucket";
+   * String serviceAccount = "client-email";
+   * Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
+   *     .setStorageClass(StorageClass.COLDLINE)
+   *     .setLocation("us-central1")
+   *     .setLifecycleRules(ImmutableList.of(
+   *             new LifecycleRule(
+   *                 LifecycleAction.newDeleteAction(),
+   *                 LifecycleCondition.newBuilder().setAge(2).build())))
+   *     .build());
+   * boolean isDisable  = bucket.disableLifeCycleRule(bucketName,serviceAccount);
+   * if (isDisable) {
+   *   // the lifecycle rules was disabled or deleted
+   * }
+   * }
+ * + * @param bucket name of the bucket. + * @param serviceAccount client_email which is present in credential.json file. * @return {@code true} if the bucket lifecycle rules was deleted. * @throws StorageException upon failure */ - boolean disableLifeCycleRule(BucketInfo bucketInfo, String serviceAccount); + boolean disableLifeCycleRule(String bucket, String serviceAccount); /** * Creates a new default blob ACL entry on the specified bucket. diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 3ae730940d..3ad15623aa 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -1159,8 +1159,8 @@ public Boolean call() { } @Override - public boolean disableLifeCycleRule(final BucketInfo bucketInfo, final String serviceAccount) { - final com.google.api.services.storage.model.Bucket bucketPb = bucketInfo.toPb(); + public boolean disableLifeCycleRule(final String bucket, final String serviceAccount) { + final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); try { return runWithRetries( new Callable() { diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index 85356ccd7c..14898ee2f7 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -34,7 +34,6 @@ import com.google.api.client.http.HttpResponseException; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.InputStreamContent; -import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.http.json.JsonHttpContent; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; @@ -1027,7 +1026,7 @@ public boolean disableLifeCycleRule(Bucket bucket, String serviceAccount) { headers.setAuthorization("Bearer " + accessTokenResponse.getAccessToken()); headers.setContentType("application/json"); - HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(); + HttpRequestFactory requestFactory = storage.getRequestFactory(); HttpContent httpContent = new JsonHttpContent(new JacksonFactory(), ""); HttpRequest httpRequest = requestFactory diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index cb16351b2b..621f6dd748 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -75,6 +75,7 @@ import com.google.cloud.storage.Storage.BlobField; import com.google.cloud.storage.Storage.BlobWriteOption; import com.google.cloud.storage.Storage.BucketField; +import com.google.cloud.storage.Storage.BucketGetOption; import com.google.cloud.storage.StorageBatch; import com.google.cloud.storage.StorageBatchResult; import com.google.cloud.storage.StorageClass; @@ -3299,12 +3300,13 @@ public void testDisableLifeCycleRule() throws Exception { assertEquals(lifeCycleRuleBucket, bucket.getName()); assertEquals(StorageClass.COLDLINE, bucket.getStorageClass()); assertEquals(lifecycleRules, bucket.getLifecycleRules()); + assertNotNull(bucket.getLifecycleRules()); boolean isDisable = - bucket.disableLifeCycleRule(bucket, getFromCredential(GOOGLE_API_CLIENT_EMAIL)); + bucket.disableLifeCycleRule( + lifeCycleRuleBucket, getFromCredential(GOOGLE_API_CLIENT_EMAIL)); if (isDisable) { bucket = - storage.get( - lifeCycleRuleBucket, Storage.BucketGetOption.fields(Storage.BucketField.values())); + storage.get(lifeCycleRuleBucket, BucketGetOption.fields(Storage.BucketField.values())); assertNull(bucket.getLifecycleRules()); } } finally { diff --git a/pom.xml b/pom.xml index 39f239b3b4..3bf673889d 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ google-cloud-storage-parent 1.93.4 1.9.0 + 0.43.0-alpha 3.11.1 4.13 1.4.4 @@ -150,7 +151,7 @@ com.google.cloud google-cloud-iamcredentials - 0.43.0-alpha + ${google.iamcredentials.version} com.google.protobuf From 2332a136a53c7969945713e3cfa84d8fdb527482 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Fri, 3 Jan 2020 17:43:59 +0530 Subject: [PATCH 03/21] feat: add unit test and resolve used undeclared dependencies warnings --- google-cloud-storage/pom.xml | 8 ++++++++ .../com/google/cloud/storage/BucketTest.java | 9 +++++++++ pom.xml | 17 +++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/google-cloud-storage/pom.xml b/google-cloud-storage/pom.xml index 086cd4d530..65ef0aff1a 100644 --- a/google-cloud-storage/pom.xml +++ b/google-cloud-storage/pom.xml @@ -92,6 +92,14 @@ org.threeten threetenbp + + com.google.api.grpc + proto-google-cloud-iamcredentials-v1 + + + com.google.code.gson + gson + diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java index a96422f141..8567706f3d 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java @@ -703,6 +703,15 @@ public void testDeleteDefaultAcl() throws Exception { assertTrue(bucket.deleteDefaultAcl(User.ofAllAuthenticatedUsers())); } + @Test + public void testDisableLifeCycleRule() { + expect(storage.getOptions()).andReturn(mockOptions).times(1); + expect(storage.disableLifeCycleRule(BUCKET_INFO.getName(), "clientEmail")).andReturn(true); + replay(storage); + initializeBucket(); + assertTrue(bucket.disableLifeCycleRule(BUCKET_INFO.getName(), "clientEmail")); + } + @Test public void testCreateDefaultAcl() throws Exception { initializeExpectedBucket(4); diff --git a/pom.xml b/pom.xml index 3bf673889d..63b6f19299 100644 --- a/pom.xml +++ b/pom.xml @@ -133,6 +133,17 @@ google-api-services-storage v1-rev20200410-1.30.9 + + com.google.guava + guava + 28.1-android + + + com.google.code.gson + gson + 2.8.6 + compile + org.checkerframework checker-compat-qual @@ -153,6 +164,12 @@ google-cloud-iamcredentials ${google.iamcredentials.version} + + com.google.api.grpc + proto-google-cloud-iamcredentials-v1 + ${google.iamcredentials.version} + compile + com.google.protobuf protobuf-java From e1bc3abeae05f52c99a1bec15216783dea80cf9b Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Mon, 6 Jan 2020 05:14:59 -0500 Subject: [PATCH 04/21] feat: fix methods name --- .../src/main/java/com/google/cloud/storage/Bucket.java | 4 ++-- .../src/main/java/com/google/cloud/storage/Storage.java | 2 +- .../main/java/com/google/cloud/storage/StorageImpl.java | 4 ++-- .../com/google/cloud/storage/spi/v1/HttpStorageRpc.java | 2 +- .../java/com/google/cloud/storage/spi/v1/StorageRpc.java | 2 +- .../java/com/google/cloud/storage/it/ITStorageTest.java | 7 ++++--- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 490aa7c99b..64369cf74d 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1141,8 +1141,8 @@ public boolean deleteDefaultAcl(Entity entity) { * @return {@code true} if the bucket lifecycle rules was deleted. * @throws StorageException upon failure */ - public boolean disableLifeCycleRule(String bucket, String serviceAccount) { - return storage.disableLifeCycleRule(bucket, serviceAccount); + public boolean disableLifeCycleRules(String bucket, String serviceAccount) { + return storage.disableLifeCycleRules(bucket, serviceAccount); } /** diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 7a63d06358..9d8636981a 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -3103,7 +3103,7 @@ PostPolicyV4 generateSignedPostPolicyV4( * @return {@code true} if the bucket lifecycle rules was deleted. * @throws StorageException upon failure */ - boolean disableLifeCycleRule(String bucket, String serviceAccount); + boolean disableLifeCycleRules(String bucket, String serviceAccount); /** * Creates a new default blob ACL entry on the specified bucket. diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 3ad15623aa..66372fe96f 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -1159,14 +1159,14 @@ public Boolean call() { } @Override - public boolean disableLifeCycleRule(final String bucket, final String serviceAccount) { + public boolean disableLifeCycleRules(final String bucket, final String serviceAccount) { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); try { return runWithRetries( new Callable() { @Override public Boolean call() { - return storageRpc.disableLifeCycleRule(bucketPb, serviceAccount); + return storageRpc.disableLifeCycleRules(bucketPb, serviceAccount); } }, getOptions().getRetrySettings(), diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index 14898ee2f7..1e14b65dc0 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -1010,7 +1010,7 @@ public boolean deleteAcl(String bucket, String entity, Map options) { } @Override - public boolean disableLifeCycleRule(Bucket bucket, String serviceAccount) { + public boolean disableLifeCycleRules(Bucket bucket, String serviceAccount) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE); Scope scope = tracer.withSpan(span); try { diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index 321089ba7a..0f61ff66f4 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -262,7 +262,7 @@ public int hashCode() { * @return {@code true} if the bucket lifecycle rule was disable. * @throws StorageException upon failure */ - boolean disableLifeCycleRule(Bucket bucket, String serviceAccount); + boolean disableLifeCycleRules(Bucket bucket, String serviceAccount); /** * Deletes the requested storage object. diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 621f6dd748..684c4367d9 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -3228,6 +3228,7 @@ public void testBucketLogging() throws ExecutionException, InterruptedException } @Test +<<<<<<< HEAD public void testSignedPostPolicyV4() throws Exception { PostFieldsV4 fields = PostFieldsV4.newBuilder().setAcl("public-read").build(); @@ -3282,13 +3283,13 @@ public void testBlobReload() throws Exception { } @Test - public void testDisableLifeCycleRule() throws Exception { + public void testDisableLifeCycleRules() throws Exception { String lifeCycleRuleBucket = RemoteStorageHelper.generateBucketName(); List lifecycleRules = ImmutableList.of( new LifecycleRule( LifecycleAction.newDeleteAction(), - LifecycleCondition.newBuilder().setAge(2).build())); + LifecycleCondition.newBuilder().setAge(2).setIsLive(Boolean.TRUE).build())); try { Bucket bucket = storage.create( @@ -3302,7 +3303,7 @@ public void testDisableLifeCycleRule() throws Exception { assertEquals(lifecycleRules, bucket.getLifecycleRules()); assertNotNull(bucket.getLifecycleRules()); boolean isDisable = - bucket.disableLifeCycleRule( + bucket.disableLifeCycleRules( lifeCycleRuleBucket, getFromCredential(GOOGLE_API_CLIENT_EMAIL)); if (isDisable) { bucket = From 42c932cb5424685789e69d1f86d450ff4e11f98a Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Mon, 6 Jan 2020 05:30:11 -0500 Subject: [PATCH 05/21] feat: fix formatter and fix method name in unit test --- .../src/main/java/com/google/cloud/storage/Storage.java | 2 -- .../src/test/java/com/google/cloud/storage/BucketTest.java | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 9d8636981a..a28b9a4288 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -2010,8 +2010,6 @@ Blob create( * BlobId blobId = BlobId.of(bucketName, blobName); * Blob blob = storage.get(blobId, BlobGetOption.decryptionKey(blobEncryptionKey)); * } - * - * @throws StorageException upon failure */ Blob get(BlobId blob, BlobGetOption... options); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java index 8567706f3d..c0ec8e1176 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java @@ -706,10 +706,10 @@ public void testDeleteDefaultAcl() throws Exception { @Test public void testDisableLifeCycleRule() { expect(storage.getOptions()).andReturn(mockOptions).times(1); - expect(storage.disableLifeCycleRule(BUCKET_INFO.getName(), "clientEmail")).andReturn(true); + expect(storage.disableLifeCycleRules(BUCKET_INFO.getName(), "clientEmail")).andReturn(true); replay(storage); initializeBucket(); - assertTrue(bucket.disableLifeCycleRule(BUCKET_INFO.getName(), "clientEmail")); + assertTrue(bucket.disableLifeCycleRules(BUCKET_INFO.getName(), "clientEmail")); } @Test From f635369669f14428fe98c6e3914f9fad4b6f9d40 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Wed, 8 Jan 2020 12:15:06 +0530 Subject: [PATCH 06/21] feat: all the notes has been resolved --- .../java/com/google/cloud/storage/Bucket.java | 33 ++++++++++-------- .../com/google/cloud/storage/Storage.java | 34 +++++++++++-------- .../com/google/cloud/storage/StorageImpl.java | 4 +-- .../cloud/storage/spi/v1/HttpStorageRpc.java | 2 +- .../cloud/storage/spi/v1/StorageRpc.java | 6 ++-- .../com/google/cloud/storage/BucketTest.java | 6 ++-- .../cloud/storage/it/ITStorageTest.java | 9 +++-- 7 files changed, 50 insertions(+), 44 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 64369cf74d..c94b229f02 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1115,24 +1115,27 @@ public boolean deleteDefaultAcl(Entity entity) { } /** - * Disable or delete lifecycle rules of the requested bucket. + * Delete lifecycle rules of the requested bucket. * - *

Example of disable or delete lifecycle rules of the bucket. + *

Example of delete lifecycle rules of the bucket. * *

{@code
    * String bucketName = "my-unique-bucket";
    * String serviceAccount = "client-email";
-   * Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
-   *     .setStorageClass(StorageClass.COLDLINE)
-   *     .setLocation("us-central1")
-   *     .setLifecycleRules(ImmutableList.of(
-   *             new LifecycleRule(
-   *                 LifecycleAction.newDeleteAction(),
-   *                 LifecycleCondition.newBuilder().setAge(2).build())))
-   *     .build());
-   * boolean isDisable  = bucket.disableLifeCycleRule(bucketName,serviceAccount);
-   * if (isDisable) {
-   *   // the lifecycle rules was disabled or deleted
+   * Bucket bucket =
+   *     storage.create(
+   *         BucketInfo.newBuilder(bucketName)
+   *             .setStorageClass(StorageClass.COLDLINE)
+   *             .setLocation("us-central1")
+   *             .setLifecycleRules(
+   *                 ImmutableList.of(
+   *                     new LifecycleRule(
+   *                         LifecycleAction.newDeleteAction(),
+   *                         LifecycleCondition.newBuilder().setAge(2).build())))
+   *             .build());
+   * boolean isDeleted = bucket.deleteLifecycleRules(bucketName, serviceAccount);
+   * if (isDeleted) {
+   *   // the lifecycle rules was deleted
    * }
    * }
* @@ -1141,8 +1144,8 @@ public boolean deleteDefaultAcl(Entity entity) { * @return {@code true} if the bucket lifecycle rules was deleted. * @throws StorageException upon failure */ - public boolean disableLifeCycleRules(String bucket, String serviceAccount) { - return storage.disableLifeCycleRules(bucket, serviceAccount); + public boolean deleteLifecycleRules(String bucket, String serviceAccount) { + return storage.deleteLifecycleRules(bucket, serviceAccount); } /** diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index a28b9a4288..a0aa242c89 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -1999,7 +1999,6 @@ Blob create( * only if supplied Decrpytion Key decrypts the blob successfully, otherwise a {@link * StorageException} is thrown. For more information review * - * @throws StorageException upon failure * @see Encrypted * Elements @@ -2010,6 +2009,8 @@ Blob create( * BlobId blobId = BlobId.of(bucketName, blobName); * Blob blob = storage.get(blobId, BlobGetOption.decryptionKey(blobEncryptionKey)); * } + * + * @throws StorageException upon failure */ Blob get(BlobId blob, BlobGetOption... options); @@ -3075,24 +3076,27 @@ PostPolicyV4 generateSignedPostPolicyV4( boolean deleteDefaultAcl(String bucket, Entity entity); /** - * Disable or delete lifecycle rules of the requested bucket. + * Delete lifecycle rules of the requested bucket. * - *

Example of disable or delete lifecycle rules of the bucket. + *

Example of delete lifecycle rules of the bucket. * *

{@code
    * String bucketName = "my-unique-bucket";
    * String serviceAccount = "client-email";
-   * Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
-   *     .setStorageClass(StorageClass.COLDLINE)
-   *     .setLocation("us-central1")
-   *     .setLifecycleRules(ImmutableList.of(
-   *             new LifecycleRule(
-   *                 LifecycleAction.newDeleteAction(),
-   *                 LifecycleCondition.newBuilder().setAge(2).build())))
-   *     .build());
-   * boolean isDisable  = bucket.disableLifeCycleRule(bucketName,serviceAccount);
-   * if (isDisable) {
-   *   // the lifecycle rules was disabled or deleted
+   * Bucket bucket =
+   *     storage.create(
+   *         BucketInfo.newBuilder(bucketName)
+   *             .setStorageClass(StorageClass.COLDLINE)
+   *             .setLocation("us-central1")
+   *             .setLifecycleRules(
+   *                 ImmutableList.of(
+   *                     new LifecycleRule(
+   *                         LifecycleAction.newDeleteAction(),
+   *                         LifecycleCondition.newBuilder().setAge(2).build())))
+   *             .build());
+   * boolean isDeleted = storage.deleteLifecycleRules(bucketName, serviceAccount);
+   * if (isDeleted) {
+   *   // the lifecycle rules was deleted
    * }
    * }
* @@ -3101,7 +3105,7 @@ PostPolicyV4 generateSignedPostPolicyV4( * @return {@code true} if the bucket lifecycle rules was deleted. * @throws StorageException upon failure */ - boolean disableLifeCycleRules(String bucket, String serviceAccount); + boolean deleteLifecycleRules(String bucket, String serviceAccount); /** * Creates a new default blob ACL entry on the specified bucket. diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 66372fe96f..eadd6e0266 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -1159,14 +1159,14 @@ public Boolean call() { } @Override - public boolean disableLifeCycleRules(final String bucket, final String serviceAccount) { + public boolean deleteLifecycleRules(final String bucket, final String serviceAccount) { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); try { return runWithRetries( new Callable() { @Override public Boolean call() { - return storageRpc.disableLifeCycleRules(bucketPb, serviceAccount); + return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount); } }, getOptions().getRetrySettings(), diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index 1e14b65dc0..e1c4af6c4f 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -1010,7 +1010,7 @@ public boolean deleteAcl(String bucket, String entity, Map options) { } @Override - public boolean disableLifeCycleRules(Bucket bucket, String serviceAccount) { + public boolean deleteLifecycleRules(Bucket bucket, String serviceAccount) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE); Scope scope = tracer.withSpan(span); try { diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index 0f61ff66f4..edebbfe8fe 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -257,12 +257,12 @@ public int hashCode() { boolean delete(Bucket bucket, Map options); /** - * Disable or delete lifecycle rules of the requested bucket. + * Delete lifecycle rules of the requested bucket. * - * @return {@code true} if the bucket lifecycle rule was disable. + * @return {@code true} if the bucket lifecycle rule was deleted. * @throws StorageException upon failure */ - boolean disableLifeCycleRules(Bucket bucket, String serviceAccount); + boolean deleteLifecycleRules(Bucket bucket, String serviceAccount); /** * Deletes the requested storage object. diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java index c0ec8e1176..74ac6bca95 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java @@ -704,12 +704,12 @@ public void testDeleteDefaultAcl() throws Exception { } @Test - public void testDisableLifeCycleRule() { + public void testDeleteLifecycleRules() { expect(storage.getOptions()).andReturn(mockOptions).times(1); - expect(storage.disableLifeCycleRules(BUCKET_INFO.getName(), "clientEmail")).andReturn(true); + expect(storage.deleteLifecycleRules(BUCKET_INFO.getName(), "clientEmail")).andReturn(true); replay(storage); initializeBucket(); - assertTrue(bucket.disableLifeCycleRules(BUCKET_INFO.getName(), "clientEmail")); + assertTrue(bucket.deleteLifecycleRules(BUCKET_INFO.getName(), "clientEmail")); } @Test diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 684c4367d9..3b2fa1ee6e 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -3228,7 +3228,6 @@ public void testBucketLogging() throws ExecutionException, InterruptedException } @Test -<<<<<<< HEAD public void testSignedPostPolicyV4() throws Exception { PostFieldsV4 fields = PostFieldsV4.newBuilder().setAcl("public-read").build(); @@ -3283,7 +3282,7 @@ public void testBlobReload() throws Exception { } @Test - public void testDisableLifeCycleRules() throws Exception { + public void testDeleteLifecycleRules() throws Exception { String lifeCycleRuleBucket = RemoteStorageHelper.generateBucketName(); List lifecycleRules = ImmutableList.of( @@ -3302,10 +3301,10 @@ public void testDisableLifeCycleRules() throws Exception { assertEquals(StorageClass.COLDLINE, bucket.getStorageClass()); assertEquals(lifecycleRules, bucket.getLifecycleRules()); assertNotNull(bucket.getLifecycleRules()); - boolean isDisable = - bucket.disableLifeCycleRules( + boolean isDeleted = + bucket.deleteLifecycleRules( lifeCycleRuleBucket, getFromCredential(GOOGLE_API_CLIENT_EMAIL)); - if (isDisable) { + if (isDeleted) { bucket = storage.get(lifeCycleRuleBucket, BucketGetOption.fields(Storage.BucketField.values())); assertNull(bucket.getLifecycleRules()); From bd15eedb060ff3d610355c99de90e3af2b25df81 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Thu, 9 Jan 2020 12:30:56 +0530 Subject: [PATCH 07/21] build: ignore clirr related errors --- google-cloud-storage/clirr-ignored-differences.xml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/google-cloud-storage/clirr-ignored-differences.xml b/google-cloud-storage/clirr-ignored-differences.xml index ae8d781dbe..f685d290a9 100644 --- a/google-cloud-storage/clirr-ignored-differences.xml +++ b/google-cloud-storage/clirr-ignored-differences.xml @@ -21,4 +21,15 @@ com.google.cloud.storage.PostPolicyV4 generateSignedPostPolicyV4(com.google.cloud.storage.BlobInfo, long, java.util.concurrent.TimeUnit, com.google.cloud.storage.Storage$PostPolicyV4Option[]) 7012 - \ No newline at end of file + + com/google/cloud/storage/Storage + boolean deleteLifecycleRules(java.lang.String, java.lang.String) + 7012 + + + com/google/cloud/storage/spi/v1/StorageRpc + boolean deleteLifecycleRules(com.google.api.services.storage.model.Bucket, java.lang.String) + 7012 + + + From 32a716e99be43162d82f4b88c5fe6615a2b0583e Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Thu, 16 Jan 2020 19:45:49 +0530 Subject: [PATCH 08/21] fix: fix review changes --- .../clirr-ignored-differences.xml | 4 ++-- .../java/com/google/cloud/storage/Bucket.java | 19 +++++++---------- .../com/google/cloud/storage/Storage.java | 16 +++++++------- .../com/google/cloud/storage/StorageImpl.java | 4 ++-- .../cloud/storage/spi/v1/HttpStorageRpc.java | 16 ++++++++++++-- .../cloud/storage/spi/v1/StorageRpc.java | 6 +++--- .../com/google/cloud/storage/BucketTest.java | 4 ++-- .../cloud/storage/it/ITStorageTest.java | 21 +++---------------- 8 files changed, 41 insertions(+), 49 deletions(-) diff --git a/google-cloud-storage/clirr-ignored-differences.xml b/google-cloud-storage/clirr-ignored-differences.xml index f685d290a9..a136652927 100644 --- a/google-cloud-storage/clirr-ignored-differences.xml +++ b/google-cloud-storage/clirr-ignored-differences.xml @@ -23,12 +23,12 @@ com/google/cloud/storage/Storage - boolean deleteLifecycleRules(java.lang.String, java.lang.String) + boolean deleteLifecycleRules(java.lang.String) 7012 com/google/cloud/storage/spi/v1/StorageRpc - boolean deleteLifecycleRules(com.google.api.services.storage.model.Bucket, java.lang.String) + boolean deleteLifecycleRules(com.google.api.services.storage.model.Bucket) 7012 diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index c94b229f02..666bd84398 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1115,13 +1115,12 @@ public boolean deleteDefaultAcl(Entity entity) { } /** - * Delete lifecycle rules of the requested bucket. + * Delete the lifecycle rules of this bucket. * - *

Example of delete lifecycle rules of the bucket. + *

Example of delete the lifecycle rules of this bucket. * *

{@code
    * String bucketName = "my-unique-bucket";
-   * String serviceAccount = "client-email";
    * Bucket bucket =
    *     storage.create(
    *         BucketInfo.newBuilder(bucketName)
@@ -1133,19 +1132,17 @@ public boolean deleteDefaultAcl(Entity entity) {
    *                         LifecycleAction.newDeleteAction(),
    *                         LifecycleCondition.newBuilder().setAge(2).build())))
    *             .build());
-   * boolean isDeleted = bucket.deleteLifecycleRules(bucketName, serviceAccount);
-   * if (isDeleted) {
-   *   // the lifecycle rules was deleted
+   * boolean rulesDeleted = bucket.deleteLifecycleRules();
+   * if (rulesDeleted) {
+   *   // the lifecycle rules were deleted
    * }
    * }
* - * @param bucket name of the bucket. - * @param serviceAccount client_email which is present in credential.json file. - * @return {@code true} if the bucket lifecycle rules was deleted. + * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure */ - public boolean deleteLifecycleRules(String bucket, String serviceAccount) { - return storage.deleteLifecycleRules(bucket, serviceAccount); + public boolean deleteLifecycleRules() { + return storage.deleteLifecycleRules(getName()); } /** diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index a0aa242c89..78c63ed507 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -3076,13 +3076,12 @@ PostPolicyV4 generateSignedPostPolicyV4( boolean deleteDefaultAcl(String bucket, Entity entity); /** - * Delete lifecycle rules of the requested bucket. + * Delete the lifecycle rules of the requested bucket. * - *

Example of delete lifecycle rules of the bucket. + *

Example of delete the lifecycle rules of the requested bucket. * *

{@code
    * String bucketName = "my-unique-bucket";
-   * String serviceAccount = "client-email";
    * Bucket bucket =
    *     storage.create(
    *         BucketInfo.newBuilder(bucketName)
@@ -3094,18 +3093,17 @@ PostPolicyV4 generateSignedPostPolicyV4(
    *                         LifecycleAction.newDeleteAction(),
    *                         LifecycleCondition.newBuilder().setAge(2).build())))
    *             .build());
-   * boolean isDeleted = storage.deleteLifecycleRules(bucketName, serviceAccount);
-   * if (isDeleted) {
-   *   // the lifecycle rules was deleted
+   * boolean rulesDeleted = storage.deleteLifecycleRules(bucketName);
+   * if (rulesDeleted) {
+   *   // the lifecycle rules were deleted
    * }
    * }
* * @param bucket name of the bucket. - * @param serviceAccount client_email which is present in credential.json file. - * @return {@code true} if the bucket lifecycle rules was deleted. + * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure */ - boolean deleteLifecycleRules(String bucket, String serviceAccount); + boolean deleteLifecycleRules(String bucket); /** * Creates a new default blob ACL entry on the specified bucket. diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index eadd6e0266..49589dc933 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -1159,14 +1159,14 @@ public Boolean call() { } @Override - public boolean deleteLifecycleRules(final String bucket, final String serviceAccount) { + public boolean deleteLifecycleRules(final String bucket) { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); try { return runWithRetries( new Callable() { @Override public Boolean call() { - return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount); + return storageRpc.deleteLifecycleRules(bucketPb); } }, getOptions().getRetrySettings(), diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index e1c4af6c4f..a902bb6678 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -55,6 +55,7 @@ import com.google.api.services.storage.model.ServiceAccount; import com.google.api.services.storage.model.StorageObject; import com.google.api.services.storage.model.TestIamPermissionsResponse; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.Tuple; import com.google.cloud.http.CensusHttpModule; import com.google.cloud.http.HttpTransportOptions; @@ -70,6 +71,8 @@ import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import com.google.protobuf.Duration; import io.opencensus.common.Scope; import io.opencensus.trace.AttributeValue; @@ -1010,14 +1013,23 @@ public boolean deleteAcl(String bucket, String entity, Map options) { } @Override - public boolean deleteLifecycleRules(Bucket bucket, String serviceAccount) { + public boolean deleteLifecycleRules(Bucket bucket) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE); Scope scope = tracer.withSpan(span); + Gson gson = new Gson(); try { + + /*Get the client email from the credentials to generate the accessToken*/ + GoogleCredentials credentials = + GoogleCredentials.getApplicationDefault().createScoped(GOOGLE_API_CLOUD_SCOPE); + JsonObject jsonCredential = gson.fromJson(gson.toJson(credentials), JsonObject.class); + String serviceAccountEmail = + jsonCredential.get("clientEmail").toString().replaceAll("^\"|\"$", ""); + IamCredentialsClient client = IamCredentialsClient.create(); GenerateAccessTokenRequest request = GenerateAccessTokenRequest.newBuilder() - .setName(serviceAccount) + .setName(serviceAccountEmail) .addScope(GOOGLE_API_CLOUD_SCOPE) .setLifetime(LIFE_TIME) .build(); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index edebbfe8fe..76f10a8a4a 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -257,12 +257,12 @@ public int hashCode() { boolean delete(Bucket bucket, Map options); /** - * Delete lifecycle rules of the requested bucket. + * Delete the lifecycle rules of the requested bucket. * - * @return {@code true} if the bucket lifecycle rule was deleted. + * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure */ - boolean deleteLifecycleRules(Bucket bucket, String serviceAccount); + boolean deleteLifecycleRules(Bucket bucket); /** * Deletes the requested storage object. diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java index 74ac6bca95..554e583f60 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java @@ -706,10 +706,10 @@ public void testDeleteDefaultAcl() throws Exception { @Test public void testDeleteLifecycleRules() { expect(storage.getOptions()).andReturn(mockOptions).times(1); - expect(storage.deleteLifecycleRules(BUCKET_INFO.getName(), "clientEmail")).andReturn(true); + expect(storage.deleteLifecycleRules(BUCKET_INFO.getName())).andReturn(true); replay(storage); initializeBucket(); - assertTrue(bucket.deleteLifecycleRules(BUCKET_INFO.getName(), "clientEmail")); + assertTrue(bucket.deleteLifecycleRules()); } @Test diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 3b2fa1ee6e..99e6583d9b 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -90,8 +90,6 @@ import com.google.common.collect.Lists; import com.google.common.io.BaseEncoding; import com.google.common.io.ByteStreams; -import com.google.gson.Gson; -import com.google.gson.JsonObject; import com.google.iam.v1.Binding; import com.google.iam.v1.IAMPolicyGrpc; import com.google.iam.v1.SetIamPolicyRequest; @@ -184,10 +182,6 @@ public class ITStorageTest { && System.getenv("GOOGLE_CLOUD_TESTS_IN_VPCSC").equalsIgnoreCase("true"); private static final List LOCATION_TYPES = ImmutableList.of("multi-region", "region", "dual-region"); - private static final String GOOGLE_API_CLIENT_EMAIL = "clientEmail"; - private static final String GOOGLE_API_CLOUD_SCOPE = - "https://www.googleapis.com/auth/cloud-platform"; - private static final String REGEXP = "^\"|\"$"; @BeforeClass public static void beforeClass() throws IOException { @@ -3301,10 +3295,9 @@ public void testDeleteLifecycleRules() throws Exception { assertEquals(StorageClass.COLDLINE, bucket.getStorageClass()); assertEquals(lifecycleRules, bucket.getLifecycleRules()); assertNotNull(bucket.getLifecycleRules()); - boolean isDeleted = - bucket.deleteLifecycleRules( - lifeCycleRuleBucket, getFromCredential(GOOGLE_API_CLIENT_EMAIL)); - if (isDeleted) { + + boolean rulesDeleted = bucket.deleteLifecycleRules(); + if (rulesDeleted) { bucket = storage.get(lifeCycleRuleBucket, BucketGetOption.fields(Storage.BucketField.values())); assertNull(bucket.getLifecycleRules()); @@ -3313,12 +3306,4 @@ public void testDeleteLifecycleRules() throws Exception { RemoteStorageHelper.forceDelete(storage, lifeCycleRuleBucket, 5, TimeUnit.SECONDS); } } - - private static String getFromCredential(String key) throws Exception { - Gson gson = new Gson(); - GoogleCredentials credentials = - GoogleCredentials.getApplicationDefault().createScoped(GOOGLE_API_CLOUD_SCOPE); - JsonObject jsonObject = gson.fromJson(gson.toJson(credentials), JsonObject.class); - return jsonObject.get(key).toString().replaceAll(REGEXP, ""); - } } From 1e4b71c54c4dc84beb097abdc96a0163ed03c0f0 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Sat, 18 Jan 2020 15:39:29 +0530 Subject: [PATCH 09/21] feat: use default service account to generate accesstoken --- .../clirr-ignored-differences.xml | 2 +- .../com/google/cloud/storage/StorageImpl.java | 5 ++++- .../cloud/storage/spi/v1/HttpStorageRpc.java | 16 ++-------------- .../google/cloud/storage/spi/v1/StorageRpc.java | 2 +- .../google/cloud/storage/it/ITStorageTest.java | 14 ++++++++------ 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/google-cloud-storage/clirr-ignored-differences.xml b/google-cloud-storage/clirr-ignored-differences.xml index a136652927..d133ffe5bf 100644 --- a/google-cloud-storage/clirr-ignored-differences.xml +++ b/google-cloud-storage/clirr-ignored-differences.xml @@ -28,7 +28,7 @@ com/google/cloud/storage/spi/v1/StorageRpc - boolean deleteLifecycleRules(com.google.api.services.storage.model.Bucket) + boolean deleteLifecycleRules(com.google.api.services.storage.model.Bucket, java.lang.String) 7012 diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 49589dc933..801802427c 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -47,6 +47,7 @@ import com.google.cloud.Policy; import com.google.cloud.ReadChannel; import com.google.cloud.RetryHelper.RetryHelperException; +import com.google.cloud.ServiceOptions; import com.google.cloud.Tuple; import com.google.cloud.storage.Acl.Entity; import com.google.cloud.storage.HmacKey.HmacKeyMetadata; @@ -1161,12 +1162,14 @@ public Boolean call() { @Override public boolean deleteLifecycleRules(final String bucket) { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); + final com.google.api.services.storage.model.ServiceAccount serviceAccount = + storageRpc.getServiceAccount(ServiceOptions.getDefaultProjectId()); try { return runWithRetries( new Callable() { @Override public Boolean call() { - return storageRpc.deleteLifecycleRules(bucketPb); + return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount.getEmailAddress()); } }, getOptions().getRetrySettings(), diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index a902bb6678..e1c4af6c4f 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -55,7 +55,6 @@ import com.google.api.services.storage.model.ServiceAccount; import com.google.api.services.storage.model.StorageObject; import com.google.api.services.storage.model.TestIamPermissionsResponse; -import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.Tuple; import com.google.cloud.http.CensusHttpModule; import com.google.cloud.http.HttpTransportOptions; @@ -71,8 +70,6 @@ import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; -import com.google.gson.Gson; -import com.google.gson.JsonObject; import com.google.protobuf.Duration; import io.opencensus.common.Scope; import io.opencensus.trace.AttributeValue; @@ -1013,23 +1010,14 @@ public boolean deleteAcl(String bucket, String entity, Map options) { } @Override - public boolean deleteLifecycleRules(Bucket bucket) { + public boolean deleteLifecycleRules(Bucket bucket, String serviceAccount) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE); Scope scope = tracer.withSpan(span); - Gson gson = new Gson(); try { - - /*Get the client email from the credentials to generate the accessToken*/ - GoogleCredentials credentials = - GoogleCredentials.getApplicationDefault().createScoped(GOOGLE_API_CLOUD_SCOPE); - JsonObject jsonCredential = gson.fromJson(gson.toJson(credentials), JsonObject.class); - String serviceAccountEmail = - jsonCredential.get("clientEmail").toString().replaceAll("^\"|\"$", ""); - IamCredentialsClient client = IamCredentialsClient.create(); GenerateAccessTokenRequest request = GenerateAccessTokenRequest.newBuilder() - .setName(serviceAccountEmail) + .setName(serviceAccount) .addScope(GOOGLE_API_CLOUD_SCOPE) .setLifetime(LIFE_TIME) .build(); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index 76f10a8a4a..084cc2ca6d 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -262,7 +262,7 @@ public int hashCode() { * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure */ - boolean deleteLifecycleRules(Bucket bucket); + boolean deleteLifecycleRules(Bucket bucket, String serviceAccount); /** * Deletes the requested storage object. diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 99e6583d9b..498380504f 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -3222,6 +3222,7 @@ public void testBucketLogging() throws ExecutionException, InterruptedException } @Test +<<<<<<< HEAD public void testSignedPostPolicyV4() throws Exception { PostFieldsV4 fields = PostFieldsV4.newBuilder().setAcl("public-read").build(); @@ -3276,7 +3277,7 @@ public void testBlobReload() throws Exception { } @Test - public void testDeleteLifecycleRules() throws Exception { + public void testDeleteLifecycleRules() throws ExecutionException, InterruptedException { String lifeCycleRuleBucket = RemoteStorageHelper.generateBucketName(); List lifecycleRules = ImmutableList.of( @@ -3297,11 +3298,12 @@ public void testDeleteLifecycleRules() throws Exception { assertNotNull(bucket.getLifecycleRules()); boolean rulesDeleted = bucket.deleteLifecycleRules(); - if (rulesDeleted) { - bucket = - storage.get(lifeCycleRuleBucket, BucketGetOption.fields(Storage.BucketField.values())); - assertNull(bucket.getLifecycleRules()); - } + assertTrue(rulesDeleted); + bucket = + storage.get(lifeCycleRuleBucket, BucketGetOption.fields(Storage.BucketField.values())); + assertNull(bucket.getLifecycleRules()); + } catch (StorageException ex) { + throw ex; } finally { RemoteStorageHelper.forceDelete(storage, lifeCycleRuleBucket, 5, TimeUnit.SECONDS); } From affa5e1f442cde33b67bd83dfd7dbb9af449b6c1 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Sat, 18 Jan 2020 15:47:29 +0530 Subject: [PATCH 10/21] build: fix unused declared dependencies --- google-cloud-storage/pom.xml | 4 ---- pom.xml | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud-storage/pom.xml b/google-cloud-storage/pom.xml index 65ef0aff1a..04e64587f0 100644 --- a/google-cloud-storage/pom.xml +++ b/google-cloud-storage/pom.xml @@ -96,10 +96,6 @@ com.google.api.grpc proto-google-cloud-iamcredentials-v1
- - com.google.code.gson - gson - diff --git a/pom.xml b/pom.xml index 63b6f19299..b23fd3531e 100644 --- a/pom.xml +++ b/pom.xml @@ -138,12 +138,16 @@ guava 28.1-android +<<<<<<< HEAD com.google.code.gson gson 2.8.6 compile +======= + +>>>>>>> build: fix unused declared dependencies org.checkerframework checker-compat-qual From 08e5c39c24e93de83b6610e169542ab217a8515a Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Mon, 3 Feb 2020 12:43:48 +0530 Subject: [PATCH 11/21] feat: use defaul service account creadential and add testcase for delete lifecycle rules --- .../java/com/google/cloud/storage/Bucket.java | 3 ++- .../java/com/google/cloud/storage/Storage.java | 4 +++- .../com/google/cloud/storage/StorageImpl.java | 11 ++++++----- .../google/cloud/storage/spi/v1/StorageRpc.java | 2 ++ .../com/google/cloud/storage/BucketTest.java | 3 ++- .../google/cloud/storage/StorageImplTest.java | 17 +++++++++++++++++ .../google/cloud/storage/it/ITStorageTest.java | 4 ++-- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 666bd84398..7abae9d5de 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1140,8 +1140,9 @@ public boolean deleteDefaultAcl(Entity entity) { * * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure + * @throws IOException */ - public boolean deleteLifecycleRules() { + public boolean deleteLifecycleRules() throws IOException { return storage.deleteLifecycleRules(getName()); } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 78c63ed507..5994b0142a 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -39,6 +39,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.io.BaseEncoding; +import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.net.URL; @@ -3102,8 +3103,9 @@ PostPolicyV4 generateSignedPostPolicyV4( * @param bucket name of the bucket. * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure + * @throws IOException */ - boolean deleteLifecycleRules(String bucket); + boolean deleteLifecycleRules(String bucket) throws IOException; /** * Creates a new default blob ACL entry on the specified bucket. diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 801802427c..cb72355cba 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -40,6 +40,7 @@ import com.google.api.services.storage.model.StorageObject; import com.google.api.services.storage.model.TestIamPermissionsResponse; import com.google.auth.ServiceAccountSigner; +import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.BaseService; import com.google.cloud.BatchResult; import com.google.cloud.PageImpl; @@ -47,7 +48,6 @@ import com.google.cloud.Policy; import com.google.cloud.ReadChannel; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.ServiceOptions; import com.google.cloud.Tuple; import com.google.cloud.storage.Acl.Entity; import com.google.cloud.storage.HmacKey.HmacKeyMetadata; @@ -71,6 +71,7 @@ import com.google.common.io.BaseEncoding; import com.google.common.primitives.Ints; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; @@ -1160,16 +1161,16 @@ public Boolean call() { } @Override - public boolean deleteLifecycleRules(final String bucket) { + public boolean deleteLifecycleRules(final String bucket) throws IOException { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); - final com.google.api.services.storage.model.ServiceAccount serviceAccount = - storageRpc.getServiceAccount(ServiceOptions.getDefaultProjectId()); + final ServiceAccountCredentials serviceAccount = + (ServiceAccountCredentials) ServiceAccountCredentials.getApplicationDefault(); try { return runWithRetries( new Callable() { @Override public Boolean call() { - return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount.getEmailAddress()); + return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount.getClientEmail()); } }, getOptions().getRetrySettings(), diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index 084cc2ca6d..53d57c02fb 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -30,6 +30,7 @@ import com.google.cloud.ServiceRpc; import com.google.cloud.Tuple; import com.google.cloud.storage.StorageException; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; @@ -261,6 +262,7 @@ public int hashCode() { * * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure + * @throws IOException */ boolean deleteLifecycleRules(Bucket bucket, String serviceAccount); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java index 554e583f60..5d128d24ca 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java @@ -45,6 +45,7 @@ import com.google.common.collect.Lists; import com.google.common.io.BaseEncoding; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.security.Key; import java.util.Collections; @@ -704,7 +705,7 @@ public void testDeleteDefaultAcl() throws Exception { } @Test - public void testDeleteLifecycleRules() { + public void testDeleteLifecycleRules() throws IOException { expect(storage.getOptions()).andReturn(mockOptions).times(1); expect(storage.deleteLifecycleRules(BUCKET_INFO.getName())).andReturn(true); replay(storage); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index 3d590e0071..be3e777303 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -2737,6 +2737,23 @@ public void testDeleteDefaultBucketAcl() { assertTrue(storage.deleteDefaultAcl(BUCKET_NAME1, User.ofAllAuthenticatedUsers())); } + @Test + public void testDeleteLifecyclesRulesOfBucket() throws IOException { + ServiceAccountCredentials credentials = + ServiceAccountCredentials.newBuilder() + .setClientEmail(SERVICE_ACCOUNT.getEmail()) + .setPrivateKey(privateKey) + .build(); + EasyMock.expect( + storageRpcMock.deleteLifecycleRules( + EasyMock.isA(com.google.api.services.storage.model.Bucket.class), + EasyMock.isA(String.class))) + .andReturn(true); + EasyMock.replay(storageRpcMock); + storage = options.toBuilder().setCredentials(credentials).build().getService(); + assertTrue(storage.deleteLifecycleRules(BUCKET_NAME1)); + } + @Test public void testCreateDefaultBucketAcl() { Acl returnedAcl = ACL.toBuilder().setEtag("ETAG").setId("ID").build(); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 498380504f..4eddb96fbe 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -3222,7 +3222,6 @@ public void testBucketLogging() throws ExecutionException, InterruptedException } @Test -<<<<<<< HEAD public void testSignedPostPolicyV4() throws Exception { PostFieldsV4 fields = PostFieldsV4.newBuilder().setAcl("public-read").build(); @@ -3277,7 +3276,8 @@ public void testBlobReload() throws Exception { } @Test - public void testDeleteLifecycleRules() throws ExecutionException, InterruptedException { + public void testDeleteLifecycleRules() + throws ExecutionException, InterruptedException, IOException { String lifeCycleRuleBucket = RemoteStorageHelper.generateBucketName(); List lifecycleRules = ImmutableList.of( From 5918c7e76f43b148c6d409e2d9909a9f41cee798 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Mon, 3 Feb 2020 16:20:14 +0530 Subject: [PATCH 12/21] feat: modified the code --- .../main/java/com/google/cloud/storage/StorageImpl.java | 3 ++- .../java/com/google/cloud/storage/StorageImplTest.java | 7 +------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index cb72355cba..6e93fb4942 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -40,6 +40,7 @@ import com.google.api.services.storage.model.StorageObject; import com.google.api.services.storage.model.TestIamPermissionsResponse; import com.google.auth.ServiceAccountSigner; +import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.BaseService; import com.google.cloud.BatchResult; @@ -1164,7 +1165,7 @@ public Boolean call() { public boolean deleteLifecycleRules(final String bucket) throws IOException { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); final ServiceAccountCredentials serviceAccount = - (ServiceAccountCredentials) ServiceAccountCredentials.getApplicationDefault(); + (ServiceAccountCredentials) GoogleCredentials.getApplicationDefault(); try { return runWithRetries( new Callable() { diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index be3e777303..dd34ac24e4 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -2739,18 +2739,13 @@ public void testDeleteDefaultBucketAcl() { @Test public void testDeleteLifecyclesRulesOfBucket() throws IOException { - ServiceAccountCredentials credentials = - ServiceAccountCredentials.newBuilder() - .setClientEmail(SERVICE_ACCOUNT.getEmail()) - .setPrivateKey(privateKey) - .build(); EasyMock.expect( storageRpcMock.deleteLifecycleRules( EasyMock.isA(com.google.api.services.storage.model.Bucket.class), EasyMock.isA(String.class))) .andReturn(true); EasyMock.replay(storageRpcMock); - storage = options.toBuilder().setCredentials(credentials).build().getService(); + initializeService(); assertTrue(storage.deleteLifecycleRules(BUCKET_NAME1)); } From 398a771f06884a242c73e0314e2efbd127d7abb7 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Mon, 3 Feb 2020 18:29:08 +0530 Subject: [PATCH 13/21] build: fix windows related builds --- .../src/main/java/com/google/cloud/storage/Bucket.java | 3 +-- .../main/java/com/google/cloud/storage/Storage.java | 4 +--- .../java/com/google/cloud/storage/StorageImpl.java | 10 +++------- .../com/google/cloud/storage/spi/v1/StorageRpc.java | 2 -- .../test/java/com/google/cloud/storage/BucketTest.java | 3 +-- .../java/com/google/cloud/storage/StorageImplTest.java | 2 ++ 6 files changed, 8 insertions(+), 16 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 7abae9d5de..666bd84398 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1140,9 +1140,8 @@ public boolean deleteDefaultAcl(Entity entity) { * * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure - * @throws IOException */ - public boolean deleteLifecycleRules() throws IOException { + public boolean deleteLifecycleRules() { return storage.deleteLifecycleRules(getName()); } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 5994b0142a..78c63ed507 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -39,7 +39,6 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.io.BaseEncoding; -import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.net.URL; @@ -3103,9 +3102,8 @@ PostPolicyV4 generateSignedPostPolicyV4( * @param bucket name of the bucket. * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure - * @throws IOException */ - boolean deleteLifecycleRules(String bucket) throws IOException; + boolean deleteLifecycleRules(String bucket); /** * Creates a new default blob ACL entry on the specified bucket. diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 6e93fb4942..2db9d44153 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -40,8 +40,6 @@ import com.google.api.services.storage.model.StorageObject; import com.google.api.services.storage.model.TestIamPermissionsResponse; import com.google.auth.ServiceAccountSigner; -import com.google.auth.oauth2.GoogleCredentials; -import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.BaseService; import com.google.cloud.BatchResult; import com.google.cloud.PageImpl; @@ -72,7 +70,6 @@ import com.google.common.io.BaseEncoding; import com.google.common.primitives.Ints; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; @@ -1162,16 +1159,15 @@ public Boolean call() { } @Override - public boolean deleteLifecycleRules(final String bucket) throws IOException { + public boolean deleteLifecycleRules(final String bucket) { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); - final ServiceAccountCredentials serviceAccount = - (ServiceAccountCredentials) GoogleCredentials.getApplicationDefault(); + final ServiceAccount serviceAccount = getServiceAccount(getOptions().getProjectId()); try { return runWithRetries( new Callable() { @Override public Boolean call() { - return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount.getClientEmail()); + return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount.getEmail()); } }, getOptions().getRetrySettings(), diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index 53d57c02fb..084cc2ca6d 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -30,7 +30,6 @@ import com.google.cloud.ServiceRpc; import com.google.cloud.Tuple; import com.google.cloud.storage.StorageException; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; @@ -262,7 +261,6 @@ public int hashCode() { * * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure - * @throws IOException */ boolean deleteLifecycleRules(Bucket bucket, String serviceAccount); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java index 5d128d24ca..554e583f60 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java @@ -45,7 +45,6 @@ import com.google.common.collect.Lists; import com.google.common.io.BaseEncoding; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.security.Key; import java.util.Collections; @@ -705,7 +704,7 @@ public void testDeleteDefaultAcl() throws Exception { } @Test - public void testDeleteLifecycleRules() throws IOException { + public void testDeleteLifecycleRules() { expect(storage.getOptions()).andReturn(mockOptions).times(1); expect(storage.deleteLifecycleRules(BUCKET_INFO.getName())).andReturn(true); replay(storage); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index dd34ac24e4..744e9ddf1b 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -2739,6 +2739,8 @@ public void testDeleteDefaultBucketAcl() { @Test public void testDeleteLifecyclesRulesOfBucket() throws IOException { + EasyMock.expect(storageRpcMock.getServiceAccount("projectId")) + .andReturn(SERVICE_ACCOUNT.toPb()); EasyMock.expect( storageRpcMock.deleteLifecycleRules( EasyMock.isA(com.google.api.services.storage.model.Bucket.class), From d48d6b6857693b3687c792c056c09e2fbe642014 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Fri, 7 Feb 2020 12:54:22 +0530 Subject: [PATCH 14/21] fix: resolve conflicts --- pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pom.xml b/pom.xml index b23fd3531e..63b6f19299 100644 --- a/pom.xml +++ b/pom.xml @@ -138,16 +138,12 @@ guava 28.1-android -<<<<<<< HEAD com.google.code.gson gson 2.8.6 compile -======= - ->>>>>>> build: fix unused declared dependencies org.checkerframework checker-compat-qual From 9e4d3fee96f1bcdb466810d814a9d6b269461961 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Fri, 7 Feb 2020 16:10:15 +0530 Subject: [PATCH 15/21] build: fix deps build --- .../google/cloud/storage/spi/v1/HttpStorageRpc.java | 3 +++ pom.xml | 10 ---------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index e1c4af6c4f..d9f80ca8ce 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -1014,9 +1014,12 @@ public boolean deleteLifecycleRules(Bucket bucket, String serviceAccount) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE); Scope scope = tracer.withSpan(span); try { + // ServiceAccountCredentials credentials = (ServiceAccountCredentials) + // GoogleCredentials.getApplicationDefault(); IamCredentialsClient client = IamCredentialsClient.create(); GenerateAccessTokenRequest request = GenerateAccessTokenRequest.newBuilder() + // .setName(credentials.getClientEmail()) .setName(serviceAccount) .addScope(GOOGLE_API_CLOUD_SCOPE) .setLifetime(LIFE_TIME) diff --git a/pom.xml b/pom.xml index 63b6f19299..b47e6ab8ea 100644 --- a/pom.xml +++ b/pom.xml @@ -133,11 +133,6 @@ google-api-services-storage v1-rev20200410-1.30.9 - - com.google.guava - guava - 28.1-android - com.google.code.gson gson @@ -170,11 +165,6 @@ ${google.iamcredentials.version} compile - - com.google.protobuf - protobuf-java - ${protobuf.version} - com.google.api.grpc proto-google-iam-v1 From 57045a398990f30c5808d70b756b36a6f1bc973a Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Fri, 21 Feb 2020 11:12:43 +0530 Subject: [PATCH 16/21] feat: code refactoring --- .../clirr-ignored-differences.xml | 2 +- google-cloud-storage/pom.xml | 8 ----- .../com/google/cloud/storage/StorageImpl.java | 3 +- .../cloud/storage/spi/v1/HttpStorageRpc.java | 29 +++---------------- .../cloud/storage/spi/v1/StorageRpc.java | 2 +- .../google/cloud/storage/StorageImplTest.java | 7 ++--- .../cloud/storage/it/ITStorageTest.java | 6 ++-- pom.xml | 11 ------- 8 files changed, 11 insertions(+), 57 deletions(-) diff --git a/google-cloud-storage/clirr-ignored-differences.xml b/google-cloud-storage/clirr-ignored-differences.xml index d133ffe5bf..a136652927 100644 --- a/google-cloud-storage/clirr-ignored-differences.xml +++ b/google-cloud-storage/clirr-ignored-differences.xml @@ -28,7 +28,7 @@ com/google/cloud/storage/spi/v1/StorageRpc - boolean deleteLifecycleRules(com.google.api.services.storage.model.Bucket, java.lang.String) + boolean deleteLifecycleRules(com.google.api.services.storage.model.Bucket) 7012 diff --git a/google-cloud-storage/pom.xml b/google-cloud-storage/pom.xml index 04e64587f0..946b9b6edb 100644 --- a/google-cloud-storage/pom.xml +++ b/google-cloud-storage/pom.xml @@ -72,10 +72,6 @@ io.opencensus opencensus-api - - com.google.cloud - google-cloud-iamcredentials - com.google.api.grpc proto-google-iam-v1 @@ -92,10 +88,6 @@ org.threeten threetenbp - - com.google.api.grpc - proto-google-cloud-iamcredentials-v1 - diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 2db9d44153..49589dc933 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -1161,13 +1161,12 @@ public Boolean call() { @Override public boolean deleteLifecycleRules(final String bucket) { final com.google.api.services.storage.model.Bucket bucketPb = BucketInfo.of(bucket).toPb(); - final ServiceAccount serviceAccount = getServiceAccount(getOptions().getProjectId()); try { return runWithRetries( new Callable() { @Override public Boolean call() { - return storageRpc.deleteLifecycleRules(bucketPb, serviceAccount.getEmail()); + return storageRpc.deleteLifecycleRules(bucketPb); } }, getOptions().getRetrySettings(), diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index d9f80ca8ce..e5adb2251e 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -58,9 +58,6 @@ import com.google.cloud.Tuple; import com.google.cloud.http.CensusHttpModule; import com.google.cloud.http.HttpTransportOptions; -import com.google.cloud.iam.credentials.v1.GenerateAccessTokenRequest; -import com.google.cloud.iam.credentials.v1.GenerateAccessTokenResponse; -import com.google.cloud.iam.credentials.v1.IamCredentialsClient; import com.google.cloud.storage.StorageException; import com.google.cloud.storage.StorageOptions; import com.google.common.base.Function; @@ -1010,34 +1007,16 @@ public boolean deleteAcl(String bucket, String entity, Map options) { } @Override - public boolean deleteLifecycleRules(Bucket bucket, String serviceAccount) { + public boolean deleteLifecycleRules(Bucket bucket) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE); Scope scope = tracer.withSpan(span); try { - // ServiceAccountCredentials credentials = (ServiceAccountCredentials) - // GoogleCredentials.getApplicationDefault(); - IamCredentialsClient client = IamCredentialsClient.create(); - GenerateAccessTokenRequest request = - GenerateAccessTokenRequest.newBuilder() - // .setName(credentials.getClientEmail()) - .setName(serviceAccount) - .addScope(GOOGLE_API_CLOUD_SCOPE) - .setLifetime(LIFE_TIME) - .build(); - GenerateAccessTokenResponse accessTokenResponse = client.generateAccessToken(request); - HttpHeaders headers = new HttpHeaders(); - headers.setAuthorization("Bearer " + accessTokenResponse.getAccessToken()); - headers.setContentType("application/json"); - HttpRequestFactory requestFactory = storage.getRequestFactory(); HttpContent httpContent = new JsonHttpContent(new JacksonFactory(), ""); HttpRequest httpRequest = - requestFactory - .buildPutRequest( - new GenericUrl( - GOOGLE_STORAGE_API_ENDPOINT + bucket.getName() + "?fields=lifecycle"), - httpContent) - .setHeaders(headers); + requestFactory.buildPutRequest( + new GenericUrl(GOOGLE_STORAGE_API_ENDPOINT + bucket.getName() + "?fields=lifecycle"), + httpContent); httpRequest.execute(); return true; } catch (IOException ex) { diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index 084cc2ca6d..76f10a8a4a 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -262,7 +262,7 @@ public int hashCode() { * @return {@code true} if the bucket lifecycle rules were deleted. * @throws StorageException upon failure */ - boolean deleteLifecycleRules(Bucket bucket, String serviceAccount); + boolean deleteLifecycleRules(Bucket bucket); /** * Deletes the requested storage object. diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index 744e9ddf1b..436d3ed021 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -2738,13 +2738,10 @@ public void testDeleteDefaultBucketAcl() { } @Test - public void testDeleteLifecyclesRulesOfBucket() throws IOException { - EasyMock.expect(storageRpcMock.getServiceAccount("projectId")) - .andReturn(SERVICE_ACCOUNT.toPb()); + public void testDeleteLifecyclesRulesOfBucket() { EasyMock.expect( storageRpcMock.deleteLifecycleRules( - EasyMock.isA(com.google.api.services.storage.model.Bucket.class), - EasyMock.isA(String.class))) + EasyMock.isA(com.google.api.services.storage.model.Bucket.class))) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 4eddb96fbe..062fbe0783 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -199,7 +199,7 @@ public static void beforeClass() throws IOException { .build()); // Prepare KMS KeyRing for CMEK tests - prepareKmsKeys(); + // prepareKmsKeys(); } @Before @@ -3276,8 +3276,7 @@ public void testBlobReload() throws Exception { } @Test - public void testDeleteLifecycleRules() - throws ExecutionException, InterruptedException, IOException { + public void testDeleteLifecycleRules() throws ExecutionException, InterruptedException { String lifeCycleRuleBucket = RemoteStorageHelper.generateBucketName(); List lifecycleRules = ImmutableList.of( @@ -3296,7 +3295,6 @@ public void testDeleteLifecycleRules() assertEquals(StorageClass.COLDLINE, bucket.getStorageClass()); assertEquals(lifecycleRules, bucket.getLifecycleRules()); assertNotNull(bucket.getLifecycleRules()); - boolean rulesDeleted = bucket.deleteLifecycleRules(); assertTrue(rulesDeleted); bucket = diff --git a/pom.xml b/pom.xml index b47e6ab8ea..0c033aeec8 100644 --- a/pom.xml +++ b/pom.xml @@ -154,17 +154,6 @@ api-common ${google.api-common.version} - - com.google.cloud - google-cloud-iamcredentials - ${google.iamcredentials.version} - - - com.google.api.grpc - proto-google-cloud-iamcredentials-v1 - ${google.iamcredentials.version} - compile - com.google.api.grpc proto-google-iam-v1 From dd3a27686caaff7d6c2d26ff8596877122be903b Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Fri, 21 Feb 2020 11:14:23 +0530 Subject: [PATCH 17/21] feat: uncomment the prepareKmsKeys --- .../test/java/com/google/cloud/storage/it/ITStorageTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 062fbe0783..0ac829347b 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -199,7 +199,7 @@ public static void beforeClass() throws IOException { .build()); // Prepare KMS KeyRing for CMEK tests - // prepareKmsKeys(); + prepareKmsKeys(); } @Before From f722f706c63366b7f9503ded65daf78d995cb570 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Fri, 21 Feb 2020 11:34:03 +0530 Subject: [PATCH 18/21] feat: update method name in HttpStorageRpcSpans and remove unused code --- .../java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java | 4 ---- .../com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index e5adb2251e..78d2cad88b 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -67,7 +67,6 @@ import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; -import com.google.protobuf.Duration; import io.opencensus.common.Scope; import io.opencensus.trace.AttributeValue; import io.opencensus.trace.Span; @@ -91,9 +90,6 @@ public class HttpStorageRpc implements StorageRpc { private static final String SOURCE_ENCRYPTION_KEY_PREFIX = "x-goog-copy-source-encryption-"; private static final String GOOGLE_STORAGE_API_ENDPOINT = "https://storage.googleapis.com/storage/v1/b/"; - private static final String GOOGLE_API_CLOUD_SCOPE = - "https://www.googleapis.com/auth/cloud-platform"; - private static final Duration LIFE_TIME = Duration.newBuilder().setSeconds(3600).build(); // declare this HttpStatus code here as it's not included in java.net.HttpURLConnection private static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416; diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java index 6428adb46d..2c1501c4f9 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java @@ -34,7 +34,7 @@ class HttpStorageRpcSpans { static final String SPAN_NAME_PATCH_OBJECT = getTraceSpanName("patch(StorageObject,Map)"); static final String SPAN_NAME_DELETE_BUCKET = getTraceSpanName("delete(Bucket,Map)"); static final String SPAN_NAME_DELETE_BUCKET_LIFECYCLE_RULE = - getTraceSpanName("delete(Bucket,String)"); + getTraceSpanName("deleteLifecycleRules(String)"); static final String SPAN_NAME_DELETE_OBJECT = getTraceSpanName("delete(StorageObject,Map)"); static final String SPAN_NAME_CREATE_BATCH = getTraceSpanName("createBatch()"); static final String SPAN_NAME_COMPOSE = getTraceSpanName("compose(Iterable,StorageObject,Map)"); From 4d273fe1de5f1f1ab76da66d888e743a3b77f6b7 Mon Sep 17 00:00:00 2001 From: Ajit Thakor Date: Thu, 27 Feb 2020 11:43:31 +0530 Subject: [PATCH 19/21] feat: fix the typos --- .../src/main/java/com/google/cloud/storage/Bucket.java | 2 +- .../src/main/java/com/google/cloud/storage/Storage.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java index 666bd84398..1cbe0e7d4e 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -1117,7 +1117,7 @@ public boolean deleteDefaultAcl(Entity entity) { /** * Delete the lifecycle rules of this bucket. * - *

Example of delete the lifecycle rules of this bucket. + *

Example of deleting the lifecycle rules of this bucket. * *

{@code
    * String bucketName = "my-unique-bucket";
diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java
index 78c63ed507..92f0dd16d4 100644
--- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java
+++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java
@@ -3078,7 +3078,7 @@ PostPolicyV4 generateSignedPostPolicyV4(
   /**
    * Delete the lifecycle rules of the requested bucket.
    *
-   * 

Example of delete the lifecycle rules of the requested bucket. + *

Example of deleting the lifecycle rules of the requested bucket. * *

{@code
    * String bucketName = "my-unique-bucket";

From c3b1e81d3ef1fbef85b19798f82f92354ed61b0e Mon Sep 17 00:00:00 2001
From: athakor 
Date: Thu, 23 Apr 2020 13:03:03 +0530
Subject: [PATCH 20/21] fix: review changes

---
 google-cloud-storage/pom.xml | 4 ----
 pom.xml                      | 8 --------
 2 files changed, 12 deletions(-)

diff --git a/google-cloud-storage/pom.xml b/google-cloud-storage/pom.xml
index 946b9b6edb..b2fd42b33b 100644
--- a/google-cloud-storage/pom.xml
+++ b/google-cloud-storage/pom.xml
@@ -190,10 +190,6 @@
       httpcore
       test
     
-    
-      com.google.protobuf
-      protobuf-java
-    
   
 
   
diff --git a/pom.xml b/pom.xml
index 0c033aeec8..9f50e1abaa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -65,8 +65,6 @@
     google-cloud-storage-parent
     1.93.4
     1.9.0
-    0.43.0-alpha
-    3.11.1
     4.13
     1.4.4
     1.3.2
@@ -133,12 +131,6 @@
         google-api-services-storage
         v1-rev20200410-1.30.9
       
-      
-        com.google.code.gson
-        gson
-        2.8.6
-        compile
-      
       
         org.checkerframework
         checker-compat-qual

From 6a9da637f3ca417834895bbd1509a98a8f47975e Mon Sep 17 00:00:00 2001
From: athakor 
Date: Mon, 18 May 2020 13:47:40 +0530
Subject: [PATCH 21/21] build: fix lint error

---
 .../com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java    | 1 +
 .../test/java/com/google/cloud/storage/it/ITStorageTest.java    | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java
index 2c1501c4f9..35ef19a7fe 100644
--- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java
+++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpcSpans.java
@@ -98,6 +98,7 @@ class HttpStorageRpcSpans {
       getTraceSpanName(RpcBatch.class.getName() + ".submit()");
   static final EndSpanOptions END_SPAN_OPTIONS =
       EndSpanOptions.builder().setSampleToLocalSpanStore(true).build();
+
   static String getTraceSpanName(String methodDescriptor) {
     return String.format(
         "%s.%s.%s", SPAN_NAME_CLIENT_PREFIX, HttpStorageRpc.class.getName(), methodDescriptor);
diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java
index 0ac829347b..e73f63fe29 100644
--- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java
+++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java
@@ -3251,7 +3251,7 @@ public void testBlobReload() throws Exception {
     String blobName = "test-blob-reload";
     BlobId blobId = BlobId.of(BUCKET, blobName);
     BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
-    Blob blob = storage.create(blobInfo, new byte[]{0, 1, 2});
+    Blob blob = storage.create(blobInfo, new byte[] {0, 1, 2});
 
     Blob blobUnchanged = blob.reload();
     assertEquals(blob, blobUnchanged);