-
Notifications
You must be signed in to change notification settings - Fork 24
/
create.go
70 lines (62 loc) · 1.84 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bucket
import (
"context"
"fmt"
"time"
"github.com/influxdata/influx-cli/v2/api"
"github.com/influxdata/influx-cli/v2/clients"
"github.com/influxdata/influx-cli/v2/pkg/duration"
)
type BucketsCreateParams struct {
clients.OrgParams
Name string
Description string
Retention string
ShardGroupDuration string
SchemaType api.SchemaType
}
func (c Client) Create(ctx context.Context, params *BucketsCreateParams) error {
orgId, err := params.GetOrgID(ctx, c.ActiveConfig, c.OrganizationsApi)
if err != nil {
return err
}
rp, err := duration.RawDurationToTimeDuration(params.Retention)
if err != nil {
return err
}
sgd, err := duration.RawDurationToTimeDuration(params.ShardGroupDuration)
if err != nil {
return err
}
var rr []api.RetentionRule
reqBody := api.PostBucketRequest{
OrgID: orgId,
Name: params.Name,
RetentionRules: &rr,
SchemaType: ¶ms.SchemaType,
}
if params.Description != "" {
reqBody.Description = ¶ms.Description
}
// Only append a retention rule if the user wants to explicitly set
// a parameter on the rule.
//
// 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))
}
if sgd > 0 {
rule.SetShardGroupDurationSeconds(int64(sgd.Round(time.Second) / time.Second))
}
*reqBody.RetentionRules = append(*reqBody.RetentionRules, *rule)
}
bucket, err := c.PostBuckets(ctx).PostBucketRequest(reqBody).Execute()
if err != nil {
return fmt.Errorf("failed to create bucket: %w", err)
}
return c.printBuckets(bucketPrintOptions{bucket: &bucket})
}