Skip to content

Commit

Permalink
feature/curve-bs-delete-snapshot
Browse files Browse the repository at this point in the history
 Signed-off-by: lng2020 <[email protected]>>
  • Loading branch information
lng2020 committed Jul 20, 2023
1 parent ab052f2 commit 2da7e17
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/file"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/peer"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/snapshot"
)

type DeleteCommand struct {
Expand All @@ -24,6 +25,7 @@ func (dCmd *DeleteCommand) AddSubCommands() {
dCmd.Cmd.AddCommand(
file.NewFileCommand(),
peer.NewCommand(),
snapshot.NewSnapShotCommand(),
)
}

Expand Down
166 changes: 166 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/delete/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Project: tools-v2
* Created Date: 2023-7-20
* Author: [email protected]
*/
package snapshot

import (
"context"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"

cmderror "github.com/opencurve/curve/tools-v2/internal/error"
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/opencurve/curve/tools-v2/proto/proto/nameserver2"
)

const (
deleteCliExample = `curve bs delete snapshot --path /curvebs-file-path --snapshotSeq 0 --user [username] [--password password]`
)

type DeleteSnapShotRpc struct {
Info *basecmd.Rpc
Request *nameserver2.DeleteSnapShotRequest
mdsClient nameserver2.CurveFSServiceClient
}

// DeleteCommand definition
type DeleteCommand struct {
basecmd.FinalCurveCmd
Rpc *DeleteSnapShotRpc
Response *nameserver2.DeleteSnapShotResponse
}

var _ basecmd.FinalCurveCmdFunc = (*DeleteCommand)(nil)

func (gRpc *DeleteSnapShotRpc) NewRpcClient(cc grpc.ClientConnInterface) {
gRpc.mdsClient = nameserver2.NewCurveFSServiceClient(cc)

}

func (gRpc *DeleteSnapShotRpc) Stub_Func(ctx context.Context) (interface{}, error) {
return gRpc.mdsClient.DeleteSnapShot(ctx, gRpc.Request)
}

func (deleteCommand *DeleteCommand) Init(cmd *cobra.Command, args []string) error {
mdsAddrs, err := config.GetBsMdsAddrSlice(deleteCommand.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
//get the default timeout and retrytimes
timeout := config.GetFlagDuration(deleteCommand.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(deleteCommand.Cmd, config.RPCRETRYTIMES)
path := config.GetBsFlagString(deleteCommand.Cmd, config.CURVEBS_PATH)
username := config.GetBsFlagString(deleteCommand.Cmd, config.CURVEBS_USER)
password := config.GetBsFlagString(deleteCommand.Cmd, config.CURVEBS_PASSWORD)
seq := config.GetBsSnapshotSeq(deleteCommand.Cmd)
date, errDat := cobrautil.GetTimeofDayUs()
if errDat.TypeCode() != cmderror.CODE_SUCCESS {
return errDat.ToError()
}
deleteRequest := nameserver2.DeleteSnapShotRequest{
FileName: &path,
Seq: &seq,
Owner: &username,
Date: &date,
}
if username == viper.GetString(config.VIPER_CURVEBS_USER) && len(password) != 0 {
strSig := cobrautil.GetString2Signature(date, username)
sig := cobrautil.CalcString2Signature(strSig, password)
deleteRequest.Signature = &sig
}
deleteCommand.Rpc = &DeleteSnapShotRpc{
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "DeleteFile"),
Request: &deleteRequest,
}
header := []string{cobrautil.ROW_RESULT}
deleteCommand.SetHeader(header)
deleteCommand.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
deleteCommand.Header, header,
))
return nil
}

func (deleteCommand *DeleteCommand) RunCommand(cmd *cobra.Command, args []string) error {
result, err := basecmd.GetRpcResponse(deleteCommand.Rpc.Info, deleteCommand.Rpc)
if err.TypeCode() != cmderror.CODE_SUCCESS {
deleteCommand.Error = err
deleteCommand.Result = result
return err.ToError()
}
deleteCommand.Response = result.(*nameserver2.DeleteSnapShotResponse)
if deleteCommand.Response.GetStatusCode() != nameserver2.StatusCode_kOK {
deleteCommand.Error = cmderror.ErrBsDeleteFile()
deleteCommand.Result = result
return deleteCommand.Error.ToError()
}
out := make(map[string]string)
out[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
list := cobrautil.Map2List(out, []string{cobrautil.ROW_RESULT})
deleteCommand.TableNew.Append(list)

deleteCommand.Result, deleteCommand.Error = result, cmderror.Success()
return nil
}

func (deleteCommand *DeleteCommand) Print(cmd *cobra.Command, args []string) error {
return output.FinalCmdOutput(&deleteCommand.FinalCurveCmd, deleteCommand)
}

func (deleteCommand *DeleteCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&deleteCommand.FinalCurveCmd)
}

