[#243] services/object: Share common parameters across services

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-12-11 14:59:16 +03:00 committed by Alex Vanin
parent a01262d8bd
commit fb50362dcc
12 changed files with 89 additions and 163 deletions

View file

@ -60,7 +60,7 @@ func (exec execCtx) isLocal() bool {
}
func (exec *execCtx) key() *ecdsa.PrivateKey {
return exec.prm.key
return exec.prm.common.PrivateKey()
}
func (exec *execCtx) address() *objectSDK.Address {
@ -76,7 +76,7 @@ func (exec *execCtx) commonParameters() *util.CommonPrm {
}
func (exec execCtx) callOptions() []client.CallOption {
return exec.prm.callOpts
return exec.prm.common.RemoteCallOptions()
}
func (exec *execCtx) newAddress(id *objectSDK.ID) *objectSDK.Address {

View file

@ -1,20 +1,14 @@
package deletesvc
import (
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
)
// Prm groups parameters of Delete service call.
type Prm struct {
key *ecdsa.PrivateKey
common *util.CommonPrm
callOpts []client.CallOption
client.DeleteObjectParams
}
@ -22,13 +16,3 @@ type Prm struct {
func (p *Prm) SetCommonParameters(common *util.CommonPrm) {
p.common = common
}
// SetPrivateKey sets private key to use during execution.
func (p *Prm) SetPrivateKey(key *ecdsa.PrivateKey) {
p.key = key
}
// SetRemoteCallOptions sets call options of remote client calls.
func (p *Prm) SetRemoteCallOptions(opts ...client.CallOption) {
p.callOpts = opts
}

View file

@ -25,12 +25,10 @@ func (w *headSvcWrapper) headAddress(exec *execCtx, addr *objectSDK.Address) (*o
wr := getsvc.NewSimpleObjectWriter()
p := getsvc.HeadPrm{}
p.SetPrivateKey(exec.key())
p.SetCommonParameters(exec.commonParameters())
p.SetHeaderWriter(wr)
p.WithRawFlag(true)
p.WithAddress(addr)
p.SetRemoteCallOptions(exec.callOptions()...)
err := (*getsvc.Service)(w).Head(exec.context(), p)
if err != nil {
@ -86,10 +84,8 @@ func (w *searchSvcWrapper) splitMembers(exec *execCtx) ([]*objectSDK.ID, error)
p := searchsvc.Prm{}
p.SetWriter(wr)
p.SetCommonParameters(exec.commonParameters())
p.SetPrivateKey(exec.key())
p.WithContainerID(exec.containerID())
p.WithSearchFilters(fs)
p.SetRemoteCallOptions(exec.callOptions()...)
err := (*searchsvc.Service)(w).Search(exec.context(), p)
if err != nil {

View file

@ -1,11 +1,9 @@
package deletesvc
import (
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/session"
deletesvc "github.com/nspcc-dev/neofs-node/pkg/services/object/delete"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
)
@ -23,9 +21,9 @@ func (s *Service) toPrm(req *objectV2.DeleteRequest, respBody *objectV2.DeleteRe
}
p := new(deletesvc.Prm)
p.SetPrivateKey(key)
p.SetCommonParameters(commonParameters(meta))
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
p.SetCommonParameters(util.CommonPrmFromV2(req).
WithPrivateKey(key),
)
body := req.GetBody()
p.WithAddress(object.NewAddressFromV2(body.GetAddress()))
@ -39,33 +37,3 @@ func (s *Service) toPrm(req *objectV2.DeleteRequest, respBody *objectV2.DeleteRe
func (w *tombstoneBodyWriter) SetAddress(addr *object.Address) {
w.body.SetTombstone(addr.ToV2())
}
func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm {
prm := new(util.CommonPrm)
if tok := meta.GetBearerToken(); tok != nil {
prm.WithBearerToken(token.NewBearerTokenFromV2(tok))
}
if tok := meta.GetSessionToken(); tok != nil {
prm.WithSessionToken(token.NewSessionTokenFromV2(tok))
}
return prm
}
// can be shared accross all services
func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption {
opts := make([]client.CallOption, 0, 3)
opts = append(opts, client.WithTTL(meta.GetTTL()-1))
if tok := meta.GetBearerToken(); tok != nil {
opts = append(opts, client.WithBearer(token.NewBearerTokenFromV2(tok)))
}
if tok := meta.GetSessionToken(); tok != nil {
opts = append(opts, client.WithSession(token.NewSessionTokenFromV2(tok)))
}
return opts
}