Airat Arifullin
3b1364e4cf
* 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>
86 lines
2.5 KiB
Go
86 lines
2.5 KiB
Go
package apemanager
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
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/key"
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
apeCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/ape"
|
|
apeSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
|
|
client_sdk "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var addCmd = &cobra.Command{
|
|
Use: "add",
|
|
Short: "Add rule chain for a target",
|
|
Run: add,
|
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
commonflags.Bind(cmd)
|
|
},
|
|
}
|
|
|
|
func parseTarget(cmd *cobra.Command) (ct apeSDK.ChainTarget) {
|
|
t := apeCmd.ParseTarget(cmd)
|
|
|
|
ct.Name = t.Name
|
|
|
|
switch t.Type {
|
|
case engine.Namespace:
|
|
ct.TargetType = apeSDK.TargetTypeNamespace
|
|
case engine.Container:
|
|
ct.TargetType = apeSDK.TargetTypeContainer
|
|
case engine.User:
|
|
ct.TargetType = apeSDK.TargetTypeUser
|
|
case engine.Group:
|
|
ct.TargetType = apeSDK.TargetTypeGroup
|
|
default:
|
|
commonCmd.ExitOnErr(cmd, "conversion error: %w", fmt.Errorf("unknown type '%c'", t.Type))
|
|
}
|
|
return ct
|
|
}
|
|
|
|
func parseChain(cmd *cobra.Command) apeSDK.Chain {
|
|
c := apeCmd.ParseChain(cmd)
|
|
serialized := c.Bytes()
|
|
return apeSDK.Chain{
|
|
Raw: serialized,
|
|
}
|
|
}
|
|
|
|
func add(cmd *cobra.Command, _ []string) {
|
|
c := parseChain(cmd)
|
|
|
|
target := parseTarget(cmd)
|
|
|
|
key := key.Get(cmd)
|
|
cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC)
|
|
|
|
res, err := cli.APEManagerAddChain(cmd.Context(), client_sdk.PrmAPEManagerAddChain{
|
|
ChainTarget: target,
|
|
Chain: c,
|
|
})
|
|
|
|
commonCmd.ExitOnErr(cmd, "add chain error: %w", err)
|
|
|
|
cmd.Println("Rule has been added.")
|
|
cmd.Println("Chain ID: ", string(res.ChainID))
|
|
}
|
|
|
|
func initAddCmd() {
|
|
commonflags.Init(addCmd)
|
|
|
|
ff := addCmd.Flags()
|
|
ff.StringArray(apeCmd.RuleFlag, []string{}, apeCmd.RuleFlagDesc)
|
|
ff.String(apeCmd.PathFlag, "", apeCmd.PathFlagDesc)
|
|
ff.String(apeCmd.ChainIDFlag, "", apeCmd.ChainIDFlagDesc)
|
|
ff.String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
|
|
ff.String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
|
|
_ = addCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
|
|
ff.Bool(apeCmd.ChainIDHexFlag, false, apeCmd.ChainIDHexFlagDesc)
|
|
|
|
addCmd.MarkFlagsMutuallyExclusive(apeCmd.PathFlag, apeCmd.RuleFlag)
|
|
}
|