2022-06-02 12:24:31 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2022-06-28 11:18:54 +00:00
|
|
|
"fmt"
|
2022-06-02 12:24:31 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
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/commonflags"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-06-28 11:18:54 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-06-02 12:24:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var deleteContainerCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Delete existing container",
|
|
|
|
Long: `Delete existing container.
|
|
|
|
Only owner of the container has a permission to remove container.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
id := parseContainerID(cmd)
|
|
|
|
|
|
|
|
var tok *session.Container
|
|
|
|
|
|
|
|
sessionTokenPath, _ := cmd.Flags().GetString(commonflags.SessionToken)
|
|
|
|
if sessionTokenPath != "" {
|
|
|
|
tok = new(session.Container)
|
|
|
|
common.ReadSessionToken(cmd, tok, sessionTokenPath)
|
|
|
|
}
|
|
|
|
|
2022-09-19 13:10:56 +00:00
|
|
|
pk := key.Get(cmd)
|
2022-06-02 12:24:31 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
|
|
|
|
2022-09-27 06:49:45 +00:00
|
|
|
if force, _ := cmd.Flags().GetBool(commonflags.ForceFlag); !force {
|
2022-06-28 11:18:54 +00:00
|
|
|
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"+
|
2022-09-27 06:49:45 +00:00
|
|
|
"Use --%s flag to remove anyway.", commonflags.ForceFlag))
|
2022-06-28 11:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 12:24:31 +00:00
|
|
|
var delPrm internalclient.DeleteContainerPrm
|
|
|
|
delPrm.SetClient(cli)
|
|
|
|
delPrm.SetContainer(id)
|
|
|
|
|
|
|
|
if tok != nil {
|
|
|
|
delPrm.WithinSession(*tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := internalclient.DeleteContainer(delPrm)
|
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
|
|
|
|
cmd.Println("container delete method invoked")
|
|
|
|
|
|
|
|
if containerAwait {
|
|
|
|
cmd.Println("awaiting...")
|
|
|
|
|
|
|
|
var getPrm internalclient.GetContainerPrm
|
|
|
|
getPrm.SetClient(cli)
|
|
|
|
getPrm.SetContainer(id)
|
|
|
|
|
|
|
|
for i := 0; i < awaitTimeout; i++ {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
_, err := internalclient.GetContainer(getPrm)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Println("container has been removed:", containerID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
common.ExitOnErr(cmd, "", errDeleteTimeout)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initContainerDeleteCmd() {
|
|
|
|
flags := deleteContainerCmd.Flags()
|
|
|
|
|
2022-09-19 13:10:56 +00:00
|
|
|
flags.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
|
|
|
|
flags.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
|
|
|
|
flags.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
|
|
|
|
|
2022-10-05 13:52:46 +00:00
|
|
|
flags.StringVar(&containerID, cidFlag, "", cidFlagUsage)
|
2022-10-11 14:02:03 +00:00
|
|
|
flags.BoolVar(&containerAwait, "await", false, "Block execution until container is removed")
|
|
|
|
flags.BoolP(commonflags.ForceFlag, commonflags.ForceFlagShorthand, false, "Do not check whether container contains locks and remove immediately")
|
2022-06-02 12:24:31 +00:00
|
|
|
}
|