Skip to content

Commit

Permalink
Additional bucket response attributes (#1258)
Browse files Browse the repository at this point in the history
* Support bucket creation time in the FS backend

This was already supported in the Memory backend.

As bucket creation time we use the creation time attribute of the
filesystem directory node backing it.

* Include the `updated` attribute in Bucket object responses

Some client libraries, like [googleCloudStorageR][1], expect the
`updated` attribute of the [Bucket][2] object to be returned.

Its value isn't hugely important, so we just use the same value as for
`timeCreated`, also considering that update timestamps are not being
tracked yet.

[1]: https://cran.r-project.org/web/packages/googleCloudStorageR
[2]: https://cloud.google.com/storage/docs/json_api/v1/buckets

* Include the `storageClass` attribute in Bucket object responses

Some client libraries, like [googleCloudStorageR][1], expect the
`storageClass` attribute of the [Bucket][2] object to be returned.

Its value isn't hugely important, so we just use "STANDARD".

[1]: https://cran.r-project.org/web/packages/googleCloudStorageR
[2]: https://cloud.google.com/storage/docs/json_api/v1/buckets
  • Loading branch information
manuteleco authored Jul 30, 2023
1 parent 94427ae commit e1e8229
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions fakestorage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func TestServerClientBucketAttrs(t *testing.T) {
if attrs.Created.Before(startTime.Truncate(time.Second)) || time.Now().Before(attrs.Created) {
t.Errorf("expecting bucket creation date between test start time %v and now %v, got %v", startTime, time.Now(), attrs.Created)
}
// TODO: Test `attrs.Updated` as soon as it is available in the [`storage.BucketAttrs`][1] type
// (not available as of cloud.google.com/go/storage v1.31.0)
// [1]: https://pkg.go.dev/cloud.google.com/go/storage#BucketAttrs
if attrs.StorageClass != "STANDARD" {
t.Errorf("wrong bucket storage class returned\nwant %q\ngot %q", "STANDARD", attrs.StorageClass)
}
})
}

Expand Down
4 changes: 4 additions & 0 deletions fakestorage/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ type bucketResponse struct {
Name string `json:"name"`
Versioning *bucketVersioning `json:"versioning,omitempty"`
TimeCreated string `json:"timeCreated,omitempty"`
Updated string `json:"updated,omitempty"`
Location string `json:"location,omitempty"`
StorageClass string `json:"storageClass,omitempty"`
}

type bucketVersioning struct {
Expand All @@ -58,7 +60,9 @@ func newBucketResponse(bucket backend.Bucket, location string) bucketResponse {
DefaultEventBasedHold: bucket.DefaultEventBasedHold,
Versioning: &bucketVersioning{bucket.VersioningEnabled},
TimeCreated: formatTime(bucket.TimeCreated),
Updated: formatTime(bucket.TimeCreated), // not tracking update times yet, reporting `updated` = `timeCreated`
Location: location,
StorageClass: "STANDARD",
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func TestBucketAttrsStoreRetrieveUpdate(t *testing.T) {
}

func TestBucketCreateGetListDelete(t *testing.T) {
startTime := time.Now()
testForStorageBackends(t, func(t *testing.T, storage Storage) {
buckets, err := storage.ListBuckets()
if err != nil {
Expand Down Expand Up @@ -344,6 +345,9 @@ func TestBucketCreateGetListDelete(t *testing.T) {
if buckets[0].Name != bucket.Name {
t.Errorf("listed bucket has unexpected name. Expected %s, actual: %v", bucket.Name, buckets[0].Name)
}
if buckets[0].TimeCreated.Before(startTime.Truncate(time.Second)) || time.Now().Before(buckets[0].TimeCreated) {
t.Errorf("listed bucket has unexpected creation time. Expected between test start time %v and now %v, actual: %v", startTime, time.Now(), buckets[0].TimeCreated)
}
err = storage.DeleteBucket(bucket.Name)
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 5 additions & 1 deletion internal/backend/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ func (s *storageFS) ListBuckets() ([]Bucket, error) {
if err != nil {
return nil, fmt.Errorf("failed to unescape object name %s: %w", info.Name(), err)
}
buckets = append(buckets, Bucket{Name: unescaped})
fileInfo, err := info.Info()
if err != nil {
return nil, fmt.Errorf("failed to get file info for %s: %w", info.Name(), err)
}
buckets = append(buckets, Bucket{Name: unescaped, TimeCreated: timespecToTime(createTimeFromFileInfo(fileInfo))})
}
}
return buckets, nil
Expand Down

0 comments on commit e1e8229

Please sign in to comment.