2022-10-10 15:35:40 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-06-05 15:54:59 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
2023-03-07 13:38:26 +00:00
|
|
|
"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"
|
2022-10-10 15:35:40 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var addCmd = &cobra.Command{
|
|
|
|
Use: "add",
|
|
|
|
Short: "Add a node to the tree service",
|
|
|
|
Run: add,
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
|
|
|
|
commonflags.Bind(cmd)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func initAddCmd() {
|
|
|
|
commonflags.Init(addCmd)
|
|
|
|
initCTID(addCmd)
|
|
|
|
|
|
|
|
ff := addCmd.Flags()
|
|
|
|
ff.StringSlice(metaFlagKey, nil, "Meta pairs in the form of Key1=[0x]Value1,Key2=[0x]Value2")
|
|
|
|
ff.Uint64(parentIDFlagKey, 0, "Parent node ID")
|
|
|
|
|
|
|
|
_ = cobra.MarkFlagRequired(ff, commonflags.RPC)
|
|
|
|
}
|
|
|
|
|
|
|
|
func add(cmd *cobra.Command, _ []string) {
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
|
|
|
var cnr cid.ID
|
2022-10-18 11:43:04 +00:00
|
|
|
err := cnr.DecodeString(cmd.Flag(commonflags.CIDFlag).Value.String())
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err)
|
2022-10-10 15:35:40 +00:00
|
|
|
|
|
|
|
tid, _ := cmd.Flags().GetString(treeIDFlagKey)
|
|
|
|
pid, _ := cmd.Flags().GetUint64(parentIDFlagKey)
|
|
|
|
|
|
|
|
meta, err := parseMeta(cmd)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "meta data parsing: %w", err)
|
2022-10-10 15:35:40 +00:00
|
|
|
|
|
|
|
ctx := cmd.Context()
|
|
|
|
|
|
|
|
cli, err := _client(ctx)
|
2023-07-11 09:36:33 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "failed to create client: %w", err)
|
2022-10-10 15:35:40 +00:00
|
|
|
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(rawCID)
|
|
|
|
|
2023-07-11 10:31:49 +00:00
|
|
|
var bt []byte
|
|
|
|
if t := common.ReadBearerToken(cmd, bearerFlagKey); t != nil {
|
|
|
|
bt = t.Marshal()
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:35:40 +00:00
|
|
|
req := new(tree.AddRequest)
|
|
|
|
req.Body = &tree.AddRequest_Body{
|
|
|
|
ContainerId: rawCID,
|
|
|
|
TreeId: tid,
|
|
|
|
ParentId: pid,
|
|
|
|
Meta: meta,
|
2023-07-11 10:31:49 +00:00
|
|
|
BearerToken: bt,
|
2022-10-10 15:35:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 09:36:33 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "signing message: %w", tree.SignMessage(req, pk))
|
2022-10-10 15:35:40 +00:00
|
|
|
|
|
|
|
resp, err := cli.Add(ctx, req)
|
2023-07-11 09:36:33 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "failed to cal add: %w", err)
|
2022-10-10 15:35:40 +00:00
|
|
|
|
2023-12-11 10:18:34 +00:00
|
|
|
cmd.Println("Node ID: ", resp.GetBody().GetNodeId())
|
2022-10-10 15:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseMeta(cmd *cobra.Command) ([]*tree.KeyValue, error) {
|
|
|
|
raws, _ := cmd.Flags().GetStringSlice(metaFlagKey)
|
|
|
|
if len(raws) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs := make([]*tree.KeyValue, 0, len(raws))
|
|
|
|
for i := range raws {
|
2023-02-27 14:19:35 +00:00
|
|
|
k, v, found := strings.Cut(raws[i], "=")
|
|
|
|
if !found {
|
2022-10-10 15:35:40 +00:00
|
|
|
return nil, fmt.Errorf("invalid meta pair format: %s", raws[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var pair tree.KeyValue
|
2023-02-27 14:19:35 +00:00
|
|
|
pair.Key = k
|
|
|
|
pair.Value = []byte(v)
|
2022-10-10 15:35:40 +00:00
|
|
|
|
|
|
|
pairs = append(pairs, &pair)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pairs, nil
|
|
|
|
}
|