diff --git a/tools-v2/internal/utils/snapshot/const.go b/tools-v2/internal/utils/snapshot/const.go index 21537cef8c..933ec4291f 100644 --- a/tools-v2/internal/utils/snapshot/const.go +++ b/tools-v2/internal/utils/snapshot/const.go @@ -55,16 +55,3 @@ const ( Limit = "100" Offset = "0" ) - -func NewSnapshotQuerySubUri(params map[string]any) string { - values := url.Values{} - - values.Add(QueryVersion, Version) - for key, value := range params { - if value != "" { - values.Add(key, fmt.Sprintf("%v", value)) - } - } - - return "/SnapshotCloneService?" + values.Encode() -} diff --git a/tools-v2/pkg/cli/command/curvebs/status/volume/snapshot/snapshot.go b/tools-v2/pkg/cli/command/curvebs/status/volume/snapshot/snapshot.go index 0687f558d3..0015d1e551 100644 --- a/tools-v2/pkg/cli/command/curvebs/status/volume/snapshot/snapshot.go +++ b/tools-v2/pkg/cli/command/curvebs/status/volume/snapshot/snapshot.go @@ -25,74 +25,22 @@ package snapshot import ( "encoding/json" "fmt" + "strconv" + "time" + cmderror "github.com/opencurve/curve/tools-v2/internal/error" cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" + snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot" 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" - "strconv" - "time" ) -type QueryResponse struct { - Code string `json:"Code"` - Message string `json:"Message"` - RequestId string `json:"RequestId"` - TotalCount int `json:"TotalCount"` - Snapshots []*SnapshotInfoData `json:"Snapshots,omitempty"` -} - -type SnapshotInfoData struct { - UUID string `json:"UUID"` - User string `json:"User"` - FileName string `json:"FileName"` - SnapshotName string `json:"SnapshotName"` - SeqNum uint64 `json:"SeqNum"` - ChunkSize uint32 `json:"ChunkSize"` - SegmentSize uint64 `json:"SegmentSize"` - FileLength uint64 `json:"FileLength"` - Time uint64 `json:"Time"` - Status int32 `json:"Status"` - StripeUnit *uint64 `json:"StripeUnit,omitempty"` - StripeCount *uint64 `json:"StripeCount,omitempty"` - Poolset *string `json:"Poolset,omitempty"` -} - const ( - SNAPSHOT_QUERY_LIMIT = 100 - SNAPSHOT_QUERY_INITIAL_OFFSET = 0 - SNAPSHOT_QUERY_CODE_SUCCESS = "0" - LENGTH_EMPTY = 0 + LENGTH_EMPTY = 0 ) -const ( - STATUS_DONE = iota - STATUS_IN_PROGRESS - STATUS_DELETING - STATUS_ERROR_DELETING - STATUS_CANCELING - STATUS_ERROR -) - -var statusList = []int{ - STATUS_DONE, - STATUS_IN_PROGRESS, - STATUS_DELETING, - STATUS_ERROR_DELETING, - STATUS_CANCELING, - STATUS_ERROR, -} - -var statusName = map[int]string{ - STATUS_DONE: "done", - STATUS_IN_PROGRESS: "in-progress", - STATUS_DELETING: "deleting", - STATUS_ERROR_DELETING: "errorDeleting", - STATUS_CANCELING: "canceling", - STATUS_ERROR: "error", -} - type SnapshotCommand struct { basecmd.FinalCurveCmd @@ -140,15 +88,15 @@ func (sCmd *SnapshotCommand) Init(cmd *cobra.Command, args []string) error { func (sCmd *SnapshotCommand) RunCommand(cmd *cobra.Command, args []string) error { params := map[string]any{ - cobrautil.QueryAction: cobrautil.ActionGetFileSnapshotList, - cobrautil.QueryUser: sCmd.user, - cobrautil.QueryFile: sCmd.filename, - cobrautil.QueryLimit: SNAPSHOT_QUERY_LIMIT, - cobrautil.QueryOffset: SNAPSHOT_QUERY_INITIAL_OFFSET, + snapshotutil.QueryAction: snapshotutil.ActionGetFileSnapshotList, + snapshotutil.QueryUser: sCmd.user, + snapshotutil.QueryFile: sCmd.filename, + snapshotutil.QueryLimit: snapshotutil.Limit, + snapshotutil.QueryOffset: snapshotutil.Offset, } count := make(map[int]int) for { - subUri := cobrautil.NewSnapshotQuerySubUri(params) + subUri := snapshotutil.NewQuerySubUri(params) metric := basecmd.NewMetric(sCmd.snapshotAddrs, subUri, sCmd.timeout) result, err := basecmd.QueryMetric(metric) @@ -156,28 +104,32 @@ func (sCmd *SnapshotCommand) RunCommand(cmd *cobra.Command, args []string) error return err.ToError() } - var resp QueryResponse + var resp struct { + snapshotutil.Response + TotalCount int `json:"TotalCount"` + SnapShots []*snapshotutil.SnapshotInfo `json:"SnapShots"` + } if err := json.Unmarshal([]byte(result), &resp); err != nil { return err } - if resp.Code != SNAPSHOT_QUERY_CODE_SUCCESS { + if resp.Code != snapshotutil.ResultSuccess { return fmt.Errorf("get clone list fail, error code: %s", resp.Code) } - if len(resp.Snapshots) == LENGTH_EMPTY { + if len(resp.SnapShots) == LENGTH_EMPTY { break } - for _, s := range resp.Snapshots { - count[int(s.Status)] += 1 + for _, s := range resp.SnapShots { + count[s.Status] += 1 } - params[cobrautil.QueryOffset] = params[cobrautil.QueryOffset].(int) + params[cobrautil.QueryLimit].(int) + params[snapshotutil.QueryOffset] = params[snapshotutil.QueryOffset].(int) + params[snapshotutil.QueryLimit].(int) } rows := make([]map[string]string, 0) - for _, s := range statusList { + for i, s := range snapshotutil.SnapshotStatus { rows = append(rows, map[string]string{ - cobrautil.ROW_STATUS: statusName[s], - cobrautil.ROW_COUNT: strconv.Itoa(count[s]), + cobrautil.ROW_STATUS: s, + cobrautil.ROW_COUNT: strconv.Itoa(count[i]), }) } list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{})