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/client"
|
|
|
|
"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-06 12:02:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gate/auth"
|
2020-08-03 11:48:33 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
|
|
|
rid = api.GetRequestID(ctx)
|
|
|
|
|
|
|
|
info = &BucketInfo{
|
|
|
|
CID: cid,
|
|
|
|
Name: cid.String(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-08-03 11:48:33 +00:00
|
|
|
bearer, err := auth.GetBearerToken(ctx)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not receive bearer token",
|
2020-10-19 01:04:37 +00:00
|
|
|
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))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = bearer
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
cli, tkn, err := n.prepareClient(ctx)
|
2020-08-03 11:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not prepare client",
|
2020-10-19 01:04:37 +00:00
|
|
|
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-10-19 01:04:37 +00:00
|
|
|
res, err := cli.GetContainer(ctx, cid, client.WithSession(tkn))
|
2020-08-03 11:48:33 +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-10-23 00:12:37 +00:00
|
|
|
info.Owner = owner.NewIDFromV2(res.GetOwnerID())
|
|
|
|
|
2020-10-22 00:19:16 +00:00
|
|
|
for _, attr := range res.GetAttributes() {
|
|
|
|
switch key, val := attr.GetKey(), attr.GetValue(); 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-10-22 00:19:16 +00:00
|
|
|
unix, err := strconv.ParseInt(attr.GetValue(), 10, 64)
|
|
|
|
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-08-11 11:32:04 +00:00
|
|
|
rid := api.GetRequestID(ctx)
|
2020-08-03 11:48:33 +00:00
|
|
|
bearer, err := auth.GetBearerToken(ctx)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not receive bearer token",
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = bearer
|
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
cli, tkn, err := n.prepareClient(ctx)
|
2020-08-03 11:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not prepare client",
|
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-19 01:04:37 +00:00
|
|
|
// own, err := GetOwnerID(bearer)
|
|
|
|
// if err != nil {
|
|
|
|
// n.log.Error("could not fetch owner id",
|
|
|
|
// zap.String("request_id", rid),
|
|
|
|
// zap.Error(err))
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2020-08-03 11:48:33 +00:00
|
|
|
|
2020-10-19 01:04:37 +00:00
|
|
|
res, err := cli.ListContainers(ctx, tkn.OwnerID(), client.WithSession(tkn))
|
2020-08-03 11:48:33 +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
|
|
|
|
}
|