From 645bef9de96cf578466c7555fd384b31f0c9e554 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Sun, 21 Feb 2021 11:33:00 +0300 Subject: [PATCH] [#401] cli: Add command to drop object from the node's local storage Add `drop-objects` sub-command to `control` command that drops objects from the node via `ControlService.DropObjects` API. Address list argument is passed in NeoFS API string format of the object address. Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/control.go | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/cmd/neofs-cli/modules/control.go b/cmd/neofs-cli/modules/control.go index f72c56792..ddb9b566f 100644 --- a/cmd/neofs-cli/modules/control.go +++ b/cmd/neofs-cli/modules/control.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/util/signature" "github.com/nspcc-dev/neofs-api-go/v2/client" "github.com/nspcc-dev/neofs-node/pkg/services/control" @@ -47,6 +48,7 @@ func init() { controlCmd.AddCommand( healthCheckCmd, setNetmapStatusCmd, + dropObjectsCmd, ) setNetmapStatusCmd.Flags().StringVarP(&netmapStatus, netmapStatusFlag, "", "", @@ -57,6 +59,11 @@ func init() { ) _ = setNetmapStatusCmd.MarkFlagRequired(netmapStatusFlag) + + dropObjectsCmd.Flags().StringSliceVarP(&dropObjectsList, dropObjectsFlag, "o", nil, + "List of object addresses to be removed in string format") + + _ = dropObjectsCmd.MarkFlagRequired(dropObjectsFlag) } func getControlServiceClient() (control.ControlServiceClient, error) { @@ -168,3 +175,70 @@ func setNetmapStatus(cmd *cobra.Command, _ []string) error { return nil } + +const dropObjectsFlag = "objects" + +var dropObjectsList []string + +var dropObjectsCmd = &cobra.Command{ + Use: "drop-objects", + Short: "Drop objects from the node's local storage", + Long: "Drop objects from the node's local storage", + RunE: func(cmd *cobra.Command, args []string) error { + key, err := getKey() + if err != nil { + return err + } + + binAddrList := make([][]byte, 0, len(dropObjectsList)) + + for i := range dropObjectsList { + a := object.NewAddress() + + err := a.Parse(dropObjectsList[i]) + if err != nil { + return errors.Wrapf(err, "could not parse address #%d", i) + } + + binAddr, err := a.Marshal() + if err != nil { + return errors.Wrap(err, "could not marshal the address") + } + + binAddrList = append(binAddrList, binAddr) + } + + req := new(control.DropObjectsRequest) + + body := new(control.DropObjectsRequest_Body) + req.SetBody(body) + + body.SetAddressList(binAddrList) + + if err := controlSvc.SignMessage(key, req); err != nil { + return err + } + + cli, err := getControlServiceClient() + if err != nil { + return err + } + + resp, err := cli.DropObjects(context.Background(), req) + if err != nil { + return err + } + + sign := resp.GetSignature() + + if err := signature.VerifyDataWithSource(resp, func() ([]byte, []byte) { + return sign.GetKey(), sign.GetSign() + }); err != nil { + return err + } + + cmd.Println("Objects were successfully marked to be removed.") + + return nil + }, +}