2022-10-12 14:48:21 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/tree"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2022-10-12 14:48:21 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var addByPathCmd = &cobra.Command{
|
|
|
|
Use: "add-by-path",
|
|
|
|
Short: "Add a node by the path",
|
|
|
|
Run: addByPath,
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
commonflags.Bind(cmd)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initAddByPathCmd() {
|
|
|
|
commonflags.Init(addByPathCmd)
|
|
|
|
initCTID(addByPathCmd)
|
|
|
|
|
|
|
|
ff := addByPathCmd.Flags()
|
|
|
|
|
|
|
|
// tree service does not allow any attribute except
|
|
|
|
// the 'FileName' but that's a limitation of the
|
|
|
|
// current implementation, not the rule
|
2023-03-07 13:38:26 +00:00
|
|
|
// ff.String(pathAttributeFlagKey, "", "Path attribute")
|
2022-10-12 14:48:21 +00:00
|
|
|
ff.String(pathFlagKey, "", "Path to a node")
|
|
|
|
ff.StringSlice(metaFlagKey, nil, "Meta pairs in the form of Key1=[0x]Value1,Key2=[0x]Value2")
|
|
|
|
|
|
|
|
_ = cobra.MarkFlagRequired(ff, commonflags.RPC)
|
|
|
|
_ = cobra.MarkFlagRequired(ff, pathFlagKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addByPath(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
cidRaw, _ := cmd.Flags().GetString(commonflags.CIDFlag)
|
2022-10-12 14:48:21 +00:00
|
|
|
|
|
|
|
var cnr cid.ID
|
|
|
|
err := cnr.DecodeString(cidRaw)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err)
|
2022-10-12 14:48:21 +00:00
|
|
|
|
|
|
|
tid, _ := cmd.Flags().GetString(treeIDFlagKey)
|
2024-09-13 08:32:03 +00:00
|
|
|
ctx, cancel := contextWithTimeout(cmd)
|
|
|
|
defer cancel()
|
2022-10-12 14:48:21 +00:00
|
|
|
|
2024-09-13 08:32:03 +00:00
|
|
|
cli, err := _client()
|
2023-07-11 09:36:33 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "failed to create client: %w", err)
|
2022-10-12 14:48:21 +00:00
|
|
|
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(rawCID)
|
|
|
|
|
|
|
|
meta, err := parseMeta(cmd)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "meta data parsing: %w", err)
|
2022-10-12 14:48:21 +00:00
|
|
|
|
|
|
|
path, _ := cmd.Flags().GetString(pathFlagKey)
|
|
|
|
|
2023-07-11 10:31:49 +00:00
|
|
|
var bt []byte
|
|
|
|
if t := common.ReadBearerToken(cmd, bearerFlagKey); t != nil {
|
|
|
|
bt = t.Marshal()
|
|
|
|
}
|
|
|
|
|
2022-10-12 14:48:21 +00:00
|
|
|
req := new(tree.AddByPathRequest)
|
|
|
|
req.Body = &tree.AddByPathRequest_Body{
|
|
|
|
ContainerId: rawCID,
|
|
|
|
TreeId: tid,
|
2023-07-06 12:36:41 +00:00
|
|
|
PathAttribute: objectSDK.AttributeFileName,
|
2023-03-07 13:38:26 +00:00
|
|
|
// PathAttribute: pAttr,
|
2022-10-12 14:48:21 +00:00
|
|
|
Path: strings.Split(path, "/"),
|
|
|
|
Meta: meta,
|
2023-07-11 10:31:49 +00:00
|
|
|
BearerToken: bt,
|
2022-10-12 14:48:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 09:36:33 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "signing message: %w", tree.SignMessage(req, pk))
|
2022-10-12 14:48:21 +00:00
|
|
|
|
|
|
|
resp, err := cli.AddByPath(ctx, req)
|
2023-07-11 09:36:33 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "failed to addByPath %w", err)
|
2022-10-12 14:48:21 +00:00
|
|
|
|
|
|
|
cmd.Printf("Parent ID: %d\n", resp.GetBody().GetParentId())
|
|
|
|
|
|
|
|
nn := resp.GetBody().GetNodes()
|
|
|
|
if len(nn) == 0 {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "No new nodes were created")
|
2022-10-12 14:48:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("Created nodes:")
|
|
|
|
for _, node := range resp.GetBody().GetNodes() {
|
|
|
|
cmd.Printf("\t%d\n", node)
|
|
|
|
}
|
|
|
|
}
|