forked from TrueCloudLab/frostfs-node
[#1501] cli: Refactor ape-manager subcommands
* Refactor ape-manager subcommands * Use `cmd/internal/common/ape` parser commands within ape-manager subcommands * Use flag names from `cmd/internal/common/ape` Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
parent
daff77b273
commit
3b1364e4cf
3 changed files with 39 additions and 107 deletions
|
@ -1,45 +1,19 @@
|
||||||
package apemanager
|
package apemanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"fmt"
|
||||||
"errors"
|
|
||||||
|
|
||||||
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
||||||
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
||||||
apeCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/ape"
|
apeCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/ape"
|
||||||
parseutil "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/ape"
|
|
||||||
apeSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
|
apeSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
|
||||||
client_sdk "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
client_sdk "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
||||||
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
||||||
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
chainIDFlag = "chain-id"
|
|
||||||
chainIDHexFlag = "chain-id-hex"
|
|
||||||
ruleFlag = "rule"
|
|
||||||
pathFlag = "path"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
targetNameFlag = "target-name"
|
|
||||||
targetNameDesc = "Resource name in APE resource name format"
|
|
||||||
targetTypeFlag = "target-type"
|
|
||||||
targetTypeDesc = "Resource type(container/namespace)"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
namespaceTarget = "namespace"
|
|
||||||
containerTarget = "container"
|
|
||||||
userTarget = "user"
|
|
||||||
groupTarget = "group"
|
|
||||||
)
|
|
||||||
|
|
||||||
var errUnknownTargetType = errors.New("unknown target type")
|
|
||||||
|
|
||||||
var addCmd = &cobra.Command{
|
var addCmd = &cobra.Command{
|
||||||
Use: "add",
|
Use: "add",
|
||||||
Short: "Add rule chain for a target",
|
Short: "Add rule chain for a target",
|
||||||
|
@ -50,55 +24,28 @@ var addCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTarget(cmd *cobra.Command) (ct apeSDK.ChainTarget) {
|
func parseTarget(cmd *cobra.Command) (ct apeSDK.ChainTarget) {
|
||||||
typ, _ := cmd.Flags().GetString(targetTypeFlag)
|
t := apeCmd.ParseTarget(cmd)
|
||||||
name, _ := cmd.Flags().GetString(targetNameFlag)
|
|
||||||
|
|
||||||
ct.Name = name
|
ct.Name = t.Name
|
||||||
|
|
||||||
switch typ {
|
switch t.Type {
|
||||||
case namespaceTarget:
|
case engine.Namespace:
|
||||||
ct.TargetType = apeSDK.TargetTypeNamespace
|
ct.TargetType = apeSDK.TargetTypeNamespace
|
||||||
case containerTarget:
|
case engine.Container:
|
||||||
var cnr cid.ID
|
|
||||||
commonCmd.ExitOnErr(cmd, "can't decode container ID: %w", cnr.DecodeString(name))
|
|
||||||
ct.TargetType = apeSDK.TargetTypeContainer
|
ct.TargetType = apeSDK.TargetTypeContainer
|
||||||
case userTarget:
|
case engine.User:
|
||||||
ct.TargetType = apeSDK.TargetTypeUser
|
ct.TargetType = apeSDK.TargetTypeUser
|
||||||
case groupTarget:
|
case engine.Group:
|
||||||
ct.TargetType = apeSDK.TargetTypeGroup
|
ct.TargetType = apeSDK.TargetTypeGroup
|
||||||
default:
|
default:
|
||||||
commonCmd.ExitOnErr(cmd, "read target type error: %w", errUnknownTargetType)
|
commonCmd.ExitOnErr(cmd, "conversion error: %w", fmt.Errorf("unknown type '%c'", t.Type))
|
||||||
}
|
}
|
||||||
return ct
|
return ct
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseChain(cmd *cobra.Command) apeSDK.Chain {
|
func parseChain(cmd *cobra.Command) apeSDK.Chain {
|
||||||
chainID, _ := cmd.Flags().GetString(chainIDFlag)
|
c := apeCmd.ParseChain(cmd)
|
||||||
hexEncoded, _ := cmd.Flags().GetBool(chainIDHexFlag)
|
serialized := c.Bytes()
|
||||||
|
|
||||||
chainIDRaw := []byte(chainID)
|
|
||||||
|
|
||||||
if hexEncoded {
|
|
||||||
var err error
|
|
||||||
chainIDRaw, err = hex.DecodeString(chainID)
|
|
||||||
commonCmd.ExitOnErr(cmd, "can't decode chain ID as hex: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
chain := new(apechain.Chain)
|
|
||||||
chain.ID = apechain.ID(chainIDRaw)
|
|
||||||
|
|
||||||
if rules, _ := cmd.Flags().GetStringArray(ruleFlag); len(rules) > 0 {
|
|
||||||
commonCmd.ExitOnErr(cmd, "parser error: %w", parseutil.ParseAPEChain(chain, rules))
|
|
||||||
} else if encPath, _ := cmd.Flags().GetString(pathFlag); encPath != "" {
|
|
||||||
commonCmd.ExitOnErr(cmd, "decode binary or json error: %w", parseutil.ParseAPEChainBinaryOrJSON(chain, encPath))
|
|
||||||
} else {
|
|
||||||
commonCmd.ExitOnErr(cmd, "parser error: %w", errors.New("rule is not passed"))
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.Println("Parsed chain:")
|
|
||||||
apeCmd.PrintHumanReadableAPEChain(cmd, chain)
|
|
||||||
|
|
||||||
serialized := chain.Bytes()
|
|
||||||
return apeSDK.Chain{
|
return apeSDK.Chain{
|
||||||
Raw: serialized,
|
Raw: serialized,
|
||||||
}
|
}
|
||||||
|
@ -127,13 +74,13 @@ func initAddCmd() {
|
||||||
commonflags.Init(addCmd)
|
commonflags.Init(addCmd)
|
||||||
|
|
||||||
ff := addCmd.Flags()
|
ff := addCmd.Flags()
|
||||||
ff.StringArray(ruleFlag, []string{}, "Rule statement")
|
ff.StringArray(apeCmd.RuleFlag, []string{}, apeCmd.RuleFlagDesc)
|
||||||
ff.String(pathFlag, "", "Path to encoded chain in JSON or binary format")
|
ff.String(apeCmd.PathFlag, "", apeCmd.PathFlagDesc)
|
||||||
ff.String(chainIDFlag, "", "Assign ID to the parsed chain")
|
ff.String(apeCmd.ChainIDFlag, "", apeCmd.ChainIDFlagDesc)
|
||||||
ff.String(targetNameFlag, "", targetNameDesc)
|
ff.String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
|
||||||
ff.String(targetTypeFlag, "", targetTypeDesc)
|
ff.String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
|
||||||
_ = addCmd.MarkFlagRequired(targetTypeFlag)
|
_ = addCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
||||||
ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex")
|
ff.Bool(apeCmd.ChainIDHexFlag, false, apeCmd.ChainIDHexFlagDesc)
|
||||||
|
|
||||||
addCmd.MarkFlagsMutuallyExclusive(pathFlag, ruleFlag)
|
addCmd.MarkFlagsMutuallyExclusive(apeCmd.PathFlag, apeCmd.RuleFlag)
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ func initListCmd() {
|
||||||
commonflags.Init(listCmd)
|
commonflags.Init(listCmd)
|
||||||
|
|
||||||
ff := listCmd.Flags()
|
ff := listCmd.Flags()
|
||||||
ff.String(targetNameFlag, "", targetNameDesc)
|
ff.String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
|
||||||
ff.String(targetTypeFlag, "", targetTypeDesc)
|
ff.String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
|
||||||
_ = listCmd.MarkFlagRequired(targetTypeFlag)
|
_ = listCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,16 @@
|
||||||
package apemanager
|
package apemanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
||||||
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
||||||
|
apeCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/ape"
|
||||||
client_sdk "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
client_sdk "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var removeCmd = &cobra.Command{
|
||||||
errEmptyChainID = errors.New("chain id cannot be empty")
|
|
||||||
|
|
||||||
removeCmd = &cobra.Command{
|
|
||||||
Use: "remove",
|
Use: "remove",
|
||||||
Short: "Remove rule chain for a target",
|
Short: "Remove rule chain for a target",
|
||||||
Run: remove,
|
Run: remove,
|
||||||
|
@ -23,7 +18,6 @@ var (
|
||||||
commonflags.Bind(cmd)
|
commonflags.Bind(cmd)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
func remove(cmd *cobra.Command, _ []string) {
|
func remove(cmd *cobra.Command, _ []string) {
|
||||||
target := parseTarget(cmd)
|
target := parseTarget(cmd)
|
||||||
|
@ -31,19 +25,9 @@ func remove(cmd *cobra.Command, _ []string) {
|
||||||
key := key.Get(cmd)
|
key := key.Get(cmd)
|
||||||
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
|
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
|
||||||
|
|
||||||
chainID, _ := cmd.Flags().GetString(chainIDFlag)
|
chainID := apeCmd.ParseChainID(cmd)
|
||||||
if chainID == "" {
|
|
||||||
commonCmd.ExitOnErr(cmd, "read chain id error: %w", errEmptyChainID)
|
|
||||||
}
|
|
||||||
chainIDRaw := []byte(chainID)
|
chainIDRaw := []byte(chainID)
|
||||||
|
|
||||||
hexEncoded, _ := cmd.Flags().GetBool(chainIDHexFlag)
|
|
||||||
if hexEncoded {
|
|
||||||
var err error
|
|
||||||
chainIDRaw, err = hex.DecodeString(chainID)
|
|
||||||
commonCmd.ExitOnErr(cmd, "can't decode chain ID as hex: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := cli.APEManagerRemoveChain(cmd.Context(), client_sdk.PrmAPEManagerRemoveChain{
|
_, err := cli.APEManagerRemoveChain(cmd.Context(), client_sdk.PrmAPEManagerRemoveChain{
|
||||||
ChainTarget: target,
|
ChainTarget: target,
|
||||||
ChainID: chainIDRaw,
|
ChainID: chainIDRaw,
|
||||||
|
@ -58,9 +42,10 @@ func initRemoveCmd() {
|
||||||
commonflags.Init(removeCmd)
|
commonflags.Init(removeCmd)
|
||||||
|
|
||||||
ff := removeCmd.Flags()
|
ff := removeCmd.Flags()
|
||||||
ff.String(targetNameFlag, "", targetNameDesc)
|
ff.String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
|
||||||
ff.String(targetTypeFlag, "", targetTypeDesc)
|
ff.String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
|
||||||
_ = removeCmd.MarkFlagRequired(targetTypeFlag)
|
_ = removeCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
||||||
ff.String(chainIDFlag, "", "Chain id")
|
ff.String(apeCmd.ChainIDFlag, "", apeCmd.ChainIDFlagDesc)
|
||||||
ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex")
|
_ = removeCmd.MarkFlagRequired(apeCmd.ChainIDFlag)
|
||||||
|
ff.Bool(apeCmd.ChainIDHexFlag, false, apeCmd.ChainIDHexFlagDesc)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue