From 5cbf57081f044d843a704b5d2c5da7367c3a040a Mon Sep 17 00:00:00 2001 From: Anton Nikiforov Date: Tue, 13 Feb 2024 16:22:08 +0300 Subject: [PATCH] [#980] adm: Introduce flag `chain-name` for APE managing commands Signed-off-by: Anton Nikiforov --- .../internal/modules/morph/ape/ape.go | 44 ++++++++++--------- .../internal/modules/morph/ape/ape_util.go | 20 +++++++++ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/cmd/frostfs-adm/internal/modules/morph/ape/ape.go b/cmd/frostfs-adm/internal/modules/morph/ape/ape.go index 8395fd4ba..c08dc512e 100644 --- a/cmd/frostfs-adm/internal/modules/morph/ape/ape.go +++ b/cmd/frostfs-adm/internal/modules/morph/ape/ape.go @@ -14,22 +14,24 @@ import ( ) const ( - namespaceTarget = "namespace" - containerTarget = "container" - jsonFlag = "json" - jsonFlagDesc = "Output rule chains in JSON format" - chainIDFlag = "chain-id" - chainIDDesc = "Rule chain ID" - ruleFlag = "rule" - ruleFlagDesc = "Rule chain in text format" - ruleJSONFlag = "rule-json" - ruleJSONFlagDesc = "Chain rule in JSON format or path to the file" - targetNameFlag = "target-name" - targetNameDesc = "Resource name in APE resource name format" - targetTypeFlag = "target-type" - targetTypeDesc = "Resource type(container/namespace)" - addrAdminFlag = "addr" - addrAdminDesc = "The address of the admins wallet" + namespaceTarget = "namespace" + containerTarget = "container" + jsonFlag = "json" + jsonFlagDesc = "Output rule chains in JSON format" + chainIDFlag = "chain-id" + chainIDDesc = "Rule chain ID" + ruleFlag = "rule" + ruleFlagDesc = "Rule chain in text format" + ruleJSONFlag = "rule-json" + ruleJSONFlagDesc = "Chain rule in JSON format or path to the file" + targetNameFlag = "target-name" + targetNameDesc = "Resource name in APE resource name format" + targetTypeFlag = "target-type" + targetTypeDesc = "Resource type(container/namespace)" + addrAdminFlag = "addr" + addrAdminDesc = "The address of the admins wallet" + chainNameFlag = "chain-name" + chainNameFlagDesc = "Chain name(ingress|s3)" ) var ( @@ -99,7 +101,7 @@ func initAddRuleChainCmd() { _ = addRuleChainCmd.MarkFlagRequired(chainIDFlag) addRuleChainCmd.Flags().String(ruleFlag, "", ruleFlagDesc) addRuleChainCmd.Flags().String(ruleJSONFlag, "", ruleJSONFlagDesc) - + addRuleChainCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc) addRuleChainCmd.MarkFlagsMutuallyExclusive(ruleFlag, ruleJSONFlag) } @@ -115,6 +117,7 @@ func initRemoveRuleChainCmd() { _ = removeRuleChainCmd.MarkFlagRequired(targetNameFlag) removeRuleChainCmd.Flags().String(chainIDFlag, "", chainIDDesc) _ = removeRuleChainCmd.MarkFlagRequired(chainIDFlag) + removeRuleChainCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc) } func initListRuleChainsCmd() { @@ -127,6 +130,7 @@ func initListRuleChainsCmd() { listRuleChainsCmd.Flags().String(targetNameFlag, "", targetNameDesc) _ = listRuleChainsCmd.MarkFlagRequired(targetNameFlag) listRuleChainsCmd.Flags().Bool(jsonFlag, false, jsonFlagDesc) + listRuleChainsCmd.Flags().String(chainNameFlag, ingress, chainNameFlagDesc) } func initSetAdminCmd() { @@ -149,7 +153,7 @@ func addRuleChain(cmd *cobra.Command, _ []string) { chain := parseChain(cmd) target := parseTarget(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...") _, err = ac.Wait(h, vub, err) commonCmd.ExitOnErr(cmd, "add rule chain error: %w", err) @@ -160,7 +164,7 @@ func removeRuleChain(cmd *cobra.Command, _ []string) { chainID := parseChainID(cmd) target := parseTarget(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...") _, err = ac.Wait(h, vub, 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) { target := parseTarget(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) if len(chains) == 0 { return diff --git a/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go b/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go index bbbdf7805..7f23e3754 100644 --- a/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go +++ b/cmd/frostfs-adm/internal/modules/morph/ape/ape_util.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "strings" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config" @@ -21,6 +22,16 @@ import ( "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 { var targetType policyengine.TargetType typ, _ := cmd.Flags().GetString(targetTypeFlag) @@ -78,6 +89,15 @@ func parseChain(cmd *cobra.Command) *apechain.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) { v := viper.GetViper() c, err := helper.GetN3Client(v)