[#105] pool: Add balance command

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-12-17 13:36:07 +03:00 committed by Alex Vanin
parent b1770ecb92
commit 2fd5802c48

View file

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neofs-sdk-go/accounting"
"github.com/nspcc-dev/neofs-sdk-go/client" "github.com/nspcc-dev/neofs-sdk-go/client"
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
"github.com/nspcc-dev/neofs-sdk-go/container" "github.com/nspcc-dev/neofs-sdk-go/container"
@ -122,6 +123,7 @@ func (pb *Builder) Build(ctx context.Context, options *BuilderOptions) (Pool, er
type Pool interface { type Pool interface {
Object Object
Container Container
Accounting
Connection() (client.Client, *session.Token, error) Connection() (client.Client, *session.Token, error)
OwnerID() *owner.ID OwnerID() *owner.ID
WaitForContainerPresence(context.Context, *cid.ID, *ContainerPollingParams) error WaitForContainerPresence(context.Context, *cid.ID, *ContainerPollingParams) error
@ -149,6 +151,10 @@ type Container interface {
AnnounceContainerUsedSpace(ctx context.Context, announce []container.UsedSpaceAnnouncement, opts ...CallOption) error AnnounceContainerUsedSpace(ctx context.Context, announce []container.UsedSpaceAnnouncement, opts ...CallOption) error
} }
type Accounting interface {
Balance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error)
}
type clientPack struct { type clientPack struct {
client client.Client client client.Client
healthy bool healthy bool
@ -891,6 +897,26 @@ func (p *pool) AnnounceContainerUsedSpace(ctx context.Context, announce []contai
return err return err
} }
func (p *pool) Balance(ctx context.Context, o *owner.ID, opts ...CallOption) (*accounting.Decimal, error) {
cfg := cfgFromOpts(opts...)
cp, options, err := p.conn(ctx, cfg)
if err != nil {
return nil, err
}
res, err := cp.client.GetBalance(ctx, o, options...)
if err == nil {
// reflect status failures in err
err = apistatus.ErrFromStatus(res.Status())
}
if err != nil {
return nil, err
}
return res.Amount(), nil
}
func (p *pool) WaitForContainerPresence(ctx context.Context, cid *cid.ID, pollParams *ContainerPollingParams) error { func (p *pool) WaitForContainerPresence(ctx context.Context, cid *cid.ID, pollParams *ContainerPollingParams) error {
conn, _, err := p.Connection() conn, _, err := p.Connection()
if err != nil { if err != nil {