2019-01-08 00:37:16 +00:00
|
|
|
package influxdb
|
2018-05-14 16:26:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-14 16:26:59 +00:00
|
|
|
"fmt"
|
2019-01-18 23:36:30 +00:00
|
|
|
"strings"
|
2018-05-14 16:26:38 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-09-25 16:16:24 +00:00
|
|
|
// BucketType defines known system-buckets.
|
2018-09-06 16:19:58 +00:00
|
|
|
type BucketType int
|
|
|
|
|
|
|
|
const (
|
2018-09-25 16:16:24 +00:00
|
|
|
// BucketTypeLogs defines the bucket ID of the system logs.
|
2018-09-14 16:26:59 +00:00
|
|
|
BucketTypeLogs = BucketType(iota + 10)
|
2018-09-06 16:19:58 +00:00
|
|
|
)
|
|
|
|
|
2018-10-18 19:09:25 +00:00
|
|
|
// InfiniteRetention is default infinite retention period.
|
|
|
|
const InfiniteRetention = 0
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// Bucket is a bucket. 🎉
|
|
|
|
type Bucket struct {
|
2018-08-02 20:55:51 +00:00
|
|
|
ID ID `json:"id,omitempty"`
|
2019-01-14 18:53:17 +00:00
|
|
|
OrganizationID ID `json:"orgID,omitempty"`
|
2018-08-02 20:55:51 +00:00
|
|
|
Organization string `json:"organization,omitempty"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
RetentionPolicyName string `json:"rp,omitempty"` // This to support v1 sources
|
|
|
|
RetentionPeriod time.Duration `json:"retentionPeriod"`
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 18:27:40 +00:00
|
|
|
// ops for buckets error and buckets op logs.
|
|
|
|
var (
|
|
|
|
OpFindBucketByID = "FindBucketByID"
|
|
|
|
OpFindBucket = "FindBucket"
|
|
|
|
OpFindBuckets = "FindBuckets"
|
|
|
|
OpCreateBucket = "CreateBucket"
|
|
|
|
OpUpdateBucket = "UpdateBucket"
|
|
|
|
OpDeleteBucket = "DeleteBucket"
|
|
|
|
)
|
|
|
|
|
2018-05-14 16:26:38 +00:00
|
|
|
// BucketService represents a service for managing bucket data.
|
|
|
|
type BucketService interface {
|
|
|
|
// FindBucketByID returns a single bucket by ID.
|
|
|
|
FindBucketByID(ctx context.Context, id ID) (*Bucket, error)
|
|
|
|
|
|
|
|
// FindBucket returns the first bucket that matches filter.
|
|
|
|
FindBucket(ctx context.Context, filter BucketFilter) (*Bucket, error)
|
|
|
|
|
|
|
|
// FindBuckets returns a list of buckets that match filter and the total count of matching buckets.
|
|
|
|
// Additional options provide pagination & sorting.
|
|
|
|
FindBuckets(ctx context.Context, filter BucketFilter, opt ...FindOptions) ([]*Bucket, int, error)
|
|
|
|
|
|
|
|
// CreateBucket creates a new bucket and sets b.ID with the new identifier.
|
|
|
|
CreateBucket(ctx context.Context, b *Bucket) error
|
|
|
|
|
|
|
|
// UpdateBucket updates a single bucket with changeset.
|
|
|
|
// Returns the new bucket state after update.
|
|
|
|
UpdateBucket(ctx context.Context, id ID, upd BucketUpdate) (*Bucket, error)
|
|
|
|
|
|
|
|
// DeleteBucket removes a bucket by ID.
|
|
|
|
DeleteBucket(ctx context.Context, id ID) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketUpdate represents updates to a bucket.
|
|
|
|
// Only fields which are set are updated.
|
|
|
|
type BucketUpdate struct {
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
RetentionPeriod *time.Duration `json:"retentionPeriod,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketFilter represents a set of filter that restrict the returned results.
|
|
|
|
type BucketFilter struct {
|
|
|
|
ID *ID
|
|
|
|
Name *string
|
2018-05-16 18:59:35 +00:00
|
|
|
OrganizationID *ID
|
|
|
|
Organization *string
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 15:25:35 +00:00
|
|
|
// QueryParams Converts BucketFilter fields to url query params.
|
|
|
|
func (f BucketFilter) QueryParams() map[string][]string {
|
|
|
|
qp := map[string][]string{}
|
|
|
|
if f.ID != nil {
|
|
|
|
qp["id"] = []string{f.ID.String()}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Name != nil {
|
|
|
|
qp["name"] = []string{*f.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.OrganizationID != nil {
|
|
|
|
qp["orgID"] = []string{f.OrganizationID.String()}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Organization != nil {
|
|
|
|
qp["org"] = []string{*f.Organization}
|
|
|
|
}
|
|
|
|
|
|
|
|
return qp
|
2018-05-14 16:26:38 +00:00
|
|
|
}
|
2018-09-14 16:26:59 +00:00
|
|
|
|
2019-01-18 23:36:30 +00:00
|
|
|
// String returns a human-readable string of the BucketFilter,
|
|
|
|
// particularly useful for error messages.
|
|
|
|
func (f BucketFilter) String() string {
|
|
|
|
// There should always be exactly 2 fields set, but if it's somehow more, that's fine.
|
|
|
|
parts := make([]string, 0, 2)
|
|
|
|
if f.ID != nil {
|
|
|
|
parts = append(parts, "Bucket ID: "+f.ID.String())
|
|
|
|
}
|
|
|
|
if f.Name != nil {
|
|
|
|
parts = append(parts, "Bucket Name: "+*f.Name)
|
|
|
|
}
|
|
|
|
if f.OrganizationID != nil {
|
|
|
|
parts = append(parts, "Org ID: "+f.OrganizationID.String())
|
|
|
|
}
|
|
|
|
if f.Organization != nil {
|
|
|
|
parts = append(parts, "Org Name: "+*f.Organization)
|
|
|
|
}
|
|
|
|
return "[" + strings.Join(parts, ", ") + "]"
|
|
|
|
}
|
|
|
|
|
2018-09-14 16:26:59 +00:00
|
|
|
// InternalBucketID returns the ID for an organization's specified internal bucket
|
|
|
|
func InternalBucketID(t BucketType) (*ID, error) {
|
|
|
|
return IDFromString(fmt.Sprintf("%d", t))
|
|
|
|
}
|