2020-08-03 11:48:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-23 20:21:15 +00:00
|
|
|
"fmt"
|
2020-10-22 00:19:16 +00:00
|
|
|
"strconv"
|
2020-08-03 11:48:33 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2021-06-04 13:01:42 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-10-23 00:12:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2021-06-23 20:21:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pkg/pool"
|
2020-08-03 11:48:33 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-05-13 20:25:31 +00:00
|
|
|
// BucketInfo stores basic bucket data.
|
2020-08-03 11:48:33 +00:00
|
|
|
BucketInfo struct {
|
|
|
|
Name string
|
2021-06-04 13:01:42 +00:00
|
|
|
CID *cid.ID
|
2020-10-23 00:12:37 +00:00
|
|
|
Owner *owner.ID
|
2020-08-03 11:48:33 +00:00
|
|
|
Created time.Time
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ListObjectsParams represents object listing request parameters.
|
2020-08-03 11:48:33 +00:00
|
|
|
ListObjectsParams struct {
|
|
|
|
Bucket string
|
|
|
|
Prefix string
|
|
|
|
Token string
|
|
|
|
Delimiter string
|
|
|
|
MaxKeys int
|
2021-06-25 12:54:25 +00:00
|
|
|
Marker string
|
2021-07-01 10:54:51 +00:00
|
|
|
Version int
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-06-04 13:01:42 +00:00
|
|
|
func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*BucketInfo, error) {
|
2020-10-22 00:19:16 +00:00
|
|
|
var (
|
2021-06-15 13:59:51 +00:00
|
|
|
err error
|
|
|
|
res *container.Container
|
|
|
|
rid = api.GetRequestID(ctx)
|
|
|
|
bearerOpt = n.BearerOpt(ctx)
|
2020-10-22 00:19:16 +00:00
|
|
|
|
|
|
|
info = &BucketInfo{
|
|
|
|
CID: cid,
|
|
|
|
Name: cid.String(),
|
|
|
|
}
|
|
|
|
)
|
2021-06-23 20:26:48 +00:00
|
|
|
res, err = n.pool.GetContainer(ctx, cid, bearerOpt)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
n.log.Error("could not fetch container",
|
|
|
|
zap.Stringer("cid", cid),
|
2020-08-11 11:32:04 +00:00
|
|
|
zap.String("request_id", rid),
|
2020-08-03 11:48:33 +00:00
|
|
|
zap.Error(err))
|
2020-10-19 01:04:37 +00:00
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-24 07:01:38 +00:00
|
|
|
info.Owner = res.OwnerID()
|
2020-10-23 00:12:37 +00:00
|
|
|
|
2020-11-24 07:01:38 +00:00
|
|
|
for _, attr := range res.Attributes() {
|
|
|
|
switch key, val := attr.Key(), attr.Value(); key {
|
2020-10-23 00:12:37 +00:00
|
|
|
case container.AttributeName:
|
2020-10-22 00:19:16 +00:00
|
|
|
info.Name = val
|
2020-10-23 00:12:37 +00:00
|
|
|
case container.AttributeTimestamp:
|
2020-11-24 07:01:38 +00:00
|
|
|
unix, err := strconv.ParseInt(attr.Value(), 10, 64)
|
2020-10-22 00:19:16 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not parse container creation time",
|
|
|
|
zap.Stringer("cid", cid),
|
|
|
|
zap.String("request_id", rid),
|
|
|
|
zap.String("created_at", val),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Created = time.Unix(unix, 0)
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2020-10-22 00:19:16 +00:00
|
|
|
return info, nil
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
func (n *layer) containerList(ctx context.Context) ([]*BucketInfo, error) {
|
2020-11-24 07:01:38 +00:00
|
|
|
var (
|
2021-06-15 13:59:51 +00:00
|
|
|
err error
|
|
|
|
own = n.Owner(ctx)
|
|
|
|
bearerOpt = n.BearerOpt(ctx)
|
|
|
|
res []*cid.ID
|
|
|
|
rid = api.GetRequestID(ctx)
|
2020-11-24 07:01:38 +00:00
|
|
|
)
|
2021-06-23 20:26:48 +00:00
|
|
|
res, err = n.pool.ListContainers(ctx, own, bearerOpt)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
2020-10-19 01:04:37 +00:00
|
|
|
n.log.Error("could not fetch container",
|
2020-08-11 11:32:04 +00:00
|
|
|
zap.String("request_id", rid),
|
2020-08-03 11:48:33 +00:00
|
|
|
zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
list := make([]*BucketInfo, 0, len(res))
|
2020-10-19 01:04:37 +00:00
|
|
|
for _, cid := range res {
|
2020-08-03 11:48:33 +00:00
|
|
|
info, err := n.containerInfo(ctx, cid)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not fetch container info",
|
2020-08-11 11:32:04 +00:00
|
|
|
zap.String("request_id", rid),
|
2020-08-03 11:48:33 +00:00
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:12:37 +00:00
|
|
|
list = append(list, info)
|
2020-08-03 11:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
2021-06-23 20:21:15 +00:00
|
|
|
|
|
|
|
func (n *layer) createContainer(ctx context.Context, p *CreateBucketParams) (*cid.ID, error) {
|
|
|
|
cnr := container.New(
|
|
|
|
container.WithPolicy(p.Policy),
|
|
|
|
container.WithCustomBasicACL(p.ACL),
|
|
|
|
container.WithAttribute(container.AttributeName, p.Name),
|
|
|
|
container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(time.Now().Unix(), 10)))
|
|
|
|
|
|
|
|
cnr.SetSessionToken(ctx.Value(api.GateData).(*accessbox.GateData).SessionToken)
|
|
|
|
cnr.SetOwnerID(n.Owner(ctx))
|
|
|
|
|
|
|
|
cid, err := n.pool.PutContainer(ctx, cnr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create a bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = n.pool.WaitForContainerPresence(ctx, cid, pool.DefaultPollingParams())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cid, nil
|
|
|
|
}
|
2021-06-23 20:25:00 +00:00
|
|
|
|
|
|
|
func (n *layer) deleteContainer(ctx context.Context, cid *cid.ID) error {
|
|
|
|
return n.pool.DeleteContainer(ctx, cid, n.SessionOpt(ctx))
|
|
|
|
}
|