policy-reader/modules/chains/list_container.go
Denis Kirillov c40ad3627d Add listing user/container chains by name
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
2024-08-29 09:42:39 +03:00

81 lines
2.8 KiB
Go

package chains
import (
"math/big"
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
"git.frostfs.info/dkirillov/policy-reader/internal/resolver"
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var listContainerCmd = &cobra.Command{
Use: "list-container",
Short: "List container related policies",
Long: "List container related policies along with filtering by service (s3/storage)",
Example: `policy-reader container -r http://localhost:40332 list --container 7h7NcXcF6k6b1yidqEHc1jkyXUm1MfUDrrTuHAefhiDe
policy-reader container -r http://localhost:40332 --policy-hash 81c1a41d09e08087a4b679418b12be5d3ab15742 list --container 7h7NcXcF6k6b1yidqEHc1jkyXUm1MfUDrrTuHAefhiDe --namespace test`,
RunE: runListContainerCmd,
}
const (
containerFlag = "container"
)
func initListContainerCmd() {
listContainerCmd.Flags().String(containerFlag, "", "Container id or bucket name in nns (if name is provided than 'namespace' should be set too)")
listContainerCmd.Flags().String(namespaceFlag, "", "Namespace where container name will be looked up")
listContainerCmd.Flags().String(serviceFlag, "none", "Service (none/s3/storage) to filter chains")
listContainerCmd.Flags().Bool(decodeIDFlag, false, "Use this flag to additionally decode chain id")
_ = listContainerCmd.MarkFlagRequired(containerFlag)
}
func runListContainerCmd(cmd *cobra.Command, _ []string) error {
service, err := parseService(viper.GetString(serviceFlag))
if err != nil {
return err
}
endpoint := viper.GetString(rpcEndpointFlag)
ns := viper.GetString(namespaceFlag)
cnrID, err := resolver.ResolveContainerID(endpoint, ns, viper.GetString(containerFlag))
policyHash, err := resolver.ResolveContractHash(endpoint, viper.GetString(policyHashFlag))
if err != nil {
return err
}
rpcCli, err := rpcclient.New(cmd.Context(), endpoint, rpcclient.Options{})
if err != nil {
return err
}
res, err := commonclient.ReadIteratorItems(invoker.New(rpcCli, nil), 100, policyHash, "iteratorChainsByPrefix", big.NewInt(int64(policycontract.Namespace)), ns, string(service))
if err != nil {
return err
}
decodeID := viper.GetBool(decodeIDFlag)
cmd.Printf("namespace '%s' policies: %d\n", ns, len(res))
if err = printChains(cmd, res, decodeID); err != nil {
return err
}
res, err = commonclient.ReadIteratorItems(invoker.New(rpcCli, nil), 100, policyHash, "iteratorChainsByPrefix", big.NewInt(int64(policycontract.Container)), cnrID.String(), string(service))
if err != nil {
return err
}
cmd.Printf("container policies: %d\n", len(res))
if err = printChains(cmd, res, decodeID); err != nil {
return err
}
return nil
}