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 daysSinceNoncurrentTime and noncurrentTimeBefore OLM options #335

Merged
merged 5 commits into from Aug 26, 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 @@ -351,7 +351,9 @@ public LifecycleRule(LifecycleAction action, LifecycleCondition condition) {
&& condition.getAge() == null
&& condition.getCreatedBefore() == null
&& condition.getMatchesStorageClass() == null
&& condition.getNumberOfNewerVersions() == null) {
&& condition.getNumberOfNewerVersions() == null
&& condition.getDaysSinceNoncurrentTime() == null
&& condition.getNoncurrentTimeBefore() == 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 @@ -419,7 +421,13 @@ Rule toPb() {
? null
: transform(
lifecycleCondition.getMatchesStorageClass(),
Functions.toStringFunction()));
Functions.toStringFunction()))
.setDaysSinceNoncurrentTime(lifecycleCondition.getDaysSinceNoncurrentTime())
.setNoncurrentTimeBefore(
lifecycleCondition.getNoncurrentTimeBefore() == null
? null
: new DateTime(
true, lifecycleCondition.getNoncurrentTimeBefore().getValue(), 0));

rule.setCondition(condition);

Expand Down Expand Up @@ -462,7 +470,9 @@ static LifecycleRule fromPb(Rule rule) {
public StorageClass apply(String storageClass) {
return StorageClass.valueOf(storageClass);
}
}));
}))
.setDaysSinceNoncurrentTime(condition.getDaysSinceNoncurrentTime())
.setNoncurrentTimeBefore(condition.getNoncurrentTimeBefore());

return new LifecycleRule(lifecycleAction, conditionBuilder.build());
}
Expand All @@ -480,13 +490,17 @@ public static class LifecycleCondition implements Serializable {
private final Integer numberOfNewerVersions;
private final Boolean isLive;
private final List<StorageClass> matchesStorageClass;
private final Integer daysSinceNoncurrentTime;
private final DateTime noncurrentTimeBefore;

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.daysSinceNoncurrentTime = builder.daysSinceNoncurrentTime;
this.noncurrentTimeBefore = builder.noncurrentTimeBefore;
}

public Builder toBuilder() {
Expand All @@ -495,7 +509,9 @@ public Builder toBuilder() {
.setCreatedBefore(this.createdBefore)
.setNumberOfNewerVersions(this.numberOfNewerVersions)
.setIsLive(this.isLive)
.setMatchesStorageClass(this.matchesStorageClass);
.setMatchesStorageClass(this.matchesStorageClass)
.setDaysSinceNoncurrentTime(this.daysSinceNoncurrentTime)
.setNoncurrentTimeBefore(this.noncurrentTimeBefore);
}

public static Builder newBuilder() {
Expand All @@ -510,6 +526,8 @@ public String toString() {
.add("numberofNewerVersions", numberOfNewerVersions)
.add("isLive", isLive)
.add("matchesStorageClass", matchesStorageClass)
.add("daysSinceNoncurrentTime", daysSinceNoncurrentTime)
.add("noncurrentTimeBefore", noncurrentTimeBefore)
.toString();
}

Expand All @@ -533,13 +551,27 @@ public List<StorageClass> getMatchesStorageClass() {
return matchesStorageClass;
}

/** Returns the number of days elapsed since the noncurrent timestamp of an object. */
public Integer getDaysSinceNoncurrentTime() {
athakor marked this conversation as resolved.
Show resolved Hide resolved
return daysSinceNoncurrentTime;
}

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

/** 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 Integer daysSinceNoncurrentTime;
private DateTime noncurrentTimeBefore;

private Builder() {}

Expand Down Expand Up @@ -594,6 +626,29 @@ public Builder setMatchesStorageClass(List<StorageClass> matchesStorageClass) {
return this;
}

/**
* Sets the number of days elapsed since the noncurrent timestamp of an object. The
* condition is satisfied if the days elapsed is at least this number. This condition is
* relevant only for versioned objects. The value of the field must be a nonnegative
* integer. If it's zero, the object version will become eligible for Lifecycle action as
* soon as it becomes noncurrent.
*/
public Builder setDaysSinceNoncurrentTime(Integer daysSinceNoncurrentTime) {
this.daysSinceNoncurrentTime = daysSinceNoncurrentTime;
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 noncurrent time on an object is before
* this date. This condition is relevant only for versioned objects.
*/
public Builder setNoncurrentTimeBefore(DateTime noncurrentTimeBefore) {
this.noncurrentTimeBefore = noncurrentTimeBefore;
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 @@ -328,6 +329,22 @@ 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()
.setIsLive(true)
.setNumberOfNewerVersions(10)
.setDaysSinceNoncurrentTime(30)
.setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis()))
.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());
}

