Skip to content

Commit

Permalink
feat: add support of customTimeBefore and daysSinceCustomTime
Browse files Browse the repository at this point in the history
  • Loading branch information
athakor committed Jun 26, 2020
1 parent 81472a4 commit 719b55c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
Expand Up @@ -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.");
Expand Down Expand Up @@ -405,15 +407,20 @@ 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(
lifecycleCondition.getMatchesStorageClass() == null
? null
: transform(
lifecycleCondition.getMatchesStorageClass(),
Functions.toStringFunction()));
Functions.toStringFunction()))
.setCustomTimeBefore(
lifecycleCondition.getCustomTimeBefore() == null
? null
: new DateTime(lifecycleCondition.getCustomTimeBefore().getValue()))
.setDaysSinceCustomTime(lifecycleCondition.getDaysSinceCustomTime());

rule.setCondition(condition);

Expand Down Expand Up @@ -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());
}
Expand All @@ -474,13 +483,17 @@ public static class LifecycleCondition implements Serializable {
private final Integer numberOfNewerVersions;
private final Boolean isLive;
private final List<StorageClass> matchesStorageClass;
private final DateTime customTimeBefore;
private final Integer daysSinceCustomTime;

private LifecycleCondition(Builder builder) {
this.age = builder.age;
this.createdBefore = builder.createdBefore;
this.numberOfNewerVersions = builder.numberOfNewerVersions;
this.isLive = builder.isLive;
this.matchesStorageClass = builder.matchesStorageClass;
this.customTimeBefore = builder.customTimeBefore;
this.daysSinceCustomTime = builder.daysSinceCustomTime;
}

public Builder toBuilder() {
Expand All @@ -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() {
Expand All @@ -504,6 +519,8 @@ public String toString() {
.add("numberofNewerVersions", numberOfNewerVersions)
.add("isLive", isLive)
.add("matchesStorageClass", matchesStorageClass)
.add("customTimeBefore", customTimeBefore)
.add("daysSinceCustomTime", daysSinceCustomTime)
.toString();
}

Expand All @@ -527,13 +544,23 @@ public List<StorageClass> getMatchesStorageClass() {
return matchesStorageClass;
}

public DateTime getCustomTimeBefore() {
return customTimeBefore;
}

public Integer getDaysSinceCustomTime() {
return daysSinceCustomTime;
}

/** Builder for {@code LifecycleCondition}. */
public static class Builder {
private Integer age;
private DateTime createdBefore;
private Integer numberOfNewerVersions;
private Boolean isLive;
private List<StorageClass> matchesStorageClass;
private DateTime customTimeBefore;
private Integer daysSinceCustomTime;

private Builder() {}

Expand Down Expand Up @@ -588,6 +615,16 @@ public Builder setMatchesStorageClass(List<StorageClass> 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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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 =
Expand All @@ -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);
}
Expand Down

0 comments on commit 719b55c

Please sign in to comment.