2020-08-03 11:48:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
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"
|
2020-10-23 00:12:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-08-11 11:32:04 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/api"
|
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
|
2020-10-19 01:04:37 +00:00
|
|
|
CID *container.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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
func (n *layer) containerInfo(ctx context.Context, cid *container.ID) (*BucketInfo, error) {
|
2020-10-22 00:19:16 +00:00
|
|
|
var (
|
2020-11-24 07:01:38 +00:00
|
|
|
err error
|
|
|
|
res *container.Container
|
2020-10-22 00:19:16 +00:00
|
|
|
rid = api.GetRequestID(ctx)
|
|
|
|
|
|
|
|
info = &BucketInfo{
|
|
|
|
CID: cid,
|
|
|
|
Name: cid.String(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-11-24 07:01:38 +00:00
|
|
|
if res, err = n.cli.Container().Get(ctx, cid); 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 (
|
|
|
|
err error
|
2020-11-27 12:36:15 +00:00
|
|
|
own = n.Owner(ctx)
|
|
|
|
res []*container.ID
|
2020-11-24 07:01:38 +00:00
|
|
|
rid = api.GetRequestID(ctx)
|
|
|
|
)
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2020-11-27 12:36:15 +00:00
|
|
|
if res, err = n.cli.Container().List(ctx, own); 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
|
|
|
|
}
|