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

View file

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

View file

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

View file

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