[#980] adm: Introduce flag chain-name for APE managing commands

Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
Anton Nikiforov 2024-02-13 16:22:08 +03:00
parent 35370283ba
commit 5cbf57081f
2 changed files with 44 additions and 20 deletions

View file

@ -14,22 +14,24 @@ import (
) )
const ( const (
namespaceTarget = "namespace" namespaceTarget = "namespace"
containerTarget = "container" containerTarget = "container"
jsonFlag = "json" jsonFlag = "json"
jsonFlagDesc = "Output rule chains in JSON format" jsonFlagDesc = "Output rule chains in JSON format"
chainIDFlag = "chain-id" chainIDFlag = "chain-id"
chainIDDesc = "Rule chain ID" chainIDDesc = "Rule chain ID"
ruleFlag = "rule" ruleFlag = "rule"
ruleFlagDesc = "Rule chain in text format" ruleFlagDesc = "Rule chain in text format"
ruleJSONFlag = "rule-json" ruleJSONFlag = "rule-json"
ruleJSONFlagDesc = "Chain rule in JSON format or path to the file" ruleJSONFlagDesc = "Chain rule in JSON format or path to the file"
targetNameFlag = "target-name" targetNameFlag = "target-name"
targetNameDesc = "Resource name in APE resource name format" targetNameDesc = "Resource name in APE resource name format"
targetTypeFlag = "target-type" targetTypeFlag = "target-type"
targetTypeDesc = "Resource type(container/namespace)" targetTypeDesc = "Resource type(container/namespace)"
addrAdminFlag = "addr" addrAdminFlag = "addr"
addrAdminDesc = "The address of the admins wallet" addrAdminDesc = "The address of the admins wallet"
chainNameFlag = "chain-name"
chainNameFlagDesc = "Chain name(ingress|s3)"
) )
var ( var (
@ -99,7 +101,7 @@ func initAddRuleChainCmd() {
_ = addRuleChainCmd.MarkFlagRequired(chainIDFlag) _ = addRuleChainCmd.MarkFlagRequired(chainIDFlag)
addRuleChainCmd.Flags().String(ruleFlag, "", ruleFlagDesc) addRuleChainCmd.Flags().String(ruleFlag, "", ruleFlagDesc)
addRuleChainCmd.Flags().String(ruleJSONFlag, "", ruleJSONFlagDesc) addRuleChainCmd.Flags().String(ruleJSONFlag, "", ruleJSONFlagDesc)
addRuleChainCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc)
addRuleChainCmd.MarkFlagsMutuallyExclusive(ruleFlag, ruleJSONFlag) addRuleChainCmd.MarkFlagsMutuallyExclusive(ruleFlag, ruleJSONFlag)
} }
@ -115,6 +117,7 @@ func initRemoveRuleChainCmd() {
_ = removeRuleChainCmd.MarkFlagRequired(targetNameFlag) _ = removeRuleChainCmd.MarkFlagRequired(targetNameFlag)
removeRuleChainCmd.Flags().String(chainIDFlag, "", chainIDDesc) removeRuleChainCmd.Flags().String(chainIDFlag, "", chainIDDesc)
_ = removeRuleChainCmd.MarkFlagRequired(chainIDFlag) _ = removeRuleChainCmd.MarkFlagRequired(chainIDFlag)
removeRuleChainCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc)
} }
func initListRuleChainsCmd() { func initListRuleChainsCmd() {
@ -127,6 +130,7 @@ func initListRuleChainsCmd() {
listRuleChainsCmd.Flags().String(targetNameFlag, "", targetNameDesc) listRuleChainsCmd.Flags().String(targetNameFlag, "", targetNameDesc)
_ = listRuleChainsCmd.MarkFlagRequired(targetNameFlag) _ = listRuleChainsCmd.MarkFlagRequired(targetNameFlag)
listRuleChainsCmd.Flags().Bool(jsonFlag, false, jsonFlagDesc) listRuleChainsCmd.Flags().Bool(jsonFlag, false, jsonFlagDesc)
listRuleChainsCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc)
} }
func initSetAdminCmd() { func initSetAdminCmd() {
@ -149,7 +153,7 @@ func addRuleChain(cmd *cobra.Command, _ []string) {
chain := parseChain(cmd) chain := parseChain(cmd)
target := parseTarget(cmd) target := parseTarget(cmd)
pci, ac := newPolicyContractInterface(cmd) pci, ac := newPolicyContractInterface(cmd)
h, vub, err := pci.AddMorphRuleChain(apechain.Ingress, target, chain) h, vub, err := pci.AddMorphRuleChain(parseChainName(cmd), target, chain)
cmd.Println("Waiting for transaction to persist...") cmd.Println("Waiting for transaction to persist...")
_, err = ac.Wait(h, vub, err) _, err = ac.Wait(h, vub, err)
commonCmd.ExitOnErr(cmd, "add rule chain error: %w", err) commonCmd.ExitOnErr(cmd, "add rule chain error: %w", err)
@ -160,7 +164,7 @@ func removeRuleChain(cmd *cobra.Command, _ []string) {
chainID := parseChainID(cmd) chainID := parseChainID(cmd)
target := parseTarget(cmd) target := parseTarget(cmd)
pci, ac := newPolicyContractInterface(cmd) pci, ac := newPolicyContractInterface(cmd)
h, vub, err := pci.RemoveMorphRuleChain(apechain.Ingress, target, chainID) h, vub, err := pci.RemoveMorphRuleChain(parseChainName(cmd), target, chainID)
cmd.Println("Waiting for transaction to persist...") cmd.Println("Waiting for transaction to persist...")
_, err = ac.Wait(h, vub, err) _, err = ac.Wait(h, vub, err)
commonCmd.ExitOnErr(cmd, "remove rule chain error: %w", err) commonCmd.ExitOnErr(cmd, "remove rule chain error: %w", err)
@ -170,7 +174,7 @@ func removeRuleChain(cmd *cobra.Command, _ []string) {
func listRuleChains(cmd *cobra.Command, _ []string) { func listRuleChains(cmd *cobra.Command, _ []string) {
target := parseTarget(cmd) target := parseTarget(cmd)
pci, _ := newPolicyContractInterface(cmd) pci, _ := newPolicyContractInterface(cmd)
chains, err := pci.ListMorphRuleChains(apechain.Ingress, target) chains, err := pci.ListMorphRuleChains(parseChainName(cmd), target)
commonCmd.ExitOnErr(cmd, "list rule chains error: %w", err) commonCmd.ExitOnErr(cmd, "list rule chains error: %w", err)
if len(chains) == 0 { if len(chains) == 0 {
return return

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config"
@ -21,6 +22,16 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const (
ingress = "ingress"
s3 = "s3"
)
var mChainName = map[string]apechain.Name{
ingress: apechain.Ingress,
s3: apechain.S3,
}
func parseTarget(cmd *cobra.Command) policyengine.Target { func parseTarget(cmd *cobra.Command) policyengine.Target {
var targetType policyengine.TargetType var targetType policyengine.TargetType
typ, _ := cmd.Flags().GetString(targetTypeFlag) typ, _ := cmd.Flags().GetString(targetTypeFlag)
@ -78,6 +89,15 @@ func parseChain(cmd *cobra.Command) *apechain.Chain {
return chain return chain
} }
func parseChainName(cmd *cobra.Command) apechain.Name {
chainName, _ := cmd.Flags().GetString(chainNameFlag)
apeChainName, ok := mChainName[strings.ToLower(chainName)]
if !ok {
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("unsupported chain name"))
}
return apeChainName
}
func newPolicyContractInterface(cmd *cobra.Command) (*morph.ContractStorage, *actor.Actor) { func newPolicyContractInterface(cmd *cobra.Command) (*morph.ContractStorage, *actor.Actor) {
v := viper.GetViper() v := viper.GetViper()
c, err := helper.GetN3Client(v) c, err := helper.GetN3Client(v)