Skip to content

Commit

Permalink
[feat]tools-v2: add bs update volume flatten
Browse files Browse the repository at this point in the history
Signed-off-by: baytan0720 <[email protected]>
  • Loading branch information
baytan0720 committed Sep 21, 2023
1 parent 862d8b0 commit 7c4c6f1
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 3 deletions.
18 changes: 18 additions & 0 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,24 @@ Output:
+---------+--------+
```

##### update volume flatten

update volume flatten in curvebs cluster

Usage:
```bash
curve bs update volume flatten
```

Output:
```
+------+--------------------------------------+---------+
| USER | TASK ID | RESULT |
+------+--------------------------------------+---------+
| root | d26e27a8-fcbd-4f7a-adf8-53795217cbb0 | success |
+------+--------------------------------------+---------+
```

#### create

##### create file
Expand Down
7 changes: 4 additions & 3 deletions tools-v2/pkg/cli/command/curvebs/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
package update

import (
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/copyset"
"github.com/spf13/cobra"

basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/copyset"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/file"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/leader"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/leader_schedule"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/peer"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/scan_state"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/throttle"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/volume"
"github.com/spf13/cobra"
)

type UpdateCommand struct {
Expand All @@ -50,6 +50,7 @@ func (updateCmd *UpdateCommand) AddSubCommands() {
scan_state.NewScanStateCommand(),
copyset.NewCopysetCommand(),
leader_schedule.NewLeaderScheduleCommand(),
volume.NewVolumeCommand(),
)
}

Expand Down
124 changes: 124 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/update/volume/flatten/flatten.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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: CurveCli
* Created Date: 2023-09-16
* Author: baytan0720
*/

package flatten

import (
"encoding/json"
"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 (
flattenExample = `$ curve bs update volume flatten`
version = "0.0.6"
)

type FlattenCmd struct {
basecmd.FinalCurveCmd
snapshotAddrs []string
timeout time.Duration

user string
taskID string
}

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

func NewCommand() *cobra.Command {
return NewFlattenCmd().Cmd
}

func NewFlattenCmd() *FlattenCmd {
fCmd := &FlattenCmd{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "flatten",
Short: "update volume flatten in curvebs cluster",
Example: flattenExample,
},
}
basecmd.NewFinalCurveCli(&fCmd.FinalCurveCmd, fCmd)
return fCmd
}

func (fCmd *FlattenCmd) AddFlags() {
config.AddBsSnapshotCloneFlagOption(fCmd.Cmd)
config.AddHttpTimeoutFlag(fCmd.Cmd)
config.AddBsUserOptionFlag(fCmd.Cmd)
config.AddBsTaskIDOptionFlag(fCmd.Cmd)
}

func (fCmd *FlattenCmd) Init(cmd *cobra.Command, args []string) error {
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(fCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 {
return err.ToError()
}
fCmd.snapshotAddrs = snapshotAddrs
fCmd.timeout = config.GetFlagDuration(fCmd.Cmd, config.HTTPTIMEOUT)
fCmd.user = config.GetBsFlagString(fCmd.Cmd, config.CURVEBS_USER)
fCmd.taskID = config.GetBsFlagString(fCmd.Cmd, config.CURVEBS_TASKID)
fCmd.SetHeader([]string{cobrautil.ROW_USER, cobrautil.ROW_TASK_ID, cobrautil.ROW_RESULT})
return nil
}

func (fCmd *FlattenCmd) RunCommand(cmd *cobra.Command, args []string) error {
params := map[string]any{
cobrautil.QueryAction: cobrautil.ActionFlatten,
cobrautil.QueryUser: fCmd.user,
cobrautil.QueryUUID: fCmd.taskID,
}
subUri := cobrautil.NewSnapshotQuerySubUri(params)
metric := basecmd.NewMetric(fCmd.snapshotAddrs, subUri, fCmd.timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
payload := map[string]any{}
if err := json.Unmarshal([]byte(result), &payload); err != nil {
return err
}
row := make(map[string]string)
row[cobrautil.ROW_USER] = fCmd.user
row[cobrautil.ROW_TASK_ID] = fCmd.taskID

if payload["Code"] != "0" {
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_FAILED
} else {
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
}

fCmd.Result = row
return nil
}

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

func (fCmd *FlattenCmd) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&fCmd.FinalCurveCmd)
}
50 changes: 50 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/update/volume/volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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: CurveCli
* Created Date: 2023-09-16
* Author: baytan0720
*/

package volume

import (
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/volume/flatten"
"github.com/spf13/cobra"
)

type VolumeCommand struct {
basecmd.MidCurveCmd
}

var _ basecmd.MidCurveCmdFunc = (*VolumeCommand)(nil) // check interface

func (volumeCmd *VolumeCommand) AddSubCommands() {
volumeCmd.Cmd.AddCommand(
flatten.NewCommand(),
)
}

func NewVolumeCommand() *cobra.Command {
volumeCmd := &VolumeCommand{
basecmd.MidCurveCmd{
Use: "volume",
Short: "update volume resources in the curvebs",
},
}
return basecmd.NewMidCurveCli(&volumeCmd.MidCurveCmd, volumeCmd)
}

0 comments on commit 7c4c6f1

Please sign in to comment.