From 02c04a7c6c5e96026ce0710748a54580065a8102 Mon Sep 17 00:00:00 2001 From: Noah Dietz Date: Tue, 3 Jan 2023 16:22:11 -0800 Subject: [PATCH] chore(storage): refactor retention_period to retention_duration (#7203) --- storage/bucket.go | 9 +++++---- storage/bucket_test.go | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/storage/bucket.go b/storage/bucket.go index 28a73b8d9953..19f266ef1e35 100644 --- a/storage/bucket.go +++ b/storage/bucket.go @@ -35,6 +35,7 @@ import ( raw "google.golang.org/api/storage/v1" dpb "google.golang.org/genproto/googleapis/type/date" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/durationpb" ) // BucketHandle provides operations on a Google Cloud Storage bucket. @@ -1389,12 +1390,12 @@ func (rp *RetentionPolicy) toProtoRetentionPolicy() *storagepb.Bucket_RetentionP } // RetentionPeriod must be greater than 0, so if it is 0, the user left it // unset, and so we should not send it in the request i.e. nil is sent. - var period *int64 + var dur *durationpb.Duration if rp.RetentionPeriod != 0 { - period = proto.Int64(int64(rp.RetentionPeriod / time.Second)) + dur = durationpb.New(rp.RetentionPeriod) } return &storagepb.Bucket_RetentionPolicy{ - RetentionPeriod: period, + RetentionDuration: dur, } } @@ -1418,7 +1419,7 @@ func toRetentionPolicyFromProto(rp *storagepb.Bucket_RetentionPolicy) *Retention return nil } return &RetentionPolicy{ - RetentionPeriod: time.Duration(rp.GetRetentionPeriod()) * time.Second, + RetentionPeriod: rp.GetRetentionDuration().AsDuration(), EffectiveTime: rp.GetEffectiveTime().AsTime(), IsLocked: rp.GetIsLocked(), } diff --git a/storage/bucket_test.go b/storage/bucket_test.go index a1d5ad78d103..bdf3b19d454b 100644 --- a/storage/bucket_test.go +++ b/storage/bucket_test.go @@ -27,6 +27,7 @@ import ( "google.golang.org/api/googleapi" raw "google.golang.org/api/storage/v1" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/durationpb" ) func TestBucketAttrsToRawBucket(t *testing.T) { @@ -725,8 +726,8 @@ func TestNewBucketFromProto(t *testing.T) { LocationType: "region", StorageClass: "class", RetentionPolicy: &storagepb.Bucket_RetentionPolicy{ - RetentionPeriod: proto.Int64(int64(3)), - EffectiveTime: toProtoTimestamp(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), + RetentionDuration: durationpb.New(3 * time.Second), + EffectiveTime: toProtoTimestamp(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), }, IamConfig: &storagepb.Bucket_IamConfig{ UniformBucketLevelAccess: &storagepb.Bucket_IamConfig_UniformBucketLevelAccess{ @@ -866,7 +867,7 @@ func TestBucketAttrsToProtoBucket(t *testing.T) { Location: "loc", StorageClass: "class", RetentionPolicy: &storagepb.Bucket_RetentionPolicy{ - RetentionPeriod: proto.Int64(int64(3)), + RetentionDuration: durationpb.New(3 * time.Second), }, IamConfig: &storagepb.Bucket_IamConfig{ UniformBucketLevelAccess: &storagepb.Bucket_IamConfig_UniformBucketLevelAccess{