2020-08-25 11:52:18 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2020-08-31 08:15:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-08-25 11:52:18 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/signature"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/client"
|
|
|
|
v2container "github.com/nspcc-dev/neofs-api-go/v2/container"
|
2020-08-31 08:15:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-08-25 11:52:18 +00:00
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) PutContainer(ctx context.Context, cnr *container.Container, opts ...CallOption) (*container.ID, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
switch c.remoteNode.Version.Major {
|
|
|
|
case 2:
|
|
|
|
return c.putContainerV2(ctx, cnr, opts...)
|
|
|
|
default:
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, unsupportedProtocolErr
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) GetContainer(ctx context.Context, id *container.ID, opts ...CallOption) (*container.Container, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
switch c.remoteNode.Version.Major {
|
|
|
|
case 2:
|
|
|
|
return c.getContainerV2(ctx, id, opts...)
|
|
|
|
default:
|
|
|
|
return nil, unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
func (c Client) ListContainers(ctx context.Context, owner *owner.ID, opts ...CallOption) ([]*container.ID, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
switch c.remoteNode.Version.Major {
|
|
|
|
case 2:
|
|
|
|
return c.listContainerV2(ctx, owner, opts...)
|
|
|
|
default:
|
|
|
|
return nil, unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) ListSelfContainers(ctx context.Context, opts ...CallOption) ([]*container.ID, error) {
|
2020-08-31 08:15:46 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
2020-08-25 11:52:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
ownerID := new(owner.ID)
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
|
|
|
|
return c.ListContainers(ctx, ownerID, opts...)
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) DeleteContainer(ctx context.Context, id *container.ID, opts ...CallOption) error {
|
2020-08-25 11:52:18 +00:00
|
|
|
switch c.remoteNode.Version.Major {
|
|
|
|
case 2:
|
|
|
|
panic("not implemented")
|
|
|
|
default:
|
|
|
|
return unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: func (c Client) GetExtendedACL
|
|
|
|
// todo: func (c Client) SetExtendedACL
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, opts ...CallOption) (*container.ID, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := defaultCallOptions()
|
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set transport version
|
|
|
|
cnr.SetVersion(c.remoteNode.Version.ToV2Version())
|
|
|
|
|
|
|
|
// if container owner is not set, then use client key as owner
|
|
|
|
if cnr.GetOwnerID() == nil {
|
2020-08-31 08:15:46 +00:00
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
2020-08-25 11:52:18 +00:00
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, err
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
ownerID := new(owner.ID)
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
|
|
|
|
cnr.SetOwnerID(ownerID.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.PutRequestBody)
|
|
|
|
reqBody.SetContainer(&cnr.Container)
|
|
|
|
|
|
|
|
// sign container
|
|
|
|
signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetContainer()}
|
|
|
|
err := signature.SignDataWithHandler(c.key, signWrapper, func(key []byte, sig []byte) {
|
2020-08-31 08:15:46 +00:00
|
|
|
containerSignature := new(refs.Signature)
|
2020-08-25 11:52:18 +00:00
|
|
|
containerSignature.SetKey(key)
|
|
|
|
containerSignature.SetSign(sig)
|
|
|
|
reqBody.SetSignature(containerSignature)
|
|
|
|
}, signature.SignWithRFC6979())
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, err
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := new(v2container.PutRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err = v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, err
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.Put(ctx, req)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, errors.Wrap(err, "transport error")
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
cidV2 := resp.GetBody().GetContainerID()
|
|
|
|
|
|
|
|
cid, err := container.IDFromV2(cidV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not convert %T to %T", cidV2, cid)
|
|
|
|
}
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
return cid, nil
|
|
|
|
default:
|
2020-08-28 13:14:04 +00:00
|
|
|
return nil, unsupportedProtocolErr
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) getContainerV2(ctx context.Context, id *container.ID, opts ...CallOption) (*container.Container, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := defaultCallOptions()
|
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.GetRequestBody)
|
2020-08-28 13:14:04 +00:00
|
|
|
reqBody.SetContainerID(id.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
req := new(v2container.GetRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.Get(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &container.Container{
|
|
|
|
Container: *resp.GetBody().GetContainer(),
|
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return nil, unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
func (c Client) listContainerV2(ctx context.Context, owner *owner.ID, opts ...CallOption) ([]*container.ID, error) {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := defaultCallOptions()
|
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.ListRequestBody)
|
2020-08-31 08:15:46 +00:00
|
|
|
reqBody.SetOwnerID(owner.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
req := new(v2container.ListRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.List(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
result := make([]*container.ID, 0, len(resp.GetBody().GetContainerIDs()))
|
|
|
|
for _, cidV2 := range resp.GetBody().GetContainerIDs() {
|
|
|
|
cid, err := container.IDFromV2(cidV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not convert %T to %T", cidV2, cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, cid)
|
2020-08-25 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
default:
|
|
|
|
return nil, unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:14:04 +00:00
|
|
|
func (c Client) delContainerV2(ctx context.Context, id *container.ID, opts ...CallOption) error {
|
2020-08-25 11:52:18 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := defaultCallOptions()
|
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2container.DeleteRequestBody)
|
2020-08-28 13:14:04 +00:00
|
|
|
reqBody.SetContainerID(id.ToV2())
|
2020-08-25 11:52:18 +00:00
|
|
|
|
|
|
|
// sign container
|
|
|
|
signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetContainerID()}
|
|
|
|
err := signature.SignDataWithHandler(c.key, signWrapper, func(key []byte, sig []byte) {
|
2020-08-31 08:15:46 +00:00
|
|
|
containerSignature := new(refs.Signature)
|
2020-08-25 11:52:18 +00:00
|
|
|
containerSignature.SetKey(key)
|
|
|
|
containerSignature.SetSign(sig)
|
|
|
|
reqBody.SetSignature(containerSignature)
|
|
|
|
}, signature.SignWithRFC6979())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := new(v2container.DeleteRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err = v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.Delete(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func v2ContainerClientFromOptions(opts *clientOptions) (cli *v2container.Client, err error) {
|
|
|
|
switch {
|
|
|
|
case opts.grpcOpts.v2ContainerClient != nil:
|
|
|
|
// return value from client cache
|
|
|
|
return opts.grpcOpts.v2ContainerClient, nil
|
|
|
|
|
|
|
|
case opts.grpcOpts.conn != nil:
|
|
|
|
cli, err = v2container.NewClient(v2container.WithGlobalOpts(
|
|
|
|
client.WithGRPCConn(opts.grpcOpts.conn)),
|
|
|
|
)
|
|
|
|
|
|
|
|
case opts.addr != "":
|
|
|
|
cli, err = v2container.NewClient(v2container.WithGlobalOpts(
|
|
|
|
client.WithNetworkAddress(opts.addr)),
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("lack of sdk client options to create accounting client")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if client correct and save in cache
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.grpcOpts.v2ContainerClient = cli
|
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|