2020-08-03 11:48:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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-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-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-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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = res
|
|
|
|
|
|
|
|
return &BucketInfo{
|
|
|
|
CID: cid,
|
2020-10-19 01:04:37 +00:00
|
|
|
Name: cid.String(), // should be fetched from container.Attributes
|
|
|
|
Created: time.Time{}, // should be fetched from container.Attributes
|
2020-08-03 11:48:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-19 01:04:37 +00:00
|
|
|
list := make([]BucketInfo, 0, len(res))
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, *info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|