Skip to content

Commit

Permalink
chore(storage): refactor retention_period to retention_duration (#7203)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz authored Jan 4, 2023
1 parent 92d9d4c commit 02c04a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
9 changes: 5 additions & 4 deletions storage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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(),
}
Expand Down
7 changes: 4 additions & 3 deletions storage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 02c04a7

Please sign in to comment.