[#165] pool: replace options with parameters

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-03-11 16:06:08 +03:00 committed by Alex Vanin
parent ec5c223f29
commit d03523a3bc
2 changed files with 331 additions and 170 deletions

View file

@ -103,60 +103,222 @@ type clientPack struct {
address string address string
} }
type CallOption func(config *callConfig) type prmCommon struct {
defaultSession bool
type callConfig struct { verb sessionv2.ObjectSessionVerb
useDefaultSession bool addr *address.Address
verb sessionv2.ObjectSessionVerb
addr *address.Address
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
btoken *token.BearerToken btoken *token.BearerToken
stoken *session.Token stoken *session.Token
} }
func WithKey(key *ecdsa.PrivateKey) CallOption { func (x *prmCommon) useDefaultSession() {
return func(config *callConfig) { x.defaultSession = true
config.key = key
}
} }
func WithBearer(token *token.BearerToken) CallOption { func (x *prmCommon) useAddress(addr *address.Address) {
return func(config *callConfig) { x.addr = addr
config.btoken = token
}
} }
func WithSession(token *session.Token) CallOption { func (x *prmCommon) useVerb(verb sessionv2.ObjectSessionVerb) {
return func(config *callConfig) { x.verb = verb
config.stoken = token
}
} }
func useDefaultSession() CallOption { // UseKey specifies private key to sign the requests.
return func(config *callConfig) { // If key is not provided, then Pool default key is used.
config.useDefaultSession = true func (x *prmCommon) UseKey(key *ecdsa.PrivateKey) {
} x.key = key
} }
func useAddress(addr *address.Address) CallOption { // UseBearer attaches bearer token to be used for the operation.
return func(config *callConfig) { func (x *prmCommon) UseBearer(token *token.BearerToken) {
config.addr = addr x.btoken = token
}
} }
func useVerb(verb sessionv2.ObjectSessionVerb) CallOption { // UseSession specifies session within which object should be read.
return func(config *callConfig) { func (x *prmCommon) UseSession(token *session.Token) {
config.verb = verb x.stoken = token
}
} }
func cfgFromOpts(opts ...CallOption) *callConfig { // PrmObjectPut groups parameters of PutObject operation.
var cfg = new(callConfig) type PrmObjectPut struct {
for _, opt := range opts { prmCommon
opt(cfg)
} hdr object.Object
return cfg
payload io.Reader
}
// SetHeader specifies header of the object.
func (x *PrmObjectPut) SetHeader(hdr object.Object) {
x.hdr = hdr
}
// SetPayload specifies payload of the object.
func (x *PrmObjectPut) SetPayload(payload io.Reader) {
x.payload = payload
}
// PrmObjectDelete groups parameters of DeleteObject operation.
type PrmObjectDelete struct {
prmCommon
addr address.Address
}
// SetAddress specifies NeoFS address of the object.
func (x *PrmObjectDelete) SetAddress(addr address.Address) {
x.addr = addr
}
// PrmObjectGet groups parameters of GetObject operation.
type PrmObjectGet struct {
prmCommon
addr address.Address
}
// SetAddress specifies NeoFS address of the object.
func (x *PrmObjectGet) SetAddress(addr address.Address) {
x.addr = addr
}
// PrmObjectHead groups parameters of HeadObject operation.
type PrmObjectHead struct {
prmCommon
addr address.Address
}
// SetAddress specifies NeoFS address of the object.
func (x *PrmObjectHead) SetAddress(addr address.Address) {
x.addr = addr
}
// PrmObjectRange groups parameters of RangeObject operation.
type PrmObjectRange struct {
prmCommon
addr address.Address
off, ln uint64
}
// SetAddress specifies NeoFS address of the object.
func (x *PrmObjectRange) SetAddress(addr address.Address) {
x.addr = addr
}
// SetOffset sets offset of the payload range to be read.
func (x *PrmObjectRange) SetOffset(offset uint64) {
x.off = offset
}
// SetLength sets length of the payload range to be read.
func (x *PrmObjectRange) SetLength(length uint64) {
x.ln = length
}
// PrmObjectSearch groups parameters of SearchObjects operation.
type PrmObjectSearch struct {
prmCommon
cnrID cid.ID
filters object.SearchFilters
}
// SetContainerID specifies the container in which to look for objects.
func (x *PrmObjectSearch) SetContainerID(cnrID cid.ID) {
x.cnrID = cnrID
}
// SetFilters specifies filters by which to select objects.
func (x *PrmObjectSearch) SetFilters(filters object.SearchFilters) {
x.filters = filters
}
// PrmContainerPut groups parameters of PutContainer operation.
type PrmContainerPut struct {
prmCommon
cnr *container.Container
}
// SetContainer specifies structured information about new NeoFS container.
func (x *PrmContainerPut) SetContainer(cnr *container.Container) {
x.cnr = cnr
}
// PrmContainerGet groups parameters of GetContainer operation.
type PrmContainerGet struct {
prmCommon
cnrID *cid.ID
}
// SetContainerID specifies identifier of the container to be read.
func (x *PrmContainerGet) SetContainerID(cnrID *cid.ID) {
x.cnrID = cnrID
}
// PrmContainerList groups parameters of ListContainers operation.
type PrmContainerList struct {
prmCommon
ownerID *owner.ID
}
// SetOwnerID specifies identifier of the NeoFS account to list the containers.
func (x *PrmContainerList) SetOwnerID(ownerID *owner.ID) {
x.ownerID = ownerID
}
// PrmContainerDelete groups parameters of DeleteContainer operation.
type PrmContainerDelete struct {
prmCommon
cnrID *cid.ID
}
// SetContainerID specifies identifier of the NeoFS container to be removed.
func (x *PrmContainerDelete) SetContainerID(cnrID *cid.ID) {
x.cnrID = cnrID
}
// PrmContainerEACL groups parameters of GetEACL operation.
type PrmContainerEACL struct {
prmCommon
cnrID *cid.ID
}
// SetContainerID specifies identifier of the NeoFS container to read the eACL table.
func (x *PrmContainerEACL) SetContainerID(cnrID *cid.ID) {
x.cnrID = cnrID
}
// PrmContainerSetEACL groups parameters of SetEACL operation.
type PrmContainerSetEACL struct {
prmCommon
table *eacl.Table
}
// SetTable specifies eACL table structure to be set for the container.
func (x *PrmContainerSetEACL) SetTable(table *eacl.Table) {
x.table = table
}
// PrmBalanceGet groups parameters of Balance operation.
type PrmBalanceGet struct {
prmCommon
ownerID *owner.ID
}
// SetOwnerID specifies identifier of the NeoFS account for which the balance is requested.
func (x *PrmBalanceGet) SetOwnerID(ownerID *owner.ID) {
x.ownerID = ownerID
} }
type Pool struct { type Pool struct {
@ -490,7 +652,7 @@ func formCacheKey(address string, key *ecdsa.PrivateKey) string {
return address + k.String() return address + k.String()
} }
func (p *Pool) conn(ctx context.Context, cfg *callConfig) (*clientPack, error) { func (p *Pool) conn(ctx context.Context, cfg prmCommon) (*clientPack, error) {
cp, err := p.connection() cp, err := p.connection()
if err != nil { if err != nil {
return nil, err return nil, err
@ -502,7 +664,7 @@ func (p *Pool) conn(ctx context.Context, cfg *callConfig) (*clientPack, error) {
} }
sessionToken := cfg.stoken sessionToken := cfg.stoken
if sessionToken == nil && cfg.useDefaultSession { if sessionToken == nil && cfg.defaultSession {
cacheKey := formCacheKey(cp.address, key) cacheKey := formCacheKey(cp.address, key)
sessionToken = p.cache.Get(cacheKey) sessionToken = p.cache.Get(cacheKey)
if sessionToken == nil { if sessionToken == nil {
@ -555,7 +717,7 @@ func createSessionTokenForDuration(ctx context.Context, c Client, dur uint64) (*
return c.SessionCreate(ctx, prm) return c.SessionCreate(ctx, prm)
} }
func (p *Pool) removeSessionTokenAfterThreshold(cfg *callConfig) error { func (p *Pool) removeSessionTokenAfterThreshold(cfg prmCommon) error {
cp, err := p.connection() cp, err := p.connection()
if err != nil { if err != nil {
return err return err
@ -592,7 +754,7 @@ type callContext struct {
sessionContext *session.ObjectContext sessionContext *session.ObjectContext
} }
func (p *Pool) initCallContext(ctx *callContext, cfg *callConfig) error { func (p *Pool) initCallContext(ctx *callContext, cfg prmCommon) error {
cp, err := p.connection() cp, err := p.connection()
if err != nil { if err != nil {
return err return err
@ -612,7 +774,7 @@ func (p *Pool) initCallContext(ctx *callContext, cfg *callConfig) error {
} }
// note that we don't override session provided by the caller // note that we don't override session provided by the caller
ctx.sessionDefault = cfg.stoken == nil && cfg.useDefaultSession ctx.sessionDefault = cfg.stoken == nil && cfg.defaultSession
if ctx.sessionDefault { if ctx.sessionDefault {
ctx.sessionContext = session.NewObjectContext() ctx.sessionContext = session.NewObjectContext()
ctx.sessionContext.ToV2().SetVerb(cfg.verb) ctx.sessionContext.ToV2().SetVerb(cfg.verb)
@ -628,7 +790,7 @@ type callContextWithRetry struct {
noRetry bool noRetry bool
} }
func (p *Pool) initCallContextWithRetry(ctx *callContextWithRetry, cfg *callConfig) error { func (p *Pool) initCallContextWithRetry(ctx *callContextWithRetry, cfg prmCommon) error {
err := p.initCallContext(&ctx.callContext, cfg) err := p.initCallContext(&ctx.callContext, cfg)
if err != nil { if err != nil {
return err return err
@ -696,11 +858,10 @@ func (p *Pool) callWithRetry(ctx *callContextWithRetry, f func() error) error {
return err return err
} }
func (p *Pool) PutObject(ctx context.Context, hdr object.Object, payload io.Reader, opts ...CallOption) (*oid.ID, error) { func (p *Pool) PutObject(ctx context.Context, prm PrmObjectPut) (*oid.ID, error) {
cfg := cfgFromOpts(append(opts, prm.useDefaultSession()
useDefaultSession(), prm.useVerb(sessionv2.ObjectVerbPut)
useVerb(sessionv2.ObjectVerbPut), prm.useAddress(newAddressFromCnrID(prm.hdr.ContainerID()))
useAddress(newAddressFromCnrID(hdr.ContainerID())))...)
// Put object is different from other object service methods. Put request // Put object is different from other object service methods. Put request
// can't be resent in case of session token failures (i.e. session token is // can't be resent in case of session token failures (i.e. session token is
@ -712,7 +873,7 @@ func (p *Pool) PutObject(ctx context.Context, hdr object.Object, payload io.Read
// by checking when the session token was accessed for the last time. If it // by checking when the session token was accessed for the last time. If it
// hits a threshold, session token is removed from cache for a new one to be // hits a threshold, session token is removed from cache for a new one to be
// issued. // issued.
err := p.removeSessionTokenAfterThreshold(cfg) err := p.removeSessionTokenAfterThreshold(prm.prmCommon)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -721,14 +882,14 @@ func (p *Pool) PutObject(ctx context.Context, hdr object.Object, payload io.Read
ctxCall.Context = ctx ctxCall.Context = ctx
err = p.initCallContext(&ctxCall, cfg) err = p.initCallContext(&ctxCall, prm.prmCommon)
if err != nil { if err != nil {
return nil, fmt.Errorf("init call context") return nil, fmt.Errorf("init call context")
} }
var prm client.PrmObjectPutInit var cliPrm client.PrmObjectPutInit
wObj, err := ctxCall.client.ObjectPutInit(ctx, prm) wObj, err := ctxCall.client.ObjectPutInit(ctx, cliPrm)
if err != nil { if err != nil {
return nil, fmt.Errorf("init writing on API client: %w", err) return nil, fmt.Errorf("init writing on API client: %w", err)
} }
@ -743,23 +904,23 @@ func (p *Pool) PutObject(ctx context.Context, hdr object.Object, payload io.Read
wObj.UseKey(*ctxCall.key) wObj.UseKey(*ctxCall.key)
if cfg.btoken != nil { if prm.btoken != nil {
wObj.WithBearerToken(*cfg.btoken) wObj.WithBearerToken(*prm.btoken)
} }
if wObj.WriteHeader(hdr) { if wObj.WriteHeader(prm.hdr) {
sz := hdr.PayloadSize() sz := prm.hdr.PayloadSize()
if data := hdr.Payload(); len(data) > 0 { if data := prm.hdr.Payload(); len(data) > 0 {
if payload != nil { if prm.payload != nil {
payload = io.MultiReader(bytes.NewReader(data), payload) prm.payload = io.MultiReader(bytes.NewReader(data), prm.payload)
} else { } else {
payload = bytes.NewReader(data) prm.payload = bytes.NewReader(data)
sz = uint64(len(data)) sz = uint64(len(data))
} }
} }
if payload != nil { if prm.payload != nil {
const defaultBufferSizePut = 3 << 20 // configure? const defaultBufferSizePut = 3 << 20 // configure?
if sz == 0 || sz > defaultBufferSizePut { if sz == 0 || sz > defaultBufferSizePut {
@ -771,7 +932,7 @@ func (p *Pool) PutObject(ctx context.Context, hdr object.Object, payload io.Read
var n int var n int
for { for {
n, err = payload.Read(buf) n, err = prm.payload.Read(buf)
if n > 0 { if n > 0 {
if !wObj.WritePayloadChunk(buf[:n]) { if !wObj.WritePayloadChunk(buf[:n]) {
break break
@ -805,36 +966,35 @@ func (p *Pool) PutObject(ctx context.Context, hdr object.Object, payload io.Read
return &id, nil return &id, nil
} }
func (p *Pool) DeleteObject(ctx context.Context, addr address.Address, opts ...CallOption) error { func (p *Pool) DeleteObject(ctx context.Context, prm PrmObjectDelete) error {
cfg := cfgFromOpts(append(opts, prm.useDefaultSession()
useDefaultSession(), prm.useVerb(sessionv2.ObjectVerbDelete)
useVerb(sessionv2.ObjectVerbDelete), prm.useAddress(&prm.addr)
useAddress(&addr))...)
var prm client.PrmObjectDelete var cliPrm client.PrmObjectDelete
var cc callContextWithRetry var cc callContextWithRetry
cc.Context = ctx cc.Context = ctx
cc.sessionTarget = prm.WithinSession cc.sessionTarget = cliPrm.WithinSession
err := p.initCallContextWithRetry(&cc, cfg) err := p.initCallContextWithRetry(&cc, prm.prmCommon)
if err != nil { if err != nil {
return err return err
} }
if cnr := addr.ContainerID(); cnr != nil { if cnr := prm.addr.ContainerID(); cnr != nil {
prm.FromContainer(*cnr) cliPrm.FromContainer(*cnr)
} }
if obj := addr.ObjectID(); obj != nil { if obj := prm.addr.ObjectID(); obj != nil {
prm.ByID(*obj) cliPrm.ByID(*obj)
} }
prm.UseKey(*cc.key) cliPrm.UseKey(*cc.key)
return p.callWithRetry(&cc, func() error { return p.callWithRetry(&cc, func() error {
_, err := cc.client.ObjectDelete(ctx, prm) _, err := cc.client.ObjectDelete(ctx, cliPrm)
if err != nil { if err != nil {
return fmt.Errorf("remove object via client: %w", err) return fmt.Errorf("remove object via client: %w", err)
} }
@ -860,36 +1020,35 @@ type ResGetObject struct {
Payload io.ReadCloser Payload io.ReadCloser
} }
func (p *Pool) GetObject(ctx context.Context, addr address.Address, opts ...CallOption) (*ResGetObject, error) { func (p *Pool) GetObject(ctx context.Context, prm PrmObjectGet) (*ResGetObject, error) {
cfg := cfgFromOpts(append(opts, prm.useDefaultSession()
useDefaultSession(), prm.useVerb(sessionv2.ObjectVerbGet)
useVerb(sessionv2.ObjectVerbGet), prm.useAddress(&prm.addr)
useAddress(&addr))...)
var prm client.PrmObjectGet var cliPrm client.PrmObjectGet
var cc callContextWithRetry var cc callContextWithRetry
cc.Context = ctx cc.Context = ctx
cc.sessionTarget = prm.WithinSession cc.sessionTarget = cliPrm.WithinSession
err := p.initCallContextWithRetry(&cc, cfg) err := p.initCallContextWithRetry(&cc, prm.prmCommon)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if cnr := addr.ContainerID(); cnr != nil { if cnr := prm.addr.ContainerID(); cnr != nil {
prm.FromContainer(*cnr) cliPrm.FromContainer(*cnr)
} }
if obj := addr.ObjectID(); obj != nil { if obj := prm.addr.ObjectID(); obj != nil {
prm.ByID(*obj) cliPrm.ByID(*obj)
} }
var res ResGetObject var res ResGetObject
err = p.callWithRetry(&cc, func() error { err = p.callWithRetry(&cc, func() error {
rObj, err := cc.client.ObjectGetInit(ctx, prm) rObj, err := cc.client.ObjectGetInit(ctx, cliPrm)
if err != nil { if err != nil {
return fmt.Errorf("init object reading on client: %w", err) return fmt.Errorf("init object reading on client: %w", err)
} }
@ -912,38 +1071,37 @@ func (p *Pool) GetObject(ctx context.Context, addr address.Address, opts ...Call
return &res, nil return &res, nil
} }
func (p *Pool) HeadObject(ctx context.Context, addr address.Address, opts ...CallOption) (*object.Object, error) { func (p *Pool) HeadObject(ctx context.Context, prm PrmObjectHead) (*object.Object, error) {
cfg := cfgFromOpts(append(opts, prm.useDefaultSession()
useDefaultSession(), prm.useVerb(sessionv2.ObjectVerbHead)
useVerb(sessionv2.ObjectVerbHead), prm.useAddress(&prm.addr)
useAddress(&addr))...)
var prm client.PrmObjectHead var cliPrm client.PrmObjectHead
var cc callContextWithRetry var cc callContextWithRetry
cc.Context = ctx cc.Context = ctx
cc.sessionTarget = prm.WithinSession cc.sessionTarget = cliPrm.WithinSession
err := p.initCallContextWithRetry(&cc, cfg) err := p.initCallContextWithRetry(&cc, prm.prmCommon)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if cnr := addr.ContainerID(); cnr != nil { if cnr := prm.addr.ContainerID(); cnr != nil {
prm.FromContainer(*cnr) cliPrm.FromContainer(*cnr)
} }
if obj := addr.ObjectID(); obj != nil { if obj := prm.addr.ObjectID(); obj != nil {
prm.ByID(*obj) cliPrm.ByID(*obj)
} }
prm.UseKey(*cc.key) cliPrm.UseKey(*cc.key)
var obj object.Object var obj object.Object
err = p.callWithRetry(&cc, func() error { err = p.callWithRetry(&cc, func() error {
res, err := cc.client.ObjectHead(ctx, prm) res, err := cc.client.ObjectHead(ctx, cliPrm)
if err != nil { if err != nil {
return fmt.Errorf("read object header via client: %w", err) return fmt.Errorf("read object header via client: %w", err)
} }
@ -974,33 +1132,32 @@ func (x *ResObjectRange) Close() error {
return err return err
} }
func (p *Pool) ObjectRange(ctx context.Context, addr address.Address, off, ln uint64, opts ...CallOption) (*ResObjectRange, error) { func (p *Pool) ObjectRange(ctx context.Context, prm PrmObjectRange) (*ResObjectRange, error) {
cfg := cfgFromOpts(append(opts, prm.useDefaultSession()
useDefaultSession(), prm.useVerb(sessionv2.ObjectVerbRange)
useVerb(sessionv2.ObjectVerbRange), prm.useAddress(&prm.addr)
useAddress(&addr))...)
var prm client.PrmObjectRange var cliPrm client.PrmObjectRange
prm.SetOffset(off) cliPrm.SetOffset(prm.off)
prm.SetLength(ln) cliPrm.SetLength(prm.ln)
var cc callContextWithRetry var cc callContextWithRetry
cc.Context = ctx cc.Context = ctx
cc.sessionTarget = prm.WithinSession cc.sessionTarget = cliPrm.WithinSession
err := p.initCallContextWithRetry(&cc, cfg) err := p.initCallContextWithRetry(&cc, prm.prmCommon)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if cnr := addr.ContainerID(); cnr != nil { if cnr := prm.addr.ContainerID(); cnr != nil {
prm.FromContainer(*cnr) cliPrm.FromContainer(*cnr)
} }
if obj := addr.ObjectID(); obj != nil { if obj := prm.addr.ObjectID(); obj != nil {
prm.ByID(*obj) cliPrm.ByID(*obj)
} }
var res ResObjectRange var res ResObjectRange
@ -1008,7 +1165,7 @@ func (p *Pool) ObjectRange(ctx context.Context, addr address.Address, off, ln ui
err = p.callWithRetry(&cc, func() error { err = p.callWithRetry(&cc, func() error {
var err error var err error
res.payload, err = cc.client.ObjectRangeInit(ctx, prm) res.payload, err = cc.client.ObjectRangeInit(ctx, cliPrm)
if err != nil { if err != nil {
return fmt.Errorf("init payload range reading on client: %w", err) return fmt.Errorf("init payload range reading on client: %w", err)
} }
@ -1050,23 +1207,22 @@ func (x *ResObjectSearch) Close() {
_, _ = x.r.Close() _, _ = x.r.Close()
} }
func (p *Pool) SearchObjects(ctx context.Context, idCnr cid.ID, filters object.SearchFilters, opts ...CallOption) (*ResObjectSearch, error) { func (p *Pool) SearchObjects(ctx context.Context, prm PrmObjectSearch) (*ResObjectSearch, error) {
cfg := cfgFromOpts(append(opts, prm.useDefaultSession()
useDefaultSession(), prm.useVerb(sessionv2.ObjectVerbSearch)
useVerb(sessionv2.ObjectVerbSearch), prm.useAddress(newAddressFromCnrID(&prm.cnrID))
useAddress(newAddressFromCnrID(&idCnr)))...)
var prm client.PrmObjectSearch var cliPrm client.PrmObjectSearch
prm.InContainer(idCnr) cliPrm.InContainer(prm.cnrID)
prm.SetFilters(filters) cliPrm.SetFilters(prm.filters)
var cc callContextWithRetry var cc callContextWithRetry
cc.Context = ctx cc.Context = ctx
cc.sessionTarget = prm.WithinSession cc.sessionTarget = cliPrm.WithinSession
err := p.initCallContextWithRetry(&cc, cfg) err := p.initCallContextWithRetry(&cc, prm.prmCommon)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1076,7 +1232,7 @@ func (p *Pool) SearchObjects(ctx context.Context, idCnr cid.ID, filters object.S
err = p.callWithRetry(&cc, func() error { err = p.callWithRetry(&cc, func() error {
var err error var err error
res.r, err = cc.client.ObjectSearchInit(ctx, prm) res.r, err = cc.client.ObjectSearchInit(ctx, cliPrm)
if err != nil { if err != nil {
return fmt.Errorf("init object searching on client: %w", err) return fmt.Errorf("init object searching on client: %w", err)
} }
@ -1092,17 +1248,16 @@ func (p *Pool) SearchObjects(ctx context.Context, idCnr cid.ID, filters object.S
return &res, nil return &res, nil
} }
func (p *Pool) PutContainer(ctx context.Context, cnr *container.Container, opts ...CallOption) (*cid.ID, error) { func (p *Pool) PutContainer(ctx context.Context, prm PrmContainerPut) (*cid.ID, error) {
cfg := cfgFromOpts(opts...) cp, err := p.conn(ctx, prm.prmCommon)
cp, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var cliPrm client.PrmContainerPut var cliPrm client.PrmContainerPut
if cnr != nil { if prm.cnr != nil {
cliPrm.SetContainer(*cnr) cliPrm.SetContainer(*prm.cnr)
} }
res, err := cp.client.ContainerPut(ctx, cliPrm) res, err := cp.client.ContainerPut(ctx, cliPrm)
@ -1113,17 +1268,16 @@ func (p *Pool) PutContainer(ctx context.Context, cnr *container.Container, opts
return res.ID(), nil return res.ID(), nil
} }
func (p *Pool) GetContainer(ctx context.Context, cid *cid.ID, opts ...CallOption) (*container.Container, error) { func (p *Pool) GetContainer(ctx context.Context, prm PrmContainerGet) (*container.Container, error) {
cfg := cfgFromOpts(opts...) cp, err := p.conn(ctx, prm.prmCommon)
cp, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var cliPrm client.PrmContainerGet var cliPrm client.PrmContainerGet
if cid != nil { if prm.cnrID != nil {
cliPrm.SetContainer(*cid) cliPrm.SetContainer(*prm.cnrID)
} }
res, err := cp.client.ContainerGet(ctx, cliPrm) res, err := cp.client.ContainerGet(ctx, cliPrm)
@ -1134,17 +1288,16 @@ func (p *Pool) GetContainer(ctx context.Context, cid *cid.ID, opts ...CallOption
return res.Container(), nil return res.Container(), nil
} }
func (p *Pool) ListContainers(ctx context.Context, ownerID *owner.ID, opts ...CallOption) ([]cid.ID, error) { func (p *Pool) ListContainers(ctx context.Context, prm PrmContainerList) ([]cid.ID, error) {
cfg := cfgFromOpts(opts...) cp, err := p.conn(ctx, prm.prmCommon)
cp, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var cliPrm client.PrmContainerList var cliPrm client.PrmContainerList
if ownerID != nil { if prm.ownerID != nil {
cliPrm.SetAccount(*ownerID) cliPrm.SetAccount(*prm.ownerID)
} }
res, err := cp.client.ContainerList(ctx, cliPrm) res, err := cp.client.ContainerList(ctx, cliPrm)
@ -1155,21 +1308,20 @@ func (p *Pool) ListContainers(ctx context.Context, ownerID *owner.ID, opts ...Ca
return res.Containers(), nil return res.Containers(), nil
} }
func (p *Pool) DeleteContainer(ctx context.Context, cid *cid.ID, opts ...CallOption) error { func (p *Pool) DeleteContainer(ctx context.Context, prm PrmContainerDelete) error {
cfg := cfgFromOpts(opts...) cp, err := p.conn(ctx, prm.prmCommon)
cp, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return err return err
} }
var cliPrm client.PrmContainerDelete var cliPrm client.PrmContainerDelete
if cid != nil { if prm.cnrID != nil {
cliPrm.SetContainer(*cid) cliPrm.SetContainer(*prm.cnrID)
} }
if cfg.stoken != nil { if prm.stoken != nil {
cliPrm.SetSessionToken(*cfg.stoken) cliPrm.SetSessionToken(*prm.stoken)
} }
_, err = cp.client.ContainerDelete(ctx, cliPrm) _, err = cp.client.ContainerDelete(ctx, cliPrm)
@ -1179,17 +1331,16 @@ func (p *Pool) DeleteContainer(ctx context.Context, cid *cid.ID, opts ...CallOpt
return err return err
} }
func (p *Pool) GetEACL(ctx context.Context, cid *cid.ID, opts ...CallOption) (*eacl.Table, error) { func (p *Pool) GetEACL(ctx context.Context, prm PrmContainerEACL) (*eacl.Table, error) {
cfg := cfgFromOpts(opts...) cp, err := p.conn(ctx, prm.prmCommon)
cp, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var cliPrm client.PrmContainerEACL var cliPrm client.PrmContainerEACL
if cid != nil { if prm.cnrID != nil {
cliPrm.SetContainer(*cid) cliPrm.SetContainer(*prm.cnrID)
} }
res, err := cp.client.ContainerEACL(ctx, cliPrm) res, err := cp.client.ContainerEACL(ctx, cliPrm)
@ -1200,17 +1351,16 @@ func (p *Pool) GetEACL(ctx context.Context, cid *cid.ID, opts ...CallOption) (*e
return res.Table(), nil return res.Table(), nil
} }
func (p *Pool) SetEACL(ctx context.Context, table *eacl.Table, opts ...CallOption) error { func (p *Pool) SetEACL(ctx context.Context, prm PrmContainerSetEACL) error {
cfg := cfgFromOpts(opts...) cp, err := p.conn(ctx, prm.prmCommon)
cp, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return err return err
} }
var cliPrm client.PrmContainerSetEACL var cliPrm client.PrmContainerSetEACL
if table != nil { if prm.table != nil {
cliPrm.SetTable(*table) cliPrm.SetTable(*prm.table)
} }
_, err = cp.client.ContainerSetEACL(ctx, cliPrm) _, err = cp.client.ContainerSetEACL(ctx, cliPrm)
@ -1220,17 +1370,16 @@ func (p *Pool) SetEACL(ctx context.Context, table *eacl.Table, opts ...CallOptio
return err return err
} }
func (p *Pool) Balance(ctx context.Context, o *owner.ID, opts ...CallOption) (*accounting.Decimal, error) { func (p *Pool) Balance(ctx context.Context, prm PrmBalanceGet) (*accounting.Decimal, error) {
cfg := cfgFromOpts(opts...) cp, err := p.conn(ctx, prm.prmCommon)
cp, err := p.conn(ctx, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var cliPrm client.PrmBalanceGet var cliPrm client.PrmBalanceGet
if o != nil { if prm.ownerID != nil {
cliPrm.SetAccount(*o) cliPrm.SetAccount(*prm.ownerID)
} }
res, err := cp.client.BalanceGet(ctx, cliPrm) res, err := cp.client.BalanceGet(ctx, cliPrm)

View file

@ -351,7 +351,10 @@ func TestSessionCache(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Contains(t, tokens, st) require.Contains(t, tokens, st)
_, err = pool.GetObject(ctx, address.Address{}) var prm PrmObjectGet
prm.SetAddress(address.Address{})
_, err = pool.GetObject(ctx, prm)
require.Error(t, err) require.Error(t, err)
// cache must not contain session token // cache must not contain session token
@ -359,7 +362,10 @@ func TestSessionCache(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Nil(t, st) require.Nil(t, st)
_, err = pool.PutObject(ctx, object.Object{}, nil) var prm2 PrmObjectPut
prm2.SetHeader(object.Object{})
_, err = pool.PutObject(ctx, prm2)
require.NoError(t, err) require.NoError(t, err)
// cache must contain session token // cache must contain session token
@ -477,7 +483,11 @@ func TestSessionCacheWithKey(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Contains(t, tokens, st) require.Contains(t, tokens, st)
_, err = pool.GetObject(ctx, address.Address{}, WithKey(newPrivateKey(t))) var prm PrmObjectGet
prm.SetAddress(address.Address{})
prm.UseKey(newPrivateKey(t))
_, err = pool.GetObject(ctx, prm)
require.NoError(t, err) require.NoError(t, err)
require.Len(t, tokens, 2) require.Len(t, tokens, 2)
} }
@ -519,8 +529,10 @@ func TestSessionTokenOwner(t *testing.T) {
anonKey := newPrivateKey(t) anonKey := newPrivateKey(t)
anonOwner := owner.NewIDFromPublicKey(&anonKey.PublicKey) anonOwner := owner.NewIDFromPublicKey(&anonKey.PublicKey)
cfg := cfgFromOpts(WithKey(anonKey), useDefaultSession()) var prm prmCommon
cp, err := p.conn(ctx, cfg) prm.UseKey(anonKey)
prm.useDefaultSession()
cp, err := p.conn(ctx, prm)
require.NoError(t, err) require.NoError(t, err)
tkn := p.cache.Get(formCacheKey(cp.address, anonKey)) tkn := p.cache.Get(formCacheKey(cp.address, anonKey))