github.com/influxdata/influxdb/v2@v2.7.6/bucket.go (about)

     1  package influxdb
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/influxdata/influxdb/v2/kit/platform"
     9  )
    10  
    11  const (
    12  	// BucketTypeUser is a user created bucket
    13  	BucketTypeUser = BucketType(0)
    14  	// BucketTypeSystem is an internally created bucket that cannot be deleted/renamed.
    15  	BucketTypeSystem = BucketType(1)
    16  	// MonitoringSystemBucketRetention is the time we should retain monitoring system bucket information
    17  	MonitoringSystemBucketRetention = time.Hour * 24 * 7
    18  	// TasksSystemBucketRetention is the time we should retain task system bucket information
    19  	TasksSystemBucketRetention = time.Hour * 24 * 3
    20  )
    21  
    22  // Bucket names constants
    23  const (
    24  	TasksSystemBucketName      = "_tasks"
    25  	MonitoringSystemBucketName = "_monitoring"
    26  )
    27  
    28  // InfiniteRetention is default infinite retention period.
    29  const InfiniteRetention = 0
    30  
    31  // Bucket is a bucket. 🎉
    32  type Bucket struct {
    33  	ID                  platform.ID   `json:"id,omitempty"`
    34  	OrgID               platform.ID   `json:"orgID,omitempty"`
    35  	Type                BucketType    `json:"type"`
    36  	Name                string        `json:"name"`
    37  	Description         string        `json:"description"`
    38  	RetentionPolicyName string        `json:"rp,omitempty"` // This to support v1 sources
    39  	RetentionPeriod     time.Duration `json:"retentionPeriod"`
    40  	ShardGroupDuration  time.Duration `json:"shardGroupDuration"`
    41  	CRUDLog
    42  }
    43  
    44  // Clone returns a shallow copy of b.
    45  func (b *Bucket) Clone() *Bucket {
    46  	other := *b
    47  	return &other
    48  }
    49  
    50  // BucketType differentiates system buckets from user buckets.
    51  type BucketType int
    52  
    53  // String converts a BucketType into a human-readable string.
    54  func (bt BucketType) String() string {
    55  	if bt == BucketTypeSystem {
    56  		return "system"
    57  	}
    58  	return "user"
    59  }
    60  
    61  // ParseBucketType parses a bucket type from a string
    62  func ParseBucketType(s string) BucketType {
    63  	if s == "system" {
    64  		return BucketTypeSystem
    65  	}
    66  	return BucketTypeUser
    67  }
    68  
    69  // ops for buckets error and buckets op logs.
    70  var (
    71  	OpFindBucketByID = "FindBucketByID"
    72  	OpFindBucket     = "FindBucket"
    73  	OpFindBuckets    = "FindBuckets"
    74  	OpCreateBucket   = "CreateBucket"
    75  	OpPutBucket      = "PutBucket"
    76  	OpUpdateBucket   = "UpdateBucket"
    77  	OpDeleteBucket   = "DeleteBucket"
    78  )
    79  
    80  // BucketService represents a service for managing bucket data.
    81  type BucketService interface {
    82  	// FindBucketByID returns a single bucket by ID.
    83  	FindBucketByID(ctx context.Context, id platform.ID) (*Bucket, error)
    84  
    85  	// FindBucket returns the first bucket that matches filter.
    86  	FindBucket(ctx context.Context, filter BucketFilter) (*Bucket, error)
    87  
    88  	// FindBuckets returns a list of buckets that match filter and the total count of matching buckets.
    89  	// Additional options provide pagination & sorting.
    90  	FindBuckets(ctx context.Context, filter BucketFilter, opt ...FindOptions) ([]*Bucket, int, error)
    91  
    92  	// CreateBucket creates a new bucket and sets b.ID with the new identifier.
    93  	CreateBucket(ctx context.Context, b *Bucket) error
    94  
    95  	// UpdateBucket updates a single bucket with changeset.
    96  	// Returns the new bucket state after update.
    97  	UpdateBucket(ctx context.Context, id platform.ID, upd BucketUpdate) (*Bucket, error)
    98  
    99  	// DeleteBucket removes a bucket by ID.
   100  	DeleteBucket(ctx context.Context, id platform.ID) error
   101  	FindBucketByName(ctx context.Context, orgID platform.ID, name string) (*Bucket, error)
   102  }
   103  
   104  // BucketUpdate represents updates to a bucket.
   105  // Only fields which are set are updated.
   106  type BucketUpdate struct {
   107  	Name               *string
   108  	Description        *string
   109  	RetentionPeriod    *time.Duration
   110  	ShardGroupDuration *time.Duration
   111  }
   112  
   113  // BucketFilter represents a set of filter that restrict the returned results.
   114  type BucketFilter struct {
   115  	ID             *platform.ID
   116  	Name           *string
   117  	OrganizationID *platform.ID
   118  	Org            *string
   119  }
   120  
   121  // QueryParams Converts BucketFilter fields to url query params.
   122  func (f BucketFilter) QueryParams() map[string][]string {
   123  	qp := map[string][]string{}
   124  	if f.ID != nil {
   125  		qp["id"] = []string{f.ID.String()}
   126  	}
   127  
   128  	if f.Name != nil {
   129  		qp["bucket"] = []string{*f.Name}
   130  	}
   131  
   132  	if f.OrganizationID != nil {
   133  		qp["orgID"] = []string{f.OrganizationID.String()}
   134  	}
   135  
   136  	if f.Org != nil {
   137  		qp["org"] = []string{*f.Org}
   138  	}
   139  
   140  	return qp
   141  }
   142  
   143  // String returns a human-readable string of the BucketFilter,
   144  // particularly useful for error messages.
   145  func (f BucketFilter) String() string {
   146  	// There should always be exactly 2 fields set, but if it's somehow more, that's fine.
   147  	parts := make([]string, 0, 2)
   148  	if f.ID != nil {
   149  		parts = append(parts, "Bucket ID: "+f.ID.String())
   150  	}
   151  	if f.Name != nil {
   152  		parts = append(parts, "Bucket Name: "+*f.Name)
   153  	}
   154  	if f.OrganizationID != nil {
   155  		parts = append(parts, "Org ID: "+f.OrganizationID.String())
   156  	}
   157  	if f.Org != nil {
   158  		parts = append(parts, "Org Name: "+*f.Org)
   159  	}
   160  	return "[" + strings.Join(parts, ", ") + "]"
   161  }