2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-09-29 16:44:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-09-21 14:31:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/transformer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type remoteTarget struct {
|
|
|
|
transformer.ObjectTarget
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
2020-09-29 16:44:59 +00:00
|
|
|
keyStorage *util.KeyStorage
|
|
|
|
|
2020-12-29 08:24:07 +00:00
|
|
|
commonPrm *util.CommonPrm
|
2020-10-20 13:44:45 +00:00
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
addr network.Address
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
obj *object.Object
|
2020-11-18 13:03:31 +00:00
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientConstructor ClientConstructor
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 11:46:40 +00:00
|
|
|
// RemoteSender represents utility for
|
|
|
|
// sending an object to a remote host.
|
|
|
|
type RemoteSender struct {
|
2020-11-23 12:59:06 +00:00
|
|
|
keyStorage *util.KeyStorage
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientConstructor ClientConstructor
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemotePutPrm groups remote put operation parameters.
|
|
|
|
type RemotePutPrm struct {
|
2021-06-18 06:00:21 +00:00
|
|
|
node network.Address
|
2020-10-21 11:46:40 +00:00
|
|
|
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
func (t *remoteTarget) WriteHeader(obj *object.RawObject) error {
|
|
|
|
t.obj = obj.Object()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) {
|
2020-12-29 08:24:07 +00:00
|
|
|
key, err := t.keyStorage.GetKey(t.commonPrm.SessionToken())
|
2020-09-29 16:44:59 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not receive private key: %w", t, err)
|
2020-09-29 16:44:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 15:17:16 +00:00
|
|
|
c, err := t.clientConstructor.Get(t.addr)
|
2020-09-24 07:37:42 +00:00
|
|
|
if err != nil {
|
2021-05-20 15:17:16 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not create SDK client %s: %w", t, t.addr, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, err := c.PutObject(t.ctx, new(client.PutObjectParams).
|
|
|
|
WithObject(
|
|
|
|
t.obj.SDK(),
|
2020-09-29 09:02:21 +00:00
|
|
|
),
|
2020-12-29 08:24:07 +00:00
|
|
|
append(
|
|
|
|
t.commonPrm.RemoteCallOptions(),
|
|
|
|
client.WithTTL(1), // FIXME: use constant
|
2021-03-13 15:22:21 +00:00
|
|
|
client.WithKey(key),
|
2020-12-29 08:24:07 +00:00
|
|
|
)...,
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-05-20 15:17:16 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not put object to %s: %w", t, t.addr, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new(transformer.AccessIdentifiers).
|
|
|
|
WithSelfID(id), nil
|
|
|
|
}
|
2020-10-21 11:46:40 +00:00
|
|
|
|
|
|
|
// NewRemoteSender creates, initializes and returns new RemoteSender instance.
|
2021-03-23 18:40:36 +00:00
|
|
|
func NewRemoteSender(keyStorage *util.KeyStorage, cons ClientConstructor) *RemoteSender {
|
2020-10-21 11:46:40 +00:00
|
|
|
return &RemoteSender{
|
2021-03-23 18:40:36 +00:00
|
|
|
keyStorage: keyStorage,
|
|
|
|
clientConstructor: cons,
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNodeAddress sets network address of the remote node.
|
2021-06-18 06:00:21 +00:00
|
|
|
func (p *RemotePutPrm) WithNodeAddress(v network.Address) *RemotePutPrm {
|
2020-10-21 11:46:40 +00:00
|
|
|
if p != nil {
|
|
|
|
p.node = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithObject sets transferred object.
|
|
|
|
func (p *RemotePutPrm) WithObject(v *object.Object) *RemotePutPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.obj = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject sends object to remote node.
|
|
|
|
func (s *RemoteSender) PutObject(ctx context.Context, p *RemotePutPrm) error {
|
|
|
|
t := &remoteTarget{
|
2021-03-23 18:40:36 +00:00
|
|
|
ctx: ctx,
|
|
|
|
keyStorage: s.keyStorage,
|
|
|
|
addr: p.node,
|
|
|
|
clientConstructor: s.clientConstructor,
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.WriteHeader(object.NewRawFromObject(p.obj)); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not send object header: %w", s, err)
|
2020-10-21 11:46:40 +00:00
|
|
|
} else if _, err := t.Close(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not send object: %w", s, err)
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|