Skip to content

Commit f0e945e

Browse files
authored
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
1 parent 12bc02d commit f0e945e

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

google-cloud-storage/clirr-ignored-differences.xml

+5
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
<method>com.google.cloud.storage.BucketInfo$Builder deleteLifecycleRules()</method>
2222
<differenceType>7013</differenceType>
2323
</difference>
24+
<difference>
25+
<className>com/google/cloud/storage/BucketInfo$Builder</className>
26+
<method>com.google.cloud.storage.BucketInfo$Builder setUpdateTime(java.lang.Long)</method>
27+
<differenceType>7013</differenceType>
28+
</difference>
2429
</differences>

google-cloud-storage/src/main/java/com/google/cloud/storage/Bucket.java

+6
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ Builder setCreateTime(Long createTime) {
601601
return this;
602602
}
603603

604+
@Override
605+
Builder setUpdateTime(Long updateTime) {
606+
infoBuilder.setUpdateTime(updateTime);
607+
return this;
608+
}
609+
604610
@Override
605611
Builder setMetageneration(Long metageneration) {
606612
infoBuilder.setMetageneration(metageneration);

google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java

+26
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo)
8484
private final List<LifecycleRule> lifecycleRules;
8585
private final String etag;
8686
private final Long createTime;
87+
private final Long updateTime;
8788
private final Long metageneration;
8889
private final List<Cors> cors;
8990
private final List<Acl> acl;
@@ -1005,6 +1006,8 @@ public abstract static class Builder {
10051006

10061007
abstract Builder setCreateTime(Long createTime);
10071008

1009+
abstract Builder setUpdateTime(Long updateTime);
1010+
10081011
abstract Builder setMetageneration(Long metageneration);
10091012

10101013
abstract Builder setLocationType(String locationType);
@@ -1090,6 +1093,7 @@ static final class BuilderImpl extends Builder {
10901093
private String location;
10911094
private String etag;
10921095
private Long createTime;
1096+
private Long updateTime;
10931097
private Long metageneration;
10941098
private List<Cors> cors;
10951099
private List<Acl> acl;
@@ -1113,6 +1117,7 @@ static final class BuilderImpl extends Builder {
11131117
name = bucketInfo.name;
11141118
etag = bucketInfo.etag;
11151119
createTime = bucketInfo.createTime;
1120+
updateTime = bucketInfo.updateTime;
11161121
metageneration = bucketInfo.metageneration;
11171122
location = bucketInfo.location;
11181123
storageClass = bucketInfo.storageClass;
@@ -1232,6 +1237,12 @@ Builder setCreateTime(Long createTime) {
12321237
return this;
12331238
}
12341239

1240+
@Override
1241+
Builder setUpdateTime(Long updateTime) {
1242+
this.updateTime = updateTime;
1243+
return this;
1244+
}
1245+
12351246
@Override
12361247
Builder setMetageneration(Long metageneration) {
12371248
this.metageneration = metageneration;
@@ -1337,6 +1348,7 @@ public BucketInfo build() {
13371348
name = builder.name;
13381349
etag = builder.etag;
13391350
createTime = builder.createTime;
1351+
updateTime = builder.updateTime;
13401352
metageneration = builder.metageneration;
13411353
location = builder.location;
13421354
storageClass = builder.storageClass;
@@ -1468,6 +1480,14 @@ public Long getCreateTime() {
14681480
return createTime;
14691481
}
14701482

1483+
/**
1484+
* Returns the last modification time of the bucket's metadata expressed as the number of
1485+
* milliseconds since the Unix epoch.
1486+
*/
1487+
public Long getUpdateTime() {
1488+
return updateTime;
1489+
}
1490+
14711491
/** Returns the metadata generation of this bucket. */
14721492
public Long getMetageneration() {
14731493
return metageneration;
@@ -1650,6 +1670,9 @@ com.google.api.services.storage.model.Bucket toPb() {
16501670
if (createTime != null) {
16511671
bucketPb.setTimeCreated(new DateTime(createTime));
16521672
}
1673+
if (updateTime != null) {
1674+
bucketPb.setUpdated(new DateTime(updateTime));
1675+
}
16531676
if (metageneration != null) {
16541677
bucketPb.setMetageneration(metageneration);
16551678
}
@@ -1797,6 +1820,9 @@ static BucketInfo fromPb(com.google.api.services.storage.model.Bucket bucketPb)
17971820
if (bucketPb.getTimeCreated() != null) {
17981821
builder.setCreateTime(bucketPb.getTimeCreated().getValue());
17991822
}
1823+
if (bucketPb.getUpdated() != null) {
1824+
builder.setUpdateTime(bucketPb.getUpdated().getValue());
1825+
}
18001826
if (bucketPb.getLocation() != null) {
18011827
builder.setLocation(bucketPb.getLocation());
18021828
}

google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class BucketInfoTest {
5656
private static final User OWNER = new User("[email protected]");
5757
private static final String SELF_LINK = "http://storage/b/n";
5858
private static final Long CREATE_TIME = System.currentTimeMillis();
59+
private static final Long UPDATE_TIME = CREATE_TIME;
5960
private static final List<Cors> CORS = Collections.singletonList(Cors.newBuilder().build());
6061
private static final List<Acl> DEFAULT_ACL =
6162
Collections.singletonList(Acl.of(User.ofAllAuthenticatedUsers(), Role.WRITER));
@@ -117,6 +118,7 @@ public class BucketInfoTest {
117118
.setSelfLink(SELF_LINK)
118119
.setCors(CORS)
119120
.setCreateTime(CREATE_TIME)
121+
.setUpdateTime(UPDATE_TIME)
120122
.setDefaultAcl(DEFAULT_ACL)
121123
.setDeleteRules(DELETE_RULES)
122124
.setLifecycleRules(LIFECYCLE_RULES)
@@ -148,6 +150,7 @@ public class BucketInfoTest {
148150
.setSelfLink(SELF_LINK)
149151
.setCors(CORS)
150152
.setCreateTime(CREATE_TIME)
153+
.setUpdateTime(UPDATE_TIME)
151154
.setDefaultAcl(DEFAULT_ACL)
152155
.setDeleteRules(DELETE_RULES)
153156
.setLifecycleRules(LIFECYCLE_RULES)
@@ -202,6 +205,7 @@ public void testBuilder() {
202205
assertEquals(OWNER, BUCKET_INFO.getOwner());
203206
assertEquals(SELF_LINK, BUCKET_INFO.getSelfLink());
204207
assertEquals(CREATE_TIME, BUCKET_INFO.getCreateTime());
208+
assertEquals(UPDATE_TIME, BUCKET_INFO.getUpdateTime());
205209
assertEquals(CORS, BUCKET_INFO.getCors());
206210
assertEquals(DEFAULT_ACL, BUCKET_INFO.getDefaultAcl());
207211
assertEquals(DELETE_RULES, BUCKET_INFO.getDeleteRules());
@@ -223,6 +227,7 @@ public void testBuilder() {
223227
}
224228

225229
@Test
230+
@SuppressWarnings({"unchecked", "deprecation"})
226231
public void testToPbAndFromPb() {
227232
compareBuckets(BUCKET_INFO, BucketInfo.fromPb(BUCKET_INFO.toPb()));
228233
BucketInfo bucketInfo =
@@ -245,6 +250,7 @@ private void compareBuckets(BucketInfo expected, BucketInfo value) {
245250
assertEquals(expected.getOwner(), value.getOwner());
246251
assertEquals(expected.getSelfLink(), value.getSelfLink());
247252
assertEquals(expected.getCreateTime(), value.getCreateTime());
253+
assertEquals(expected.getUpdateTime(), value.getUpdateTime());
248254
assertEquals(expected.getCors(), value.getCors());
249255
assertEquals(expected.getDefaultAcl(), value.getDefaultAcl());
250256
assertEquals(expected.getDeleteRules(), value.getDeleteRules());

google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class BucketTest {
6969
private static final User OWNER = new User("[email protected]");
7070
private static final String SELF_LINK = "http://storage/b/n";
7171
private static final Long CREATE_TIME = System.currentTimeMillis();
72+
private static final Long UPDATE_TIME = CREATE_TIME - 1L;
7273
private static final List<Cors> CORS = Collections.singletonList(Cors.newBuilder().build());
7374
private static final List<Acl> DEFAULT_ACL =
7475
Collections.singletonList(Acl.of(User.ofAllAuthenticatedUsers(), WRITER));
@@ -111,6 +112,7 @@ public class BucketTest {
111112
.setSelfLink(SELF_LINK)
112113
.setCors(CORS)
113114
.setCreateTime(CREATE_TIME)
115+
.setUpdateTime(UPDATE_TIME)
114116
.setDefaultAcl(DEFAULT_ACL)
115117
.setDeleteRules(DELETE_RULES)
116118
.setLifecycleRules(LIFECYCLE_RULES)
@@ -803,6 +805,7 @@ public void testBuilder() {
803805
.setSelfLink(SELF_LINK)
804806
.setCors(CORS)
805807
.setCreateTime(CREATE_TIME)
808+
.setUpdateTime(UPDATE_TIME)
806809
.setDefaultAcl(DEFAULT_ACL)
807810
.setDeleteRules(DELETE_RULES)
808811
.setLifecycleRules(LIFECYCLE_RULES)
@@ -828,6 +831,7 @@ public void testBuilder() {
828831
assertEquals(OWNER, bucket.getOwner());
829832
assertEquals(SELF_LINK, bucket.getSelfLink());
830833
assertEquals(CREATE_TIME, bucket.getCreateTime());
834+
assertEquals(UPDATE_TIME, bucket.getUpdateTime());
831835
assertEquals(CORS, bucket.getCors());
832836
assertEquals(DEFAULT_ACL, bucket.getDefaultAcl());
833837
assertEquals(DELETE_RULES, bucket.getDeleteRules());

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -3535,4 +3535,26 @@ public void testRemoveBucketCORS() throws ExecutionException, InterruptedExcepti
35353535
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
35363536
}
35373537
}
3538+
3539+
@Test
3540+
public void testBucketUpdateTime() throws ExecutionException, InterruptedException {
3541+
String bucketName = RemoteStorageHelper.generateBucketName();
3542+
BucketInfo bucketInfo =
3543+
BucketInfo.newBuilder(bucketName).setLocation("us").setVersioningEnabled(true).build();
3544+
try {
3545+
Bucket bucket = storage.create(bucketInfo);
3546+
assertThat(bucket).isNotNull();
3547+
assertThat(bucket.versioningEnabled()).isTrue();
3548+
assertThat(bucket.getCreateTime()).isNotNull();
3549+
assertThat(bucket.getUpdateTime()).isEqualTo(bucket.getCreateTime());
3550+
3551+
Bucket updatedBucket = bucket.toBuilder().setVersioningEnabled(false).build().update();
3552+
assertThat(updatedBucket.versioningEnabled()).isFalse();
3553+
assertThat(updatedBucket.getUpdateTime()).isNotNull();
3554+
assertThat(updatedBucket.getCreateTime()).isEqualTo(bucket.getCreateTime());
3555+
assertThat(updatedBucket.getUpdateTime()).isGreaterThan(bucket.getCreateTime());
3556+
} finally {
3557+
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
3558+
}
3559+
}
35383560
}

0 commit comments

Comments
 (0)