generated from TrueCloudLab/basic
89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package contract
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"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 listTargetsCmd = &cobra.Command{
|
|
Use: "list-targets",
|
|
Short: "Invoke 'listTargets' method",
|
|
Long: "Invoke 'listTargets' method in policy contract and print results to stdout",
|
|
Example: `policy-reader contract -r http://localhost:40332 list-targets
|
|
policy-reader contract -r http://localhost:40332 --policy-hash 81c1a41d09e08087a4b679418b12be5d3ab15742 list-targets --type container`,
|
|
RunE: runListTargetsCmd,
|
|
}
|
|
|
|
var errUnknownTargetType = errors.New("unknown target type")
|
|
|
|
const (
|
|
typeFlag = "type"
|
|
)
|
|
|
|
func initListTargetsCmd() {
|
|
listTargetsCmd.Flags().String(typeFlag, "namespace", "Target type to list (namespace/container/group/user/iam)")
|
|
}
|
|
|
|
func runListTargetsCmd(cmd *cobra.Command, _ []string) error {
|
|
typ := viper.GetString(typeFlag)
|
|
entity, err := parseTargetType(typ)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
endpoint := viper.GetString(rpcEndpointFlag)
|
|
|
|
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, "listTargets", entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Printf("%s targets: %d\n", typ, len(res))
|
|
|
|
for _, re := range res {
|
|
bytes, err := re.TryBytes()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Printf("%s\t(base64: '%s')\n", string(bytes), base64.StdEncoding.EncodeToString(bytes))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func parseTargetType(typ string) (*big.Int, error) {
|
|
switch typ {
|
|
case "namespace":
|
|
return big.NewInt(int64(policycontract.Namespace)), nil
|
|
case "container":
|
|
return big.NewInt(int64(policycontract.Container)), nil
|
|
case "user":
|
|
return big.NewInt(int64(policycontract.User)), nil
|
|
case "group":
|
|
return big.NewInt(int64(policycontract.Group)), nil
|
|
case "iam":
|
|
return big.NewInt(int64(policycontract.IAM)), nil
|
|
}
|
|
|
|
return nil, errUnknownTargetType
|
|
}
|