Skip to content

Commit

Permalink
feat(storage): add update time in bucketAttrs (#10710)
Browse files Browse the repository at this point in the history
Add update time field in bucketAttrs struct.

Fixes #9361
  • Loading branch information
shubham-diwakar authored Aug 22, 2024
1 parent 74cf45e commit 5f06ae1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ type BucketAttrs struct {
// This field is read-only.
Created time.Time

// Updated is the time at which the bucket was last modified.
// This field is read-only.
Updated time.Time

// VersioningEnabled reports whether this bucket has versioning enabled.
VersioningEnabled bool

Expand Down Expand Up @@ -824,6 +828,7 @@ func newBucket(b *raw.Bucket) (*BucketAttrs, error) {
DefaultEventBasedHold: b.DefaultEventBasedHold,
StorageClass: b.StorageClass,
Created: convertTime(b.TimeCreated),
Updated: convertTime(b.Updated),
VersioningEnabled: b.Versioning != nil && b.Versioning.Enabled,
ACL: toBucketACLRules(b.Acl),
DefaultObjectACL: toObjectACLRules(b.DefaultObjectAcl),
Expand Down Expand Up @@ -861,6 +866,7 @@ func newBucketFromProto(b *storagepb.Bucket) *BucketAttrs {
DefaultEventBasedHold: b.GetDefaultEventBasedHold(),
StorageClass: b.GetStorageClass(),
Created: b.GetCreateTime().AsTime(),
Updated: b.GetUpdateTime().AsTime(),
VersioningEnabled: b.GetVersioning().GetEnabled(),
ACL: toBucketACLRulesFromProto(b.GetAcl()),
DefaultObjectACL: toObjectACLRulesFromProto(b.GetDefaultObjectAcl()),
Expand Down
4 changes: 4 additions & 0 deletions storage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ func TestNewBucket(t *testing.T) {
Metageneration: 3,
StorageClass: "sc",
TimeCreated: "2017-10-23T04:05:06Z",
Updated: "2024-08-21T17:24:53Z",
Versioning: &raw.BucketVersioning{Enabled: true},
Labels: labels,
Billing: &raw.BucketBilling{RequesterPays: true},
Expand Down Expand Up @@ -676,6 +677,7 @@ func TestNewBucket(t *testing.T) {
MetaGeneration: 3,
StorageClass: "sc",
Created: time.Date(2017, 10, 23, 4, 5, 6, 0, time.UTC),
Updated: time.Date(2024, 8, 21, 17, 24, 53, 0, time.UTC),
VersioningEnabled: true,
Labels: labels,
Etag: "Zkyw9ACJZUvcYmlFaKGChzhmtnE/dt1zHSfweiWpwzdGsqXwuJZqiD0",
Expand Down Expand Up @@ -767,6 +769,7 @@ func TestNewBucketFromProto(t *testing.T) {
Rpo: rpoAsyncTurbo,
Metageneration: int64(39),
CreateTime: toProtoTimestamp(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)),
UpdateTime: toProtoTimestamp(time.Date(2024, 1, 2, 3, 4, 5, 6, time.UTC)),
Labels: map[string]string{"label": "value"},
Cors: []*storagepb.Bucket_Cors{
{
Expand Down Expand Up @@ -820,6 +823,7 @@ func TestNewBucketFromProto(t *testing.T) {
RPO: RPOAsyncTurbo,
MetaGeneration: 39,
Created: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
Updated: time.Date(2024, 1, 2, 3, 4, 5, 6, time.UTC),
Labels: map[string]string{"label": "value"},
CORS: []CORS{
{
Expand Down
15 changes: 15 additions & 0 deletions storage/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ func TestIntegration_BucketUpdate(t *testing.T) {
if !testutil.Equal(attrs.Labels, wantLabels) {
t.Fatalf("add labels: got %v, want %v", attrs.Labels, wantLabels)
}
if !attrs.Created.Before(attrs.Updated) {
t.Errorf("got attrs.Updated %v before attrs.Created %v, want Attrs.Updated to be after", attrs.Updated, attrs.Created)
}

// Turn off versioning again; add and remove some more labels.
ua = BucketAttrsToUpdate{VersioningEnabled: false}
Expand All @@ -607,6 +610,9 @@ func TestIntegration_BucketUpdate(t *testing.T) {
if !testutil.Equal(attrs.Labels, wantLabels) {
t.Fatalf("got %v, want %v", attrs.Labels, wantLabels)
}
if !attrs.Created.Before(attrs.Updated) {
t.Errorf("got attrs.Updated %v before attrs.Created %v, want Attrs.Updated to be after", attrs.Updated, attrs.Created)
}

// Configure a lifecycle
wantLifecycle := Lifecycle{
Expand All @@ -626,6 +632,9 @@ func TestIntegration_BucketUpdate(t *testing.T) {
if !testutil.Equal(attrs.Lifecycle, wantLifecycle) {
t.Fatalf("got %v, want %v", attrs.Lifecycle, wantLifecycle)
}
if !attrs.Created.Before(attrs.Updated) {
t.Errorf("got attrs.Updated %v before attrs.Created %v, want Attrs.Updated to be after", attrs.Updated, attrs.Created)
}
// Check that StorageClass has "STANDARD" value for unset field by default
// before passing new value.
wantStorageClass := "STANDARD"
Expand All @@ -638,6 +647,9 @@ func TestIntegration_BucketUpdate(t *testing.T) {
if !testutil.Equal(attrs.StorageClass, wantStorageClass) {
t.Fatalf("got %v, want %v", attrs.StorageClass, wantStorageClass)
}
if !attrs.Created.Before(attrs.Updated) {
t.Errorf("got attrs.Updated %v before attrs.Created %v, want Attrs.Updated to be after", attrs.Updated, attrs.Created)
}

// Empty update should succeed without changing the bucket.
gotAttrs, err := b.Update(ctx, BucketAttrsToUpdate{})
Expand All @@ -647,6 +659,9 @@ func TestIntegration_BucketUpdate(t *testing.T) {
if !testutil.Equal(attrs, gotAttrs) {
t.Fatalf("empty update: got %v, want %v", gotAttrs, attrs)
}
if !attrs.Created.Before(attrs.Updated) {
t.Errorf("got attrs.Updated %v before attrs.Created %v, want Attrs.Updated to be after", attrs.Updated, attrs.Created)
}
})
}

Expand Down

0 comments on commit 5f06ae1

Please sign in to comment.