Migrate api/layer to SDK

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-11-24 10:01:38 +03:00
parent e3b1e8f369
commit 0ee3a5f9ba
4 changed files with 72 additions and 215 deletions

View file

@ -5,11 +5,11 @@ import (
"strconv"
"time"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-s3-gate/api"
"github.com/nspcc-dev/neofs-s3-gate/auth"
"go.uber.org/zap"
)
@ -32,6 +32,8 @@ type (
func (n *layer) containerInfo(ctx context.Context, cid *container.ID) (*BucketInfo, error) {
var (
err error
res *container.Container
rid = api.GetRequestID(ctx)
info = &BucketInfo{
@ -40,29 +42,7 @@ func (n *layer) containerInfo(ctx context.Context, cid *container.ID) (*BucketIn
}
)
bearer, err := auth.GetBearerToken(ctx)
if err != nil {
n.log.Error("could not receive bearer token",
zap.Stringer("cid", cid),
zap.String("request_id", rid),
zap.Error(err))
return nil, err
}
_ = bearer
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
n.log.Error("could not prepare client",
zap.Stringer("cid", cid),
zap.String("request_id", rid),
zap.Error(err))
return nil, err
}
res, err := cli.GetContainer(ctx, cid, client.WithSession(tkn))
if err != nil {
if res, err = n.cli.Container().Get(ctx, cid); err != nil {
n.log.Error("could not fetch container",
zap.Stringer("cid", cid),
zap.String("request_id", rid),
@ -71,14 +51,14 @@ func (n *layer) containerInfo(ctx context.Context, cid *container.ID) (*BucketIn
return nil, err
}
info.Owner = owner.NewIDFromV2(res.GetOwnerID())
info.Owner = res.OwnerID()
for _, attr := range res.GetAttributes() {
switch key, val := attr.GetKey(), attr.GetValue(); key {
for _, attr := range res.Attributes() {
switch key, val := attr.Key(), attr.Value(); key {
case container.AttributeName:
info.Name = val
case container.AttributeTimestamp:
unix, err := strconv.ParseInt(attr.GetValue(), 10, 64)
unix, err := strconv.ParseInt(attr.Value(), 10, 64)
if err != nil {
n.log.Error("could not parse container creation time",
zap.Stringer("cid", cid),
@ -97,34 +77,20 @@ func (n *layer) containerInfo(ctx context.Context, cid *container.ID) (*BucketIn
}
func (n *layer) containerList(ctx context.Context) ([]*BucketInfo, error) {
rid := api.GetRequestID(ctx)
bearer, err := auth.GetBearerToken(ctx)
if err != nil {
var (
err error
tkn *token.BearerToken
rid = api.GetRequestID(ctx)
)
if tkn, err = sdk.BearerToken(ctx); err != nil {
n.log.Error("could not receive bearer token",
zap.String("request_id", rid),
zap.Error(err))
return nil, err
}
_ = bearer
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
n.log.Error("could not prepare client",
zap.String("request_id", rid),
zap.Error(err))
return nil, err
}
// 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
// }
res, err := cli.ListContainers(ctx, tkn.OwnerID(), client.WithSession(tkn))
res, err := n.cli.Container().List(ctx, tkn.Issuer())
if err != nil {
n.log.Error("could not fetch container",
zap.String("request_id", rid),

View file

@ -2,19 +2,16 @@ package layer
import (
"context"
"crypto/ecdsa"
"io"
"net/url"
"strings"
"time"
"github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
"github.com/nspcc-dev/cdn-neofs-sdk/creds/neofs"
"github.com/nspcc-dev/cdn-neofs-sdk/pool"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-s3-gate/api"
"github.com/nspcc-dev/neofs-s3-gate/api/pool"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
@ -23,19 +20,15 @@ import (
type (
layer struct {
uid *owner.ID
cli sdk.Client
log *zap.Logger
cli pool.Client
key *ecdsa.PrivateKey
reqTimeout time.Duration
}
Params struct {
Pool pool.Client
Logger *zap.Logger
Timeout time.Duration
NFKey *ecdsa.PrivateKey
Pool pool.Client
Logger *zap.Logger
Timeout time.Duration
Credential neofs.Credentials
}
GetObjectParams struct {
@ -88,36 +81,16 @@ type (
// NewGatewayLayer creates instance of layer. It checks credentials
// and establishes gRPC connection with node.
func NewLayer(p *Params) (Client, error) {
wallet, err := owner.NEO3WalletFromPublicKey(&p.NFKey.PublicKey)
if err != nil {
return nil, err
}
uid := owner.NewID()
uid.SetNeo3Wallet(wallet)
func NewLayer(log *zap.Logger, cli sdk.Client) Client {
return &layer{
uid: uid,
cli: p.Pool,
key: p.NFKey,
log: p.Logger,
reqTimeout: p.Timeout,
}, nil
cli: cli,
log: log,
}
}
// Get NeoFS Object by refs.Address (should be used by auth.Center)
func (n *layer) Get(ctx context.Context, address *object.Address) (*object.Object, error) {
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
return nil, err
}
gop := new(client.GetObjectParams)
gop.WithAddress(address)
return cli.GetObject(ctx, gop, client.WithSession(tkn))
return n.cli.Object().Get(ctx, address)
}
// GetBucketInfo returns bucket name.
@ -221,9 +194,9 @@ func (n *layer) ListObjects(ctx context.Context, p *ListObjectsParams) (*ListObj
oi = objectInfoFromMeta(bkt, meta)
} else { // if there are sub-entities in tail - dir
oi = &ObjectInfo{
id: meta.GetID(),
id: meta.ID(),
Owner: meta.GetOwnerID(),
Owner: meta.OwnerID(),
Bucket: bkt.Name,
Name: tail[:ind+1], // dir MUST have slash symbol in the end
// IsDir: true,
@ -263,7 +236,7 @@ func (n *layer) GetObject(ctx context.Context, p *GetObjectParams) error {
_, err = n.objectGet(ctx, &getParams{
Writer: p.Writer,
addr: addr,
address: addr,
offset: p.Offset,
length: p.Length,
@ -298,17 +271,6 @@ func (n *layer) GetObjectInfo(ctx context.Context, bucketName, filename string)
return objectInfoFromMeta(bkt, meta), nil
}
func GetOwnerID(tkn *token.BearerToken) (*owner.ID, error) {
switch pkg.SDKVersion().GetMajor() {
case 2:
id := tkn.ToV2().GetBody().GetOwnerID()
return owner.NewIDFromV2(id), nil
default:
return nil, errors.New("unknown version")
}
}
// PutObject into storage.
func (n *layer) PutObject(ctx context.Context, p *PutObjectParams) (*ObjectInfo, error) {
return n.objectPut(ctx, p)

View file

@ -10,11 +10,12 @@ import (
"strconv"
"time"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
sdk "github.com/nspcc-dev/cdn-neofs-sdk"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-s3-gate/api"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
@ -30,57 +31,28 @@ type (
getParams struct {
io.Writer
addr *object.Address
offset int64
length int64
offset int64
length int64
address *object.Address
}
)
func (n *layer) prepareClient(ctx context.Context) (*client.Client, *token.SessionToken, error) {
conn, err := n.cli.Connection(ctx)
if err != nil {
return nil, nil, err
}
tkn, err := n.cli.Token(ctx, conn)
if err != nil {
return nil, nil, err
}
cli, err := client.New(n.key, client.WithGRPCConnection(conn))
if err != nil {
return nil, nil, err
}
return cli, tkn, nil
}
// objectSearch returns all available objects by search params.
func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID, error) {
opts := []sdk.ObjectSearchOption{
sdk.SearchRootObjects(),
}
filename, err := url.QueryUnescape(p.val)
if err != nil {
return nil, err
}
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
return nil, err
}
filter := object.NewSearchFilters()
filter.AddRootFilter()
sop := new(client.SearchObjectParams)
sop.WithContainerID(p.cid)
if p.val != "" {
filter.AddFilter(object.AttributeFileName, filename, object.MatchStringEqual)
opts = append(opts, sdk.SearchByFilename(filename))
}
sop.WithSearchFilters(filter)
return cli.SearchObject(ctx, sop, client.WithSession(tkn))
return n.cli.Object().Search(ctx, p.cid, opts...)
}
// objectFindID returns object id (uuid) based on it's nice name in s3. If
@ -98,36 +70,18 @@ func (n *layer) objectFindID(ctx context.Context, p *findParams) (*object.ID, er
}
// objectHead returns all object's headers.
func (n *layer) objectHead(ctx context.Context, addr *object.Address) (*object.Object, error) {
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
return nil, err
}
ohp := new(client.ObjectHeaderParams)
ohp.WithAddress(addr)
ohp.WithAllFields()
return cli.GetObjectHeader(ctx, ohp, client.WithSession(tkn))
func (n *layer) objectHead(ctx context.Context, address *object.Address) (*object.Object, error) {
return n.cli.Object().Head(ctx, address, sdk.WithFullHeaders())
}
// objectGet and write it into provided io.Reader.
func (n *layer) objectGet(ctx context.Context, p *getParams) (*object.Object, error) {
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
return nil, err
}
// prepare length/offset writer
b := bufio.NewWriter(p.Writer)
w := newWriter(b, p.offset, p.length)
writer := newWriter(w, p.offset, p.length)
gop := new(client.GetObjectParams)
gop.WithAddress(p.addr)
gop.WithPayloadWriter(writer)
return cli.GetObject(ctx, gop, client.WithSession(tkn))
return n.cli.Object().Get(ctx, p.address, sdk.WithGetWriter(writer))
}
// objectPut into NeoFS, took payload from io.Reader.
@ -135,23 +89,15 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
var (
err error
obj string
own *owner.ID
oid *object.ID
bkt *BucketInfo
// brt *token.BearerToken
brt *token.BearerToken
address *object.Address
)
// if brt, err = auth.GetBearerToken(ctx); err != nil {
// return nil, err
// }
// else if own, err = GetOwnerID(brt); err != nil {
// return nil, err
// }
_ = own
if obj, err = url.QueryUnescape(p.Object); err != nil {
if brt, err = sdk.BearerToken(ctx); err != nil {
return nil, err
} else if obj, err = url.QueryUnescape(p.Object); err != nil {
return nil, err
} else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
return nil, err
@ -162,11 +108,6 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
}
}
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
return nil, err
}
attributes := make([]*object.Attribute, 0, len(p.Header)+1)
unix := strconv.FormatInt(time.Now().UTC().Unix(), 10)
@ -190,43 +131,31 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
}
b := new(bytes.Buffer)
r := io.TeeReader(p.Reader, b)
raw := object.NewRaw()
raw.SetOwnerID(tkn.OwnerID()) // should be replaced with BearerToken.Issuer()
raw.SetOwnerID(brt.Issuer())
raw.SetContainerID(bkt.CID)
raw.SetAttributes(attributes...)
pop := new(client.PutObjectParams)
pop.WithPayloadReader(r)
pop.WithObject(raw.Object())
if oid, err = cli.PutObject(ctx, pop, client.WithSession(tkn)); err != nil {
return nil, errors.Wrapf(err, "owner_id = %s", tkn.OwnerID())
r := io.TeeReader(p.Reader, b)
if address, err = n.cli.Object().Put(ctx, raw.Object(), sdk.WithPutReader(r)); err != nil {
return nil, err
}
return &ObjectInfo{
id: oid,
id: address.ObjectID(),
Bucket: p.Bucket,
Name: p.Object,
Size: p.Size,
Created: time.Now(),
ContentType: http.DetectContentType(b.Bytes()),
Owner: own,
Owner: brt.Issuer(),
Headers: p.Header,
}, nil
}
// objectDelete puts tombstone object into neofs.
func (n *layer) objectDelete(ctx context.Context, address *object.Address) error {
cli, tkn, err := n.prepareClient(ctx)
if err != nil {
return err
}
dob := new(client.DeleteObjectParams)
dob.WithAddress(address)
return cli.DeleteObject(ctx, dob, client.WithSession(tkn))
return n.cli.Object().Delete(ctx, address)
}

View file

@ -55,7 +55,7 @@ func userHeaders(attrs []*object.Attribute) map[string]string {
result := make(map[string]string, len(attrs))
for _, attr := range attrs {
result[attr.GetKey()] = attr.GetValue()
result[attr.Key()] = attr.Value()
}
return result
@ -64,10 +64,10 @@ func userHeaders(attrs []*object.Attribute) map[string]string {
func objectInfoFromMeta(bkt *BucketInfo, meta *object.Object) *ObjectInfo {
var (
creation time.Time
filename = meta.GetID().String()
filename = meta.ID().String()
)
userHeaders := userHeaders(meta.GetAttributes())
userHeaders := userHeaders(meta.Attributes())
if val, ok := userHeaders[object.AttributeFileName]; ok {
filename = val
delete(userHeaders, object.AttributeFileName)
@ -80,26 +80,26 @@ func objectInfoFromMeta(bkt *BucketInfo, meta *object.Object) *ObjectInfo {
delete(userHeaders, object.AttributeTimestamp)
}
mimeType := http.DetectContentType(meta.GetPayload())
mimeType := http.DetectContentType(meta.Payload())
return &ObjectInfo{
id: meta.GetID(),
id: meta.ID(),
Bucket: bkt.Name,
Name: filename,
Created: creation,
ContentType: mimeType,
Headers: userHeaders,
Size: int64(meta.GetPayloadSize()),
Size: int64(meta.PayloadSize()),
}
}
func nameFromObject(o *object.Object) (string, string) {
var name = o.GetID().String()
var name = o.ID().String()
for _, attr := range o.GetAttributes() {
if attr.GetKey() == object.AttributeFileName {
name = attr.GetValue()
for _, attr := range o.Attributes() {
if attr.Key() == object.AttributeFileName {
name = attr.Value()
break
}