From e915f3058b41a24d748f755deac80401807c3ab3 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 24 Aug 2022 15:47:40 -0700 Subject: [PATCH] fix: remove workaround and update tests --- storage/bucket.go | 46 ++++++++++-------------------------------- storage/bucket_test.go | 41 +++++++++---------------------------- 2 files changed, 21 insertions(+), 66 deletions(-) diff --git a/storage/bucket.go b/storage/bucket.go index abc0d882eeef..5f08f982dc85 100644 --- a/storage/bucket.go +++ b/storage/bucket.go @@ -1429,19 +1429,6 @@ func toCORSFromProto(rc []*storagepb.Bucket_Cors) []CORS { return out } -// Used to handle breaking change in Autogen Storage client OLM Age field -// from int64 to *int64 gracefully in the manual client -// TODO(#6240): Method should be removed once breaking change is made and introduced to this client -func setAgeCondition(age int64, ageField interface{}) { - c := reflect.ValueOf(ageField).Elem() - switch c.Kind() { - case reflect.Int64: - c.SetInt(age) - case reflect.Ptr: - c.Set(reflect.ValueOf(&age)) - } -} - func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle { var rl raw.BucketLifecycle if len(l.Rules) == 0 { @@ -1462,13 +1449,16 @@ func toRawLifecycle(l Lifecycle) *raw.BucketLifecycle { NumNewerVersions: r.Condition.NumNewerVersions, }, } + + if r.Condition.AgeInDays > 0 { + rr.Condition.Age = googleapi.Int64(r.Condition.AgeInDays) + } + if r.Condition.AllObjects { - rr.Condition.Age = 0 + rr.Condition.Age = googleapi.Int64(0) rr.Condition.ForceSendFields = []string{"Age"} } - setAgeCondition(r.Condition.AgeInDays, &rr.Condition.Age) - switch r.Condition.Liveness { case LiveAndArchived: rr.Condition.IsLive = nil @@ -1544,21 +1534,6 @@ func toProtoLifecycle(l Lifecycle) *storagepb.Bucket_Lifecycle { return &rl } -// Used to handle breaking change in Autogen Storage client OLM Age field -// from int64 to *int64 gracefully in the manual client -// TODO(#6240): Method should be removed once breaking change is made and introduced to this client -func getAgeCondition(ageField interface{}) int64 { - v := reflect.ValueOf(ageField) - if v.Kind() == reflect.Int64 { - return v.Interface().(int64) - } else if v.Kind() == reflect.Ptr { - if val, ok := v.Interface().(*int64); ok { - return *val - } - } - return 0 -} - func toLifecycle(rl *raw.BucketLifecycle) Lifecycle { var l Lifecycle if rl == nil { @@ -1579,10 +1554,11 @@ func toLifecycle(rl *raw.BucketLifecycle) Lifecycle { NumNewerVersions: rr.Condition.NumNewerVersions, }, } - r.Condition.AgeInDays = getAgeCondition(rr.Condition.Age) - - if rr.Condition.Age == 0 { - r.Condition.AllObjects = true + if rr.Condition.Age != nil { + r.Condition.AgeInDays = *rr.Condition.Age + if rr.Condition.Age == googleapi.Int64(0) { + r.Condition.AllObjects = true + } } if rr.Condition.IsLive == nil { diff --git a/storage/bucket_test.go b/storage/bucket_test.go index 1b31284b651f..22d20455c5e2 100644 --- a/storage/bucket_test.go +++ b/storage/bucket_test.go @@ -25,7 +25,6 @@ import ( raw "google.golang.org/api/storage/v1" ) -// TODO(#6539): re-enable tests after breaking change is released and AgeInDays is a int64* func TestBucketAttrsToRawBucket(t *testing.T) { t.Skip("TestBucketAttrsToRawBucket skipped: https://github.com/googleapis/google-cloud-go/issues/6539") t.Parallel() @@ -170,7 +169,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) { StorageClass: "NEARLINE", }, Condition: &raw.BucketLifecycleRuleCondition{ - //Age: 10, + Age: googleapi.Int64(10), IsLive: googleapi.Bool(true), CreatedBefore: "2017-01-02", MatchesStorageClass: []string{"STANDARD"}, @@ -206,7 +205,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) { Type: DeleteAction, }, Condition: &raw.BucketLifecycleRuleCondition{ - //Age: 10, + Age: googleapi.Int64(10), MatchesPrefix: []string{"testPrefix"}, MatchesSuffix: []string{"testSuffix"}, NumNewerVersions: 3, @@ -217,6 +216,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) { Type: DeleteAction, }, Condition: &raw.BucketLifecycleRuleCondition{ + Age: googleapi.Int64(0), IsLive: googleapi.Bool(false), }, }, @@ -225,7 +225,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) { Type: AbortIncompleteMPUAction, }, Condition: &raw.BucketLifecycleRuleCondition{ - //Age: 20, + Age: googleapi.Int64(20), }, }, { @@ -233,7 +233,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) { Type: DeleteAction, }, Condition: &raw.BucketLifecycleRuleCondition{ - Age: 0, + Age: googleapi.Int64(0), ForceSendFields: []string{"Age"}, }, }, @@ -366,9 +366,7 @@ func TestBucketAttrsToRawBucket(t *testing.T) { } } -// TODO(#6539): re-enable tests after breaking change is released and AgeInDays is a int64* func TestBucketAttrsToUpdateToRawBucket(t *testing.T) { - t.Skip("TestBucketAttrsToUpdateToRawBucket skipped: https://github.com/googleapis/google-cloud-go/issues/6539") t.Parallel() au := &BucketAttrsToUpdate{ VersioningEnabled: false, @@ -424,12 +422,12 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) { Lifecycle: &raw.BucketLifecycle{ Rule: []*raw.BucketLifecycleRule{ { - Action: &raw.BucketLifecycleRuleAction{Type: "Delete"}, - //Condition: &raw.BucketLifecycleRuleCondition{Age: 30}, + Action: &raw.BucketLifecycleRuleAction{Type: "Delete"}, + Condition: &raw.BucketLifecycleRuleCondition{Age: googleapi.Int64(30)}, }, { - Action: &raw.BucketLifecycleRuleAction{Type: AbortIncompleteMPUAction}, - //Condition: &raw.BucketLifecycleRuleCondition{Age: 13}, + Action: &raw.BucketLifecycleRuleAction{Type: AbortIncompleteMPUAction}, + Condition: &raw.BucketLifecycleRuleCondition{Age: googleapi.Int64(13)}, }, }, }, @@ -580,26 +578,7 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) { } } -func TestAgeConditionBackwardCompat(t *testing.T) { - var ti int64 - var want int64 = 100 - setAgeCondition(want, &ti) - if getAgeCondition(ti) != want { - t.Fatalf("got %v, want %v", getAgeCondition(ti), want) - } - - var tp *int64 - want = 10 - setAgeCondition(want, &tp) - if getAgeCondition(tp) != want { - t.Fatalf("got %v, want %v", getAgeCondition(tp), want) - } - -} - -// TODO(#6539): re-enable tests after breaking change is released and AgeInDays is a int64* func TestNewBucket(t *testing.T) { - t.Skip("TestNewBucket skipped: https://github.com/googleapis/google-cloud-go/issues/6539") labels := map[string]string{"a": "b"} matchClasses := []string{"STANDARD"} aTime := time.Date(2017, 1, 2, 0, 0, 0, 0, time.UTC) @@ -621,7 +600,7 @@ func TestNewBucket(t *testing.T) { StorageClass: "NEARLINE", }, Condition: &raw.BucketLifecycleRuleCondition{ - // Age: 10, + Age: googleapi.Int64(10), IsLive: googleapi.Bool(true), CreatedBefore: "2017-01-02", MatchesStorageClass: matchClasses,