@Test
Expand Down
Expand Up @@ -29,6 +29,7 @@
import static org.junit.Assert.assertTrue;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.util.DateTime;
import com.google.api.core.ApiClock;
import com.google.api.services.storage.model.Policy.Bindings;
import com.google.api.services.storage.model.StorageObject;
Expand Down Expand Up @@ -2232,4 +2233,40 @@ public void testV4PostPolicy() {
assertEquals(outputFields.get("key"), "my-object");
assertEquals("https://storage.googleapis.com/my-bucket/", policy.getUrl());
}

@Test
public void testBucketLifecycleRules() {
BucketInfo bucketInfo =
BucketInfo.newBuilder("b")
.setLocation("us")
.setLifecycleRules(
ImmutableList.of(
new BucketInfo.LifecycleRule(
BucketInfo.LifecycleRule.LifecycleAction.newSetStorageClassAction(
StorageClass.COLDLINE),
BucketInfo.LifecycleRule.LifecycleCondition.newBuilder()
.setAge(1)
.setNumberOfNewerVersions(3)
.setIsLive(false)
.setCreatedBefore(new DateTime(System.currentTimeMillis()))
.setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE))
.setDaysSinceNoncurrentTime(30)
.setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis()))
.build())))
.build();
EasyMock.expect(
storageRpcMock.create(bucketInfo.toPb(), new HashMap<StorageRpc.Option, Object>()))
.andReturn(bucketInfo.toPb());
EasyMock.replay(storageRpcMock);
initializeService();
Bucket bucket = storage.create(bucketInfo);
BucketInfo.LifecycleRule lifecycleRule = bucket.getLifecycleRules().get(0);
assertEquals(3, lifecycleRule.getCondition().getNumberOfNewerVersions().intValue());
assertNotNull(lifecycleRule.getCondition().getCreatedBefore());
assertFalse(lifecycleRule.getCondition().getIsLive());
assertEquals(1, lifecycleRule.getCondition().getAge().intValue());
assertEquals(1, lifecycleRule.getCondition().getMatchesStorageClass().size());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceNoncurrentTime().intValue());
assertNotNull(lifecycleRule.getCondition().getNoncurrentTimeBefore());
}
}
Expand Up @@ -456,6 +456,8 @@ public void testGetBucketLifecycleRules() {
.setIsLive(false)
.setCreatedBefore(new DateTime(System.currentTimeMillis()))
.setMatchesStorageClass(ImmutableList.of(StorageClass.COLDLINE))
.setDaysSinceNoncurrentTime(30)
.setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis()))
.build())))
.build());
Bucket remoteBucket =
Expand All @@ -472,6 +474,8 @@ public void testGetBucketLifecycleRules() {
assertFalse(lifecycleRule.getCondition().getIsLive());
assertEquals(1, lifecycleRule.getCondition().getAge().intValue());
assertEquals(1, lifecycleRule.getCondition().getMatchesStorageClass().size());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceNoncurrentTime().intValue());
assertNotNull(lifecycleRule.getCondition().getNoncurrentTimeBefore());
} finally {
storage.delete(lifecycleTestBucketName);
}
Expand Down