forked from TrueCloudLab/frostfs-node
[#950] cli: Refactor usage of NeoFS API client
The client needs of the CLI application are limited and change not often. Interface changes of the client library should not affect the operation of various application packages, if they do not change their requirements for the provided functionality. To localize the use of the base client and facilitate further support, an auxiliary package is implemented that will only be used by the CLI application. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
a3414b36dd
commit
bbc2b873ab
9 changed files with 1098 additions and 403 deletions
614
cmd/neofs-cli/internal/client/client.go
Normal file
614
cmd/neofs-cli/internal/client/client.go
Normal file
|
@ -0,0 +1,614 @@
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/accounting"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
||||||
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BalanceOfPrm groups parameters of BalanceOf operation.
|
||||||
|
type BalanceOfPrm struct {
|
||||||
|
commonPrm
|
||||||
|
ownerIDPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// BalanceOfRes groups resulting values of BalanceOf operation.
|
||||||
|
type BalanceOfRes struct {
|
||||||
|
cliRes *accounting.Decimal
|
||||||
|
}
|
||||||
|
|
||||||
|
// Balance returns current balance.
|
||||||
|
func (x BalanceOfRes) Balance() *accounting.Decimal {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// BalanceOf requests current balance of NeoFS user.
|
||||||
|
func BalanceOf(prm BalanceOfPrm) (res BalanceOfRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.GetBalance(context.Background(), prm.ownerID,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListContainersPrm groups parameters of ListContainers operation.
|
||||||
|
type ListContainersPrm struct {
|
||||||
|
commonPrm
|
||||||
|
ownerIDPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListContainersRes groups resulting values of ListContainers operation.
|
||||||
|
type ListContainersRes struct {
|
||||||
|
cliRes []*cid.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDList returns list of identifiers of user's containers.
|
||||||
|
func (x ListContainersRes) IDList() []*cid.ID {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListContainers requests list of NeoFS user's containers.
|
||||||
|
func ListContainers(prm ListContainersPrm) (res ListContainersRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.ListContainers(context.Background(), prm.ownerID,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutContainerPrm groups parameters of PutContainer operation.
|
||||||
|
type PutContainerPrm struct {
|
||||||
|
commonPrm
|
||||||
|
sessionTokenPrm
|
||||||
|
|
||||||
|
cnr *container.Container
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContainer sets container structure.
|
||||||
|
func (x *PutContainerPrm) SetContainer(cnr *container.Container) {
|
||||||
|
x.cnr = cnr
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutContainerRes groups resulting values of PutContainer operation.
|
||||||
|
type PutContainerRes struct {
|
||||||
|
cliRes *cid.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID returns identifier of the created container.
|
||||||
|
func (x PutContainerRes) ID() *cid.ID {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutContainer sends request to save container in NeoFS.
|
||||||
|
//
|
||||||
|
// Operation is asynchronous and no guaranteed even in the absence of errors.
|
||||||
|
// The required time is also not predictable.
|
||||||
|
//
|
||||||
|
// Success can be verified by reading by identifier.
|
||||||
|
func PutContainer(prm PutContainerPrm) (res PutContainerRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.PutContainer(context.Background(), prm.cnr,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContainerPrm groups parameters of GetContainer operation.
|
||||||
|
type GetContainerPrm struct {
|
||||||
|
commonPrm
|
||||||
|
containerIDPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContainerRes groups resulting values of GetContainer operation.
|
||||||
|
type GetContainerRes struct {
|
||||||
|
cliRes *container.Container
|
||||||
|
}
|
||||||
|
|
||||||
|
// Container returns structured of the requested container.
|
||||||
|
func (x GetContainerRes) Container() *container.Container {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContainer reads container from NeoFS by ID.
|
||||||
|
func GetContainer(prm GetContainerPrm) (res GetContainerRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.GetContainer(context.Background(), prm.cnrID,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteContainerPrm groups parameters of DeleteContainerPrm operation.
|
||||||
|
type DeleteContainerPrm struct {
|
||||||
|
commonPrm
|
||||||
|
sessionTokenPrm
|
||||||
|
containerIDPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteContainerRes groups resulting values of DeleteContainer operation.
|
||||||
|
type DeleteContainerRes struct{}
|
||||||
|
|
||||||
|
// DeleteContainer sends request to remove container from NeoFS by ID.
|
||||||
|
//
|
||||||
|
// Operation is asynchronous and no guaranteed even in the absence of errors.
|
||||||
|
// The required time is also not predictable.
|
||||||
|
//
|
||||||
|
// Success can be verified by reading by identifier.
|
||||||
|
func DeleteContainer(prm DeleteContainerPrm) (res DeleteContainerRes, err error) {
|
||||||
|
err = prm.cli.DeleteContainer(context.Background(), prm.cnrID,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// EACLPrm groups parameters of EACL operation.
|
||||||
|
type EACLPrm struct {
|
||||||
|
commonPrm
|
||||||
|
containerIDPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// EACLRes groups resulting values of EACL operation.
|
||||||
|
type EACLRes struct {
|
||||||
|
cliRes *client.EACLWithSignature
|
||||||
|
}
|
||||||
|
|
||||||
|
// EACL returns requested eACL table.
|
||||||
|
func (x EACLRes) EACL() *eacl.Table {
|
||||||
|
return x.cliRes.EACL()
|
||||||
|
}
|
||||||
|
|
||||||
|
// EACL reads eACL table from NeoFS by container ID.
|
||||||
|
func EACL(prm EACLPrm) (res EACLRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.GetEACL(context.Background(), prm.cnrID,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetEACLPrm groups parameters of SetEACL operation.
|
||||||
|
type SetEACLPrm struct {
|
||||||
|
commonPrm
|
||||||
|
sessionTokenPrm
|
||||||
|
|
||||||
|
eaclTable *eacl.Table
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetEACLTable sets eACL table structure.
|
||||||
|
func (x *SetEACLPrm) SetEACLTable(table *eacl.Table) {
|
||||||
|
x.eaclTable = table
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetEACLRes groups resulting values of SetEACL operation.
|
||||||
|
type SetEACLRes struct{}
|
||||||
|
|
||||||
|
// SetEACL requests to save eACL table in NeoFS.
|
||||||
|
//
|
||||||
|
// Operation is asynchronous and no guaranteed even in the absence of errors.
|
||||||
|
// The required time is also not predictable.
|
||||||
|
//
|
||||||
|
// Success can be verified by reading by container identifier.
|
||||||
|
func SetEACL(prm SetEACLPrm) (res SetEACLRes, err error) {
|
||||||
|
err = prm.cli.SetEACL(context.Background(), prm.eaclTable,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NetworkInfoPrm groups parameters of NetworkInfo operation.
|
||||||
|
type NetworkInfoPrm struct {
|
||||||
|
commonPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// NetworkInfoRes groups resulting values of NetworkInfo operation.
|
||||||
|
type NetworkInfoRes struct {
|
||||||
|
cliRes *netmap.NetworkInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// NetworkInfo returns structured information about the NeoFS network.
|
||||||
|
func (x NetworkInfoRes) NetworkInfo() *netmap.NetworkInfo {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// NetworkInfo reads information about the NeoFS network.
|
||||||
|
func NetworkInfo(prm NetworkInfoPrm) (res NetworkInfoRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.NetworkInfo(context.Background(),
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// NodeInfoPrm groups parameters of NodeInfo operation.
|
||||||
|
type NodeInfoPrm struct {
|
||||||
|
commonPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// NodeInfoRes groups resulting values of NodeInfo operation.
|
||||||
|
type NodeInfoRes struct {
|
||||||
|
cliRes *client.EndpointInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// NodeInfo returns information about the node from netmap.
|
||||||
|
func (x NodeInfoRes) NodeInfo() *netmap.NodeInfo {
|
||||||
|
return x.cliRes.NodeInfo()
|
||||||
|
}
|
||||||
|
|
||||||
|
// LatestVersion returns latest NeoFS API version in use.
|
||||||
|
func (x NodeInfoRes) LatestVersion() *pkg.Version {
|
||||||
|
return x.cliRes.LatestVersion()
|
||||||
|
}
|
||||||
|
|
||||||
|
// NodeInfo requests information about the remote server from NeoFS netmap.
|
||||||
|
func NodeInfo(prm NodeInfoPrm) (res NodeInfoRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.EndpointInfo(context.Background(),
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateSessionPrm groups parameters of CreateSession operation.
|
||||||
|
type CreateSessionPrm struct {
|
||||||
|
commonPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateSessionRes groups resulting values of CreateSession operation.
|
||||||
|
type CreateSessionRes struct {
|
||||||
|
cliRes *session.Token
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID returns session identifier.
|
||||||
|
func (x CreateSessionRes) ID() []byte {
|
||||||
|
return x.cliRes.ID()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionKey returns public session key in a binary format.
|
||||||
|
func (x CreateSessionRes) SessionKey() []byte {
|
||||||
|
return x.cliRes.SessionKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateSession opens new unlimited session with the remote node.
|
||||||
|
func CreateSession(prm CreateSessionPrm) (res CreateSessionRes, err error) {
|
||||||
|
res.cliRes, err = prm.cli.CreateSession(context.Background(), math.MaxUint64,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutObjectPrm groups parameters of PutObject operation.
|
||||||
|
type PutObjectPrm struct {
|
||||||
|
commonObjectPrm
|
||||||
|
|
||||||
|
hdr *object.Object
|
||||||
|
|
||||||
|
rdr io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHeader sets object header.
|
||||||
|
func (x *PutObjectPrm) SetHeader(hdr *object.Object) {
|
||||||
|
x.hdr = hdr
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPayloadReader sets reader of the object payload.
|
||||||
|
func (x *PutObjectPrm) SetPayloadReader(rdr io.Reader) {
|
||||||
|
x.rdr = rdr
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutObjectRes groups resulting values of PutObject operation.
|
||||||
|
type PutObjectRes struct {
|
||||||
|
cliRes *object.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID returns identifier of the created object.
|
||||||
|
func (x PutObjectRes) ID() *object.ID {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutObject saves the object in NeoFS network.
|
||||||
|
func PutObject(prm PutObjectPrm) (res PutObjectRes, err error) {
|
||||||
|
var putPrm client.PutObjectParams
|
||||||
|
|
||||||
|
putPrm.WithObject(prm.hdr)
|
||||||
|
putPrm.WithPayloadReader(prm.rdr)
|
||||||
|
|
||||||
|
res.cliRes, err = prm.cli.PutObject(context.Background(), &putPrm, append(prm.opts,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)...)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteObjectPrm groups parameters of DeleteObject operation.
|
||||||
|
type DeleteObjectPrm struct {
|
||||||
|
commonObjectPrm
|
||||||
|
objectAddressPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteObjectRes groups resulting values of DeleteObject operation.
|
||||||
|
type DeleteObjectRes struct {
|
||||||
|
cliRes *object.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
// TombstoneAddress returns address of the created object with tombstone.
|
||||||
|
func (x DeleteObjectRes) TombstoneAddress() *object.Address {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteObject marks object to be removed from NeoFS through tombstone placement.
|
||||||
|
func DeleteObject(prm DeleteObjectPrm) (res DeleteObjectRes, err error) {
|
||||||
|
var delPrm client.DeleteObjectParams
|
||||||
|
|
||||||
|
delPrm.WithAddress(prm.objAddr)
|
||||||
|
|
||||||
|
res.cliRes, err = client.DeleteObject(context.Background(), prm.cli, &delPrm, append(prm.opts,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)...)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetObjectPrm groups parameters of GetObject operation.
|
||||||
|
type GetObjectPrm struct {
|
||||||
|
commonObjectPrm
|
||||||
|
objectAddressPrm
|
||||||
|
rawPrm
|
||||||
|
payloadWriterPrm
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetObjectRes groups resulting values of GetObject operation.
|
||||||
|
type GetObjectRes struct {
|
||||||
|
cliRes *object.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
// Object returns header of the request object.
|
||||||
|
func (x GetObjectRes) Header() *object.Object {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetObject reads the object by address.
|
||||||
|
//
|
||||||
|
// Interrupts on any writer error. If successful, payload is written to writer.
|
||||||
|
func GetObject(prm GetObjectPrm) (res GetObjectRes, err error) {
|
||||||
|
var getPrm client.GetObjectParams
|
||||||
|
|
||||||
|
getPrm.WithAddress(prm.objAddr)
|
||||||
|
getPrm.WithPayloadWriter(prm.wrt)
|
||||||
|
getPrm.WithRawFlag(prm.raw)
|
||||||
|
|
||||||
|
res.cliRes, err = prm.cli.GetObject(context.Background(), &getPrm, append(prm.opts,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)...)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// HeadObjectPrm groups parameters of HeadObject operation.
|
||||||
|
type HeadObjectPrm struct {
|
||||||
|
commonObjectPrm
|
||||||
|
objectAddressPrm
|
||||||
|
rawPrm
|
||||||
|
|
||||||
|
mainOnly bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMainOnlyFlag sets flag to get only main fields of object header in terms of NeoFS API.
|
||||||
|
func (x *HeadObjectPrm) SetMainOnlyFlag(v bool) {
|
||||||
|
x.mainOnly = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// HeadObjectRes groups resulting values of HeadObject operation.
|
||||||
|
type HeadObjectRes struct {
|
||||||
|
cliRes *object.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header returns requested object header.
|
||||||
|
func (x HeadObjectRes) Header() *object.Object {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// HeadObject reads object header by address.
|
||||||
|
//
|
||||||
|
// For raw reading, returns *object.SplitInfoError error if object is virtual.
|
||||||
|
func HeadObject(prm HeadObjectPrm) (res HeadObjectRes, err error) {
|
||||||
|
var cliPrm client.ObjectHeaderParams
|
||||||
|
|
||||||
|
cliPrm.WithAddress(prm.objAddr)
|
||||||
|
cliPrm.WithRawFlag(prm.raw)
|
||||||
|
|
||||||
|
if prm.mainOnly {
|
||||||
|
cliPrm.WithMainFields()
|
||||||
|
} else {
|
||||||
|
cliPrm.WithAllFields()
|
||||||
|
}
|
||||||
|
|
||||||
|
res.cliRes, err = prm.cli.GetObjectHeader(context.Background(), &cliPrm, append(prm.opts,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)...)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchObjectsPrm groups parameters of SearchObjects operation.
|
||||||
|
type SearchObjectsPrm struct {
|
||||||
|
commonObjectPrm
|
||||||
|
containerIDPrm
|
||||||
|
|
||||||
|
filters object.SearchFilters
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFilters sets search filters.
|
||||||
|
func (x *SearchObjectsPrm) SetFilters(filters object.SearchFilters) {
|
||||||
|
x.filters = filters
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchObjectsRes groups resulting values of SearchObjects operation.
|
||||||
|
type SearchObjectsRes struct {
|
||||||
|
cliRes []*object.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDList returns identifiers of the matched objects.
|
||||||
|
func (x SearchObjectsRes) IDList() []*object.ID {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchObjects selects objects from container which match the filters.
|
||||||
|
func SearchObjects(prm SearchObjectsPrm) (res SearchObjectsRes, err error) {
|
||||||
|
var cliPrm client.SearchObjectParams
|
||||||
|
|
||||||
|
cliPrm.WithSearchFilters(prm.filters)
|
||||||
|
cliPrm.WithContainerID(prm.cnrID)
|
||||||
|
|
||||||
|
res.cliRes, err = prm.cli.SearchObject(context.Background(), &cliPrm, append(prm.opts,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)...)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// HashPayloadRangesPrm groups parameters of HashPayloadRanges operation.
|
||||||
|
type HashPayloadRangesPrm struct {
|
||||||
|
commonObjectPrm
|
||||||
|
objectAddressPrm
|
||||||
|
|
||||||
|
tz bool
|
||||||
|
|
||||||
|
rngs []*object.Range
|
||||||
|
|
||||||
|
salt []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// TZ sets flag to request Tillich-Zemor hashes.
|
||||||
|
func (x *HashPayloadRangesPrm) TZ() {
|
||||||
|
x.tz = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRanges sets list of payload ranges to hash.
|
||||||
|
func (x *HashPayloadRangesPrm) SetRanges(rngs []*object.Range) {
|
||||||
|
x.rngs = rngs
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSalt sets data for each range to be XOR'ed with.
|
||||||
|
func (x *HashPayloadRangesPrm) SetSalt(salt []byte) {
|
||||||
|
x.salt = salt
|
||||||
|
}
|
||||||
|
|
||||||
|
// HashPayloadRangesRes groups resulting values of HashPayloadRanges operation.
|
||||||
|
type HashPayloadRangesRes struct {
|
||||||
|
cliRes [][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// HashList returns list of hashes of the payload ranges keeping order.
|
||||||
|
func (x HashPayloadRangesRes) HashList() [][]byte {
|
||||||
|
return x.cliRes
|
||||||
|
}
|
||||||
|
|
||||||
|
// HashPayloadRanges requests hashes (by default SHA256) of the object payload ranges.
|
||||||
|
//
|
||||||
|
// Returns an error if number of received hashes differs with the number of requested ranges.
|
||||||
|
func HashPayloadRanges(prm HashPayloadRangesPrm) (res HashPayloadRangesRes, err error) {
|
||||||
|
var cliPrm client.RangeChecksumParams
|
||||||
|
|
||||||
|
cliPrm.WithAddress(prm.objAddr)
|
||||||
|
cliPrm.WithSalt(prm.salt)
|
||||||
|
cliPrm.WithRangeList(prm.rngs...)
|
||||||
|
|
||||||
|
if prm.tz {
|
||||||
|
var hs [][sha256.Size]byte
|
||||||
|
|
||||||
|
hs, err = prm.cli.ObjectPayloadRangeSHA256(context.Background(), &cliPrm, append(prm.opts,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)...)
|
||||||
|
if err == nil {
|
||||||
|
res.cliRes = make([][]byte, 0, len(hs))
|
||||||
|
|
||||||
|
for i := range hs {
|
||||||
|
res.cliRes = append(res.cliRes, hs[i][:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var hs [][client.TZSize]byte
|
||||||
|
|
||||||
|
hs, err = prm.cli.ObjectPayloadRangeTZ(context.Background(), &cliPrm, append(prm.opts,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)...)
|
||||||
|
if err == nil {
|
||||||
|
res.cliRes = make([][]byte, 0, len(hs))
|
||||||
|
|
||||||
|
for i := range hs {
|
||||||
|
res.cliRes = append(res.cliRes, hs[i][:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// PayloadRangePrm groups parameters of PayloadRange operation.
|
||||||
|
type PayloadRangePrm struct {
|
||||||
|
commonObjectPrm
|
||||||
|
objectAddressPrm
|
||||||
|
rawPrm
|
||||||
|
payloadWriterPrm
|
||||||
|
|
||||||
|
rng *object.Range
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRange sets payload range to read.
|
||||||
|
func (x *PayloadRangePrm) SetRange(rng *object.Range) {
|
||||||
|
x.rng = rng
|
||||||
|
}
|
||||||
|
|
||||||
|
// PayloadRangeRes groups resulting values of PayloadRange operation.
|
||||||
|
type PayloadRangeRes struct{}
|
||||||
|
|
||||||
|
// PayloadRange reads object payload range from NeoFS and writes it to specified writer.
|
||||||
|
//
|
||||||
|
// Interrupts on any writer error.
|
||||||
|
func PayloadRange(prm PayloadRangePrm) (res PayloadRangeRes, err error) {
|
||||||
|
var cliPrm client.RangeDataParams
|
||||||
|
|
||||||
|
cliPrm.WithRaw(prm.raw)
|
||||||
|
cliPrm.WithAddress(prm.objAddr)
|
||||||
|
cliPrm.WithDataWriter(prm.wrt)
|
||||||
|
cliPrm.WithRange(prm.rng)
|
||||||
|
|
||||||
|
_, err = prm.cli.ObjectPayloadRangeData(context.Background(), &cliPrm,
|
||||||
|
client.WithKey(prm.privKey),
|
||||||
|
client.WithSession(prm.sessionToken),
|
||||||
|
client.WithBearer(prm.bearerToken),
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
12
cmd/neofs-cli/internal/client/doc.go
Normal file
12
cmd/neofs-cli/internal/client/doc.go
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
// Package internal provides functionality for NeoFS CLI application communication with NeoFS network.
|
||||||
|
//
|
||||||
|
// The base client for accessing remote nodes via NeoFS API is a NeoFS SDK Go API client.
|
||||||
|
// However, although it encapsulates a useful piece of business logic (e.g. the signature mechanism),
|
||||||
|
// the NeoFS CLI application does not fully use the client's flexible interface.
|
||||||
|
//
|
||||||
|
// In this regard, this package provides functions over base API client necessary for the application.
|
||||||
|
// This allows you to concentrate the entire spectrum of the client's use in one place (this will be convenient
|
||||||
|
// both when updating the base client and for evaluating the UX of SDK library). So it is expected that all
|
||||||
|
// application packages will be limited to this package for the development of functionality requiring
|
||||||
|
// NeoFS API communication.
|
||||||
|
package internal
|
114
cmd/neofs-cli/internal/client/prm.go
Normal file
114
cmd/neofs-cli/internal/client/prm.go
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||||
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
||||||
|
"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/session"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
// here are small structures with public setters to share between parameter structures
|
||||||
|
|
||||||
|
type commonPrm struct {
|
||||||
|
cli client.Client
|
||||||
|
|
||||||
|
privKey *ecdsa.PrivateKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetClient sets base client for NeoFS API communication.
|
||||||
|
func (x *commonPrm) SetClient(cli client.Client) {
|
||||||
|
x.cli = cli
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetKey sets private key to sign the request(s).
|
||||||
|
func (x *commonPrm) SetKey(key *ecdsa.PrivateKey) {
|
||||||
|
x.privKey = key
|
||||||
|
}
|
||||||
|
|
||||||
|
type ownerIDPrm struct {
|
||||||
|
ownerID *owner.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOwner sets identifier of NeoFS user.
|
||||||
|
func (x *ownerIDPrm) SetOwner(id *owner.ID) {
|
||||||
|
x.ownerID = id
|
||||||
|
}
|
||||||
|
|
||||||
|
type containerIDPrm struct {
|
||||||
|
cnrID *cid.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContainerID sets container identifier.
|
||||||
|
func (x *containerIDPrm) SetContainerID(id *cid.ID) {
|
||||||
|
x.cnrID = id
|
||||||
|
}
|
||||||
|
|
||||||
|
type sessionTokenPrm struct {
|
||||||
|
sessionToken *session.Token
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSessionToken sets token of the session within which request should be sent.
|
||||||
|
func (x *sessionTokenPrm) SetSessionToken(tok *session.Token) {
|
||||||
|
x.sessionToken = tok
|
||||||
|
}
|
||||||
|
|
||||||
|
type bearerTokenPrm struct {
|
||||||
|
bearerToken *token.BearerToken
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBearerToken sets bearer token to be attached to the request.
|
||||||
|
func (x *bearerTokenPrm) SetBearerToken(tok *token.BearerToken) {
|
||||||
|
x.bearerToken = tok
|
||||||
|
}
|
||||||
|
|
||||||
|
type objectAddressPrm struct {
|
||||||
|
objAddr *object.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *objectAddressPrm) SetAddress(addr *object.Address) {
|
||||||
|
x.objAddr = addr
|
||||||
|
}
|
||||||
|
|
||||||
|
type rawPrm struct {
|
||||||
|
raw bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRawFlag sets flag of raw request.
|
||||||
|
func (x *rawPrm) SetRawFlag(raw bool) {
|
||||||
|
x.raw = raw
|
||||||
|
}
|
||||||
|
|
||||||
|
type payloadWriterPrm struct {
|
||||||
|
wrt io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPayloadWriter sets writer of the object payload.
|
||||||
|
func (x *payloadWriterPrm) SetPayloadWriter(wrt io.Writer) {
|
||||||
|
x.wrt = wrt
|
||||||
|
}
|
||||||
|
|
||||||
|
type commonObjectPrm struct {
|
||||||
|
commonPrm
|
||||||
|
sessionTokenPrm
|
||||||
|
bearerTokenPrm
|
||||||
|
|
||||||
|
opts []client.CallOption
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTTL sets request TTL value.
|
||||||
|
func (x *commonObjectPrm) SetTTL(ttl uint32) {
|
||||||
|
x.opts = append(x.opts, client.WithTTL(ttl))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetXHeaders sets request X-Headers.
|
||||||
|
func (x *commonObjectPrm) SetXHeaders(xhdrs []*pkg.XHeader) {
|
||||||
|
for _, xhdr := range xhdrs {
|
||||||
|
x.opts = append(x.opts, client.WithXHeader(xhdr))
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,12 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/accounting"
|
"github.com/nspcc-dev/neofs-api-go/pkg/accounting"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
||||||
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
@ -37,19 +37,11 @@ var accountingBalanceCmd = &cobra.Command{
|
||||||
Short: "Get internal balance of NeoFS account",
|
Short: "Get internal balance of NeoFS account",
|
||||||
Long: `Get internal balance of NeoFS account`,
|
Long: `Get internal balance of NeoFS account`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var (
|
var oid *owner.ID
|
||||||
response *accounting.Decimal
|
|
||||||
oid *owner.ID
|
|
||||||
|
|
||||||
ctx = context.Background()
|
|
||||||
)
|
|
||||||
|
|
||||||
key, err := getKey()
|
key, err := getKey()
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
if balanceOwner == "" {
|
if balanceOwner == "" {
|
||||||
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
@ -60,11 +52,16 @@ var accountingBalanceCmd = &cobra.Command{
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err = cli.GetBalance(ctx, oid, globalCallOptions()...)
|
var prm internalclient.BalanceOfPrm
|
||||||
|
|
||||||
|
prepareAPIClientWithKey(cmd, key, &prm)
|
||||||
|
prm.SetOwner(oid)
|
||||||
|
|
||||||
|
res, err := internalclient.BalanceOf(prm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
// print to stdout
|
// print to stdout
|
||||||
prettyPrintDecimal(cmd, response)
|
prettyPrintDecimal(cmd, res.Balance())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,9 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -16,13 +14,13 @@ import (
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg"
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/acl"
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
"github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl"
|
||||||
"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/container"
|
||||||
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
"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/owner"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
||||||
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/core/version"
|
"github.com/nspcc-dev/neofs-node/pkg/core/version"
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/pkg/policy"
|
"github.com/nspcc-dev/neofs-sdk-go/pkg/policy"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -92,19 +90,11 @@ var listContainersCmd = &cobra.Command{
|
||||||
Short: "List all created containers",
|
Short: "List all created containers",
|
||||||
Long: "List all created containers",
|
Long: "List all created containers",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var (
|
var oid *owner.ID
|
||||||
response []*cid.ID
|
|
||||||
oid *owner.ID
|
|
||||||
|
|
||||||
ctx = context.Background()
|
|
||||||
)
|
|
||||||
|
|
||||||
key, err := getKey()
|
key, err := getKey()
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
if containerOwner == "" {
|
if containerOwner == "" {
|
||||||
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
@ -115,11 +105,16 @@ var listContainersCmd = &cobra.Command{
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err = cli.ListContainers(ctx, oid, globalCallOptions()...)
|
var prm internalclient.ListContainersPrm
|
||||||
|
|
||||||
|
prepareAPIClientWithKey(cmd, key, &prm)
|
||||||
|
prm.SetOwner(oid)
|
||||||
|
|
||||||
|
res, err := internalclient.ListContainers(prm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
// print to stdout
|
// print to stdout
|
||||||
prettyPrintContainerList(cmd, response)
|
prettyPrintContainerList(cmd, res.IDList())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,14 +124,6 @@ var createContainerCmd = &cobra.Command{
|
||||||
Long: `Create new container and register it in the NeoFS.
|
Long: `Create new container and register it in the NeoFS.
|
||||||
It will be stored in sidechain when inner ring will accepts it.`,
|
It will be stored in sidechain when inner ring will accepts it.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
placementPolicy, err := parseContainerPolicy(containerPolicy)
|
placementPolicy, err := parseContainerPolicy(containerPolicy)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
|
@ -160,18 +147,31 @@ It will be stored in sidechain when inner ring will accepts it.`,
|
||||||
cnr.SetSessionToken(tok)
|
cnr.SetSessionToken(tok)
|
||||||
cnr.SetOwnerID(tok.OwnerID())
|
cnr.SetOwnerID(tok.OwnerID())
|
||||||
|
|
||||||
id, err := cli.PutContainer(ctx, cnr, globalCallOptions()...)
|
var (
|
||||||
|
putPrm internalclient.PutContainerPrm
|
||||||
|
getPrm internalclient.GetContainerPrm
|
||||||
|
)
|
||||||
|
|
||||||
|
prepareAPIClient(cmd, &putPrm, &getPrm)
|
||||||
|
putPrm.SetContainer(cnr)
|
||||||
|
putPrm.SetSessionToken(tok)
|
||||||
|
|
||||||
|
res, err := internalclient.PutContainer(putPrm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
id := res.ID()
|
||||||
|
|
||||||
cmd.Println("container ID:", id)
|
cmd.Println("container ID:", id)
|
||||||
|
|
||||||
if containerAwait {
|
if containerAwait {
|
||||||
cmd.Println("awaiting...")
|
cmd.Println("awaiting...")
|
||||||
|
|
||||||
|
getPrm.SetContainerID(id)
|
||||||
|
|
||||||
for i := 0; i < awaitTimeout; i++ {
|
for i := 0; i < awaitTimeout; i++ {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
_, err := cli.GetContainer(ctx, id, globalCallOptions()...)
|
_, err := internalclient.GetContainer(getPrm)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cmd.Println("container has been persisted on sidechain")
|
cmd.Println("container has been persisted on sidechain")
|
||||||
return
|
return
|
||||||
|
@ -189,27 +189,22 @@ var deleteContainerCmd = &cobra.Command{
|
||||||
Long: `Delete existing container.
|
Long: `Delete existing container.
|
||||||
Only owner of the container has a permission to remove container.`,
|
Only owner of the container has a permission to remove container.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
id, err := parseContainerID(containerID)
|
id, err := parseContainerID(containerID)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
tok, err := getSessionToken(sessionTokenPath)
|
tok, err := getSessionToken(sessionTokenPath)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
callOpts := globalCallOptions()
|
var (
|
||||||
|
delPrm internalclient.DeleteContainerPrm
|
||||||
|
getPrm internalclient.GetContainerPrm
|
||||||
|
)
|
||||||
|
|
||||||
if tok != nil {
|
prepareAPIClient(cmd, &delPrm, &getPrm)
|
||||||
callOpts = append(callOpts, client.WithSession(tok))
|
delPrm.SetContainerID(id)
|
||||||
}
|
delPrm.SetSessionToken(tok)
|
||||||
|
|
||||||
err = cli.DeleteContainer(ctx, id, callOpts...)
|
_, err = internalclient.DeleteContainer(delPrm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
cmd.Println("container delete method invoked")
|
cmd.Println("container delete method invoked")
|
||||||
|
@ -217,10 +212,12 @@ Only owner of the container has a permission to remove container.`,
|
||||||
if containerAwait {
|
if containerAwait {
|
||||||
cmd.Println("awaiting...")
|
cmd.Println("awaiting...")
|
||||||
|
|
||||||
|
getPrm.SetContainerID(id)
|
||||||
|
|
||||||
for i := 0; i < awaitTimeout; i++ {
|
for i := 0; i < awaitTimeout; i++ {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
_, err := cli.GetContainer(ctx, id, globalCallOptions()...)
|
_, err := internalclient.GetContainer(getPrm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Println("container has been removed:", containerID)
|
cmd.Println("container has been removed:", containerID)
|
||||||
return
|
return
|
||||||
|
@ -237,34 +234,24 @@ var listContainerObjectsCmd = &cobra.Command{
|
||||||
Short: "List existing objects in container",
|
Short: "List existing objects in container",
|
||||||
Long: `List existing objects in container`,
|
Long: `List existing objects in container`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
id, err := parseContainerID(containerID)
|
id, err := parseContainerID(containerID)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
sessionToken, err := cli.CreateSession(ctx, math.MaxUint64)
|
|
||||||
exitOnErr(cmd, errf("can't create session token: %w", err))
|
|
||||||
|
|
||||||
filters := new(object.SearchFilters)
|
filters := new(object.SearchFilters)
|
||||||
filters.AddRootFilter() // search only user created objects
|
filters.AddRootFilter() // search only user created objects
|
||||||
|
|
||||||
searchQuery := new(client.SearchObjectParams)
|
var prm internalclient.SearchObjectsPrm
|
||||||
searchQuery.WithContainerID(id)
|
|
||||||
searchQuery.WithSearchFilters(*filters)
|
|
||||||
|
|
||||||
objectIDs, err := cli.SearchObject(ctx, searchQuery,
|
prepareSessionPrm(cmd, &prm)
|
||||||
append(globalCallOptions(),
|
prepareObjectPrm(cmd, &prm)
|
||||||
client.WithSession(sessionToken),
|
prm.SetContainerID(id)
|
||||||
)...,
|
prm.SetFilters(*filters)
|
||||||
)
|
|
||||||
|
res, err := internalclient.SearchObjects(prm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
objectIDs := res.IDList()
|
||||||
|
|
||||||
for i := range objectIDs {
|
for i := range objectIDs {
|
||||||
cmd.Println(objectIDs[i])
|
cmd.Println(objectIDs[i])
|
||||||
}
|
}
|
||||||
|
@ -276,11 +263,7 @@ var getContainerInfoCmd = &cobra.Command{
|
||||||
Short: "Get container field info",
|
Short: "Get container field info",
|
||||||
Long: `Get container field info`,
|
Long: `Get container field info`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var (
|
var cnr *container.Container
|
||||||
cnr *container.Container
|
|
||||||
|
|
||||||
ctx = context.Background()
|
|
||||||
)
|
|
||||||
|
|
||||||
if containerPathFrom != "" {
|
if containerPathFrom != "" {
|
||||||
data, err := os.ReadFile(containerPathFrom)
|
data, err := os.ReadFile(containerPathFrom)
|
||||||
|
@ -290,17 +273,18 @@ var getContainerInfoCmd = &cobra.Command{
|
||||||
err = cnr.Unmarshal(data)
|
err = cnr.Unmarshal(data)
|
||||||
exitOnErr(cmd, errf("can't unmarshal container: %w", err))
|
exitOnErr(cmd, errf("can't unmarshal container: %w", err))
|
||||||
} else {
|
} else {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
id, err := parseContainerID(containerID)
|
id, err := parseContainerID(containerID)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
cnr, err = cli.GetContainer(ctx, id, globalCallOptions()...)
|
var prm internalclient.GetContainerPrm
|
||||||
|
|
||||||
|
prepareAPIClient(cmd, &prm)
|
||||||
|
prm.SetContainerID(id)
|
||||||
|
|
||||||
|
res, err := internalclient.GetContainer(prm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
cnr = res.Container()
|
||||||
}
|
}
|
||||||
|
|
||||||
prettyPrintContainer(cmd, cnr, containerJSON)
|
prettyPrintContainer(cmd, cnr, containerJSON)
|
||||||
|
@ -330,21 +314,19 @@ var getExtendedACLCmd = &cobra.Command{
|
||||||
Short: "Get extended ACL table of container",
|
Short: "Get extended ACL table of container",
|
||||||
Long: `Get extended ACL talbe of container`,
|
Long: `Get extended ACL talbe of container`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
id, err := parseContainerID(containerID)
|
id, err := parseContainerID(containerID)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
res, err := cli.GetEACL(ctx, id, globalCallOptions()...)
|
var eaclPrm internalclient.EACLPrm
|
||||||
|
|
||||||
|
prepareAPIClient(cmd, &eaclPrm)
|
||||||
|
eaclPrm.SetContainerID(id)
|
||||||
|
|
||||||
|
res, err := internalclient.EACL(eaclPrm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
eaclTable := res.EACL()
|
eaclTable := res.EACL()
|
||||||
|
|
||||||
sig := eaclTable.Signature()
|
sig := eaclTable.Signature()
|
||||||
|
|
||||||
if containerPathTo == "" {
|
if containerPathTo == "" {
|
||||||
|
@ -383,14 +365,6 @@ var setExtendedACLCmd = &cobra.Command{
|
||||||
Long: `Set new extended ACL table for container.
|
Long: `Set new extended ACL table for container.
|
||||||
Container ID in EACL table will be substituted with ID from the CLI.`,
|
Container ID in EACL table will be substituted with ID from the CLI.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
id, err := parseContainerID(containerID)
|
id, err := parseContainerID(containerID)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
|
@ -403,7 +377,16 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
|
||||||
eaclTable.SetCID(id)
|
eaclTable.SetCID(id)
|
||||||
eaclTable.SetSessionToken(tok)
|
eaclTable.SetSessionToken(tok)
|
||||||
|
|
||||||
err = cli.SetEACL(ctx, eaclTable, globalCallOptions()...)
|
var (
|
||||||
|
setEACLPrm internalclient.SetEACLPrm
|
||||||
|
getEACLPrm internalclient.EACLPrm
|
||||||
|
)
|
||||||
|
|
||||||
|
prepareAPIClient(cmd, &setEACLPrm, &getEACLPrm)
|
||||||
|
setEACLPrm.SetSessionToken(tok)
|
||||||
|
setEACLPrm.SetEACLTable(eaclTable)
|
||||||
|
|
||||||
|
_, err = internalclient.SetEACL(setEACLPrm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
if containerAwait {
|
if containerAwait {
|
||||||
|
@ -412,13 +395,15 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
|
||||||
|
|
||||||
cmd.Println("awaiting...")
|
cmd.Println("awaiting...")
|
||||||
|
|
||||||
|
getEACLPrm.SetContainerID(id)
|
||||||
|
|
||||||
for i := 0; i < awaitTimeout; i++ {
|
for i := 0; i < awaitTimeout; i++ {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
tableSig, err := cli.GetEACL(ctx, id, globalCallOptions()...)
|
res, err := internalclient.EACL(getEACLPrm)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// compare binary values because EACL could have been set already
|
// compare binary values because EACL could have been set already
|
||||||
got, err := tableSig.EACL().Marshal()
|
got, err := res.EACL().Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mr-tron/base58"
|
"github.com/mr-tron/base58"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
||||||
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -61,15 +61,15 @@ var getEpochCmd = &cobra.Command{
|
||||||
Short: "Get current epoch number",
|
Short: "Get current epoch number",
|
||||||
Long: "Get current epoch number",
|
Long: "Get current epoch number",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
key, err := getKey()
|
var prm internalclient.NetworkInfoPrm
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
prepareAPIClient(cmd, &prm)
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
res, err := internalclient.NetworkInfo(prm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
netInfo := res.NetworkInfo()
|
||||||
|
|
||||||
cmd.Println(netInfo.CurrentEpoch())
|
cmd.Println(netInfo.CurrentEpoch())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -79,16 +79,14 @@ var localNodeInfoCmd = &cobra.Command{
|
||||||
Short: "Get local node info",
|
Short: "Get local node info",
|
||||||
Long: `Get local node info`,
|
Long: `Get local node info`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
key, err := getKey()
|
var prm internalclient.NodeInfoPrm
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
prepareAPIClient(cmd, &prm)
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
nodeInfo, err := cli.EndpointInfo(context.Background(), globalCallOptions()...)
|
res, err := internalclient.NodeInfo(prm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
prettyPrintNodeInfo(cmd, nodeInfo.NodeInfo(), nodeInfoJSON)
|
prettyPrintNodeInfo(cmd, res.NodeInfo(), nodeInfoJSON)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,15 +151,15 @@ var netInfoCmd = &cobra.Command{
|
||||||
Short: "Get information about NeoFS network",
|
Short: "Get information about NeoFS network",
|
||||||
Long: "Get information about NeoFS network",
|
Long: "Get information about NeoFS network",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
key, err := getKey()
|
var prm internalclient.NetworkInfoPrm
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cli, err := getSDKClient(key)
|
prepareAPIClient(cmd, &prm)
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
res, err := internalclient.NetworkInfo(prm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
netInfo := res.NetworkInfo()
|
||||||
|
|
||||||
cmd.Printf("Epoch: %d\n", netInfo.CurrentEpoch())
|
cmd.Printf("Epoch: %d\n", netInfo.CurrentEpoch())
|
||||||
|
|
||||||
magic := netInfo.MagicNumber()
|
magic := netInfo.MagicNumber()
|
||||||
|
|
|
@ -1,26 +1,25 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
||||||
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
"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/owner"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||||
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
||||||
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -36,6 +35,8 @@ const (
|
||||||
getRangeHashSaltFlag = "salt"
|
getRangeHashSaltFlag = "salt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const bearerTokenFlag = "bearer"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// objectCmd represents the object command
|
// objectCmd represents the object command
|
||||||
objectCmd = &cobra.Command{
|
objectCmd = &cobra.Command{
|
||||||
|
@ -249,7 +250,7 @@ func init() {
|
||||||
for _, objCommand := range objectChildCommands {
|
for _, objCommand := range objectChildCommands {
|
||||||
flags := objCommand.Flags()
|
flags := objCommand.Flags()
|
||||||
|
|
||||||
flags.String("bearer", "", "File with signed JSON or binary encoded bearer token")
|
flags.String(bearerTokenFlag, "", "File with signed JSON or binary encoded bearer token")
|
||||||
flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage)
|
flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage)
|
||||||
flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage)
|
flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage)
|
||||||
}
|
}
|
||||||
|
@ -273,16 +274,78 @@ func init() {
|
||||||
initObjectRangeCmd()
|
initObjectRangeCmd()
|
||||||
}
|
}
|
||||||
|
|
||||||
func initSession(ctx context.Context, key *ecdsa.PrivateKey) (client.Client, *session.Token, error) {
|
type clientKeySession interface {
|
||||||
cli, err := getSDKClient(key)
|
clientWithKey
|
||||||
if err != nil {
|
SetSessionToken(*session.Token)
|
||||||
return nil, nil, fmt.Errorf("can't create client: %w", err)
|
}
|
||||||
|
|
||||||
|
func prepareSessionPrm(cmd *cobra.Command, prms ...clientKeySession) {
|
||||||
|
key, err := getKey()
|
||||||
|
exitOnErr(cmd, errf("get private key: %w", err))
|
||||||
|
|
||||||
|
prepareSessionPrmWithKey(cmd, key, prms...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareSessionPrmWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, prms ...clientKeySession) {
|
||||||
|
ownerID, err := getOwnerID(key)
|
||||||
|
exitOnErr(cmd, errf("owner ID from key: %w", err))
|
||||||
|
|
||||||
|
prepareSessionPrmWithOwner(cmd, key, ownerID, prms...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareSessionPrmWithOwner(
|
||||||
|
cmd *cobra.Command,
|
||||||
|
key *ecdsa.PrivateKey,
|
||||||
|
ownerID *owner.ID,
|
||||||
|
prms ...clientKeySession,
|
||||||
|
) {
|
||||||
|
var sessionPrm internalclient.CreateSessionPrm
|
||||||
|
|
||||||
|
cws := make([]clientWithKey, 1, len(prms)+1)
|
||||||
|
cws[0] = &sessionPrm
|
||||||
|
|
||||||
|
for i := range prms {
|
||||||
|
cws = append(cws, prms[i])
|
||||||
}
|
}
|
||||||
tok, err := cli.CreateSession(ctx, math.MaxUint64)
|
|
||||||
if err != nil {
|
prepareAPIClientWithKey(cmd, key, cws...)
|
||||||
return nil, nil, fmt.Errorf("can't create session: %w", err)
|
|
||||||
|
sessionRes, err := internalclient.CreateSession(sessionPrm)
|
||||||
|
exitOnErr(cmd, errf("open session: %w", err))
|
||||||
|
|
||||||
|
tok := session.NewToken()
|
||||||
|
tok.SetID(sessionRes.ID())
|
||||||
|
tok.SetSessionKey(sessionRes.SessionKey())
|
||||||
|
tok.SetOwnerID(ownerID)
|
||||||
|
|
||||||
|
for i := range prms {
|
||||||
|
prms[i].SetSessionToken(tok)
|
||||||
}
|
}
|
||||||
return cli, tok, nil
|
}
|
||||||
|
|
||||||
|
type objectPrm interface {
|
||||||
|
bearerPrm
|
||||||
|
SetTTL(uint32)
|
||||||
|
SetXHeaders([]*pkg.XHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareObjectPrm(cmd *cobra.Command, prms ...objectPrm) {
|
||||||
|
for i := range prms {
|
||||||
|
prepareBearerPrm(cmd, prms[i])
|
||||||
|
|
||||||
|
prms[i].SetTTL(getTTL())
|
||||||
|
prms[i].SetXHeaders(parseXHeaders())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareObjectPrmRaw(cmd *cobra.Command, prm interface {
|
||||||
|
objectPrm
|
||||||
|
SetRawFlag(bool)
|
||||||
|
}) {
|
||||||
|
prepareObjectPrm(cmd, prm)
|
||||||
|
|
||||||
|
raw, _ := cmd.Flags().GetBool(rawFlag)
|
||||||
|
prm.SetRawFlag(raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
func putObject(cmd *cobra.Command, _ []string) {
|
func putObject(cmd *cobra.Command, _ []string) {
|
||||||
|
@ -328,56 +391,40 @@ func putObject(cmd *cobra.Command, _ []string) {
|
||||||
obj.SetOwnerID(ownerID)
|
obj.SetOwnerID(ownerID)
|
||||||
obj.SetAttributes(attrs...)
|
obj.SetAttributes(attrs...)
|
||||||
|
|
||||||
ctx := context.Background()
|
var prm internalclient.PutObjectPrm
|
||||||
cli, tok, err := initSession(ctx, key)
|
|
||||||
exitOnErr(cmd, err)
|
prepareSessionPrmWithOwner(cmd, key, ownerID, &prm)
|
||||||
btok, err := getBearerToken(cmd, "bearer")
|
prepareObjectPrm(cmd, &prm)
|
||||||
exitOnErr(cmd, err)
|
prm.SetHeader(obj.Object())
|
||||||
oid, err := cli.PutObject(ctx,
|
prm.SetPayloadReader(f)
|
||||||
new(client.PutObjectParams).
|
|
||||||
WithObject(obj.Object()).
|
res, err := internalclient.PutObject(prm)
|
||||||
WithPayloadReader(f),
|
|
||||||
append(globalCallOptions(),
|
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(btok),
|
|
||||||
)...,
|
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
cmd.Printf("[%s] Object successfully stored\n", filename)
|
cmd.Printf("[%s] Object successfully stored\n", filename)
|
||||||
cmd.Printf(" ID: %s\n CID: %s\n", oid, cid)
|
cmd.Printf(" ID: %s\n CID: %s\n", res.ID(), cid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteObject(cmd *cobra.Command, _ []string) {
|
func deleteObject(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
objAddr, err := getObjectAddress(cmd)
|
objAddr, err := getObjectAddress(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
ctx := context.Background()
|
var prm internalclient.DeleteObjectPrm
|
||||||
cli, tok, err := initSession(ctx, key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
btok, err := getBearerToken(cmd, "bearer")
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
tombstoneAddr, err := client.DeleteObject(ctx, cli,
|
prepareSessionPrm(cmd, &prm)
|
||||||
new(client.DeleteObjectParams).WithAddress(objAddr),
|
prepareObjectPrm(cmd, &prm)
|
||||||
append(globalCallOptions(),
|
prm.SetAddress(objAddr)
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(btok),
|
res, err := internalclient.DeleteObject(prm)
|
||||||
)...,
|
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
tombstoneAddr := res.TombstoneAddress()
|
||||||
|
|
||||||
cmd.Println("Object removed successfully.")
|
cmd.Println("Object removed successfully.")
|
||||||
cmd.Printf(" ID: %s\n CID: %s\n", tombstoneAddr.ObjectID(), tombstoneAddr.ContainerID())
|
cmd.Printf(" ID: %s\n CID: %s\n", tombstoneAddr.ObjectID(), tombstoneAddr.ContainerID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func getObject(cmd *cobra.Command, _ []string) {
|
func getObject(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
objAddr, err := getObjectAddress(cmd)
|
objAddr, err := getObjectAddress(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
|
@ -396,24 +443,14 @@ func getObject(cmd *cobra.Command, _ []string) {
|
||||||
out = f
|
out = f
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
var prm internalclient.GetObjectPrm
|
||||||
cli, tok, err := initSession(ctx, key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
btok, err := getBearerToken(cmd, "bearer")
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
raw, _ := cmd.Flags().GetBool(rawFlag)
|
prepareSessionPrm(cmd, &prm)
|
||||||
|
prepareObjectPrmRaw(cmd, &prm)
|
||||||
|
prm.SetAddress(objAddr)
|
||||||
|
prm.SetPayloadWriter(out)
|
||||||
|
|
||||||
obj, err := cli.GetObject(ctx,
|
res, err := internalclient.GetObject(prm)
|
||||||
new(client.GetObjectParams).
|
|
||||||
WithAddress(objAddr).
|
|
||||||
WithPayloadWriter(out).
|
|
||||||
WithRawFlag(raw),
|
|
||||||
append(globalCallOptions(),
|
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(btok),
|
|
||||||
)...,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ok := printSplitInfoErr(cmd, err); ok {
|
if ok := printSplitInfoErr(cmd, err); ok {
|
||||||
return
|
return
|
||||||
|
@ -429,37 +466,25 @@ func getObject(cmd *cobra.Command, _ []string) {
|
||||||
// Print header only if file is not streamed to stdout.
|
// Print header only if file is not streamed to stdout.
|
||||||
hdrFile := cmd.Flag("header").Value.String()
|
hdrFile := cmd.Flag("header").Value.String()
|
||||||
if filename != "" || hdrFile != "" {
|
if filename != "" || hdrFile != "" {
|
||||||
err = saveAndPrintHeader(cmd, obj, hdrFile)
|
err = saveAndPrintHeader(cmd, res.Header(), hdrFile)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getObjectHeader(cmd *cobra.Command, _ []string) {
|
func getObjectHeader(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
objAddr, err := getObjectAddress(cmd)
|
objAddr, err := getObjectAddress(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
ctx := context.Background()
|
mainOnly, _ := cmd.Flags().GetBool("main-only")
|
||||||
cli, tok, err := initSession(ctx, key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
btok, err := getBearerToken(cmd, "bearer")
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
ps := new(client.ObjectHeaderParams).WithAddress(objAddr)
|
|
||||||
if ok, _ := cmd.Flags().GetBool("main-only"); ok {
|
|
||||||
ps = ps.WithMainFields()
|
|
||||||
}
|
|
||||||
|
|
||||||
raw, _ := cmd.Flags().GetBool(rawFlag)
|
var prm internalclient.HeadObjectPrm
|
||||||
ps.WithRawFlag(raw)
|
|
||||||
|
|
||||||
obj, err := cli.GetObjectHeader(ctx, ps,
|
prepareSessionPrm(cmd, &prm)
|
||||||
append(globalCallOptions(),
|
prepareObjectPrmRaw(cmd, &prm)
|
||||||
client.WithSession(tok),
|
prm.SetAddress(objAddr)
|
||||||
client.WithBearer(btok),
|
prm.SetMainOnlyFlag(mainOnly)
|
||||||
)...,
|
|
||||||
)
|
res, err := internalclient.HeadObject(prm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ok := printSplitInfoErr(cmd, err); ok {
|
if ok := printSplitInfoErr(cmd, err); ok {
|
||||||
return
|
return
|
||||||
|
@ -468,33 +493,29 @@ func getObjectHeader(cmd *cobra.Command, _ []string) {
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
err = saveAndPrintHeader(cmd, obj, cmd.Flag("file").Value.String())
|
err = saveAndPrintHeader(cmd, res.Header(), cmd.Flag("file").Value.String())
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func searchObject(cmd *cobra.Command, _ []string) {
|
func searchObject(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cid, err := getCID(cmd)
|
cid, err := getCID(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
sf, err := parseSearchFilters(cmd)
|
sf, err := parseSearchFilters(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
ctx := context.Background()
|
var prm internalclient.SearchObjectsPrm
|
||||||
cli, tok, err := initSession(ctx, key)
|
|
||||||
exitOnErr(cmd, err)
|
prepareSessionPrm(cmd, &prm)
|
||||||
btok, err := getBearerToken(cmd, "bearer")
|
prepareObjectPrm(cmd, &prm)
|
||||||
exitOnErr(cmd, err)
|
prm.SetContainerID(cid)
|
||||||
ps := new(client.SearchObjectParams).WithContainerID(cid).WithSearchFilters(sf)
|
prm.SetFilters(sf)
|
||||||
ids, err := cli.SearchObject(ctx, ps,
|
|
||||||
append(globalCallOptions(),
|
res, err := internalclient.SearchObjects(prm)
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(btok),
|
|
||||||
)...,
|
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
ids := res.IDList()
|
||||||
|
|
||||||
cmd.Printf("Found %d objects.\n", len(ids))
|
cmd.Printf("Found %d objects.\n", len(ids))
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
cmd.Println(id)
|
cmd.Println(id)
|
||||||
|
@ -502,9 +523,6 @@ func searchObject(cmd *cobra.Command, _ []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getObjectHash(cmd *cobra.Command, _ []string) {
|
func getObjectHash(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, errf("can't fetch private key: %w", err))
|
|
||||||
|
|
||||||
objAddr, err := getObjectAddress(cmd)
|
objAddr, err := getObjectAddress(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
ranges, err := getRangeList(cmd)
|
ranges, err := getRangeList(cmd)
|
||||||
|
@ -517,59 +535,61 @@ func getObjectHash(cmd *cobra.Command, _ []string) {
|
||||||
salt, err := hex.DecodeString(strSalt)
|
salt, err := hex.DecodeString(strSalt)
|
||||||
exitOnErr(cmd, errf("could not decode salt: %w", err))
|
exitOnErr(cmd, errf("could not decode salt: %w", err))
|
||||||
|
|
||||||
ctx := context.Background()
|
var (
|
||||||
cli, tok, err := initSession(ctx, key)
|
hashPrm internalclient.HashPayloadRangesPrm
|
||||||
exitOnErr(cmd, err)
|
headPrm internalclient.HeadObjectPrm
|
||||||
btok, err := getBearerToken(cmd, "bearer")
|
|
||||||
exitOnErr(cmd, err)
|
sesPrms = []clientKeySession{&hashPrm}
|
||||||
if len(ranges) == 0 { // hash of full payload
|
objPrms = []objectPrm{&hashPrm}
|
||||||
obj, err := cli.GetObjectHeader(ctx,
|
)
|
||||||
new(client.ObjectHeaderParams).WithAddress(objAddr),
|
|
||||||
append(globalCallOptions(),
|
fullHash := len(ranges) == 0
|
||||||
client.WithSession(tok),
|
if fullHash {
|
||||||
client.WithBearer(btok),
|
sesPrms = append(sesPrms, &headPrm)
|
||||||
)...,
|
objPrms = append(objPrms, &headPrm)
|
||||||
)
|
}
|
||||||
|
|
||||||
|
prepareSessionPrm(cmd, sesPrms...)
|
||||||
|
prepareObjectPrm(cmd, objPrms...)
|
||||||
|
|
||||||
|
tz := typ == hashTz
|
||||||
|
|
||||||
|
if fullHash {
|
||||||
|
headPrm.SetAddress(objAddr)
|
||||||
|
|
||||||
|
// get hash of full payload through HEAD (may be user can do it through dedicated command?)
|
||||||
|
res, err := internalclient.HeadObject(headPrm)
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
switch typ {
|
|
||||||
case hashSha256:
|
var cs *pkg.Checksum
|
||||||
cmd.Println(hex.EncodeToString(obj.PayloadChecksum().Sum()))
|
|
||||||
case hashTz:
|
if tz {
|
||||||
cmd.Println(hex.EncodeToString(obj.PayloadHomomorphicHash().Sum()))
|
cs = res.Header().PayloadHomomorphicHash()
|
||||||
|
} else {
|
||||||
|
cs = res.Header().PayloadChecksum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd.Println(hex.EncodeToString(cs.Sum()))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ps := new(client.RangeChecksumParams).
|
hashPrm.SetAddress(objAddr)
|
||||||
WithAddress(objAddr).
|
hashPrm.SetSalt(salt)
|
||||||
WithRangeList(ranges...).
|
hashPrm.SetRanges(ranges)
|
||||||
WithSalt(salt)
|
|
||||||
|
|
||||||
switch typ {
|
if tz {
|
||||||
case hashSha256:
|
hashPrm.TZ()
|
||||||
res, err := cli.ObjectPayloadRangeSHA256(ctx, ps,
|
}
|
||||||
append(globalCallOptions(),
|
|
||||||
client.WithSession(tok),
|
res, err := internalclient.HashPayloadRanges(hashPrm)
|
||||||
client.WithBearer(btok),
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
)...,
|
|
||||||
)
|
hs := res.HashList()
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
|
||||||
for i := range res {
|
for i := range hs {
|
||||||
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
|
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
|
||||||
hex.EncodeToString(res[i][:]))
|
hex.EncodeToString(hs[i]))
|
||||||
}
|
|
||||||
case hashTz:
|
|
||||||
res, err := cli.ObjectPayloadRangeTZ(ctx, ps,
|
|
||||||
append(globalCallOptions(),
|
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(btok),
|
|
||||||
)...,
|
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
|
||||||
for i := range res {
|
|
||||||
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
|
|
||||||
hex.EncodeToString(res[i][:]))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -899,9 +919,6 @@ func getBearerToken(cmd *cobra.Command, flagname string) (*token.BearerToken, er
|
||||||
}
|
}
|
||||||
|
|
||||||
func getObjectRange(cmd *cobra.Command, _ []string) {
|
func getObjectRange(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, errf("can't fetch private key: %w", err))
|
|
||||||
|
|
||||||
objAddr, err := getObjectAddress(cmd)
|
objAddr, err := getObjectAddress(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
|
@ -928,27 +945,15 @@ func getObjectRange(cmd *cobra.Command, _ []string) {
|
||||||
out = f
|
out = f
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
var prm internalclient.PayloadRangePrm
|
||||||
|
|
||||||
c, sessionToken, err := initSession(ctx, key)
|
prepareSessionPrm(cmd, &prm)
|
||||||
exitOnErr(cmd, err)
|
prepareObjectPrmRaw(cmd, &prm)
|
||||||
|
prm.SetAddress(objAddr)
|
||||||
|
prm.SetRange(ranges[0])
|
||||||
|
prm.SetPayloadWriter(out)
|
||||||
|
|
||||||
bearerToken, err := getBearerToken(cmd, "bearer")
|
_, err = internalclient.PayloadRange(prm)
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
raw, _ := cmd.Flags().GetBool(rawFlag)
|
|
||||||
|
|
||||||
_, err = c.ObjectPayloadRangeData(ctx,
|
|
||||||
new(client.RangeDataParams).
|
|
||||||
WithAddress(objAddr).
|
|
||||||
WithRange(ranges[0]).
|
|
||||||
WithDataWriter(out).
|
|
||||||
WithRaw(raw),
|
|
||||||
append(globalCallOptions(),
|
|
||||||
client.WithSession(sessionToken),
|
|
||||||
client.WithBearer(bearerToken),
|
|
||||||
)...,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ok := printSplitInfoErr(cmd, err); ok {
|
if ok := printSplitInfoErr(cmd, err); ok {
|
||||||
return
|
return
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg"
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
||||||
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||||
"github.com/nspcc-dev/neofs-node/misc"
|
"github.com/nspcc-dev/neofs-node/misc"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/network"
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -287,6 +288,41 @@ func getEndpointAddress(endpointFlag string) (addr network.Address, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type clientWithKey interface {
|
||||||
|
SetClient(client.Client)
|
||||||
|
SetKey(*ecdsa.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// reads private key from command args and call prepareAPIClientWithKey with it.
|
||||||
|
func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
|
||||||
|
key, err := getKey()
|
||||||
|
exitOnErr(cmd, errf("get private key: %w", err))
|
||||||
|
|
||||||
|
prepareAPIClientWithKey(cmd, key, dst...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// creates NeoFS API client and writes it to target along with the private key.
|
||||||
|
func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) {
|
||||||
|
cli, err := getSDKClient(key)
|
||||||
|
exitOnErr(cmd, errf("create API client: %w", err))
|
||||||
|
|
||||||
|
for _, d := range dst {
|
||||||
|
d.SetClient(cli)
|
||||||
|
d.SetKey(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type bearerPrm interface {
|
||||||
|
SetBearerToken(prm *token.BearerToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
|
||||||
|
btok, err := getBearerToken(cmd, bearerTokenFlag)
|
||||||
|
exitOnErr(cmd, errf("bearer token: %w", err))
|
||||||
|
|
||||||
|
prm.SetBearerToken(btok)
|
||||||
|
}
|
||||||
|
|
||||||
// getSDKClient returns default neofs-api-go sdk client. Consider using
|
// getSDKClient returns default neofs-api-go sdk client. Consider using
|
||||||
// opts... to provide TTL or other global configuration flags.
|
// opts... to provide TTL or other global configuration flags.
|
||||||
func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) {
|
func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) {
|
||||||
|
@ -356,19 +392,6 @@ func parseXHeaders() []*pkg.XHeader {
|
||||||
return xs
|
return xs
|
||||||
}
|
}
|
||||||
|
|
||||||
func globalCallOptions() []client.CallOption {
|
|
||||||
xHdrs := parseXHeaders()
|
|
||||||
|
|
||||||
opts := make([]client.CallOption, 0, len(xHdrs)+1) // + TTL
|
|
||||||
opts = append(opts, client.WithTTL(getTTL()))
|
|
||||||
|
|
||||||
for i := range xHdrs {
|
|
||||||
opts = append(opts, client.WithXHeader(xHdrs[i]))
|
|
||||||
}
|
|
||||||
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
|
||||||
// add common flags to the command:
|
// add common flags to the command:
|
||||||
// - key;
|
// - key;
|
||||||
// - wallet;
|
// - wallet;
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
||||||
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
|
||||||
storagegroupAPI "github.com/nspcc-dev/neofs-api-go/pkg/storagegroup"
|
storagegroupAPI "github.com/nspcc-dev/neofs-api-go/pkg/storagegroup"
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -59,7 +57,6 @@ var sgDelCmd = &cobra.Command{
|
||||||
const (
|
const (
|
||||||
sgMembersFlag = "members"
|
sgMembersFlag = "members"
|
||||||
sgIDFlag = "id"
|
sgIDFlag = "id"
|
||||||
sgBearerFlag = "bearer"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -124,7 +121,7 @@ func init() {
|
||||||
for _, sgCommand := range storageGroupChildCommands {
|
for _, sgCommand := range storageGroupChildCommands {
|
||||||
flags := sgCommand.Flags()
|
flags := sgCommand.Flags()
|
||||||
|
|
||||||
flags.String(sgBearerFlag, "", "File with signed JSON or binary encoded bearer token")
|
flags.String(bearerTokenFlag, "", "File with signed JSON or binary encoded bearer token")
|
||||||
flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage)
|
flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage)
|
||||||
flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage)
|
flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage)
|
||||||
}
|
}
|
||||||
|
@ -136,24 +133,13 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type sgHeadReceiver struct {
|
type sgHeadReceiver struct {
|
||||||
ctx context.Context
|
prm internalclient.HeadObjectPrm
|
||||||
|
|
||||||
tok *session.Token
|
|
||||||
|
|
||||||
c client.Client
|
|
||||||
|
|
||||||
bearerToken *token.BearerToken
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *sgHeadReceiver) Head(addr *objectSDK.Address) (interface{}, error) {
|
func (c sgHeadReceiver) Head(addr *objectSDK.Address) (interface{}, error) {
|
||||||
obj, err := c.c.GetObjectHeader(c.ctx,
|
c.prm.SetAddress(addr)
|
||||||
new(client.ObjectHeaderParams).
|
|
||||||
WithAddress(addr).
|
res, err := internalclient.HeadObject(c.prm)
|
||||||
WithRawFlag(true),
|
|
||||||
client.WithTTL(2),
|
|
||||||
client.WithSession(c.tok),
|
|
||||||
client.WithBearer(c.bearerToken),
|
|
||||||
)
|
|
||||||
|
|
||||||
var errSplitInfo *objectSDK.SplitInfoError
|
var errSplitInfo *objectSDK.SplitInfoError
|
||||||
|
|
||||||
|
@ -161,16 +147,12 @@ func (c *sgHeadReceiver) Head(addr *objectSDK.Address) (interface{}, error) {
|
||||||
default:
|
default:
|
||||||
return nil, err
|
return nil, err
|
||||||
case err == nil:
|
case err == nil:
|
||||||
return object.NewFromSDK(obj), nil
|
return object.NewFromSDK(res.Header()), nil
|
||||||
case errors.As(err, &errSplitInfo):
|
case errors.As(err, &errSplitInfo):
|
||||||
return errSplitInfo.SplitInfo(), nil
|
return errSplitInfo.SplitInfo(), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sgBearerToken(cmd *cobra.Command) (*token.BearerToken, error) {
|
|
||||||
return getBearerToken(cmd, sgBearerFlag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func putSG(cmd *cobra.Command, _ []string) {
|
func putSG(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
key, err := getKey()
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
@ -192,19 +174,18 @@ func putSG(cmd *cobra.Command, _ []string) {
|
||||||
members = append(members, id)
|
members = append(members, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
bearerToken, err := sgBearerToken(cmd)
|
var (
|
||||||
exitOnErr(cmd, err)
|
headPrm internalclient.HeadObjectPrm
|
||||||
|
putPrm internalclient.PutObjectPrm
|
||||||
|
)
|
||||||
|
|
||||||
ctx := context.Background()
|
prepareSessionPrmWithOwner(cmd, key, ownerID, &headPrm, &putPrm)
|
||||||
|
prepareObjectPrm(cmd, &headPrm, &putPrm)
|
||||||
|
|
||||||
cli, tok, err := initSession(ctx, key)
|
headPrm.SetRawFlag(true)
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
sg, err := storagegroup.CollectMembers(&sgHeadReceiver{
|
sg, err := storagegroup.CollectMembers(sgHeadReceiver{
|
||||||
ctx: ctx,
|
prm: headPrm,
|
||||||
tok: tok,
|
|
||||||
c: cli,
|
|
||||||
bearerToken: bearerToken,
|
|
||||||
}, cid, members)
|
}, cid, members)
|
||||||
exitOnErr(cmd, errf("could not collect storage group members: %w", err))
|
exitOnErr(cmd, errf("could not collect storage group members: %w", err))
|
||||||
|
|
||||||
|
@ -215,20 +196,15 @@ func putSG(cmd *cobra.Command, _ []string) {
|
||||||
obj.SetContainerID(cid)
|
obj.SetContainerID(cid)
|
||||||
obj.SetOwnerID(ownerID)
|
obj.SetOwnerID(ownerID)
|
||||||
obj.SetType(objectSDK.TypeStorageGroup)
|
obj.SetType(objectSDK.TypeStorageGroup)
|
||||||
obj.SetPayload(sgContent)
|
|
||||||
|
|
||||||
oid, err := cli.PutObject(ctx,
|
putPrm.SetHeader(obj.Object())
|
||||||
new(client.PutObjectParams).
|
putPrm.SetPayloadReader(bytes.NewReader(sgContent))
|
||||||
WithObject(obj.Object()),
|
|
||||||
append(globalCallOptions(),
|
res, err := internalclient.PutObject(putPrm)
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(bearerToken),
|
|
||||||
)...,
|
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
cmd.Println("Storage group successfully stored")
|
cmd.Println("Storage group successfully stored")
|
||||||
cmd.Printf(" ID: %s\n CID: %s\n", oid, cid)
|
cmd.Printf(" ID: %s\n CID: %s\n", res.ID(), cid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSGID() (*objectSDK.ID, error) {
|
func getSGID() (*objectSDK.ID, error) {
|
||||||
|
@ -242,40 +218,31 @@ func getSGID() (*objectSDK.ID, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSG(cmd *cobra.Command, _ []string) {
|
func getSG(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cid, err := getCID(cmd)
|
cid, err := getCID(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
id, err := getSGID()
|
id, err := getSGID()
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
bearerToken, err := sgBearerToken(cmd)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
addr := objectSDK.NewAddress()
|
addr := objectSDK.NewAddress()
|
||||||
addr.SetContainerID(cid)
|
addr.SetContainerID(cid)
|
||||||
addr.SetObjectID(id)
|
addr.SetObjectID(id)
|
||||||
|
|
||||||
ctx := context.Background()
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
cli, tok, err := initSession(ctx, key)
|
var prm internalclient.GetObjectPrm
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
obj, err := cli.GetObject(ctx,
|
prepareSessionPrm(cmd, &prm)
|
||||||
new(client.GetObjectParams).
|
prepareObjectPrmRaw(cmd, &prm)
|
||||||
WithAddress(addr),
|
prm.SetAddress(addr)
|
||||||
append(globalCallOptions(),
|
prm.SetPayloadWriter(buf)
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(bearerToken),
|
_, err = internalclient.GetObject(prm)
|
||||||
)...,
|
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
sg := storagegroupAPI.New()
|
sg := storagegroupAPI.New()
|
||||||
|
|
||||||
err = sg.Unmarshal(obj.Payload())
|
err = sg.Unmarshal(buf.Bytes())
|
||||||
exitOnErr(cmd, errf("could not unmarshal storage group: %w", err))
|
exitOnErr(cmd, errf("could not unmarshal storage group: %w", err))
|
||||||
|
|
||||||
cmd.Printf("Expiration epoch: %d\n", sg.ExpirationEpoch())
|
cmd.Printf("Expiration epoch: %d\n", sg.ExpirationEpoch())
|
||||||
|
@ -292,31 +259,21 @@ func getSG(cmd *cobra.Command, _ []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func listSG(cmd *cobra.Command, _ []string) {
|
func listSG(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cid, err := getCID(cmd)
|
cid, err := getCID(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
bearerToken, err := sgBearerToken(cmd)
|
var prm internalclient.SearchObjectsPrm
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
prepareSessionPrm(cmd, &prm)
|
||||||
|
prepareObjectPrm(cmd, &prm)
|
||||||
|
prm.SetContainerID(cid)
|
||||||
|
prm.SetFilters(storagegroup.SearchQuery())
|
||||||
|
|
||||||
cli, tok, err := initSession(ctx, key)
|
res, err := internalclient.SearchObjects(prm)
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
ids, err := cli.SearchObject(ctx,
|
|
||||||
new(client.SearchObjectParams).
|
|
||||||
WithContainerID(cid).
|
|
||||||
WithSearchFilters(storagegroup.SearchQuery()),
|
|
||||||
append(globalCallOptions(),
|
|
||||||
client.WithSession(tok),
|
|
||||||
client.WithBearer(bearerToken),
|
|
||||||
)...,
|
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
ids := res.IDList()
|
||||||
|
|
||||||
cmd.Printf("Found %d storage groups.\n", len(ids))
|
cmd.Printf("Found %d storage groups.\n", len(ids))
|
||||||
|
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
|
@ -325,37 +282,27 @@ func listSG(cmd *cobra.Command, _ []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func delSG(cmd *cobra.Command, _ []string) {
|
func delSG(cmd *cobra.Command, _ []string) {
|
||||||
key, err := getKey()
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
cid, err := getCID(cmd)
|
cid, err := getCID(cmd)
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
id, err := getSGID()
|
id, err := getSGID()
|
||||||
exitOnErr(cmd, err)
|
exitOnErr(cmd, err)
|
||||||
|
|
||||||
bearerToken, err := sgBearerToken(cmd)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
cli, tok, err := initSession(ctx, key)
|
|
||||||
exitOnErr(cmd, err)
|
|
||||||
|
|
||||||
addr := objectSDK.NewAddress()
|
addr := objectSDK.NewAddress()
|
||||||
addr.SetContainerID(cid)
|
addr.SetContainerID(cid)
|
||||||
addr.SetObjectID(id)
|
addr.SetObjectID(id)
|
||||||
|
|
||||||
tombstone, err := client.DeleteObject(ctx, cli,
|
var prm internalclient.DeleteObjectPrm
|
||||||
new(client.DeleteObjectParams).
|
|
||||||
WithAddress(addr),
|
prepareSessionPrm(cmd, &prm)
|
||||||
append(globalCallOptions(),
|
prepareObjectPrm(cmd, &prm)
|
||||||
client.WithSession(tok),
|
prm.SetAddress(addr)
|
||||||
client.WithBearer(bearerToken),
|
|
||||||
)...,
|
res, err := internalclient.DeleteObject(prm)
|
||||||
)
|
|
||||||
exitOnErr(cmd, errf("rpc error: %w", err))
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
||||||
|
|
||||||
|
tombstone := res.TombstoneAddress()
|
||||||
|
|
||||||
cmd.Println("Storage group removed successfully.")
|
cmd.Println("Storage group removed successfully.")
|
||||||
cmd.Printf(" Tombstone: %s\n", tombstone.ObjectID())
|
cmd.Printf(" Tombstone: %s\n", tombstone.ObjectID())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue