Dmitrii Stepanov
11add38e87
All checks were successful
DCO action / DCO (pull_request) Successful in 1m38s
Vulncheck / Vulncheck (pull_request) Successful in 3m2s
Build / Build Components (1.21) (pull_request) Successful in 3m51s
Build / Build Components (1.20) (pull_request) Successful in 4m3s
Tests and linters / Staticcheck (pull_request) Successful in 5m17s
Tests and linters / Lint (pull_request) Successful in 6m11s
Tests and linters / Tests (1.20) (pull_request) Successful in 11m7s
Tests and linters / Tests (1.21) (pull_request) Successful in 11m32s
Tests and linters / Tests with -race (pull_request) Successful in 12m22s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
package tree
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"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"
|
|
"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
|
|
err := cnr.DecodeString(cmd.Flag(commonflags.CIDFlag).Value.String())
|
|
commonCmd.ExitOnErr(cmd, "decode container ID string: %w", err)
|
|
|
|
tid, _ := cmd.Flags().GetString(treeIDFlagKey)
|
|
pid, _ := cmd.Flags().GetUint64(parentIDFlagKey)
|
|
|
|
meta, err := parseMeta(cmd)
|
|
commonCmd.ExitOnErr(cmd, "meta data parsing: %w", err)
|
|
|
|
ctx := cmd.Context()
|
|
|
|
cli, err := _client(ctx)
|
|
commonCmd.ExitOnErr(cmd, "failed to create client: %w", err)
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
cnr.Encode(rawCID)
|
|
|
|
var bt []byte
|
|
if t := common.ReadBearerToken(cmd, bearerFlagKey); t != nil {
|
|
bt = t.Marshal()
|
|
}
|
|
|
|
req := new(tree.AddRequest)
|
|
req.Body = &tree.AddRequest_Body{
|
|
ContainerId: rawCID,
|
|
TreeId: tid,
|
|
ParentId: pid,
|
|
Meta: meta,
|
|
BearerToken: bt,
|
|
}
|
|
|
|
commonCmd.ExitOnErr(cmd, "signing message: %w", tree.SignMessage(req, pk))
|
|
|
|
resp, err := cli.Add(ctx, req)
|
|
commonCmd.ExitOnErr(cmd, "failed to cal add: %w", err)
|
|
|
|
cmd.Println("Node ID: ", resp.GetBody().GetNodeId())
|
|
}
|
|
|
|
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 {
|
|
k, v, found := strings.Cut(raws[i], "=")
|
|
if !found {
|
|
return nil, fmt.Errorf("invalid meta pair format: %s", raws[i])
|
|
}
|
|
|
|
var pair tree.KeyValue
|
|
pair.Key = k
|
|
pair.Value = []byte(v)
|
|
|
|
pairs = append(pairs, &pair)
|
|
}
|
|
|
|
return pairs, nil
|
|
}
|