Skip to content

Commit

Permalink
Prefix/Suffix Matches Lifecycle Condition
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-munro committed May 13, 2022
1 parent 4e93c8e commit 412337f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
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 @@ -601,6 +605,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 @@ -612,6 +618,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 @@ -624,7 +632,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 @@ -643,6 +653,8 @@ public String toString() {
.add("noncurrentTimeBefore", noncurrentTimeBefore)
.add("customTimeBefore", customTimeBefore)
.add("daysSinceCustomTime", daysSinceCustomTime)
.add("matchesPrefix", matchesPrefix)
.add("matchesSuffix", matchesSuffix)
.toString();
}

Expand Down Expand Up @@ -688,6 +700,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 @@ -699,6 +719,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 @@ -797,6 +819,26 @@ 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;
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;
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 @@ -357,6 +357,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 @@ -367,6 +369,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));

Rule unsupportedRule =
new LifecycleRule(
Expand Down

0 comments on commit 412337f

Please sign in to comment.