Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]tools-v2: add bs query volume clone-recover #2773

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 19 additions & 42 deletions tools-v2/pkg/cli/command/curvebs/delete/volume/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@
package clone

import (
"encoding/json"
"fmt"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/spf13/cobra"
"sync"
"time"

"github.com/spf13/cobra"

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/pkg/cli/command/curvebs/query/volume/clone_recover"
)

const (
Expand Down Expand Up @@ -77,45 +76,23 @@ func (rCmd *CloneCmd) Init(cmd *cobra.Command, args []string) error {
}

func (rCmd *CloneCmd) RunCommand(cmd *cobra.Command, args []string) error {
params := map[string]any{
cobrautil.QueryAction: cobrautil.ActionGetCloneTaskList,
cobrautil.QueryType: cobrautil.TypeCloneTask,
cobrautil.QueryUser: rCmd.user,
cobrautil.QueryUUID: rCmd.taskID,
cobrautil.QuerySource: rCmd.src,
cobrautil.QueryDestination: rCmd.dest,
cobrautil.QueryStatus: rCmd.status,
cobrautil.QueryLimit: 100,
cobrautil.QueryOffset: 0,
}
records := make([]map[string]string, 0)
for {
subUri := cobrautil.NewSnapshotQuerySubUri(params)
metric := basecmd.NewMetric(rCmd.snapshotAddrs, subUri, rCmd.timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}

var resp struct {
Code string `json:"Code"`
TaskInfos []map[string]string `json:"TaskInfos"`
TotalCount int `json:"TotalCount"`
}
if err := json.Unmarshal([]byte(result), &resp); err != nil {
return err
}
if resp.Code != "0" {
return fmt.Errorf("get clone list fail, error code: %s", resp.Code)
}
if len(resp.TaskInfos) == 0 {
break
} else {
records = append(records, resp.TaskInfos...)
params[cobrautil.QueryOffset] = params[cobrautil.QueryOffset].(int) + params[cobrautil.QueryLimit].(int)
}
cloneRecoverCmd := CloneRecoverCmd{
FinalCurveCmd: rCmd.FinalCurveCmd,
SnapshotAddrs: rCmd.snapshotAddrs,
Timeout: rCmd.timeout,
User: rCmd.user,
Src: rCmd.src,
Dest: rCmd.dest,
TaskID: rCmd.taskID,
All: rCmd.all,
Failed: rCmd.failed,
Status: rCmd.status,
}

records, err := QueryTaskList(&cloneRecoverCmd)
if err != nil {
return err
}
wg := sync.WaitGroup{}
for _, item := range records {
wg.Add(1)
Expand Down
55 changes: 17 additions & 38 deletions tools-v2/pkg/cli/command/curvebs/delete/volume/recover/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
package recover

import (
"encoding/json"
"fmt"
"sync"
"time"

Expand All @@ -33,6 +31,8 @@ import (
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/spf13/cobra"

. "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/query/volume/clone_recover"
)

const (
Expand Down Expand Up @@ -76,43 +76,22 @@ func (rCmd *RecoverCmd) Init(cmd *cobra.Command, args []string) error {
}

func (rCmd *RecoverCmd) RunCommand(cmd *cobra.Command, args []string) error {
params := map[string]any{
cobrautil.QueryAction: cobrautil.ActionGetCloneTaskList,
cobrautil.QueryType: cobrautil.TypeRecoverTask,
cobrautil.QueryUser: rCmd.user,
cobrautil.QueryUUID: rCmd.taskID,
cobrautil.QuerySource: rCmd.src,
cobrautil.QueryDestination: rCmd.dest,
cobrautil.QueryStatus: rCmd.status,
cobrautil.QueryLimit: 100,
cobrautil.QueryOffset: 0,
cloneRecoverCmd := CloneRecoverCmd{
FinalCurveCmd: rCmd.FinalCurveCmd,
SnapshotAddrs: rCmd.snapshotAddrs,
Timeout: rCmd.timeout,
User: rCmd.user,
Src: rCmd.src,
Dest: rCmd.dest,
TaskID: rCmd.taskID,
All: rCmd.all,
Failed: rCmd.failed,
Status: rCmd.status,
}
records := make([]map[string]string, 0)
for {
subUri := cobrautil.NewSnapshotQuerySubUri(params)
metric := basecmd.NewMetric(rCmd.snapshotAddrs, subUri, rCmd.timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}

var resp struct {
Code string `json:"Code"`
TaskInfos []map[string]string `json:"TaskInfos"`
TotalCount int `json:"TotalCount"`
}
if err := json.Unmarshal([]byte(result), &resp); err != nil {
return err
}
if resp.Code != "0" {
return fmt.Errorf("get clone list fail, error code: %s", resp.Code)
}
if len(resp.TaskInfos) == 0 {
break
} else {
records = append(records, resp.TaskInfos...)
params[cobrautil.QueryOffset] = params[cobrautil.QueryOffset].(int) + params[cobrautil.QueryLimit].(int)
}

records, err := QueryTaskList(&cloneRecoverCmd)
if err != nil {
return err
}

wg := sync.WaitGroup{}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2023 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Project: curve
* Created Date: 2023-09-21
* Author: xr-stb
*/

package clone_recover

import (
"encoding/json"
"fmt"
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"
"time"
)

type CloneRecoverCmd struct {
basecmd.FinalCurveCmd
SnapshotAddrs []string
Timeout time.Duration

User string
Src string
Dest string
TaskID string
All bool
Failed bool
Status string
}

func QueryTaskList(rCmd *CloneRecoverCmd) ([]map[string]string, error) {
params := map[string]interface{}{
cobrautil.QueryAction: cobrautil.ActionGetCloneTaskList,
cobrautil.QueryType: cobrautil.TypeCloneTask,
cobrautil.QueryUser: rCmd.User,
cobrautil.QueryUUID: rCmd.TaskID,
cobrautil.QuerySource: rCmd.Src,
cobrautil.QueryDestination: rCmd.Dest,
cobrautil.QueryStatus: rCmd.Status,
cobrautil.QueryLimit: 100,
cobrautil.QueryOffset: 0,
}
records := make([]map[string]string, 0)

for {
subUri := cobrautil.NewSnapshotQuerySubUri(params)
metric := basecmd.NewMetric(rCmd.SnapshotAddrs, subUri, rCmd.Timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return nil, err.ToError()
}

var resp struct {
Code string `json:"Code"`
TaskInfos []map[string]string `json:"TaskInfos"`
TotalCount int `json:"TotalCount"`
}
if err := json.Unmarshal([]byte(result), &resp); err != nil {
return nil, err
}
if resp.Code != "0" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined as constant

return nil, fmt.Errorf("get clone list fail, error code: %s", resp.Code)
}
if len(resp.TaskInfos) == 0 {
break
} else {
records = append(records, resp.TaskInfos...)
params[cobrautil.QueryOffset] = params[cobrautil.QueryOffset].(int) + params[cobrautil.QueryLimit].(int)
}
}

return records, nil
}