Skip to content

Commit d8bd2c1

Browse files
authored
fix(storage): allow empty soft delete on Create (#10394)
Currently setting an empty soft delete policy to disable the feature does not work on bucket creation (only update). This fixes the issue by forcing the library to send a zero value in this case (sending a null does not seem to work). Also adds this case to the relevant integration test. Fixes #10380
1 parent 36b70b7 commit d8bd2c1

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

storage/bucket.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,8 +2123,11 @@ func (p *SoftDeletePolicy) toRawSoftDeletePolicy() *raw.BucketSoftDeletePolicy {
21232123
return nil
21242124
}
21252125
// Excluding read only field EffectiveTime.
2126+
// ForceSendFields must be set to send a zero value for RetentionDuration and disable
2127+
// soft delete.
21262128
return &raw.BucketSoftDeletePolicy{
21272129
RetentionDurationSeconds: int64(p.RetentionDuration.Seconds()),
2130+
ForceSendFields: []string{"RetentionDurationSeconds"},
21282131
}
21292132
}
21302133

storage/bucket_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) {
172172
Logging: &raw.BucketLogging{LogBucket: "lb", LogObjectPrefix: "p"},
173173
Website: &raw.BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"},
174174
Autoclass: &raw.BucketAutoclass{Enabled: true, TerminalStorageClass: "NEARLINE"},
175-
SoftDeletePolicy: &raw.BucketSoftDeletePolicy{RetentionDurationSeconds: 60 * 60},
175+
SoftDeletePolicy: &raw.BucketSoftDeletePolicy{RetentionDurationSeconds: 60 * 60, ForceSendFields: []string{"RetentionDurationSeconds"}},
176176
HierarchicalNamespace: &raw.BucketHierarchicalNamespace{Enabled: true},
177177
Lifecycle: &raw.BucketLifecycle{
178178
Rule: []*raw.BucketLifecycleRule{{
@@ -448,7 +448,7 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) {
448448
Website: &raw.BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"},
449449
StorageClass: "NEARLINE",
450450
Autoclass: &raw.BucketAutoclass{Enabled: true, TerminalStorageClass: "ARCHIVE", ForceSendFields: []string{"Enabled"}},
451-
SoftDeletePolicy: &raw.BucketSoftDeletePolicy{RetentionDurationSeconds: 3600},
451+
SoftDeletePolicy: &raw.BucketSoftDeletePolicy{RetentionDurationSeconds: 3600, ForceSendFields: []string{"RetentionDurationSeconds"}},
452452
ForceSendFields: []string{"DefaultEventBasedHold", "Lifecycle", "Autoclass"},
453453
}
454454
if msg := testutil.Diff(got, want); msg != "" {

storage/integration_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4377,7 +4377,23 @@ func TestIntegration_SoftDelete(t *testing.T) {
43774377
t.Fatalf("effective time of soft delete policy should not be in the past, got: %v, test start: %v", got.EffectiveTime, testStart.UTC())
43784378
}
43794379

4380-
// Update the soft delete policy.
4380+
// Create a second bucket with an empty soft delete policy to verify
4381+
// that this leads to no retention.
4382+
b2 := client.Bucket(prefix + uidSpace.New())
4383+
if err := b2.Create(ctx, testutil.ProjID(), &BucketAttrs{SoftDeletePolicy: &SoftDeletePolicy{}}); err != nil {
4384+
t.Fatalf("error creating bucket with soft delete disabled: %v", err)
4385+
}
4386+
t.Cleanup(func() { h.mustDeleteBucket(b2) })
4387+
4388+
attrs2, err := b2.Attrs(ctx)
4389+
if err != nil {
4390+
t.Fatalf("Attrs(%q): %v", b2.name, err)
4391+
}
4392+
if got, expect := attrs2.SoftDeletePolicy.RetentionDuration, time.Duration(0); got != expect {
4393+
t.Fatalf("mismatching retention duration; got: %+v, expected: %+v", got, expect)
4394+
}
4395+
4396+
// Update the soft delete policy of the original bucket.
43814397
policy.RetentionDuration = time.Hour * 24 * 9
43824398

43834399
attrs, err = b.Update(ctx, BucketAttrsToUpdate{SoftDeletePolicy: policy})

0 commit comments

Comments
 (0)