2021-03-30 22:46:33 +00:00
|
|
|
package neofs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2021-04-05 14:48:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gate/connections"
|
2021-03-30 22:46:33 +00:00
|
|
|
)
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// BaseOptions represents basic NeoFS request options.
|
2021-03-31 16:58:42 +00:00
|
|
|
type BaseOptions struct {
|
2021-03-30 22:46:33 +00:00
|
|
|
Client client.Client
|
|
|
|
SessionToken *token.SessionToken
|
|
|
|
BearerToken *token.BearerToken
|
2021-03-31 16:58:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// PutOptions represents NeoFS Put request options.
|
2021-03-31 16:58:42 +00:00
|
|
|
type PutOptions struct {
|
|
|
|
BaseOptions
|
2021-04-23 13:14:17 +00:00
|
|
|
Attributes []*object.Attribute
|
|
|
|
ContainerID *container.ID
|
|
|
|
OwnerID *owner.ID
|
|
|
|
Reader io.Reader
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// GetOptions represents NeoFS Get request options.
|
2021-03-30 22:46:33 +00:00
|
|
|
type GetOptions struct {
|
2021-03-31 16:58:42 +00:00
|
|
|
BaseOptions
|
2021-03-30 22:46:33 +00:00
|
|
|
ObjectAddress *object.Address
|
|
|
|
Writer io.Writer
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// SearchOptions represents NeoFS Search request options.
|
2021-03-30 22:46:33 +00:00
|
|
|
type SearchOptions struct {
|
2021-03-31 16:58:42 +00:00
|
|
|
BaseOptions
|
2021-03-30 22:46:33 +00:00
|
|
|
ContainerID *container.ID
|
|
|
|
Attribute struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// DeleteOptions represents NeoFS Delete request options.
|
2021-03-30 22:46:33 +00:00
|
|
|
type DeleteOptions struct {
|
2021-03-31 16:58:42 +00:00
|
|
|
BaseOptions
|
2021-03-30 22:46:33 +00:00
|
|
|
ObjectAddress *object.Address
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// ObjectClient wraps basic NeoFS requests.
|
2021-03-30 22:46:33 +00:00
|
|
|
type ObjectClient interface {
|
|
|
|
Put(context.Context, *PutOptions) (*object.Address, error)
|
|
|
|
Get(context.Context, *GetOptions) (*object.Object, error)
|
|
|
|
Search(context.Context, *SearchOptions) ([]*object.ID, error)
|
|
|
|
Delete(context.Context, *DeleteOptions) error
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// ClientPlant provides connections to NeoFS nodes from pool and allows to
|
|
|
|
// get local owner ID.
|
2021-03-30 22:46:33 +00:00
|
|
|
type ClientPlant interface {
|
2021-04-07 12:54:30 +00:00
|
|
|
ConnectionArtifacts() (client.Client, *token.SessionToken, error)
|
2021-03-30 22:46:33 +00:00
|
|
|
Object() ObjectClient
|
|
|
|
OwnerID() *owner.ID
|
|
|
|
}
|
|
|
|
|
2021-04-01 06:51:06 +00:00
|
|
|
type neofsObjectClient struct {
|
2021-03-30 22:46:33 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2021-04-05 14:48:01 +00:00
|
|
|
pool connections.Pool
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 06:51:06 +00:00
|
|
|
type neofsClientPlant struct {
|
2021-03-30 22:46:33 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
ownerID *owner.ID
|
2021-04-05 14:48:01 +00:00
|
|
|
pool connections.Pool
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// ConnectionArtifacts returns connection from pool.
|
2021-04-07 12:54:30 +00:00
|
|
|
func (cp *neofsClientPlant) ConnectionArtifacts() (client.Client, *token.SessionToken, error) {
|
|
|
|
return cp.pool.ConnectionArtifacts()
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Object returns ObjectClient instance from plant.
|
2021-04-29 08:55:26 +00:00
|
|
|
func (cp *neofsClientPlant) Object() ObjectClient {
|
2021-04-05 14:48:01 +00:00
|
|
|
return &neofsObjectClient{
|
2021-04-29 08:55:26 +00:00
|
|
|
key: cp.key,
|
|
|
|
pool: cp.pool,
|
2021-04-05 14:48:01 +00:00
|
|
|
}
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// OwnerID returns plant's owner ID.
|
2021-04-29 08:55:26 +00:00
|
|
|
func (cp *neofsClientPlant) OwnerID() *owner.ID {
|
|
|
|
return cp.ownerID
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// NewClientPlant creates new ClientPlant from given context, pool and credentials.
|
2021-04-05 14:48:01 +00:00
|
|
|
func NewClientPlant(ctx context.Context, pool connections.Pool, creds Credentials) (ClientPlant, error) {
|
|
|
|
return &neofsClientPlant{key: creds.PrivateKey(), ownerID: creds.Owner(), pool: pool}, nil
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Put does NeoFS Put request, returning new object address if successful.
|
2021-04-01 06:51:06 +00:00
|
|
|
func (oc *neofsObjectClient) Put(ctx context.Context, options *PutOptions) (*object.Address, error) {
|
2021-03-30 22:46:33 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
objectID *object.ID
|
|
|
|
)
|
|
|
|
address := object.NewAddress()
|
2021-04-23 13:14:17 +00:00
|
|
|
rawObject := object.NewRaw()
|
|
|
|
rawObject.SetContainerID(options.ContainerID)
|
|
|
|
rawObject.SetOwnerID(options.OwnerID)
|
|
|
|
rawObject.SetAttributes(options.Attributes...)
|
|
|
|
ops := new(client.PutObjectParams).
|
|
|
|
WithObject(rawObject.Object()).
|
|
|
|
WithPayloadReader(options.Reader)
|
|
|
|
objectID, err = options.Client.PutObject(
|
|
|
|
ctx,
|
|
|
|
ops,
|
|
|
|
client.WithSession(options.SessionToken),
|
|
|
|
client.WithBearer(options.BearerToken),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-03-30 22:46:33 +00:00
|
|
|
}
|
2021-04-23 13:14:17 +00:00
|
|
|
address.SetObjectID(objectID)
|
2021-03-30 22:46:33 +00:00
|
|
|
address.SetContainerID(options.ContainerID)
|
|
|
|
return address, nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Get does NeoFS Get request, returning an object received if successful.
|
2021-04-01 06:51:06 +00:00
|
|
|
func (oc *neofsObjectClient) Get(ctx context.Context, options *GetOptions) (*object.Object, error) {
|
2021-03-30 22:46:33 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
obj *object.Object
|
|
|
|
)
|
|
|
|
ops := new(client.GetObjectParams).
|
|
|
|
WithAddress(options.ObjectAddress).
|
|
|
|
WithPayloadWriter(options.Writer)
|
|
|
|
obj, err = options.Client.GetObject(
|
|
|
|
ctx,
|
|
|
|
ops,
|
|
|
|
client.WithSession(options.SessionToken),
|
|
|
|
client.WithBearer(options.BearerToken),
|
|
|
|
)
|
|
|
|
return obj, err
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Search does NeoFS Search request, returning object IDs if successful.
|
2021-04-01 06:51:06 +00:00
|
|
|
func (oc *neofsObjectClient) Search(ctx context.Context, options *SearchOptions) ([]*object.ID, error) {
|
2021-03-30 22:46:33 +00:00
|
|
|
sfs := object.NewSearchFilters()
|
|
|
|
sfs.AddRootFilter()
|
|
|
|
sfs.AddFilter(options.Attribute.Key, options.Attribute.Value, object.MatchStringEqual)
|
|
|
|
sops := new(client.SearchObjectParams)
|
|
|
|
sops.WithContainerID(options.ContainerID)
|
|
|
|
sops.WithSearchFilters(sfs)
|
|
|
|
return options.Client.SearchObject(
|
|
|
|
ctx,
|
|
|
|
sops,
|
|
|
|
client.WithSession(options.SessionToken),
|
|
|
|
client.WithBearer(options.BearerToken),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:22:03 +00:00
|
|
|
// Delete deletes NeoFS object.
|
2021-04-01 06:51:06 +00:00
|
|
|
func (oc *neofsObjectClient) Delete(ctx context.Context, options *DeleteOptions) error {
|
2021-03-30 22:46:33 +00:00
|
|
|
ops := new(client.DeleteObjectParams).WithAddress(options.ObjectAddress)
|
|
|
|
err := options.Client.DeleteObject(
|
|
|
|
ctx,
|
|
|
|
ops,
|
|
|
|
client.WithSession(options.SessionToken),
|
|
|
|
client.WithBearer(options.BearerToken),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|