From d92b1d1bf848d69f8e10ab519e7586bc1949e4a5 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 16 Feb 2022 18:56:16 +0300 Subject: [PATCH] [#1175] CLI: Implement command to lock the objects Implement `lock` command and add it to `object` section. The command accepts container argument (string `cid.ID`) and list of locked objects (string `oid.ID` list). From the provided input `LOCK` object is constructed and stored using NeoFS API protocol. Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/lock.go | 65 +++++++++++++++++++++++++++++++++ cmd/neofs-cli/modules/object.go | 2 + 2 files changed, 67 insertions(+) create mode 100644 cmd/neofs-cli/modules/lock.go diff --git a/cmd/neofs-cli/modules/lock.go b/cmd/neofs-cli/modules/lock.go new file mode 100644 index 000000000..b6ab40de4 --- /dev/null +++ b/cmd/neofs-cli/modules/lock.go @@ -0,0 +1,65 @@ +package cmd + +import ( + "fmt" + + internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" + objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object" + cid "github.com/nspcc-dev/neofs-sdk-go/container/id" + "github.com/nspcc-dev/neofs-sdk-go/object" + oid "github.com/nspcc-dev/neofs-sdk-go/object/id" + "github.com/spf13/cobra" +) + +// object lock command. +var cmdObjectLock = &cobra.Command{ + Use: "lock CONTAINER OBJECT...", + Short: "Lock object in container", + Long: "Lock object in container", + Args: cobra.MinimumNArgs(2), + Run: func(cmd *cobra.Command, args []string) { + var cnr cid.ID + + err := cnr.Parse(args[0]) + exitOnErr(cmd, errf("Incorrect container arg: %v", err)) + + argsList := args[1:] + + lockList := make([]oid.ID, len(argsList)) + + for i := range argsList { + err = lockList[i].Parse(argsList[i]) + exitOnErr(cmd, errf(fmt.Sprintf("Incorrect object arg #%d: %%v", i+1), err)) + } + + key, err := getKey() + exitOnErr(cmd, errf("can't fetch private key: %w", err)) + + idOwner, err := getOwnerID(key) + exitOnErr(cmd, err) + + var lock object.Lock + lock.WriteMembers(lockList) + + obj := object.NewRaw() + obj.SetContainerID(&cnr) + obj.SetOwnerID(idOwner) + obj.SetType(object.TypeLock) + obj.SetPayload(lock.Marshal()) + + var prm internalclient.PutObjectPrm + + prepareSessionPrmWithOwner(cmd, objectcore.AddressOf(obj), key, idOwner, &prm) + prepareObjectPrm(cmd, &prm) + prm.SetHeader(obj.Object()) + + _, err = internalclient.PutObject(prm) + exitOnErr(cmd, errf("Store lock object in NeoFS: %w", err)) + + cmd.Println("Objects successfully locked.") + }, +} + +func initCommandObjectLock() { + initCommonFlags(cmdObjectLock) +} diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index 67b0cea78..9fe893faf 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -246,6 +246,7 @@ func init() { objectHeadCmd, objectHashCmd, objectRangeCmd, + cmdObjectLock, } rootCmd.AddCommand(objectCmd) @@ -276,6 +277,7 @@ func init() { initObjectHeadCmd() initObjectHashCmd() initObjectRangeCmd() + initCommandObjectLock() } type clientKeySession interface {