Skip to content

Commit

Permalink
storage: add noncurrent time OLM conditions
Browse files Browse the repository at this point in the history
Support setting DaysSinceNoncurrentTime and NoncurrentTimeBefore
conditions in lifecycle rules.

Fixes googleapis#2208
  • Loading branch information
tritone committed Jun 24, 2020
1 parent f1a22b3 commit bc784de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
28 changes: 22 additions & 6 deletions storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ type LifecycleCondition struct {
// the specified date in UTC.
CreatedBefore time.Time

// DaysSinceNoncurrentTime is the days elapsed since the noncurrent timestamp
// of the object. This condition is relevant only for versioned objects.
DaysSinceNoncurrentTime int64

// Liveness specifies the object's liveness. Relevant only for versioned objects
Liveness Liveness

Expand All @@ -464,6 +468,10 @@ type LifecycleCondition struct {
// Values include "STANDARD", "NEARLINE", "COLDLINE" and "ARCHIVE".
MatchesStorageClasses []string

// NoncurrentTimeBefore is the noncurrent timestamp of the object. This
// condition is relevant only for versioned objects.
NoncurrentTimeBefore time.Time

// NumNewerVersions is the condition matching objects with a number of newer versions.
//
// If the value is N, this condition is satisfied when there are at least N
Expand Down Expand Up @@ -946,9 +954,10 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
StorageClass: r.Action.StorageClass,
},
Condition: &raw.BucketLifecycleRuleCondition{
Age: r.Condition.AgeInDays,
MatchesStorageClass: r.Condition.MatchesStorageClasses,
NumNewerVersions: r.Condition.NumNewerVersions,
Age: r.Condition.AgeInDays,
DaysSinceNoncurrentTime: r.Condition.DaysSinceNoncurrentTime,
MatchesStorageClass: r.Condition.MatchesStorageClasses,
NumNewerVersions: r.Condition.NumNewerVersions,
},
}

Expand All @@ -964,6 +973,9 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle {
if !r.Condition.CreatedBefore.IsZero() {
rr.Condition.CreatedBefore = r.Condition.CreatedBefore.Format(rfc3339Date)
}
if !r.Condition.NoncurrentTimeBefore.IsZero() {
rr.Condition.NoncurrentTimeBefore = r.Condition.NoncurrentTimeBefore.Format(rfc3339Date)
}
rl.Rule = append(rl.Rule, rr)
}
return &rl
Expand All @@ -981,9 +993,10 @@ func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
StorageClass: rr.Action.StorageClass,
},
Condition: LifecycleCondition{
AgeInDays: rr.Condition.Age,
MatchesStorageClasses: rr.Condition.MatchesStorageClass,
NumNewerVersions: rr.Condition.NumNewerVersions,
AgeInDays: rr.Condition.Age,
DaysSinceNoncurrentTime: rr.Condition.DaysSinceNoncurrentTime,
MatchesStorageClasses: rr.Condition.MatchesStorageClass,
NumNewerVersions: rr.Condition.NumNewerVersions,
},
}

Expand All @@ -998,6 +1011,9 @@ func toLifecycle(rl *raw.BucketLifecycle) Lifecycle {
if rr.Condition.CreatedBefore != "" {
r.Condition.CreatedBefore, _ = time.Parse(rfc3339Date, rr.Condition.CreatedBefore)
}
if rr.Condition.NoncurrentTimeBefore != "" {
r.Condition.NoncurrentTimeBefore, _ = time.Parse(rfc3339Date, rr.Condition.NoncurrentTimeBefore)
}
l.Rules = append(l.Rules, r)
}
return l
Expand Down
20 changes: 10 additions & 10 deletions storage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
Type: DeleteAction,
},
Condition: LifecycleCondition{
AgeInDays: 30,
Liveness: Live,
CreatedBefore: time.Date(2017, 1, 2, 3, 4, 5, 6, time.UTC),
MatchesStorageClasses: []string{"NEARLINE"},
NumNewerVersions: 10,
DaysSinceNoncurrentTime: 30,
Liveness: Live,
NoncurrentTimeBefore: time.Date(2017, 1, 2, 3, 4, 5, 6, time.UTC),
MatchesStorageClasses: []string{"NEARLINE"},
NumNewerVersions: 10,
},
}, {
Action: LifecycleAction{
Expand Down Expand Up @@ -142,11 +142,11 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
Type: DeleteAction,
},
Condition: &raw.BucketLifecycleRuleCondition{
Age: 30,
IsLive: googleapi.Bool(true),
CreatedBefore: "2017-01-02",
MatchesStorageClass: []string{"NEARLINE"},
NumNewerVersions: 10,
DaysSinceNoncurrentTime: 30,
IsLive: googleapi.Bool(true),
NoncurrentTimeBefore: "2017-01-02",
MatchesStorageClass: []string{"NEARLINE"},
NumNewerVersions: 10,
},
}, {
Action: &raw.BucketLifecycleRuleAction{
Expand Down

0 comments on commit bc784de

Please sign in to comment.