2022-06-01 12:42:28 +00:00
|
|
|
package object
|
2022-02-16 15:56:16 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-17 07:36:23 +00:00
|
|
|
"context"
|
2022-07-19 12:45:58 +00:00
|
|
|
"errors"
|
2022-02-16 15:56:16 +00:00
|
|
|
"fmt"
|
2022-05-31 16:59:07 +00:00
|
|
|
"strconv"
|
2022-06-17 07:36:23 +00:00
|
|
|
"time"
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
objectV2 "github.com/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
internalclient "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd "github.com/TrueCloudLab/frostfs-node/cmd/internal/common"
|
2022-12-23 17:35:35 +00:00
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2022-02-16 15:56:16 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// object lock command.
|
2022-06-01 12:42:28 +00:00
|
|
|
var objectLockCmd = &cobra.Command{
|
2022-10-28 19:24:58 +00:00
|
|
|
Use: "lock",
|
2022-02-16 15:56:16 +00:00
|
|
|
Short: "Lock object in container",
|
|
|
|
Long: "Lock object in container",
|
2022-10-28 19:24:58 +00:00
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
2022-10-18 11:43:04 +00:00
|
|
|
cidRaw, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-10-28 19:24:58 +00:00
|
|
|
var cnr cid.ID
|
|
|
|
err := cnr.DecodeString(cidRaw)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "Incorrect container arg: %v", err)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
oidsRaw, _ := cmd.Flags().GetStringSlice(commonflags.OIDFlag)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-10-28 19:24:58 +00:00
|
|
|
lockList := make([]oid.ID, len(oidsRaw))
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-10-28 19:24:58 +00:00
|
|
|
for i := range oidsRaw {
|
|
|
|
err = lockList[i].DecodeString(oidsRaw[i])
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.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-07-19 12:45:58 +00:00
|
|
|
exp, _ := cmd.Flags().GetUint64(commonflags.ExpireAt)
|
|
|
|
lifetime, _ := cmd.Flags().GetUint64(commonflags.Lifetime)
|
|
|
|
if exp == 0 && lifetime == 0 { // mutual exclusion is ensured by cobra
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", errors.New("either expiration epoch of a lifetime is required"))
|
2022-07-19 12:45:58 +00:00
|
|
|
}
|
2022-05-31 16:59:07 +00:00
|
|
|
|
2022-07-19 12:45:58 +00:00
|
|
|
if lifetime != 0 {
|
2022-06-17 07:36:23 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
endpoint, _ := cmd.Flags().GetString(commonflags.RPC)
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
currEpoch, err := internalclient.GetCurrentEpoch(ctx, cmd, endpoint)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "Request current epoch: %w", err)
|
2022-05-31 16:59:07 +00:00
|
|
|
|
2022-11-01 17:20:11 +00:00
|
|
|
exp = currEpoch + lifetime
|
2022-05-31 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Lock object will expire after %d epoch", exp)
|
2022-11-01 17:20:11 +00:00
|
|
|
|
2022-05-31 16:59:07 +00:00
|
|
|
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-10-20 09:40:33 +00:00
|
|
|
ReadOrOpenSession(cmd, &prm, key, cnr, nil)
|
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
|
|
|
|
2022-07-19 13:23:31 +00:00
|
|
|
res, err := internalclient.PutObject(prm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "Store lock object in NeoFS: %w", err)
|
2022-02-16 15:56:16 +00:00
|
|
|
|
2022-07-19 13:23:31 +00:00
|
|
|
cmd.Printf("Lock object ID: %s\n", res.ID())
|
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)
|
2022-10-20 09:40:33 +00:00
|
|
|
initFlagSession(objectLockCmd, "PUT")
|
2022-05-31 16:59:07 +00:00
|
|
|
|
2022-10-28 19:24:58 +00:00
|
|
|
ff := objectLockCmd.Flags()
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
|
|
|
_ = objectLockCmd.MarkFlagRequired(commonflags.CIDFlag)
|
2022-10-28 19:24:58 +00:00
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
ff.StringSlice(commonflags.OIDFlag, nil, commonflags.OIDFlagUsage)
|
|
|
|
_ = objectLockCmd.MarkFlagRequired(commonflags.OIDFlag)
|
2022-10-28 19:24:58 +00:00
|
|
|
|
2022-12-21 07:59:35 +00:00
|
|
|
ff.Uint64P(commonflags.ExpireAt, "e", 0, "The last active epoch for the lock")
|
2022-10-28 19:24:58 +00:00
|
|
|
|
|
|
|
ff.Uint64(commonflags.Lifetime, 0, "Lock lifetime")
|
2022-07-19 12:45:58 +00:00
|
|
|
objectLockCmd.MarkFlagsMutuallyExclusive(commonflags.ExpireAt, commonflags.Lifetime)
|
2022-02-16 15:56:16 +00:00
|
|
|
}
|