[#315] cmd/cli: Add set-status command to control section

Add set-status command that uses SetNetmapStatus rpc of Control API.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-01-15 14:55:06 +03:00 committed by Alex Vanin
parent fa0ea35f83
commit 0690a54299

View file

@ -2,11 +2,13 @@ package cmd
import ( import (
"context" "context"
"fmt"
"github.com/nspcc-dev/neofs-api-go/util/signature" "github.com/nspcc-dev/neofs-api-go/util/signature"
"github.com/nspcc-dev/neofs-api-go/v2/client" "github.com/nspcc-dev/neofs-api-go/v2/client"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -23,12 +25,38 @@ var healthCheckCmd = &cobra.Command{
RunE: healthCheck, RunE: healthCheck,
} }
var setNetmapStatusCmd = &cobra.Command{
Use: "set-status",
Short: "Set status of the storage node in NeoFS network map",
Long: "Set status of the storage node in NeoFS network map",
RunE: setNetmapStatus,
}
const (
netmapStatusFlag = "status"
netmapStatusOnline = "online"
netmapStatusOffline = "offline"
)
var netmapStatus string
func init() { func init() {
rootCmd.AddCommand(controlCmd) rootCmd.AddCommand(controlCmd)
controlCmd.AddCommand(healthCheckCmd) controlCmd.AddCommand(
healthCheckCmd,
setNetmapStatusCmd,
)
controlCmd.AddCommand(snapshotCmd) setNetmapStatusCmd.Flags().StringVarP(&netmapStatus, netmapStatusFlag, "", "",
fmt.Sprintf("new netmap status keyword ('%s', '%s')",
netmapStatusOnline,
netmapStatusOffline,
),
)
_ = setNetmapStatusCmd.MarkFlagRequired(netmapStatusFlag)
} }
func getControlServiceClient() (control.ControlServiceClient, error) { func getControlServiceClient() (control.ControlServiceClient, error) {
@ -89,3 +117,54 @@ func healthCheck(cmd *cobra.Command, _ []string) error {
return nil return nil
} }
func setNetmapStatus(cmd *cobra.Command, _ []string) error {
key, err := getKey()
if err != nil {
return err
}
var status control.NetmapStatus
switch netmapStatus {
default:
return errors.Errorf("unsupported status %s", netmapStatus)
case netmapStatusOnline:
status = control.NetmapStatus_ONLINE
case netmapStatusOffline:
status = control.NetmapStatus_OFFLINE
}
req := new(control.SetNetmapStatusRequest)
body := new(control.SetNetmapStatusRequest_Body)
req.SetBody(body)
body.SetStatus(status)
if err := controlSvc.SignMessage(key, req); err != nil {
return err
}
cli, err := getControlServiceClient()
if err != nil {
return err
}
resp, err := cli.SetNetmapStatus(context.Background(), req)
if err != nil {
return err
}
sign := resp.GetSignature()
if err := signature.VerifyDataWithSource(resp, func() ([]byte, []byte) {
return sign.GetKey(), sign.GetSign()
}); err != nil {
return err
}
cmd.Println("Network status update request successfully sent.")
return nil
}