2019-07-08 23:32:18 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-11-12 20:47:41 +00:00
|
|
|
package buckets
|
2019-07-08 23:32:18 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-18 09:54:59 +01:00
|
|
|
"time"
|
2019-07-08 23:32:18 +01:00
|
|
|
|
2023-04-13 13:04:07 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/macaroon"
|
2023-04-19 14:44:54 +01:00
|
|
|
"storj.io/common/pb"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2019-07-08 23:32:18 +01:00
|
|
|
)
|
|
|
|
|
2023-04-13 13:04:07 +01:00
|
|
|
var (
|
|
|
|
// ErrBucket is an error class for general bucket errors.
|
|
|
|
ErrBucket = errs.Class("bucket")
|
|
|
|
|
|
|
|
// ErrNoBucket is an error class for using empty bucket name.
|
|
|
|
ErrNoBucket = errs.Class("no bucket specified")
|
|
|
|
|
|
|
|
// ErrBucketNotFound is an error class for non-existing bucket.
|
|
|
|
ErrBucketNotFound = errs.Class("bucket not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bucket contains information about a specific bucket.
|
2021-10-18 09:54:59 +01:00
|
|
|
type Bucket struct {
|
2023-04-13 13:04:07 +01:00
|
|
|
ID uuid.UUID
|
|
|
|
Name string
|
|
|
|
ProjectID uuid.UUID
|
|
|
|
UserAgent []byte
|
|
|
|
Created time.Time
|
|
|
|
PathCipher storj.CipherSuite
|
|
|
|
DefaultSegmentsSize int64
|
|
|
|
DefaultRedundancyScheme storj.RedundancyScheme
|
|
|
|
DefaultEncryptionParameters storj.EncryptionParameters
|
|
|
|
Placement storj.PlacementConstraint
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:25:12 +01:00
|
|
|
// ListDirection specifies listing direction.
|
2023-04-19 14:44:54 +01:00
|
|
|
type ListDirection = pb.ListDirection
|
2023-04-19 13:25:12 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
// DirectionForward lists forwards from cursor, including cursor.
|
2023-04-19 14:44:54 +01:00
|
|
|
DirectionForward = pb.ListDirection_FORWARD
|
2023-04-19 13:25:12 +01:00
|
|
|
// DirectionAfter lists forwards from cursor, without cursor.
|
2023-04-19 14:44:54 +01:00
|
|
|
DirectionAfter = pb.ListDirection_AFTER
|
2023-04-19 13:25:12 +01:00
|
|
|
)
|
|
|
|
|
2023-04-13 13:04:07 +01:00
|
|
|
// MinimalBucket contains minimal bucket fields for metainfo protocol.
|
|
|
|
type MinimalBucket struct {
|
2021-10-18 09:54:59 +01:00
|
|
|
Name []byte
|
|
|
|
CreatedAt time.Time
|
|
|
|
}
|
|
|
|
|
2023-04-13 13:04:07 +01:00
|
|
|
// ListOptions lists objects.
|
|
|
|
type ListOptions struct {
|
|
|
|
Cursor string
|
2023-04-19 13:25:12 +01:00
|
|
|
Direction ListDirection
|
2023-04-13 13:04:07 +01:00
|
|
|
Limit int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextPage returns options for listing the next page.
|
|
|
|
func (opts ListOptions) NextPage(list List) ListOptions {
|
|
|
|
if !list.More || len(list.Items) == 0 {
|
|
|
|
return ListOptions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ListOptions{
|
|
|
|
Cursor: list.Items[len(list.Items)-1].Name,
|
2023-04-19 13:25:12 +01:00
|
|
|
Direction: DirectionAfter,
|
2023-04-13 13:04:07 +01:00
|
|
|
Limit: opts.Limit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is a list of buckets.
|
|
|
|
type List struct {
|
|
|
|
More bool
|
|
|
|
Items []Bucket
|
|
|
|
}
|
|
|
|
|
2021-11-12 20:47:41 +00:00
|
|
|
// DB is the interface for the database to interact with buckets.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2021-11-12 20:47:41 +00:00
|
|
|
type DB interface {
|
|
|
|
// CreateBucket creates a new bucket
|
2023-04-13 13:04:07 +01:00
|
|
|
CreateBucket(ctx context.Context, bucket Bucket) (_ Bucket, err error)
|
2021-11-12 20:47:41 +00:00
|
|
|
// GetBucket returns an existing bucket
|
2023-04-13 13:04:07 +01:00
|
|
|
GetBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (bucket Bucket, err error)
|
2021-10-27 09:50:27 +01:00
|
|
|
// GetBucketPlacement returns with the placement constraint identifier.
|
|
|
|
GetBucketPlacement(ctx context.Context, bucketName []byte, projectID uuid.UUID) (placement storj.PlacementConstraint, err error)
|
2021-10-18 09:54:59 +01:00
|
|
|
// GetMinimalBucket returns existing bucket with minimal number of fields.
|
2023-04-13 13:04:07 +01:00
|
|
|
GetMinimalBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (bucket MinimalBucket, err error)
|
2021-04-02 17:19:17 +01:00
|
|
|
// HasBucket returns if a bucket exists.
|
|
|
|
HasBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (exists bool, err error)
|
2020-06-25 15:47:44 +01:00
|
|
|
// GetBucketID returns an existing bucket id.
|
2020-07-24 18:13:15 +01:00
|
|
|
GetBucketID(ctx context.Context, bucket metabase.BucketLocation) (id uuid.UUID, err error)
|
2019-07-19 16:17:34 +01:00
|
|
|
// UpdateBucket updates an existing bucket
|
2023-04-13 13:04:07 +01:00
|
|
|
UpdateBucket(ctx context.Context, bucket Bucket) (_ Bucket, err error)
|
2023-06-13 16:58:24 +01:00
|
|
|
// UpdateUserAgent updates buckets user agent.
|
|
|
|
UpdateUserAgent(ctx context.Context, projectID uuid.UUID, bucketName string, userAgent []byte) error
|
2021-11-12 20:47:41 +00:00
|
|
|
// DeleteBucket deletes a bucket
|
2019-07-08 23:32:18 +01:00
|
|
|
DeleteBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (err error)
|
2021-11-12 20:47:41 +00:00
|
|
|
// ListBuckets returns all buckets for a project
|
2023-04-13 13:04:07 +01:00
|
|
|
ListBuckets(ctx context.Context, projectID uuid.UUID, listOpts ListOptions, allowedBuckets macaroon.AllowedBuckets) (bucketList List, err error)
|
2020-06-30 22:49:29 +01:00
|
|
|
// CountBuckets returns the number of buckets a project currently has
|
|
|
|
CountBuckets(ctx context.Context, projectID uuid.UUID) (int, error)
|
2022-10-05 11:53:02 +01:00
|
|
|
// IterateBucketLocations iterates through all buckets from some point with limit.
|
2022-12-06 11:16:55 +00:00
|
|
|
IterateBucketLocations(ctx context.Context, projectID uuid.UUID, bucketName string, limit int, fn func([]metabase.BucketLocation) error) (more bool, err error)
|
2019-07-08 23:32:18 +01:00
|
|
|
}
|