Skip to content

Commit

Permalink
feat(storage): add missing StorageClass in BucketAttrsToUpdate (#3038)
Browse files Browse the repository at this point in the history
Allows StorageClass to be set on a bucket via BucketAttrsToUpdate
  • Loading branch information
AlisskaPie committed Dec 3, 2020
1 parent 722f04d commit 2fa1b72
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions storage/bucket.go
Expand Up @@ -661,6 +661,14 @@ type BucketAttrsToUpdate struct {
// for more information.
UniformBucketLevelAccess *UniformBucketLevelAccess

// StorageClass is the default storage class of the bucket. This defines
// how objects in the bucket are stored and determines the SLA
// and the cost of storage. Typical values are "STANDARD", "NEARLINE",
// "COLDLINE" and "ARCHIVE". Defaults to "STANDARD".
// See https://cloud.google.com/storage/docs/storage-classes for all
// valid values.
StorageClass string

// If set, updates the retention policy of the bucket. Using
// RetentionPolicy.RetentionPeriod = 0 will delete the existing policy.
//
Expand Down Expand Up @@ -801,6 +809,7 @@ func (ua *BucketAttrsToUpdate) toRawBucket() *raw.Bucket {
rb.DefaultObjectAcl = nil
rb.ForceSendFields = append(rb.ForceSendFields, "DefaultObjectAcl")
}
rb.StorageClass = ua.StorageClass
if ua.setLabels != nil || ua.deleteLabels != nil {
rb.Labels = map[string]string{}
for k, v := range ua.setLabels {
Expand Down
6 changes: 4 additions & 2 deletions storage/bucket_test.go
Expand Up @@ -268,8 +268,9 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) {
},
},
},
Logging: &BucketLogging{LogBucket: "lb", LogObjectPrefix: "p"},
Website: &BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"},
Logging: &BucketLogging{LogBucket: "lb", LogObjectPrefix: "p"},
Website: &BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"},
StorageClass: "NEARLINE",
}
au.SetLabel("a", "foo")
au.DeleteLabel("b")
Expand Down Expand Up @@ -308,6 +309,7 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) {
},
Logging: &raw.BucketLogging{LogBucket: "lb", LogObjectPrefix: "p"},
Website: &raw.BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"},
StorageClass: "NEARLINE",
ForceSendFields: []string{"DefaultEventBasedHold", "Lifecycle"},
}
if msg := testutil.Diff(got, want); msg != "" {
Expand Down
14 changes: 13 additions & 1 deletion storage/integration_test.go
Expand Up @@ -351,7 +351,7 @@ func TestIntegration_BucketUpdate(t *testing.T) {
t.Fatalf("got %v, want %v", attrs.Labels, wantLabels)
}

// Turn off versioning again; add and remove some more labels.
// Turn off versioning again; add and remove some more labels.
ua = BucketAttrsToUpdate{VersioningEnabled: false}
ua.SetLabel("l1", "v2") // update
ua.SetLabel("new", "new") // create
Expand Down Expand Up @@ -383,6 +383,18 @@ func TestIntegration_BucketUpdate(t *testing.T) {
if !testutil.Equal(attrs.Lifecycle, wantLifecycle) {
t.Fatalf("got %v, want %v", attrs.Lifecycle, wantLifecycle)
}
// Check that StorageClass has "STANDARD" value for unset field by default
// before passing new value.
wantStorageClass := "STANDARD"
if !testutil.Equal(attrs.StorageClass, wantStorageClass) {
t.Fatalf("got %v, want %v", attrs.StorageClass, wantStorageClass)
}
wantStorageClass = "NEARLINE"
ua = BucketAttrsToUpdate{StorageClass: wantStorageClass}
attrs = h.mustUpdateBucket(b, ua)
if !testutil.Equal(attrs.StorageClass, wantStorageClass) {
t.Fatalf("got %v, want %v", attrs.StorageClass, wantStorageClass)
}
}

func TestIntegration_BucketPolicyOnly(t *testing.T) {
Expand Down

0 comments on commit 2fa1b72

Please sign in to comment.