[#172] client/container: Support listing containers with containersOf

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-03-24 11:55:14 +03:00 committed by Gitea
parent bf7d80f44b
commit 9e54646248
2 changed files with 60 additions and 6 deletions

View file

@ -26,6 +26,7 @@ const (
deleteMethod = "delete" deleteMethod = "delete"
getMethod = "get" getMethod = "get"
listMethod = "list" listMethod = "list"
containersOfMethod = "containersOf"
eaclMethod = "eACL" eaclMethod = "eACL"
setEACLMethod = "setEACL" setEACLMethod = "setEACL"

View file

@ -0,0 +1,53 @@
package container
import (
"errors"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// ContainersOf returns a list of container identifiers belonging
// to the specified user of FrostFS system. If idUser is nil, returns the list of all containers.
//
// If remote RPC does not support neo-go session API, fallback to List() method.
func (c *Client) ContainersOf(idUser *user.ID) ([]cid.ID, error) {
var rawID []byte
if idUser != nil {
rawID = idUser.WalletBytes()
}
var cidList []cid.ID
cb := func(item stackitem.Item) error {
rawID, err := client.BytesFromStackItem(item)
if err != nil {
return fmt.Errorf("could not get byte array from stack item (%s): %w", containersOfMethod, err)
}
var id cid.ID
err = id.Decode(rawID)
if err != nil {
return fmt.Errorf("decode container ID: %w", err)
}
cidList = append(cidList, id)
return nil
}
cnrHash := c.client.ContractAddress()
err := c.client.Morph().TestInvokeIterator(cb, cnrHash, containersOfMethod, rawID)
if err != nil {
if errors.Is(err, unwrap.ErrNoSessionID) {
return c.List(idUser)
}
return nil, err
}
return cidList, nil
}