Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support of customTimeBefore and daysSinceCustomTime #396

Merged
merged 5 commits into from Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -353,7 +353,9 @@ public LifecycleRule(LifecycleAction action, LifecycleCondition condition) {
&& condition.getMatchesStorageClass() == null
&& condition.getNumberOfNewerVersions() == null
&& condition.getDaysSinceNoncurrentTime() == null
&& condition.getNoncurrentTimeBefore() == null) {
&& condition.getNoncurrentTimeBefore() == 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 @@ -427,7 +429,12 @@ Rule toPb() {
lifecycleCondition.getNoncurrentTimeBefore() == null
? null
: new DateTime(
true, lifecycleCondition.getNoncurrentTimeBefore().getValue(), 0));
true, lifecycleCondition.getNoncurrentTimeBefore().getValue(), 0))
.setCustomTimeBefore(
lifecycleCondition.getCustomTimeBefore() == null
? null
: new DateTime(true, lifecycleCondition.getCustomTimeBefore().getValue(), 0))
.setDaysSinceCustomTime(lifecycleCondition.getDaysSinceCustomTime());

rule.setCondition(condition);

Expand Down Expand Up @@ -472,7 +479,9 @@ public StorageClass apply(String storageClass) {
}
}))
.setDaysSinceNoncurrentTime(condition.getDaysSinceNoncurrentTime())
.setNoncurrentTimeBefore(condition.getNoncurrentTimeBefore());
.setNoncurrentTimeBefore(condition.getNoncurrentTimeBefore())
.setCustomTimeBefore(condition.getCustomTimeBefore())
.setDaysSinceCustomTime(condition.getDaysSinceCustomTime());

return new LifecycleRule(lifecycleAction, conditionBuilder.build());
}
Expand All @@ -492,6 +501,8 @@ public static class LifecycleCondition implements Serializable {
private final List<StorageClass> matchesStorageClass;
private final Integer daysSinceNoncurrentTime;
private final DateTime noncurrentTimeBefore;
private final DateTime customTimeBefore;
private final Integer daysSinceCustomTime;

private LifecycleCondition(Builder builder) {
this.age = builder.age;
Expand All @@ -501,6 +512,8 @@ private LifecycleCondition(Builder builder) {
this.matchesStorageClass = builder.matchesStorageClass;
this.daysSinceNoncurrentTime = builder.daysSinceNoncurrentTime;
this.noncurrentTimeBefore = builder.noncurrentTimeBefore;
this.customTimeBefore = builder.customTimeBefore;
this.daysSinceCustomTime = builder.daysSinceCustomTime;
}

public Builder toBuilder() {
Expand All @@ -511,7 +524,9 @@ public Builder toBuilder() {
.setIsLive(this.isLive)
.setMatchesStorageClass(this.matchesStorageClass)
.setDaysSinceNoncurrentTime(this.daysSinceNoncurrentTime)
.setNoncurrentTimeBefore(this.noncurrentTimeBefore);
.setNoncurrentTimeBefore(this.noncurrentTimeBefore)
.setCustomTimeBefore(this.customTimeBefore)
.setDaysSinceCustomTime(this.daysSinceCustomTime);
}

public static Builder newBuilder() {
Expand All @@ -528,6 +543,8 @@ public String toString() {
.add("matchesStorageClass", matchesStorageClass)
.add("daysSinceNoncurrentTime", daysSinceNoncurrentTime)
.add("noncurrentTimeBefore", noncurrentTimeBefore)
.add("customTimeBefore", customTimeBefore)
.add("daysSinceCustomTime", daysSinceCustomTime)
.toString();
}

Expand Down Expand Up @@ -563,6 +580,16 @@ public DateTime getNoncurrentTimeBefore() {
return noncurrentTimeBefore;
}

/* Returns the date in RFC 3339 format with only the date part (for instance, "2013-01-15").*/
public DateTime getCustomTimeBefore() {
return customTimeBefore;
}

/** Returns the number of days elapsed since the user-specified timestamp set on an object. */
public Integer getDaysSinceCustomTime() {
return daysSinceCustomTime;
}

/** Builder for {@code LifecycleCondition}. */
public static class Builder {
private Integer age;
Expand All @@ -572,6 +599,8 @@ public static class Builder {
private List<StorageClass> matchesStorageClass;
private Integer daysSinceNoncurrentTime;
private DateTime noncurrentTimeBefore;
private DateTime customTimeBefore;
private Integer daysSinceCustomTime;

private Builder() {}

Expand Down Expand Up @@ -649,6 +678,27 @@ public Builder setNoncurrentTimeBefore(DateTime noncurrentTimeBefore) {
return this;
}

/**
* Sets the date in RFC 3339 format with only the date part (for instance, "2013-01-15").
* Note that only date part will be considered, if the time is specified it will be
* truncated. This condition is satisfied when the custom time on an object is before this
* date in UTC.
*/
public Builder setCustomTimeBefore(DateTime customTimeBefore) {
this.customTimeBefore = customTimeBefore;
return this;
}

/**
* Sets the number of days elapsed since the user-specified timestamp set on an object. The
* condition is satisfied if the days elapsed is at least this number. If no custom
* timestamp is specified on an object, the condition does not apply.
*/
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 @@ -338,13 +338,18 @@ public void testLifecycleRules() {
.setNumberOfNewerVersions(10)
.setDaysSinceNoncurrentTime(30)
.setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis()))
.setCustomTimeBefore(new DateTime(System.currentTimeMillis()))
.setDaysSinceCustomTime(30)
.build())
.toPb();
assertEquals(StorageClass.COLDLINE.toString(), lifecycleRule.getAction().getStorageClass());
assertTrue(lifecycleRule.getCondition().getIsLive());
assertEquals(10, lifecycleRule.getCondition().getNumNewerVersions().intValue());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceNoncurrentTime().intValue());
assertNotNull(lifecycleRule.getCondition().getNoncurrentTimeBefore());
assertEquals(StorageClass.COLDLINE.toString(), lifecycleRule.getAction().getStorageClass());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue());
assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore());
}

@Test
Expand Down
Expand Up @@ -2252,6 +2252,8 @@ public void testBucketLifecycleRules() {
.setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE))
.setDaysSinceNoncurrentTime(30)
.setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis()))
.setCustomTimeBefore(new DateTime(System.currentTimeMillis()))
.setDaysSinceCustomTime(30)
.build())))
.build();
EasyMock.expect(
Expand All @@ -2268,5 +2270,7 @@ public void testBucketLifecycleRules() {
assertEquals(1, lifecycleRule.getCondition().getMatchesStorageClass().size());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceNoncurrentTime().intValue());
assertNotNull(lifecycleRule.getCondition().getNoncurrentTimeBefore());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue());
assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore());
}
}
Expand Up @@ -458,6 +458,8 @@ public void testGetBucketLifecycleRules() {
.setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE))
.setDaysSinceNoncurrentTime(30)
.setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis()))
.setCustomTimeBefore(new DateTime(System.currentTimeMillis()))
.setDaysSinceCustomTime(30)
.build())))
.build());
Bucket remoteBucket =
Expand All @@ -476,6 +478,8 @@ public void testGetBucketLifecycleRules() {
assertEquals(1, lifecycleRule.getCondition().getMatchesStorageClass().size());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceNoncurrentTime().intValue());
assertNotNull(lifecycleRule.getCondition().getNoncurrentTimeBefore());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue());
assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore());
} finally {
storage.delete(lifecycleTestBucketName);
}
Expand Down