From 89f63ed41adb4ccb14331d4570499e8b587bd342 Mon Sep 17 00:00:00 2001 From: Anton Nikiforov Date: Fri, 26 Jan 2024 17:15:28 +0300 Subject: [PATCH] [#922] cli: Add new command `control list-targets` Signed-off-by: Anton Nikiforov --- .../modules/control/list_targets.go | 82 +++++++++++++++++++ cmd/frostfs-cli/modules/control/root.go | 2 + 2 files changed, 84 insertions(+) create mode 100644 cmd/frostfs-cli/modules/control/list_targets.go diff --git a/cmd/frostfs-cli/modules/control/list_targets.go b/cmd/frostfs-cli/modules/control/list_targets.go new file mode 100644 index 000000000..29033d017 --- /dev/null +++ b/cmd/frostfs-cli/modules/control/list_targets.go @@ -0,0 +1,82 @@ +package control + +import ( + "bytes" + "crypto/sha256" + "fmt" + "strconv" + "text/tabwriter" + + "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" + "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key" + commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control" + cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" + "github.com/spf13/cobra" +) + +const ( + chainNameFlag = "chain-name" + chainNameFlagUsage = "Chain name(ingress|s3)" +) + +var listTargetsCmd = &cobra.Command{ + Use: "list-targets", + Short: "List local targets", + Long: "List local APE overrides of the node", + Run: listTargets, +} + +func listTargets(cmd *cobra.Command, _ []string) { + pk := key.Get(cmd) + + var cnr cid.ID + chainName, _ := cmd.Flags().GetString(chainNameFlag) + + rawCID := make([]byte, sha256.Size) + cnr.Encode(rawCID) + + req := &control.ListTargetsLocalOverridesRequest{ + Body: &control.ListTargetsLocalOverridesRequest_Body{ + ChainName: chainName, + }, + } + + signRequest(cmd, pk, req) + + cli := getClient(cmd, pk) + + var resp *control.ListTargetsLocalOverridesResponse + var err error + err = cli.ExecRaw(func(client *client.Client) error { + resp, err = control.ListTargetsLocalOverrides(client, req) + return err + }) + commonCmd.ExitOnErr(cmd, "rpc error: %w", err) + + verifyResponse(cmd, resp.GetSignature(), resp.GetBody()) + + targets := resp.GetBody().GetTargets() + if len(targets) == 0 { + cmd.Println("Local overrides are not defined for the container.") + return + } + + buf := bytes.NewBuffer(nil) + tw := tabwriter.NewWriter(buf, 0, 2, 2, ' ', 0) + _, _ = tw.Write([]byte("#\tName\tType\n")) + for i, t := range targets { + _, _ = tw.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", strconv.Itoa(i), t.GetName(), t.GetType()))) + } + _ = tw.Flush() + cmd.Print(buf.String()) +} + +func initControlListTargetsCmd() { + initControlFlags(listTargetsCmd) + + ff := listTargetsCmd.Flags() + ff.String(chainNameFlag, "", chainNameFlagUsage) + + _ = cobra.MarkFlagRequired(ff, chainNameFlag) +} diff --git a/cmd/frostfs-cli/modules/control/root.go b/cmd/frostfs-cli/modules/control/root.go index f7c846eba..41222fc00 100644 --- a/cmd/frostfs-cli/modules/control/root.go +++ b/cmd/frostfs-cli/modules/control/root.go @@ -38,6 +38,7 @@ func init() { removeRuleCmd, listRulesCmd, getRuleCmd, + listTargetsCmd, ) initControlHealthCheckCmd() @@ -50,4 +51,5 @@ func init() { initControlRemoveRuleCmd() initControlListRulesCmd() initControGetRuleCmd() + initControlListTargetsCmd() }