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: Prefix/Suffix Matches Lifecycle Condition #1389

Merged
merged 5 commits into from
Jun 7, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ public LifecycleRule(LifecycleAction action, LifecycleCondition condition) {
&& condition.getDaysSinceNoncurrentTime() == null
&& condition.getNoncurrentTimeBefore() == null
&& condition.getCustomTimeBefore() == null
&& condition.getDaysSinceCustomTime() == null) {
&& condition.getDaysSinceCustomTime() == null
&& condition.getMatchesPrefix() == null
&& condition.getMatchesSuffix() == null) {
log.warning(
"Creating a lifecycle condition with no supported conditions:\n"
+ this
Expand Down Expand Up @@ -527,7 +529,9 @@ Rule toPb() {
lifecycleCondition.getCustomTimeBefore() == null
? null
: new DateTime(true, lifecycleCondition.getCustomTimeBefore().getValue(), 0))
.setDaysSinceCustomTime(lifecycleCondition.getDaysSinceCustomTime());
.setDaysSinceCustomTime(lifecycleCondition.getDaysSinceCustomTime())
.setMatchesPrefix(lifecycleCondition.getMatchesPrefix())
.setMatchesSuffix(lifecycleCondition.getMatchesSuffix());

rule.setCondition(condition);

Expand Down Expand Up @@ -604,6 +608,8 @@ public static class LifecycleCondition implements Serializable {
private final DateTime noncurrentTimeBefore;
private final DateTime customTimeBefore;
private final Integer daysSinceCustomTime;
private final List<String> matchesPrefix;
private final List<String> matchesSuffix;

private LifecycleCondition(Builder builder) {
this.age = builder.age;
Expand All @@ -615,6 +621,8 @@ private LifecycleCondition(Builder builder) {
this.noncurrentTimeBefore = builder.noncurrentTimeBefore;
this.customTimeBefore = builder.customTimeBefore;
this.daysSinceCustomTime = builder.daysSinceCustomTime;
this.matchesPrefix = builder.matchesPrefix;
this.matchesSuffix = builder.matchesSuffix;
}

public Builder toBuilder() {
Expand All @@ -627,7 +635,9 @@ public Builder toBuilder() {
.setDaysSinceNoncurrentTime(this.daysSinceNoncurrentTime)
.setNoncurrentTimeBefore(this.noncurrentTimeBefore)
.setCustomTimeBefore(this.customTimeBefore)
.setDaysSinceCustomTime(this.daysSinceCustomTime);
.setDaysSinceCustomTime(this.daysSinceCustomTime)
.setMatchesPrefix(this.matchesPrefix)
.setMatchesSuffix(this.matchesSuffix);
}

public static Builder newBuilder() {
Expand All @@ -646,6 +656,8 @@ public String toString() {
.add("noncurrentTimeBefore", noncurrentTimeBefore)
.add("customTimeBefore", customTimeBefore)
.add("daysSinceCustomTime", daysSinceCustomTime)
.add("matchesPrefix", matchesPrefix)
.add("matchesSuffix", matchesSuffix)
.toString();
}

Expand Down Expand Up @@ -691,6 +703,14 @@ public Integer getDaysSinceCustomTime() {
return daysSinceCustomTime;
}

public List<String> getMatchesPrefix() {
return matchesPrefix;
}

public List<String> getMatchesSuffix() {
return matchesSuffix;
}

/** Builder for {@code LifecycleCondition}. */
public static class Builder {
private Integer age;
Expand All @@ -702,6 +722,8 @@ public static class Builder {
private DateTime noncurrentTimeBefore;
private DateTime customTimeBefore;
private Integer daysSinceCustomTime;
private List<String> matchesPrefix;
private List<String> matchesSuffix;

private Builder() {}

Expand Down Expand Up @@ -800,6 +822,24 @@ public Builder setDaysSinceCustomTime(Integer daysSinceCustomTime) {
return this;
}

/**
* Sets the list of prefixes. If any prefix matches the beginning of the object’s name, this
* portion of the condition is satisfied for that object.
*/
public Builder setMatchesPrefix(List<String> matchesPrefix) {
this.matchesPrefix = matchesPrefix != null ? ImmutableList.copyOf(matchesPrefix) : null;
return this;
}

/**
* Sets the list of suffixes. If any suffix matches the end of the object’s name, this
* portion of the condition is satisfied for that object.
*/
public Builder setMatchesSuffix(List<String> matchesSuffix) {
this.matchesSuffix = matchesSuffix != null ? ImmutableList.copyOf(matchesSuffix) : null;
return this;
}

/** Builds a {@code LifecycleCondition} object. * */
public LifecycleCondition build() {
return new LifecycleCondition(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ public void testLifecycleRules() {
.setNoncurrentTimeBefore(new DateTime(System.currentTimeMillis()))
.setCustomTimeBefore(new DateTime(System.currentTimeMillis()))
.setDaysSinceCustomTime(30)
.setMatchesSuffix(Collections.singletonList("-suffix"))
.setMatchesPrefix(Collections.singletonList("prefix-"))
.build())
.toPb();
assertEquals(StorageClass.COLDLINE.toString(), lifecycleRule.getAction().getStorageClass());
Expand All @@ -375,6 +377,8 @@ public void testLifecycleRules() {
assertEquals(StorageClass.COLDLINE.toString(), lifecycleRule.getAction().getStorageClass());
assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue());
assertNotNull(lifecycleRule.getCondition().getCustomTimeBefore());
assertEquals("prefix-", lifecycleRule.getCondition().getMatchesPrefix().get(0));
assertEquals("-suffix", lifecycleRule.getCondition().getMatchesSuffix().get(0));
assertTrue(
LifecycleRule.fromPb(lifecycleRule).getAction() instanceof SetStorageClassLifecycleAction);

Expand Down