From 7c2e9bdf56f17ea5e4cddf2689265cfa3d65a3a3 Mon Sep 17 00:00:00 2001 From: Cathy Ouyang Date: Thu, 21 Apr 2022 14:52:26 -0700 Subject: [PATCH] feat(storage): add support for OLM Prefix/Suffix --- storage/bucket.go | 16 ++++++++++++++++ storage/bucket_test.go | 24 +++++++++++++++++++++++- storage/integration_test.go | 18 ++++++++++++++++-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/storage/bucket.go b/storage/bucket.go index 44607f031b7..c83be0a8e4b 100644 --- a/storage/bucket.go +++ b/storage/bucket.go @@ -719,12 +719,20 @@ type LifecycleCondition struct { // Liveness specifies the object's liveness. Relevant only for versioned objects Liveness Liveness + // MatchesPrefix is the condition matching an object if any of the + // matches_prefix strings are an exact prefix of the object's name. + MatchesPrefix []string + // MatchesStorageClasses is the condition matching the object's storage // class. // // Values include "STANDARD", "NEARLINE", "COLDLINE" and "ARCHIVE". MatchesStorageClasses []string + // MatchesSuffix is the condition matching an object if any of the + // matches_suffix strings are an exact suffix of the object's name. + MatchesSuffix []string + // NoncurrentTimeBefore is the noncurrent timestamp of the object. This // condition is satisfied when an object's noncurrent timestamp is before // midnight of the specified date in UTC. @@ -1418,7 +1426,9 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle { Age: r.Condition.AgeInDays, DaysSinceCustomTime: r.Condition.DaysSinceCustomTime, DaysSinceNoncurrentTime: r.Condition.DaysSinceNoncurrentTime, + MatchesPrefix: r.Condition.MatchesPrefix, MatchesStorageClass: r.Condition.MatchesStorageClasses, + MatchesSuffix: r.Condition.MatchesSuffix, NumNewerVersions: r.Condition.NumNewerVersions, }, } @@ -1463,7 +1473,9 @@ func toProtoLifecycle(l Lifecycle) *storagepb.Bucket_Lifecycle { AgeDays: proto.Int32(int32(r.Condition.AgeInDays)), DaysSinceCustomTime: proto.Int32(int32(r.Condition.DaysSinceCustomTime)), DaysSinceNoncurrentTime: proto.Int32(int32(r.Condition.DaysSinceNoncurrentTime)), + MatchesPrefix: r.Condition.MatchesPrefix, MatchesStorageClass: r.Condition.MatchesStorageClasses, + MatchesSuffix: r.Condition.MatchesSuffix, NumNewerVersions: proto.Int32(int32(r.Condition.NumNewerVersions)), }, } @@ -1506,7 +1518,9 @@ func toLifecycle(rl *raw.BucketLifecycle) Lifecycle { AgeInDays: rr.Condition.Age, DaysSinceCustomTime: rr.Condition.DaysSinceCustomTime, DaysSinceNoncurrentTime: rr.Condition.DaysSinceNoncurrentTime, + MatchesPrefix: rr.Condition.MatchesPrefix, MatchesStorageClasses: rr.Condition.MatchesStorageClass, + MatchesSuffix: rr.Condition.MatchesSuffix, NumNewerVersions: rr.Condition.NumNewerVersions, }, } @@ -1548,7 +1562,9 @@ func toLifecycleFromProto(rl *storagepb.Bucket_Lifecycle) Lifecycle { AgeInDays: int64(rr.GetCondition().GetAgeDays()), DaysSinceCustomTime: int64(rr.GetCondition().GetDaysSinceCustomTime()), DaysSinceNoncurrentTime: int64(rr.GetCondition().GetDaysSinceNoncurrentTime()), + MatchesPrefix: rr.GetCondition().GetMatchesPrefix(), MatchesStorageClasses: rr.GetCondition().GetMatchesStorageClass(), + MatchesSuffix: rr.GetCondition().GetMatchesSuffix(), NumNewerVersions: int64(rr.GetCondition().GetNumNewerVersions()), }, } diff --git a/storage/bucket_test.go b/storage/bucket_test.go index f1fc510bd2b..4031db55681 100644 --- a/storage/bucket_test.go +++ b/storage/bucket_test.go @@ -96,6 +96,16 @@ func TestBucketAttrsToRawBucket(t *testing.T) { MatchesStorageClasses: []string{"NEARLINE"}, NumNewerVersions: 10, }, + }, { + Action: LifecycleAction{ + Type: DeleteAction, + }, + Condition: LifecycleCondition{ + AgeInDays: 10, + MatchesPrefix: []string{"testPrefix"}, + MatchesSuffix: []string{"testSuffix"}, + NumNewerVersions: 3, + }, }, { Action: LifecycleAction{ Type: DeleteAction, @@ -177,7 +187,19 @@ func TestBucketAttrsToRawBucket(t *testing.T) { MatchesStorageClass: []string{"NEARLINE"}, NumNewerVersions: 10, }, - }, { + }, + { + Action: &raw.BucketLifecycleRuleAction{ + Type: DeleteAction, + }, + Condition: &raw.BucketLifecycleRuleCondition{ + Age: 10, + MatchesPrefix: []string{"testPrefix"}, + MatchesSuffix: []string{"testSuffix"}, + NumNewerVersions: 3, + }, + }, + { Action: &raw.BucketLifecycleRuleAction{ Type: DeleteAction, }, diff --git a/storage/integration_test.go b/storage/integration_test.go index 955b6d6feda..356f3216351 100644 --- a/storage/integration_test.go +++ b/storage/integration_test.go @@ -299,6 +299,16 @@ func TestIntegration_BucketCreateDelete(t *testing.T) { MatchesStorageClasses: []string{"NEARLINE"}, NumNewerVersions: 10, }, + }, { + Action: LifecycleAction{ + Type: DeleteAction, + }, + Condition: LifecycleCondition{ + AgeInDays: 10, + MatchesPrefix: []string{"testPrefix"}, + MatchesSuffix: []string{"testSuffix"}, + NumNewerVersions: 3, + }, }}, } @@ -475,8 +485,12 @@ func TestIntegration_BucketUpdate(t *testing.T) { wantLifecycle := Lifecycle{ Rules: []LifecycleRule{ { - Action: LifecycleAction{Type: "Delete"}, - Condition: LifecycleCondition{AgeInDays: 30}, + Action: LifecycleAction{Type: "Delete"}, + Condition: LifecycleCondition{ + AgeInDays: 30, + MatchesPrefix: []string{"testPrefix"}, + MatchesSuffix: []string{"testSuffix"}, + }, }, }, }