2022-06-01 12:42:28 +00:00
|
|
|
package object
|
2022-02-16 15:56:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-31 16:59:07 +00:00
|
|
|
"strconv"
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2022-02-16 15:56:16 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2022-05-18 11:03:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-18 09:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-05-24 08:22:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-05-30 13:43:02 +00:00
|
|
|
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
|
2022-02-16 15:56:16 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-31 16:59:07 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-16 15:56:16 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-06-01 12:42:28 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-02-16 15:56:16 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
const lockExpiresOnFlag = "expires-on"
|
|
|
|
|
2022-02-16 15:56:16 +00:00
|
|
|
// object lock command.
|
2022-06-01 12:42:28 +00:00
|
|
|
var objectLockCmd = &cobra.Command{
|
2022-02-16 15:56:16 +00:00
|
|
|
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
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
err := cnr.DecodeString(args[0])
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "Incorrect container arg: %v", err)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
|
|
|
argsList := args[1:]
|
|
|
|
|
|
|
|
lockList := make([]oid.ID, len(argsList))
|
|
|
|
|
|
|
|
for i := range argsList {
|
2022-05-12 16:37:46 +00:00
|
|
|
err = lockList[i].DecodeString(argsList[i])
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, fmt.Sprintf("Incorrect object arg #%d: %%v", i+1), err)
|
2022-02-16 15:56:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 08:22:23 +00:00
|
|
|
key := key.GetOrGenerate(cmd)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-06-01 12:42:28 +00:00
|
|
|
var idOwner user.ID
|
|
|
|
user.IDFromKey(&idOwner, key.PublicKey)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
var lock objectSDK.Lock
|
2022-02-16 15:56:16 +00:00
|
|
|
lock.WriteMembers(lockList)
|
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
exp, relative, err := common.ParseEpoch(cmd, lockExpiresOnFlag)
|
|
|
|
common.ExitOnErr(cmd, "Parsing expiration epoch: %w", err)
|
|
|
|
|
|
|
|
if relative {
|
|
|
|
endpoint, _ := cmd.Flags().GetString(commonflags.RPC)
|
|
|
|
|
|
|
|
currEpoch, err := internalclient.GetCurrentEpoch(endpoint)
|
|
|
|
common.ExitOnErr(cmd, "Request current epoch: %w", err)
|
|
|
|
|
|
|
|
exp += currEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
var expirationAttr objectSDK.Attribute
|
|
|
|
expirationAttr.SetKey(objectV2.SysAttributeExpEpoch)
|
|
|
|
expirationAttr.SetValue(strconv.FormatUint(exp, 10))
|
|
|
|
|
|
|
|
obj := objectSDK.New()
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetContainerID(cnr)
|
2022-06-01 12:42:28 +00:00
|
|
|
obj.SetOwnerID(&idOwner)
|
2022-05-31 16:59:07 +00:00
|
|
|
obj.SetType(objectSDK.TypeLock)
|
|
|
|
obj.SetAttributes(expirationAttr)
|
2022-02-16 15:56:16 +00:00
|
|
|
obj.SetPayload(lock.Marshal())
|
|
|
|
|
|
|
|
var prm internalclient.PutObjectPrm
|
|
|
|
|
2022-05-30 13:43:02 +00:00
|
|
|
sessionCli.Prepare(cmd, cnr, nil, key, &prm)
|
2022-06-01 12:42:28 +00:00
|
|
|
Prepare(cmd, &prm)
|
2022-03-05 08:49:19 +00:00
|
|
|
prm.SetHeader(obj)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
|
|
|
_, err = internalclient.PutObject(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "Store lock object in NeoFS: %w", err)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
|
|
|
cmd.Println("Objects successfully locked.")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initCommandObjectLock() {
|
2022-06-01 12:42:28 +00:00
|
|
|
commonflags.Init(objectLockCmd)
|
|
|
|
commonflags.InitSession(objectLockCmd)
|
2022-05-31 16:59:07 +00:00
|
|
|
|
|
|
|
objectLockCmd.Flags().StringP(lockExpiresOnFlag, "e", "", "Lock expiration epoch")
|
|
|
|
_ = objectLockCmd.MarkFlagRequired(lockExpiresOnFlag)
|
2022-02-16 15:56:16 +00:00
|
|
|
}
|