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 f2e82e173..10a95135c 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 @@ -345,7 +345,9 @@ public LifecycleRule(LifecycleAction action, LifecycleCondition condition) { && condition.getAge() == null && condition.getCreatedBefore() == null && condition.getMatchesStorageClass() == null - && condition.getNumberOfNewerVersions() == null) { + && condition.getNumberOfNewerVersions() == null + && condition.getCustomTimeBefore() == null + && condition.getDaysSinceCustomTime() == null) { throw new IllegalArgumentException( "You must specify at least one condition to use object lifecycle " + "management. Please see https://cloud.google.com/storage/docs/lifecycle for details."); @@ -405,7 +407,7 @@ Rule toPb() { .setCreatedBefore( lifecycleCondition.getCreatedBefore() == null ? null - : new DateTime(true, lifecycleCondition.getCreatedBefore().getValue(), 0)) + : new DateTime(false, lifecycleCondition.getCreatedBefore().getValue(), 0)) .setIsLive(lifecycleCondition.getIsLive()) .setNumNewerVersions(lifecycleCondition.getNumberOfNewerVersions()) .setMatchesStorageClass( @@ -413,7 +415,12 @@ Rule toPb() { ? null : transform( lifecycleCondition.getMatchesStorageClass(), - Functions.toStringFunction())); + Functions.toStringFunction())) + .setCustomTimeBefore( + lifecycleCondition.getCustomTimeBefore() == null + ? null + : new DateTime(lifecycleCondition.getCustomTimeBefore().getValue())) + .setDaysSinceCustomTime(lifecycleCondition.getDaysSinceCustomTime()); rule.setCondition(condition); @@ -456,7 +463,9 @@ static LifecycleRule fromPb(Rule rule) { public StorageClass apply(String storageClass) { return StorageClass.valueOf(storageClass); } - })); + })) + .setCustomTimeBefore(condition.getCustomTimeBefore()) + .setDaysSinceCustomTime(condition.getDaysSinceCustomTime()); return new LifecycleRule(lifecycleAction, conditionBuilder.build()); } @@ -474,6 +483,8 @@ public static class LifecycleCondition implements Serializable { private final Integer numberOfNewerVersions; private final Boolean isLive; private final List matchesStorageClass; + private final DateTime customTimeBefore; + private final Integer daysSinceCustomTime; private LifecycleCondition(Builder builder) { this.age = builder.age; @@ -481,6 +492,8 @@ private LifecycleCondition(Builder builder) { this.numberOfNewerVersions = builder.numberOfNewerVersions; this.isLive = builder.isLive; this.matchesStorageClass = builder.matchesStorageClass; + this.customTimeBefore = builder.customTimeBefore; + this.daysSinceCustomTime = builder.daysSinceCustomTime; } public Builder toBuilder() { @@ -489,7 +502,9 @@ public Builder toBuilder() { .setCreatedBefore(this.createdBefore) .setNumberOfNewerVersions(this.numberOfNewerVersions) .setIsLive(this.isLive) - .setMatchesStorageClass(this.matchesStorageClass); + .setMatchesStorageClass(this.matchesStorageClass) + .setCustomTimeBefore(this.customTimeBefore) + .setDaysSinceCustomTime(this.daysSinceCustomTime); } public static Builder newBuilder() { @@ -504,6 +519,8 @@ public String toString() { .add("numberofNewerVersions", numberOfNewerVersions) .add("isLive", isLive) .add("matchesStorageClass", matchesStorageClass) + .add("customTimeBefore", customTimeBefore) + .add("daysSinceCustomTime", daysSinceCustomTime) .toString(); } @@ -527,6 +544,14 @@ public List getMatchesStorageClass() { return matchesStorageClass; } + public DateTime getCustomTimeBefore() { + return customTimeBefore; + } + + public Integer getDaysSinceCustomTime() { + return daysSinceCustomTime; + } + /** Builder for {@code LifecycleCondition}. */ public static class Builder { private Integer age; @@ -534,6 +559,8 @@ public static class Builder { private Integer numberOfNewerVersions; private Boolean isLive; private List matchesStorageClass; + private DateTime customTimeBefore; + private Integer daysSinceCustomTime; private Builder() {} @@ -588,6 +615,16 @@ public Builder setMatchesStorageClass(List matchesStorageClass) { return this; } + public Builder setCustomTimeBefore(DateTime customTimeBefore) { + this.customTimeBefore = customTimeBefore; + return this; + } + + public Builder setDaysSinceCustomTime(Integer daysSinceCustomTime) { + this.daysSinceCustomTime = daysSinceCustomTime; + return this; + } + /** Builds a {@code LifecycleCondition} object. * */ public LifecycleCondition build() { return new LifecycleCondition(this); 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 196e6b493..ed95f27ba 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 @@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import com.google.api.client.util.DateTime; import com.google.api.services.storage.model.Bucket; import com.google.api.services.storage.model.Bucket.Lifecycle.Rule; import com.google.cloud.storage.Acl.Project; @@ -321,6 +322,18 @@ public void testLifecycleRules() { setStorageClassLifecycleRule.getAction().getStorageClass()); assertTrue(setStorageClassLifecycleRule.getCondition().getIsLive()); assertEquals(10, setStorageClassLifecycleRule.getCondition().getNumNewerVersions().intValue()); + + Rule lifecycleRule = + new LifecycleRule( + LifecycleAction.newSetStorageClassAction(StorageClass.COLDLINE), + LifecycleCondition.newBuilder() + .setCustomTimeBefore(new DateTime(System.currentTimeMillis())) + .setDaysSinceCustomTime(30) + .build()) + .toPb(); + assertEquals(StorageClass.COLDLINE.toString(), lifecycleRule.getAction().getStorageClass()); + assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue()); + assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore()); } @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 7966c9281..f89c6c341 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 @@ -444,6 +444,8 @@ public void testGetBucketLifecycleRules() { .setIsLive(false) .setCreatedBefore(new DateTime(System.currentTimeMillis())) .setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE)) + .setCustomTimeBefore(new DateTime(System.currentTimeMillis())) + .setDaysSinceCustomTime(30) .build()))) .build()); Bucket remoteBucket = @@ -460,6 +462,8 @@ public void testGetBucketLifecycleRules() { assertFalse(lifecycleRule.getCondition().getIsLive()); assertEquals(1, lifecycleRule.getCondition().getAge().intValue()); assertEquals(1, lifecycleRule.getCondition().getMatchesStorageClass().size()); + assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue()); + assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore()); } finally { storage.delete(lifecycleTestBucketName); }