From f0e945e14662b86594298557b83151d3cb7e1ebb Mon Sep 17 00:00:00 2001
From: Ajit Thakor <49403056+athakor@users.noreply.github.com>
Date: Wed, 5 Aug 2020 23:49:39 +0530
Subject: [PATCH] feat: expose updateTime field of the bucket (#449)
* feat: expose updateTime field of the bucket
* fix: package imports
* feat: make the bucket fields output only
* fix: address review changes
* feat: add more checks
* feat: remove redundant checks
---
.../clirr-ignored-differences.xml | 5 ++++
.../java/com/google/cloud/storage/Bucket.java | 6 +++++
.../com/google/cloud/storage/BucketInfo.java | 26 +++++++++++++++++++
.../google/cloud/storage/BucketInfoTest.java | 6 +++++
.../com/google/cloud/storage/BucketTest.java | 4 +++
.../cloud/storage/it/ITStorageTest.java | 22 ++++++++++++++++
6 files changed, 69 insertions(+)
diff --git a/google-cloud-storage/clirr-ignored-differences.xml b/google-cloud-storage/clirr-ignored-differences.xml
index bca2faaff..114310c67 100644
--- a/google-cloud-storage/clirr-ignored-differences.xml
+++ b/google-cloud-storage/clirr-ignored-differences.xml
@@ -21,4 +21,9 @@
com.google.cloud.storage.BucketInfo$Builder deleteLifecycleRules()
7013
+
+ com/google/cloud/storage/BucketInfo$Builder
+ com.google.cloud.storage.BucketInfo$Builder setUpdateTime(java.lang.Long)
+ 7013
+
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 c64a3dc5e..3714e6e94 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
@@ -601,6 +601,12 @@ Builder setCreateTime(Long createTime) {
return this;
}
+ @Override
+ Builder setUpdateTime(Long updateTime) {
+ infoBuilder.setUpdateTime(updateTime);
+ return this;
+ }
+
@Override
Builder setMetageneration(Long metageneration) {
infoBuilder.setMetageneration(metageneration);
diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java
index 361363638..b3e96113b 100644
--- a/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java
+++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java
@@ -84,6 +84,7 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo)
private final List lifecycleRules;
private final String etag;
private final Long createTime;
+ private final Long updateTime;
private final Long metageneration;
private final List cors;
private final List acl;
@@ -1005,6 +1006,8 @@ public abstract static class Builder {
abstract Builder setCreateTime(Long createTime);
+ abstract Builder setUpdateTime(Long updateTime);
+
abstract Builder setMetageneration(Long metageneration);
abstract Builder setLocationType(String locationType);
@@ -1090,6 +1093,7 @@ static final class BuilderImpl extends Builder {
private String location;
private String etag;
private Long createTime;
+ private Long updateTime;
private Long metageneration;
private List cors;
private List acl;
@@ -1113,6 +1117,7 @@ static final class BuilderImpl extends Builder {
name = bucketInfo.name;
etag = bucketInfo.etag;
createTime = bucketInfo.createTime;
+ updateTime = bucketInfo.updateTime;
metageneration = bucketInfo.metageneration;
location = bucketInfo.location;
storageClass = bucketInfo.storageClass;
@@ -1232,6 +1237,12 @@ Builder setCreateTime(Long createTime) {
return this;
}
+ @Override
+ Builder setUpdateTime(Long updateTime) {
+ this.updateTime = updateTime;
+ return this;
+ }
+
@Override
Builder setMetageneration(Long metageneration) {
this.metageneration = metageneration;
@@ -1337,6 +1348,7 @@ public BucketInfo build() {
name = builder.name;
etag = builder.etag;
createTime = builder.createTime;
+ updateTime = builder.updateTime;
metageneration = builder.metageneration;
location = builder.location;
storageClass = builder.storageClass;
@@ -1468,6 +1480,14 @@ public Long getCreateTime() {
return createTime;
}
+ /**
+ * Returns the last modification time of the bucket's metadata expressed as the number of
+ * milliseconds since the Unix epoch.
+ */
+ public Long getUpdateTime() {
+ return updateTime;
+ }
+
/** Returns the metadata generation of this bucket. */
public Long getMetageneration() {
return metageneration;
@@ -1650,6 +1670,9 @@ com.google.api.services.storage.model.Bucket toPb() {
if (createTime != null) {
bucketPb.setTimeCreated(new DateTime(createTime));
}
+ if (updateTime != null) {
+ bucketPb.setUpdated(new DateTime(updateTime));
+ }
if (metageneration != null) {
bucketPb.setMetageneration(metageneration);
}
@@ -1797,6 +1820,9 @@ static BucketInfo fromPb(com.google.api.services.storage.model.Bucket bucketPb)
if (bucketPb.getTimeCreated() != null) {
builder.setCreateTime(bucketPb.getTimeCreated().getValue());
}
+ if (bucketPb.getUpdated() != null) {
+ builder.setUpdateTime(bucketPb.getUpdated().getValue());
+ }
if (bucketPb.getLocation() != null) {
builder.setLocation(bucketPb.getLocation());
}
diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java
index b10839426..425cfb702 100644
--- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java
+++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java
@@ -56,6 +56,7 @@ public class BucketInfoTest {
private static final User OWNER = new User("user@gmail.com");
private static final String SELF_LINK = "http://storage/b/n";
private static final Long CREATE_TIME = System.currentTimeMillis();
+ private static final Long UPDATE_TIME = CREATE_TIME;
private static final List CORS = Collections.singletonList(Cors.newBuilder().build());
private static final List DEFAULT_ACL =
Collections.singletonList(Acl.of(User.ofAllAuthenticatedUsers(), Role.WRITER));
@@ -117,6 +118,7 @@ public class BucketInfoTest {
.setSelfLink(SELF_LINK)
.setCors(CORS)
.setCreateTime(CREATE_TIME)
+ .setUpdateTime(UPDATE_TIME)
.setDefaultAcl(DEFAULT_ACL)
.setDeleteRules(DELETE_RULES)
.setLifecycleRules(LIFECYCLE_RULES)
@@ -148,6 +150,7 @@ public class BucketInfoTest {
.setSelfLink(SELF_LINK)
.setCors(CORS)
.setCreateTime(CREATE_TIME)
+ .setUpdateTime(UPDATE_TIME)
.setDefaultAcl(DEFAULT_ACL)
.setDeleteRules(DELETE_RULES)
.setLifecycleRules(LIFECYCLE_RULES)
@@ -202,6 +205,7 @@ public void testBuilder() {
assertEquals(OWNER, BUCKET_INFO.getOwner());
assertEquals(SELF_LINK, BUCKET_INFO.getSelfLink());
assertEquals(CREATE_TIME, BUCKET_INFO.getCreateTime());
+ assertEquals(UPDATE_TIME, BUCKET_INFO.getUpdateTime());
assertEquals(CORS, BUCKET_INFO.getCors());
assertEquals(DEFAULT_ACL, BUCKET_INFO.getDefaultAcl());
assertEquals(DELETE_RULES, BUCKET_INFO.getDeleteRules());
@@ -223,6 +227,7 @@ public void testBuilder() {
}
@Test
+ @SuppressWarnings({"unchecked", "deprecation"})
public void testToPbAndFromPb() {
compareBuckets(BUCKET_INFO, BucketInfo.fromPb(BUCKET_INFO.toPb()));
BucketInfo bucketInfo =
@@ -245,6 +250,7 @@ private void compareBuckets(BucketInfo expected, BucketInfo value) {
assertEquals(expected.getOwner(), value.getOwner());
assertEquals(expected.getSelfLink(), value.getSelfLink());
assertEquals(expected.getCreateTime(), value.getCreateTime());
+ assertEquals(expected.getUpdateTime(), value.getUpdateTime());
assertEquals(expected.getCors(), value.getCors());
assertEquals(expected.getDefaultAcl(), value.getDefaultAcl());
assertEquals(expected.getDeleteRules(), value.getDeleteRules());
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 862fbb358..e8a8e3f46 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
@@ -69,6 +69,7 @@ public class BucketTest {
private static final User OWNER = new User("user@gmail.com");
private static final String SELF_LINK = "http://storage/b/n";
private static final Long CREATE_TIME = System.currentTimeMillis();
+ private static final Long UPDATE_TIME = CREATE_TIME - 1L;
private static final List CORS = Collections.singletonList(Cors.newBuilder().build());
private static final List DEFAULT_ACL =
Collections.singletonList(Acl.of(User.ofAllAuthenticatedUsers(), WRITER));
@@ -111,6 +112,7 @@ public class BucketTest {
.setSelfLink(SELF_LINK)
.setCors(CORS)
.setCreateTime(CREATE_TIME)
+ .setUpdateTime(UPDATE_TIME)
.setDefaultAcl(DEFAULT_ACL)
.setDeleteRules(DELETE_RULES)
.setLifecycleRules(LIFECYCLE_RULES)
@@ -803,6 +805,7 @@ public void testBuilder() {
.setSelfLink(SELF_LINK)
.setCors(CORS)
.setCreateTime(CREATE_TIME)
+ .setUpdateTime(UPDATE_TIME)
.setDefaultAcl(DEFAULT_ACL)
.setDeleteRules(DELETE_RULES)
.setLifecycleRules(LIFECYCLE_RULES)
@@ -828,6 +831,7 @@ public void testBuilder() {
assertEquals(OWNER, bucket.getOwner());
assertEquals(SELF_LINK, bucket.getSelfLink());
assertEquals(CREATE_TIME, bucket.getCreateTime());
+ assertEquals(UPDATE_TIME, bucket.getUpdateTime());
assertEquals(CORS, bucket.getCors());
assertEquals(DEFAULT_ACL, bucket.getDefaultAcl());
assertEquals(DELETE_RULES, bucket.getDeleteRules());
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 59000a078..a741f3eb7 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
@@ -3535,4 +3535,26 @@ public void testRemoveBucketCORS() throws ExecutionException, InterruptedExcepti
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}
+
+ @Test
+ public void testBucketUpdateTime() throws ExecutionException, InterruptedException {
+ String bucketName = RemoteStorageHelper.generateBucketName();
+ BucketInfo bucketInfo =
+ BucketInfo.newBuilder(bucketName).setLocation("us").setVersioningEnabled(true).build();
+ try {
+ Bucket bucket = storage.create(bucketInfo);
+ assertThat(bucket).isNotNull();
+ assertThat(bucket.versioningEnabled()).isTrue();
+ assertThat(bucket.getCreateTime()).isNotNull();
+ assertThat(bucket.getUpdateTime()).isEqualTo(bucket.getCreateTime());
+
+ Bucket updatedBucket = bucket.toBuilder().setVersioningEnabled(false).build().update();
+ assertThat(updatedBucket.versioningEnabled()).isFalse();
+ assertThat(updatedBucket.getUpdateTime()).isNotNull();
+ assertThat(updatedBucket.getCreateTime()).isEqualTo(bucket.getCreateTime());
+ assertThat(updatedBucket.getUpdateTime()).isGreaterThan(bucket.getCreateTime());
+ } finally {
+ RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
+ }
+ }
}