From 20c88489d80d716da28f78fed628b54345f32ca4 Mon Sep 17 00:00:00 2001 From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com> Date: Tue, 7 Jun 2022 14:06:45 -0700 Subject: [PATCH] feat: Prefix/Suffix Matches Lifecycle Condition (#1389) * 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 --- .../com/google/cloud/storage/BucketInfo.java | 46 +++++++++++++++++-- .../google/cloud/storage/BucketInfoTest.java | 4 ++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java index 16d802476..029906806 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/BucketInfo.java @@ -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 @@ -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); @@ -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 matchesPrefix; + private final List matchesSuffix; private LifecycleCondition(Builder builder) { this.age = builder.age; @@ -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() { @@ -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() { @@ -646,6 +656,8 @@ public String toString() { .add("noncurrentTimeBefore", noncurrentTimeBefore) .add("customTimeBefore", customTimeBefore) .add("daysSinceCustomTime", daysSinceCustomTime) + .add("matchesPrefix", matchesPrefix) + .add("matchesSuffix", matchesSuffix) .toString(); } @@ -691,6 +703,14 @@ public Integer getDaysSinceCustomTime() { return daysSinceCustomTime; } + public List getMatchesPrefix() { + return matchesPrefix; + } + + public List getMatchesSuffix() { + return matchesSuffix; + } + /** Builder for {@code LifecycleCondition}. */ public static class Builder { private Integer age; @@ -702,6 +722,8 @@ public static class Builder { private DateTime noncurrentTimeBefore; private DateTime customTimeBefore; private Integer daysSinceCustomTime; + private List matchesPrefix; + private List matchesSuffix; private Builder() {} @@ -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 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 matchesSuffix) { + this.matchesSuffix = matchesSuffix != null ? ImmutableList.copyOf(matchesSuffix) : null; + return this; + } + /** Builds a {@code LifecycleCondition} object. * */ public LifecycleCondition build() { return new LifecycleCondition(this); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java index 96eeb38e0..e05d9747c 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BucketInfoTest.java @@ -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()); @@ -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);