2019-01-18 15:10:21 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-04-25 09:46:32 +01:00
|
|
|
package metainfo
|
2019-01-18 15:10:21 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-04 12:55:38 +01:00
|
|
|
"context"
|
|
|
|
|
2020-02-14 12:52:00 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-01-18 15:10:21 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/macaroon"
|
|
|
|
"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-01-18 15:10:21 +00:00
|
|
|
)
|
|
|
|
|
2020-02-14 12:52:00 +00:00
|
|
|
var (
|
|
|
|
// ErrBucketNotEmpty is returned when bucket is required to be empty for an operation.
|
|
|
|
ErrBucketNotEmpty = errs.Class("bucket not empty")
|
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Service provides the metainfo service dependencies.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-01-18 15:10:21 +00:00
|
|
|
type Service struct {
|
2020-10-29 16:54:35 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
bucketsDB BucketsDB
|
|
|
|
metabaseDB MetabaseDB
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 18:14:35 +00:00
|
|
|
// NewService creates new metainfo service.
|
2020-12-22 10:38:32 +00:00
|
|
|
func NewService(logger *zap.Logger, bucketsDB BucketsDB, metabaseDB MetabaseDB) *Service {
|
2020-10-29 16:54:35 +00:00
|
|
|
return &Service{
|
|
|
|
logger: logger,
|
|
|
|
bucketsDB: bucketsDB,
|
|
|
|
metabaseDB: metabaseDB,
|
|
|
|
}
|
2019-01-18 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// CreateBucket creates a new bucket in the buckets db.
|
2019-07-08 23:32:18 +01:00
|
|
|
func (s *Service) CreateBucket(ctx context.Context, bucket storj.Bucket) (_ storj.Bucket, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.CreateBucket(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
2021-04-02 17:19:17 +01:00
|
|
|
// HasBucket returns if a bucket exists.
|
|
|
|
func (s *Service) HasBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (ok bool, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.HasBucket(ctx, bucketName, projectID)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetBucket returns an existing bucket in the buckets db.
|
2019-07-08 23:32:18 +01:00
|
|
|
func (s *Service) GetBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (_ storj.Bucket, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.GetBucket(ctx, bucketName, projectID)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UpdateBucket returns an updated bucket in the buckets db.
|
2019-07-19 16:17:34 +01:00
|
|
|
func (s *Service) UpdateBucket(ctx context.Context, bucket storj.Bucket) (_ storj.Bucket, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.UpdateBucket(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DeleteBucket deletes a bucket from the bucekts db.
|
2019-07-08 23:32:18 +01:00
|
|
|
func (s *Service) DeleteBucket(ctx context.Context, bucketName []byte, projectID uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-02-14 12:52:00 +00:00
|
|
|
|
|
|
|
empty, err := s.IsBucketEmpty(ctx, projectID, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
return ErrBucketNotEmpty.New("")
|
|
|
|
}
|
|
|
|
|
2019-07-08 23:32:18 +01:00
|
|
|
return s.bucketsDB.DeleteBucket(ctx, bucketName, projectID)
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:52:00 +00:00
|
|
|
// IsBucketEmpty returns whether bucket is empty.
|
|
|
|
func (s *Service) IsBucketEmpty(ctx context.Context, projectID uuid.UUID, bucketName []byte) (bool, error) {
|
2020-11-17 17:37:58 +00:00
|
|
|
empty, err := s.metabaseDB.BucketEmpty(ctx, metabase.BucketEmpty{
|
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: string(bucketName),
|
|
|
|
})
|
|
|
|
return empty, Error.Wrap(err)
|
2020-02-14 12:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// ListBuckets returns a list of buckets for a project.
|
2019-07-12 13:57:02 +01:00
|
|
|
func (s *Service) ListBuckets(ctx context.Context, projectID uuid.UUID, listOpts storj.BucketListOptions, allowedBuckets macaroon.AllowedBuckets) (bucketList storj.BucketList, err error) {
|
2019-07-08 23:32:18 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.ListBuckets(ctx, projectID, listOpts, allowedBuckets)
|
|
|
|
}
|
2020-06-30 22:49:29 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// CountBuckets returns the number of buckets a project currently has.
|
2020-06-30 22:49:29 +01:00
|
|
|
func (s *Service) CountBuckets(ctx context.Context, projectID uuid.UUID) (count int, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return s.bucketsDB.CountBuckets(ctx, projectID)
|
|
|
|
}
|