Skip to content

Commit

Permalink
feat: Prefix/Suffix Matches Lifecycle Condition (#1389)
Browse files Browse the repository at this point in the history
* Prefix/Suffix Matches Lifecycle Condition

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
sydney-munro and gcf-owl-bot[bot] committed Jun 7, 2022
1 parent 7c3aba2 commit 20c8848
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
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
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

0 comments on commit 20c8848

Please sign in to comment.