frostfs-node/cmd/frostfs-cli/modules/ape_manager/add_chain.go
Airat Arifullin 8a465b74ad
All checks were successful
Tests and linters / Run gofumpt (pull_request) Successful in 50s
DCO action / DCO (pull_request) Successful in 1m6s
Vulncheck / Vulncheck (pull_request) Successful in 2m12s
Tests and linters / Staticcheck (pull_request) Successful in 2m41s
Build / Build Components (pull_request) Successful in 2m48s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m49s
Tests and linters / gopls check (pull_request) Successful in 3m21s
Tests and linters / Lint (pull_request) Successful in 3m46s
Tests and linters / Tests (pull_request) Successful in 3m58s
Tests and linters / Tests with -race (pull_request) Successful in 4m16s
[#1501] cli: Refactor APE-related commands
* Move common rule parsing logic to a common package that
  also can be imported out of `frostfs-node`;
* Move common flags and commands to `cmd/internal/common/ape` package to
  avoid duplication. Since `frostfs-adm` and `frostfs-cli` subcommands
  are able to use commands, thee same flag names and their descriptions.

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
2024-11-15 15:59:37 +03:00

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, false)
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)
}