2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-12-17 16:15:27 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-05-31 11:03:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2020-12-17 16:15:27 +00:00
|
|
|
storagegroupAPI "github.com/nspcc-dev/neofs-api-go/pkg/storagegroup"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// storagegroupCmd represents the storagegroup command
|
|
|
|
var storagegroupCmd = &cobra.Command{
|
|
|
|
Use: "storagegroup",
|
|
|
|
Short: "Operations with Storage Groups",
|
|
|
|
Long: `Operations with Storage Groups`,
|
|
|
|
}
|
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
var sgPutCmd = &cobra.Command{
|
|
|
|
Use: "put",
|
|
|
|
Short: "Put storage group to NeoFS",
|
|
|
|
Long: "Put storage group to NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: putSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sgGetCmd = &cobra.Command{
|
|
|
|
Use: "get",
|
|
|
|
Short: "Get storage group from NeoFS",
|
|
|
|
Long: "Get storage group from NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: getSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sgListCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List storage groups in NeoFS container",
|
|
|
|
Long: "List storage groups in NeoFS container",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: listSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sgDelCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Delete storage group from NeoFS",
|
|
|
|
Long: "Delete storage group from NeoFS",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: delSG,
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
sgMembersFlag = "members"
|
|
|
|
sgIDFlag = "id"
|
2021-02-09 21:55:38 +00:00
|
|
|
sgBearerFlag = "bearer"
|
2020-12-17 16:15:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sgMembers []string
|
|
|
|
sgID string
|
|
|
|
)
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(storagegroupCmd)
|
|
|
|
|
2021-02-09 21:55:38 +00:00
|
|
|
storagegroupCmd.PersistentFlags().String(sgBearerFlag, "",
|
|
|
|
"File with signed JSON or binary encoded bearer token")
|
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
storagegroupCmd.AddCommand(sgPutCmd)
|
|
|
|
sgPutCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = sgPutCmd.MarkFlagRequired("cid")
|
|
|
|
sgPutCmd.Flags().StringSliceVarP(&sgMembers, sgMembersFlag, "m", nil,
|
|
|
|
"ID list of storage group members")
|
|
|
|
_ = sgPutCmd.MarkFlagRequired(sgMembersFlag)
|
|
|
|
|
|
|
|
storagegroupCmd.AddCommand(sgGetCmd)
|
|
|
|
sgGetCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = sgGetCmd.MarkFlagRequired("cid")
|
|
|
|
sgGetCmd.Flags().StringVarP(&sgID, sgIDFlag, "", "", "storage group identifier")
|
|
|
|
_ = sgGetCmd.MarkFlagRequired(sgIDFlag)
|
|
|
|
|
|
|
|
storagegroupCmd.AddCommand(sgListCmd)
|
|
|
|
sgListCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = sgListCmd.MarkFlagRequired("cid")
|
|
|
|
|
|
|
|
storagegroupCmd.AddCommand(sgDelCmd)
|
|
|
|
sgDelCmd.Flags().String("cid", "", "Container ID")
|
|
|
|
_ = sgDelCmd.MarkFlagRequired("cid")
|
|
|
|
sgDelCmd.Flags().StringVarP(&sgID, sgIDFlag, "", "", "storage group identifier")
|
|
|
|
_ = sgDelCmd.MarkFlagRequired(sgIDFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sgHeadReceiver struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
tok *session.Token
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-03-17 11:34:59 +00:00
|
|
|
c client.Client
|
2021-02-19 10:32:22 +00:00
|
|
|
|
|
|
|
bearerToken *token.BearerToken
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sgHeadReceiver) Head(addr *objectSDK.Address) (interface{}, error) {
|
|
|
|
obj, err := c.c.GetObjectHeader(c.ctx,
|
|
|
|
new(client.ObjectHeaderParams).
|
|
|
|
WithAddress(addr).
|
|
|
|
WithRawFlag(true),
|
|
|
|
client.WithTTL(2),
|
|
|
|
client.WithSession(c.tok),
|
2021-02-19 10:32:22 +00:00
|
|
|
client.WithBearer(c.bearerToken),
|
2020-12-17 16:15:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errSplitInfo *objectSDK.SplitInfoError
|
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
case err == nil:
|
|
|
|
return object.NewFromSDK(obj), nil
|
|
|
|
case errors.As(err, &errSplitInfo):
|
|
|
|
return errSplitInfo.SplitInfo(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 21:55:38 +00:00
|
|
|
func sgBearerToken(cmd *cobra.Command) (*token.BearerToken, error) {
|
|
|
|
return getBearerToken(cmd, sgBearerFlag)
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func putSG(cmd *cobra.Command, _ []string) {
|
2021-06-22 16:21:18 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't fetch private key: %w", err))
|
2021-06-22 16:21:18 +00:00
|
|
|
|
|
|
|
ownerID, err := getOwnerID(key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
cid, err := getCID(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
members := make([]*objectSDK.ID, 0, len(sgMembers))
|
|
|
|
|
|
|
|
for i := range sgMembers {
|
|
|
|
id := objectSDK.NewID()
|
2021-07-06 12:27:54 +00:00
|
|
|
|
|
|
|
err = id.Parse(sgMembers[i])
|
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
members = append(members, id)
|
|
|
|
}
|
|
|
|
|
2021-02-09 21:55:38 +00:00
|
|
|
bearerToken, err := sgBearerToken(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-02-09 21:55:38 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-22 16:21:18 +00:00
|
|
|
cli, tok, err := initSession(ctx, key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
sg, err := storagegroup.CollectMembers(&sgHeadReceiver{
|
2021-02-19 10:32:22 +00:00
|
|
|
ctx: ctx,
|
|
|
|
tok: tok,
|
|
|
|
c: cli,
|
|
|
|
bearerToken: bearerToken,
|
2020-12-17 16:15:27 +00:00
|
|
|
}, cid, members)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
sgContent, err := sg.Marshal()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
obj := objectSDK.NewRaw()
|
|
|
|
obj.SetContainerID(cid)
|
|
|
|
obj.SetOwnerID(ownerID)
|
|
|
|
obj.SetType(objectSDK.TypeStorageGroup)
|
|
|
|
obj.SetPayload(sgContent)
|
|
|
|
|
|
|
|
oid, err := cli.PutObject(ctx,
|
|
|
|
new(client.PutObjectParams).
|
|
|
|
WithObject(obj.Object()),
|
2021-02-09 21:48:19 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
2021-02-09 21:55:38 +00:00
|
|
|
client.WithBearer(bearerToken),
|
2021-02-09 21:48:19 +00:00
|
|
|
)...,
|
|
|
|
)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't put storage group: %w", err))
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
cmd.Println("Storage group successfully stored")
|
2021-02-09 21:59:49 +00:00
|
|
|
cmd.Printf(" ID: %s\n CID: %s\n", oid, cid)
|
2020-12-17 16:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSGID() (*objectSDK.ID, error) {
|
|
|
|
oid := objectSDK.NewID()
|
|
|
|
err := oid.Parse(sgID)
|
|
|
|
|
|
|
|
return oid, err
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func getSG(cmd *cobra.Command, _ []string) {
|
2021-06-22 16:21:18 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't fetch private key: %w", err))
|
2021-06-22 16:21:18 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
cid, err := getCID(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
id, err := getSGID()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-02-09 21:55:38 +00:00
|
|
|
bearerToken, err := sgBearerToken(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-02-09 21:55:38 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
addr := objectSDK.NewAddress()
|
|
|
|
addr.SetContainerID(cid)
|
|
|
|
addr.SetObjectID(id)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-22 16:21:18 +00:00
|
|
|
cli, tok, err := initSession(ctx, key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
obj, err := cli.GetObject(ctx,
|
|
|
|
new(client.GetObjectParams).
|
|
|
|
WithAddress(addr),
|
2021-02-09 21:48:19 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
2021-02-09 21:55:38 +00:00
|
|
|
client.WithBearer(bearerToken),
|
2021-02-09 21:48:19 +00:00
|
|
|
)...,
|
|
|
|
)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't get storage group: %w", err))
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
sg := storagegroupAPI.New()
|
2021-07-06 12:27:54 +00:00
|
|
|
|
|
|
|
err = sg.Unmarshal(obj.Payload())
|
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
cmd.Printf("Expiration epoch: %d\n", sg.ExpirationEpoch())
|
|
|
|
cmd.Printf("Group size: %d\n", sg.ValidationDataSize())
|
|
|
|
cmd.Printf("Group hash: %s\n", sg.ValidationDataHash())
|
|
|
|
|
|
|
|
if members := sg.Members(); len(members) > 0 {
|
|
|
|
cmd.Println("Members:")
|
|
|
|
|
|
|
|
for i := range members {
|
|
|
|
cmd.Printf("\t%s\n", members[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func listSG(cmd *cobra.Command, _ []string) {
|
2021-06-22 16:21:18 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't fetch private key: %w", err))
|
2021-06-22 16:21:18 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
cid, err := getCID(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-02-09 21:55:38 +00:00
|
|
|
bearerToken, err := sgBearerToken(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-02-09 21:55:38 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-22 16:21:18 +00:00
|
|
|
cli, tok, err := initSession(ctx, key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
ids, err := cli.SearchObject(ctx,
|
|
|
|
new(client.SearchObjectParams).
|
|
|
|
WithContainerID(cid).
|
|
|
|
WithSearchFilters(storagegroup.SearchQuery()),
|
2021-02-09 21:48:19 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
2021-02-09 21:55:38 +00:00
|
|
|
client.WithBearer(bearerToken),
|
2021-02-09 21:48:19 +00:00
|
|
|
)...,
|
2020-12-17 16:15:27 +00:00
|
|
|
)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't search storage groups: %w", err))
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
cmd.Printf("Found %d storage groups.\n", len(ids))
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
cmd.Println(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func delSG(cmd *cobra.Command, _ []string) {
|
2021-06-22 16:21:18 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't fetch private key: %w", err))
|
2021-06-22 16:21:18 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
cid, err := getCID(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
id, err := getSGID()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
2021-02-09 21:55:38 +00:00
|
|
|
bearerToken, err := sgBearerToken(cmd)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-02-09 21:55:38 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-22 16:21:18 +00:00
|
|
|
cli, tok, err := initSession(ctx, key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-12-17 16:15:27 +00:00
|
|
|
|
|
|
|
addr := objectSDK.NewAddress()
|
|
|
|
addr.SetContainerID(cid)
|
|
|
|
addr.SetObjectID(id)
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
tombstone, err := client.DeleteObject(ctx, cli,
|
2020-12-17 16:15:27 +00:00
|
|
|
new(client.DeleteObjectParams).
|
|
|
|
WithAddress(addr),
|
2021-02-09 21:48:19 +00:00
|
|
|
append(globalCallOptions(),
|
|
|
|
client.WithSession(tok),
|
2021-02-09 21:55:38 +00:00
|
|
|
client.WithBearer(bearerToken),
|
2021-02-09 21:48:19 +00:00
|
|
|
)...,
|
|
|
|
)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("can't get storage group: %w", err))
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-12-17 16:15:27 +00:00
|
|
|
cmd.Println("Storage group removed successfully.")
|
|
|
|
cmd.Printf(" Tombstone: %s\n", tombstone.ObjectID())
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|