[#128] sdk: Remove old container ID with the new one
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
8df69c4042
commit
2b20230d7f
2 changed files with 33 additions and 46 deletions
|
@ -13,16 +13,16 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c Client) PutContainer(ctx context.Context, cnr *container.Container, opts ...CallOption) (refs.ContainerID, error) {
|
func (c Client) PutContainer(ctx context.Context, cnr *container.Container, opts ...CallOption) (*container.ID, error) {
|
||||||
switch c.remoteNode.Version.Major {
|
switch c.remoteNode.Version.Major {
|
||||||
case 2:
|
case 2:
|
||||||
return c.putContainerV2(ctx, cnr, opts...)
|
return c.putContainerV2(ctx, cnr, opts...)
|
||||||
default:
|
default:
|
||||||
return refs.ContainerID{}, unsupportedProtocolErr
|
return nil, unsupportedProtocolErr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) GetContainer(ctx context.Context, id refs.ContainerID, opts ...CallOption) (*container.Container, error) {
|
func (c Client) GetContainer(ctx context.Context, id *container.ID, opts ...CallOption) (*container.Container, error) {
|
||||||
switch c.remoteNode.Version.Major {
|
switch c.remoteNode.Version.Major {
|
||||||
case 2:
|
case 2:
|
||||||
return c.getContainerV2(ctx, id, opts...)
|
return c.getContainerV2(ctx, id, opts...)
|
||||||
|
@ -31,7 +31,7 @@ func (c Client) GetContainer(ctx context.Context, id refs.ContainerID, opts ...C
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) ListContainers(ctx context.Context, owner refs.NEO3Wallet, opts ...CallOption) ([]refs.ContainerID, error) {
|
func (c Client) ListContainers(ctx context.Context, owner refs.NEO3Wallet, opts ...CallOption) ([]*container.ID, error) {
|
||||||
switch c.remoteNode.Version.Major {
|
switch c.remoteNode.Version.Major {
|
||||||
case 2:
|
case 2:
|
||||||
return c.listContainerV2(ctx, owner, opts...)
|
return c.listContainerV2(ctx, owner, opts...)
|
||||||
|
@ -40,7 +40,7 @@ func (c Client) ListContainers(ctx context.Context, owner refs.NEO3Wallet, opts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) ListSelfContainers(ctx context.Context, opts ...CallOption) ([]refs.ContainerID, error) {
|
func (c Client) ListSelfContainers(ctx context.Context, opts ...CallOption) ([]*container.ID, error) {
|
||||||
owner, err := refs.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
owner, err := refs.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -49,7 +49,7 @@ func (c Client) ListSelfContainers(ctx context.Context, opts ...CallOption) ([]r
|
||||||
return c.ListContainers(ctx, owner, opts...)
|
return c.ListContainers(ctx, owner, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) DeleteContainer(ctx context.Context, id refs.ContainerID, opts ...CallOption) error {
|
func (c Client) DeleteContainer(ctx context.Context, id *container.ID, opts ...CallOption) error {
|
||||||
switch c.remoteNode.Version.Major {
|
switch c.remoteNode.Version.Major {
|
||||||
case 2:
|
case 2:
|
||||||
panic("not implemented")
|
panic("not implemented")
|
||||||
|
@ -61,15 +61,13 @@ func (c Client) DeleteContainer(ctx context.Context, id refs.ContainerID, opts .
|
||||||
// todo: func (c Client) GetExtendedACL
|
// todo: func (c Client) GetExtendedACL
|
||||||
// todo: func (c Client) SetExtendedACL
|
// todo: func (c Client) SetExtendedACL
|
||||||
|
|
||||||
func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, opts ...CallOption) (refs.ContainerID, error) {
|
func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, opts ...CallOption) (*container.ID, error) {
|
||||||
// apply all available options
|
// apply all available options
|
||||||
callOptions := defaultCallOptions()
|
callOptions := defaultCallOptions()
|
||||||
for i := range opts {
|
for i := range opts {
|
||||||
opts[i].apply(&callOptions)
|
opts[i].apply(&callOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
cid := refs.ContainerID{}
|
|
||||||
|
|
||||||
// set transport version
|
// set transport version
|
||||||
cnr.SetVersion(c.remoteNode.Version.ToV2Version())
|
cnr.SetVersion(c.remoteNode.Version.ToV2Version())
|
||||||
|
|
||||||
|
@ -77,7 +75,7 @@ func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, op
|
||||||
if cnr.GetOwnerID() == nil {
|
if cnr.GetOwnerID() == nil {
|
||||||
owner, err := refs.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
owner, err := refs.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
v2Owner := new(v2refs.OwnerID)
|
v2Owner := new(v2refs.OwnerID)
|
||||||
|
@ -97,7 +95,7 @@ func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, op
|
||||||
reqBody.SetSignature(containerSignature)
|
reqBody.SetSignature(containerSignature)
|
||||||
}, signature.SignWithRFC6979())
|
}, signature.SignWithRFC6979())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req := new(v2container.PutRequest)
|
req := new(v2container.PutRequest)
|
||||||
|
@ -106,46 +104,48 @@ func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, op
|
||||||
|
|
||||||
err = v2signature.SignServiceMessage(c.key, req)
|
err = v2signature.SignServiceMessage(c.key, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch c.remoteNode.Protocol {
|
switch c.remoteNode.Protocol {
|
||||||
case GRPC:
|
case GRPC:
|
||||||
cli, err := v2ContainerClientFromOptions(c.opts)
|
cli, err := v2ContainerClientFromOptions(c.opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid, errors.Wrap(err, "can't create grpc client")
|
return nil, errors.Wrap(err, "can't create grpc client")
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := cli.Put(ctx, req)
|
resp, err := cli.Put(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid, errors.Wrap(err, "transport error")
|
return nil, errors.Wrap(err, "transport error")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = v2signature.VerifyServiceMessage(resp)
|
err = v2signature.VerifyServiceMessage(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid, errors.Wrap(err, "can't verify response message")
|
return nil, errors.Wrap(err, "can't verify response message")
|
||||||
}
|
}
|
||||||
|
|
||||||
copy(cid[:], resp.GetBody().GetContainerID().GetValue())
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
return cid, nil
|
return cid, nil
|
||||||
default:
|
default:
|
||||||
return cid, unsupportedProtocolErr
|
return nil, unsupportedProtocolErr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) getContainerV2(ctx context.Context, id refs.ContainerID, opts ...CallOption) (*container.Container, error) {
|
func (c Client) getContainerV2(ctx context.Context, id *container.ID, opts ...CallOption) (*container.Container, error) {
|
||||||
// apply all available options
|
// apply all available options
|
||||||
callOptions := defaultCallOptions()
|
callOptions := defaultCallOptions()
|
||||||
for i := range opts {
|
for i := range opts {
|
||||||
opts[i].apply(&callOptions)
|
opts[i].apply(&callOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
cid := new(v2refs.ContainerID)
|
|
||||||
cid.SetValue(id[:])
|
|
||||||
|
|
||||||
reqBody := new(v2container.GetRequestBody)
|
reqBody := new(v2container.GetRequestBody)
|
||||||
reqBody.SetContainerID(cid)
|
reqBody.SetContainerID(id.ToV2())
|
||||||
|
|
||||||
req := new(v2container.GetRequest)
|
req := new(v2container.GetRequest)
|
||||||
req.SetBody(reqBody)
|
req.SetBody(reqBody)
|
||||||
|
@ -181,7 +181,7 @@ func (c Client) getContainerV2(ctx context.Context, id refs.ContainerID, opts ..
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) listContainerV2(ctx context.Context, owner refs.NEO3Wallet, opts ...CallOption) ([]refs.ContainerID, error) {
|
func (c Client) listContainerV2(ctx context.Context, owner refs.NEO3Wallet, opts ...CallOption) ([]*container.ID, error) {
|
||||||
// apply all available options
|
// apply all available options
|
||||||
callOptions := defaultCallOptions()
|
callOptions := defaultCallOptions()
|
||||||
for i := range opts {
|
for i := range opts {
|
||||||
|
@ -220,9 +220,14 @@ func (c Client) listContainerV2(ctx context.Context, owner refs.NEO3Wallet, opts
|
||||||
return nil, errors.Wrap(err, "can't verify response message")
|
return nil, errors.Wrap(err, "can't verify response message")
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]refs.ContainerID, len(resp.GetBody().GetContainerIDs()))
|
result := make([]*container.ID, 0, len(resp.GetBody().GetContainerIDs()))
|
||||||
for i, cid := range resp.GetBody().GetContainerIDs() {
|
for _, cidV2 := range resp.GetBody().GetContainerIDs() {
|
||||||
copy(result[i][:], cid.GetValue())
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
|
@ -231,18 +236,15 @@ func (c Client) listContainerV2(ctx context.Context, owner refs.NEO3Wallet, opts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) delContainerV2(ctx context.Context, id refs.ContainerID, opts ...CallOption) error {
|
func (c Client) delContainerV2(ctx context.Context, id *container.ID, opts ...CallOption) error {
|
||||||
// apply all available options
|
// apply all available options
|
||||||
callOptions := defaultCallOptions()
|
callOptions := defaultCallOptions()
|
||||||
for i := range opts {
|
for i := range opts {
|
||||||
opts[i].apply(&callOptions)
|
opts[i].apply(&callOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
cid := new(v2refs.ContainerID)
|
|
||||||
cid.SetValue(id[:])
|
|
||||||
|
|
||||||
reqBody := new(v2container.DeleteRequestBody)
|
reqBody := new(v2container.DeleteRequestBody)
|
||||||
reqBody.SetContainerID(cid)
|
reqBody.SetContainerID(id.ToV2())
|
||||||
|
|
||||||
// sign container
|
// sign container
|
||||||
signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetContainerID()}
|
signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetContainerID()}
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
package refs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/sha256"
|
|
||||||
|
|
||||||
"github.com/mr-tron/base58"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
ContainerID [sha256.Size]byte
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c ContainerID) String() string {
|
|
||||||
return base58.Encode(c[:])
|
|
||||||
}
|
|
Loading…
Reference in a new issue