diff --git a/clients/bucket/create.go b/clients/bucket/create.go index c09f8f66..b55b1af5 100644 --- a/clients/bucket/create.go +++ b/clients/bucket/create.go @@ -45,16 +45,17 @@ func (c Client) Create(ctx context.Context, params *BucketsCreateParams) error { reqBody.Description = ¶ms.Description } // Only append a retention rule if the user wants to explicitly set - // a parameter on the rule. + // a parameter on the rule, + // or the shard group duration is set, to be able to set a retention of infinity // // This is for backwards-compatibility with older versions of the API, // which didn't support setting shard-group durations and used an empty // array of rules to represent infinite retention. if rp > 0 || sgd > 0 { rule := api.NewRetentionRuleWithDefaults() - if rp > 0 { - rule.SetEverySeconds(int64(rp.Round(time.Second) / time.Second)) - } + + rule.SetEverySeconds(int64(rp.Round(time.Second) / time.Second)) + if sgd > 0 { rule.SetShardGroupDurationSeconds(int64(sgd.Round(time.Second) / time.Second)) } diff --git a/clients/bucket/create_test.go b/clients/bucket/create_test.go index 6c51d49a..9dc8426d 100644 --- a/clients/bucket/create_test.go +++ b/clients/bucket/create_test.go @@ -119,6 +119,36 @@ func TestBucketsCreate(t *testing.T) { }, nil) }, }, + { + name: "shard-group duration and infinite retention", + params: bucket.BucketsCreateParams{ + OrgParams: clients.OrgParams{OrgID: "123"}, + Name: "my-bucket", + Retention: "0", + ShardGroupDuration: "10s", + }, + registerBucketExpectations: func(t *testing.T, bucketsApi *mock.MockBucketsApi) { + bucketsApi.EXPECT().PostBuckets(gomock.Any()).Return(api.ApiPostBucketsRequest{ApiService: bucketsApi}) + bucketsApi.EXPECT(). + PostBucketsExecute(tmock.MatchedBy(func(in api.ApiPostBucketsRequest) bool { + body := in.GetPostBucketRequest() + retentionRules := *body.RetentionRules + return assert.NotNil(t, body) && + assert.Equal(t, "123", body.OrgID) && + assert.Equal(t, "my-bucket", body.Name) && + assert.Nil(t, body.Description) && + assert.Len(t, *body.RetentionRules, 1) && + assert.Equal(t, int64(0), retentionRules[0].EverySeconds) && + assert.Equal(t, int64(10), *(retentionRules[0].ShardGroupDurationSeconds)) + })). + Return(api.Bucket{ + Id: api.PtrString("456"), + OrgID: api.PtrString("123"), + Name: "my-bucket", + RetentionRules: []api.RetentionRule{{EverySeconds: 0, ShardGroupDurationSeconds: api.PtrInt64(10)}}, + }, nil) + }, + }, { name: "create bucket with explicit schema", params: bucket.BucketsCreateParams{