func (deleteCommand *DeleteCommand) AddFlags() {
config.AddBsMdsFlagOption(deleteCommand.Cmd)
config.AddRpcTimeoutFlag(deleteCommand.Cmd)
config.AddRpcRetryTimesFlag(deleteCommand.Cmd)

config.AddBsPathRequiredFlag(deleteCommand.Cmd)
config.AddBsSnapshotSeqRequiredFlag(deleteCommand.Cmd)
config.AddBsUserOptionFlag(deleteCommand.Cmd)
config.AddBsPasswordOptionFlag(deleteCommand.Cmd)
}

// NewCommand return the mid cli
func NewDeleteSnapShotCommand() *DeleteCommand {
deleteCommand := &DeleteCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "snapshot",
Short: "delete volumn snapshot in curvebs",
Example: deleteCliExample,
},
}
basecmd.NewFinalCurveCli(&deleteCommand.FinalCurveCmd, deleteCommand)
return deleteCommand
}

func NewSnapShotCommand() *cobra.Command {
return NewDeleteSnapShotCommand().Cmd
}

// DeleteFile function wraps the DeleteCertainFile rpc
func DeleteFile(caller *cobra.Command) (*nameserver2.DeleteSnapShotResponse, *cmderror.CmdError) {
delCmd := NewDeleteSnapShotCommand()
config.AlignFlagsValue(caller, delCmd.Cmd, []string{
config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR,
config.CURVEBS_PATH, config.CURVEBS_USER, config.CURVEBS_PASSWORD,
config.CURVEBS_SNAPSHOT_SEQ,
})
delCmd.Cmd.SilenceErrors = true
delCmd.Cmd.SilenceUsage = true
delCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT})
err := delCmd.Cmd.Execute()
if err != nil {
retErr := cmderror.ErrBsDeleteFile()
retErr.Format(err.Error())
return delCmd.Response, retErr
}
return delCmd.Response, cmderror.Success()
}
13 changes: 13 additions & 0 deletions tools-v2/pkg/config/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ const (
CURVEBS_ALL = "all"
VIPER_CURVEBS_ALL = "curvebs.all"
CURVEBS_DEFAULT_ALL = false
CURVEBS_SNAPSHOT_SEQ = "snapshotSeq"
VIPER_CURVEBS_SNAPSHOT_SEQ = "curvebs.snapshotSeq"
CURVEBS_SNAPSHOT_SEQ_DEFAULT = uint64(0)
)

var (
Expand Down Expand Up @@ -177,6 +180,7 @@ var (
CURVEBS_CHUNKSERVER_ADDRESS: VIPER_CURVEBS_CHUNKSERVER_ADDRESS,
CURVEBS_FIlTER: VIPER_CURVEBS_FILTER,
CURVEBS_ALL: VIPER_CURVEBS_ALL,
CURVEBS_SNAPSHOT_SEQ: VIPER_CURVEBS_SNAPSHOT_SEQ,
}

BSFLAG2DEFAULT = map[string]interface{}{
Expand All @@ -200,6 +204,7 @@ var (
CURVEBS_ALL: CURVEBS_DEFAULT_ALL,
CURVEBS_LOGIC_POOL_ID: CURVEBS_DEFAULT_LOGIC_POOL_ID,
CURVEBS_COPYSET_ID: CURVEBS_DEFAULT_COPYSET_ID,
CURVEBS_SNAPSHOT_SEQ: CURVEBS_SNAPSHOT_SEQ_DEFAULT,
}
)

Expand Down Expand Up @@ -582,6 +587,10 @@ func AddBsChunkServerAddressSliceRequiredFlag(cmd *cobra.Command) {
AddBsStringSliceRequiredFlag(cmd, CURVEBS_CHUNKSERVER_ADDRESS, "chunk server address")
}

func AddBsSnapshotSeqRequiredFlag(cmd *cobra.Command) {
AddBsUint64RequiredFlag(cmd, CURVEBS_SNAPSHOT_SEQ, "snapshot sequence num")
}

// get stingslice flag
func GetBsFlagStringSlice(cmd *cobra.Command, flagName string) []string {
var value []string
Expand Down Expand Up @@ -765,3 +774,7 @@ func GetBsChunkServerId(cmd *cobra.Command) []uint32 {
}
return chunkserveridSlice
}

func GetBsSnapshotSeq(cmd *cobra.Command) uint64 {
return GetBsFlagUint64(cmd, CURVEBS_SNAPSHOT_SEQ)
}

0 comments on commit 2da7e17

Please sign in to comment.