-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package stop | ||
|
||
import ( | ||
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" | ||
snapshot "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/stop/volumeSnapshot" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type StopCommand struct { | ||
basecmd.MidCurveCmd | ||
} | ||
|
||
var _ basecmd.MidCurveCmdFunc = (*StopCommand)(nil) | ||
|
||
func (sCmd *StopCommand) AddSubCommands() { | ||
sCmd.Cmd.AddCommand( | ||
snapshot.NewStopVolumeSnapshotCommand(), | ||
) | ||
} | ||
|
||
func NewStopCommand() *cobra.Command { | ||
sCmd := &StopCommand{ | ||
basecmd.MidCurveCmd{ | ||
Use: "stop", | ||
Short: "stop volume snapshot in the curvebs", | ||
}, | ||
} | ||
return basecmd.NewMidCurveCli(&sCmd.MidCurveCmd, sCmd) | ||
} |
170 changes: 170 additions & 0 deletions
170
tools-v2/pkg/cli/command/curvebs/stop/volumeSnapshot/stop_volume.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package volumeSnapshot | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
cmderror "github.com/opencurve/curve/tools-v2/internal/error" | ||
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" | ||
) | ||
|
||
const ( | ||
VERSION = "0.0.6" | ||
LIMIT = "100" | ||
OFFSET = "0" | ||
) | ||
|
||
type Stop struct { | ||
serverAddress []string | ||
timeout time.Duration | ||
|
||
User string | ||
FileName string | ||
UUID string | ||
|
||
messages []string | ||
} | ||
|
||
func newStopSnapShot(serverAddress []string, timeout time.Duration, user, fileName, uuid string) *Stop { | ||
return &Stop{ | ||
serverAddress: serverAddress, | ||
timeout: timeout, | ||
User: user, | ||
FileName: fileName, | ||
UUID: uuid, | ||
} | ||
} | ||
|
||
type Record struct { | ||
UUID string `json:"UUID"` | ||
User string `json:"User"` | ||
File string `json:"File"` | ||
|
||
Result string `json:"Result"` | ||
} | ||
|
||
func (s *Stop) queryStopBy() ([]*Record, *cmderror.CmdError) { | ||
records, err := s.getSnapShotListAll() | ||
return records, err | ||
} | ||
|
||
func (s *Stop) getSnapShotListAll() ([]*Record, *cmderror.CmdError) { | ||
var receiveRecords []*Record | ||
params := QueryParams{ | ||
Action: "GetFileSnapshotList", | ||
Version: VERSION, | ||
User: s.User, | ||
UUID: s.UUID, | ||
File: s.FileName, | ||
Limit: LIMIT, | ||
Offset: OFFSET, | ||
} | ||
for { | ||
records, err := s.getSnapShotList(params) | ||
if err != nil || len(records) == 0 || records == nil { | ||
return receiveRecords, err | ||
} | ||
receiveRecords = append(receiveRecords, records...) | ||
|
||
params.Offset = params.Offset + params.Limit | ||
} | ||
} | ||
|
||
type QueryParams struct { | ||
Action string `json:"Action"` | ||
Version string `json:"Version"` | ||
User string `json:"User"` | ||
File string `json:"File"` | ||
UUID string `json:"UUID"` | ||
Limit string `json:"limit"` | ||
Offset string `json:"Offset"` | ||
} | ||
|
||
func (s *Stop) getSnapShotList(params QueryParams) ([]*Record, *cmderror.CmdError) { | ||
var resp struct { | ||
Code string | ||
Message string | ||
RequestId string | ||
TotalCount int | ||
SnapShots []*Record | ||
} | ||
err := s.query(params, &resp) | ||
if err != nil || resp.Code != "0" { | ||
return resp.SnapShots, err | ||
} | ||
return resp.SnapShots, nil | ||
} | ||
|
||
func (s *Stop) query(params QueryParams, data interface{}) *cmderror.CmdError { | ||
encodedParams := s.encodeParam(params) | ||
|
||
subUri := fmt.Sprintf("/SnapshotCloneService?%s", encodedParams) | ||
|
||
metric := basecmd.NewMetric(s.serverAddress, subUri, s.timeout) | ||
|
||
result, err := basecmd.QueryMetric(metric) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS { | ||
return err | ||
} | ||
|
||
if err := json.Unmarshal([]byte(result), &data); err != nil { | ||
retErr := cmderror.ErrUnmarshalJson() | ||
retErr.Format(err.Error()) | ||
return retErr | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Stop) encodeParam(params QueryParams) string { | ||
paramsMap := map[string]string{} | ||
if params.Action == "CancelSnapshot" { | ||
paramsMap = map[string]string{ | ||
"Action": params.Action, | ||
"Version": params.Version, | ||
"User": params.User, | ||
"UUID": params.UUID, | ||
"File": params.File, | ||
} | ||
} else { | ||
paramsMap = map[string]string{ | ||
"Action": params.Action, | ||
"Version": params.Version, | ||
"User": params.User, | ||
"UUID": params.UUID, | ||
"File": params.File, | ||
"Limit": params.Limit, | ||
"Offset": params.Offset, | ||
} | ||
} | ||
|
||
values := strings.Builder{} | ||
for key, value := range paramsMap { | ||
if value != "" { | ||
values.WriteString(key) | ||
values.WriteString("=") | ||
values.WriteString(value) | ||
values.WriteString("&") | ||
} | ||
} | ||
str := values.String() | ||
return str[:len(str)-1] | ||
} | ||
|
||
func (s *Stop) stopSnapShot(uuid, user, file string) *cmderror.CmdError { | ||
params := QueryParams{ | ||
Action: "CancelSnapshot", | ||
Version: VERSION, | ||
User: user, | ||
UUID: uuid, | ||
File: file, | ||
} | ||
|
||
err := s.query(params, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
} |
97 changes: 97 additions & 0 deletions
97
tools-v2/pkg/cli/command/curvebs/stop/volumeSnapshot/volumeSnapshot.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package volumeSnapshot | ||
|
||
import ( | ||
"time" | ||
|
||
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/spf13/cobra" | ||
) | ||
|
||
const ( | ||
stopExample = `$ curve bs stop volumeSnapshot` | ||
) | ||
|
||
type StopCmd struct { | ||
basecmd.FinalCurveCmd | ||
snapshotAddrs []string | ||
timeout time.Duration | ||
|
||
user string | ||
fileName string | ||
uuid string | ||
} | ||
|
||
var _ basecmd.FinalCurveCmdFunc = (*StopCmd)(nil) | ||
|
||
func (sCmd *StopCmd) Init(cmd *cobra.Command, args []string) error { | ||
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 { | ||
return err.ToError() | ||
} | ||
sCmd.snapshotAddrs = snapshotAddrs | ||
sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT) | ||
sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER) | ||
sCmd.fileName = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_PATH) | ||
sCmd.uuid = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOTSEQID) | ||
header := []string{cobrautil.ROW_ID, cobrautil.ROW_RESULT} | ||
sCmd.SetHeader(header) | ||
return nil | ||
} | ||
|
||
func (sCmd *StopCmd) RunCommand(cmd *cobra.Command, args []string) error { | ||
s := newStopSnapShot(sCmd.snapshotAddrs, sCmd.timeout, sCmd.user, sCmd.fileName, sCmd.uuid) | ||
records, err := s.queryStopBy() | ||
if err != nil { | ||
sCmd.Error = err | ||
return sCmd.Error.ToError() | ||
} | ||
if records == nil || len(records) == 0 { | ||
sCmd.Result = cobrautil.ROW_VALUE_FAILED | ||
sCmd.Error = cmderror.ErrBsEligibleSnapShot() | ||
return sCmd.Error.ToError() | ||
} | ||
for _, item := range records { | ||
err := s.stopSnapShot(item.UUID, item.User, item.File) | ||
if err.TypeCode() == cmderror.CODE_SUCCESS { | ||
item.Result = cobrautil.ROW_VALUE_SUCCESS | ||
} else { | ||
item.Result = cobrautil.ROW_VALUE_FAILED | ||
} | ||
sCmd.TableNew.Append([]string{item.UUID, item.Result}) | ||
} | ||
sCmd.Result = records | ||
sCmd.Error = cmderror.Success() | ||
return nil | ||
} | ||
|
||
func (sCmd *StopCmd) Print(cmd *cobra.Command, args []string) error { | ||
return output.FinalCmdOutput(&sCmd.FinalCurveCmd, sCmd) | ||
} | ||
|
||
func (sCmd *StopCmd) ResultPlainOutput() error { | ||
return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd) | ||
} | ||
|
||
func (sCmd *StopCmd) AddFlags() { | ||
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd) | ||
config.AddHttpTimeoutFlag(sCmd.Cmd) | ||
config.AddBsUserOptionFlag(sCmd.Cmd) | ||
config.AddBsSnapshotSeqIDOptionFlag(sCmd.Cmd) | ||
config.AddBsPathOptionFlag(sCmd.Cmd) | ||
} | ||
|
||
func NewStopVolumeSnapshotCommand() *cobra.Command { | ||
sCmd := &StopCmd{ | ||
FinalCurveCmd: basecmd.FinalCurveCmd{ | ||
Use: "volumeSnapshot", | ||
Short: "stop volume snapshot in curvebs cluster", | ||
Example: stopExample, | ||
}, | ||
} | ||
basecmd.NewFinalCurveCli(&sCmd.FinalCurveCmd, sCmd) | ||
return sCmd.Cmd | ||
} |
Oops, something went wrong.