[#1500] neofs-cli: Check if container contains LOCK objects before removal

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-06-28 14:18:54 +03:00 committed by fyrchik
parent a421344727
commit 6eb5260562
2 changed files with 28 additions and 0 deletions

View file

@ -8,6 +8,7 @@ Changelog for NeoFS Node
### Changed ### Changed
- Require SG members to be unique (#1490) - Require SG members to be unique (#1490)
- `neofs-cli` now doesn't remove container with LOCK objects without `--force` flag (#1500)
### Fixed ### Fixed

View file

@ -1,16 +1,20 @@
package container package container
import ( import (
"fmt"
"time" "time"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
"github.com/nspcc-dev/neofs-sdk-go/session" "github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const forceFlag = "force"
var deleteContainerCmd = &cobra.Command{ var deleteContainerCmd = &cobra.Command{
Use: "delete", Use: "delete",
Short: "Delete existing container", Short: "Delete existing container",
@ -30,6 +34,28 @@ Only owner of the container has a permission to remove container.`,
pk := key.GetOrGenerate(cmd) pk := key.GetOrGenerate(cmd)
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC) cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
if force, _ := cmd.Flags().GetBool(forceFlag); !force {
fs := objectSDK.NewSearchFilters()
fs.AddTypeFilter(objectSDK.MatchStringEqual, objectSDK.TypeLock)
var searchPrm internalclient.SearchObjectsPrm
searchPrm.SetClient(cli)
searchPrm.SetContainerID(id)
searchPrm.SetFilters(fs)
searchPrm.SetTTL(2)
common.PrintVerbose("Searching for LOCK objects...")
res, err := internalclient.SearchObjects(searchPrm)
common.ExitOnErr(cmd, "can't search for LOCK objects: %w", err)
if len(res.IDList()) != 0 {
common.ExitOnErr(cmd, "",
fmt.Errorf("Container wasn't removed because LOCK objects were found.\n"+
"Use --%s flag to remove anyway.", forceFlag))
}
}
var delPrm internalclient.DeleteContainerPrm var delPrm internalclient.DeleteContainerPrm
delPrm.SetClient(cli) delPrm.SetClient(cli)
delPrm.SetContainer(id) delPrm.SetContainer(id)
@ -72,4 +98,5 @@ func initContainerDeleteCmd() {
flags.StringVar(&containerID, "cid", "", "container ID") flags.StringVar(&containerID, "cid", "", "container ID")
flags.BoolVar(&containerAwait, "await", false, "block execution until container is removed") flags.BoolVar(&containerAwait, "await", false, "block execution until container is removed")
flags.Bool(forceFlag, false, "do not check whether container contains locks and remove immediately")
} }