frostfs-node/pkg/morph/client/container/list.go

59 lines
1.6 KiB
Go
Raw Normal View History

2020-07-24 13:54:03 +00:00
package container
import (
"fmt"
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
2020-07-24 13:54:03 +00:00
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/owner"
2020-07-24 13:54:03 +00:00
)
// List returns a list of container identifiers belonging
// to the specified owner of NeoFS system. The list is composed
// through Container contract call.
//
// Returns the identifiers of all NeoFS containers if pointer
// to owner identifier is nil.
func (c *Client) List(ownerID *owner.ID) ([]*cid.ID, error) {
var rawID []byte
if ownerID == nil {
rawID = []byte{}
} else if v2 := ownerID.ToV2(); v2 == nil {
return nil, errUnsupported // use other major version if there any
} else {
rawID = v2.GetValue()
}
2020-07-24 13:54:03 +00:00
prm := client.TestInvokePrm{}
prm.SetMethod(listMethod)
prm.SetArgs(rawID)
2020-07-24 13:54:03 +00:00
res, err := c.client.TestInvoke(prm)
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listMethod, err)
} else if ln := len(res); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", listMethod, ln)
2020-07-24 13:54:03 +00:00
}
res, err = client.ArrayFromStackItem(res[0])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", listMethod, err)
2020-07-24 13:54:03 +00:00
}
cidList := make([]*cid.ID, 0, len(res))
for i := range res {
rawCid, err := client.BytesFromStackItem(res[i])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", listMethod, err)
2020-07-24 13:54:03 +00:00
}
v2 := new(v2refs.ContainerID)
v2.SetValue(rawCid)
cidList = append(cidList, cid.NewFromV2(v2))
2020-07-24 13:54:03 +00:00
}
return cidList, nil
2020-07-24 13:54:03 +00:00
}