-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: lng2020 <nanguanlin6@gmail.com>
- Loading branch information
Showing
7 changed files
with
437 additions
and
8 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
105 changes: 105 additions & 0 deletions
105
tools-v2/pkg/cli/command/curvebs/create/volume/clone.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,105 @@ | ||
/* | ||
* 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-08-23 | ||
* Author: lng2020 | ||
*/ | ||
|
||
package volume | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
cmderror "github.com/opencurve/curve/tools-v2/internal/error" | ||
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" | ||
) | ||
|
||
const ( | ||
cloneExample = `$ curve bs create volume clone` | ||
) | ||
|
||
type CloneCmd struct { | ||
basecmd.FinalCurveCmd | ||
snapshotAddrs []string | ||
timeout time.Duration | ||
|
||
user string | ||
src string | ||
dest string | ||
lazy bool | ||
} | ||
|
||
var _ basecmd.FinalCurveCmdFunc = (*CloneCmd)(nil) | ||
|
||
func (cCmd *CloneCmd) Init(cmd *cobra.Command, args []string) error { | ||
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(cCmd.Cmd) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 { | ||
return err.ToError() | ||
} | ||
cCmd.snapshotAddrs = snapshotAddrs | ||
cCmd.timeout = config.GetFlagDuration(cCmd.Cmd, config.HTTPTIMEOUT) | ||
cCmd.user = config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_USER) | ||
cCmd.src = config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_SRC) | ||
cCmd.dest = config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_DEST) | ||
cCmd.lazy = config.GetBsFlagBool(cCmd.Cmd, config.CURVEBS_LAZY) | ||
return nil | ||
} | ||
|
||
func (cCmd *CloneCmd) RunCommand(cmd *cobra.Command, args []string) error { | ||
c := newCloneOrRecover(cCmd.snapshotAddrs, cCmd.timeout, cCmd.user, cCmd.src, cCmd.dest, cCmd.lazy) | ||
if err := c.CreateClone(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cCmd *CloneCmd) Print(cmd *cobra.Command, args []string) error { | ||
return output.FinalCmdOutput(&cCmd.FinalCurveCmd, cCmd) | ||
} | ||
|
||
func (cCmd *CloneCmd) ResultPlainOutput() error { | ||
return output.FinalCmdOutputPlain(&cCmd.FinalCurveCmd) | ||
} | ||
|
||
func (cCmd *CloneCmd) AddFlags() { | ||
config.AddBsSnapshotCloneFlagOption(cCmd.Cmd) | ||
config.AddHttpTimeoutFlag(cCmd.Cmd) | ||
config.AddBsUserOptionFlag(cCmd.Cmd) | ||
config.AddBsSrcOptionFlag(cCmd.Cmd) | ||
config.AddBsDestOptionFlag(cCmd.Cmd) | ||
config.AddBsLazyOptionFlag(cCmd.Cmd) | ||
} | ||
|
||
func NewCreateVolumeCloneCommand() *CloneCmd { | ||
cCmd := &CloneCmd{ | ||
FinalCurveCmd: basecmd.FinalCurveCmd{ | ||
Use: "clone", | ||
Short: "create volume clone tasks in curvebs cluster", | ||
Example: cloneExample, | ||
}, | ||
} | ||
basecmd.NewFinalCurveCli(&cCmd.FinalCurveCmd, cCmd) | ||
return cCmd | ||
} | ||
|
||
func NewCloneCommand() *cobra.Command { | ||
return NewCreateVolumeCloneCommand().Cmd | ||
} |
148 changes: 148 additions & 0 deletions
148
tools-v2/pkg/cli/command/curvebs/create/volume/clone_or_recover.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,148 @@ | ||
/* | ||
* 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-08-23 | ||
* Author: lng2020 | ||
*/ | ||
|
||
package volume | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
"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" | ||
) | ||
|
||
type CloneOrRecover struct { | ||
serverAddress []string | ||
timeout time.Duration | ||
|
||
User string | ||
Src string | ||
Dest string | ||
Lazy bool | ||
} | ||
|
||
func newCloneOrRecover(serverAddress []string, timeout time.Duration, user, src, dest string, lazy bool) *CloneOrRecover { | ||
return &CloneOrRecover{ | ||
serverAddress: serverAddress, | ||
timeout: timeout, | ||
User: user, | ||
Src: src, | ||
Dest: dest, | ||
Lazy: lazy, | ||
} | ||
} | ||
|
||
type QueryParams struct { | ||
Action string `json:"Action"` | ||
Version string `json:"Version"` | ||
User string `json:"User"` | ||
Source string `json:"Source"` | ||
Destination string `json:"Destination"` | ||
Lazy string `json:"Lazy"` | ||
} | ||
|
||
func (c *CloneOrRecover) CreateRecover() error { | ||
params := QueryParams{ | ||
Action: "Recover", | ||
Version: version, | ||
User: c.User, | ||
Source: c.Src, | ||
Destination: c.Dest, | ||
Lazy: strconv.FormatBool(c.Lazy), | ||
} | ||
|
||
var resp struct { | ||
Code string | ||
} | ||
err := c.query(params, &resp) | ||
if err != nil || resp.Code != "0" { | ||
return fmt.Errorf("create recover failed, error=%s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *CloneOrRecover) CreateClone() error { | ||
params := QueryParams{ | ||
Action: "Clone", | ||
Version: version, | ||
User: c.User, | ||
Source: c.Src, | ||
Destination: c.Dest, | ||
Lazy: strconv.FormatBool(c.Lazy), | ||
} | ||
|
||
var resp struct { | ||
Code string | ||
} | ||
err := c.query(params, &resp) | ||
if err != nil || resp.Code != "0" { | ||
return fmt.Errorf("create clone failed, error=%s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *CloneOrRecover) query(params QueryParams, data interface{}) error { | ||
encodedParams := c.encodeParam(params) | ||
|
||
subUri := fmt.Sprintf("/SnapshotCloneService?%s", encodedParams) | ||
|
||
metric := basecmd.NewMetric(c.serverAddress, subUri, c.timeout) | ||
|
||
result, err := basecmd.QueryMetric(metric) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS { | ||
return err.ToError() | ||
} | ||
|
||
if err := json.Unmarshal([]byte(result), &data); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *CloneOrRecover) encodeParam(params QueryParams) string { | ||
values := url.Values{} | ||
paramsMap := map[string]string{ | ||
"Action": params.Action, | ||
"Version": params.Version, | ||
"User": params.User, | ||
"Source": params.Source, | ||
"Destination": params.Destination, | ||
"Lazy": params.Lazy, | ||
} | ||
|
||
for key, value := range paramsMap { | ||
if value != "" { | ||
values.Add(key, value) | ||
} | ||
} | ||
|
||
return values.Encode() | ||
} |
105 changes: 105 additions & 0 deletions
105
tools-v2/pkg/cli/command/curvebs/create/volume/recover.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,105 @@ | ||
/* | ||
* 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-08-23 | ||
* Author: lng2020 | ||
*/ | ||
|
||
package volume | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
cmderror "github.com/opencurve/curve/tools-v2/internal/error" | ||
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" | ||
) | ||
|
||
const ( | ||
recoverExample = `$ curve bs create volume recover` | ||
) | ||
|
||
type RecoverCmd struct { | ||
basecmd.FinalCurveCmd | ||
snapshotAddrs []string | ||
timeout time.Duration | ||
|
||
user string | ||
src string | ||
dest string | ||
lazy bool | ||
} | ||
|
||
var _ basecmd.FinalCurveCmdFunc = (*RecoverCmd)(nil) | ||
|
||
func (rCmd *RecoverCmd) Init(cmd *cobra.Command, args []string) error { | ||
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(rCmd.Cmd) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 { | ||
return err.ToError() | ||
} | ||
rCmd.snapshotAddrs = snapshotAddrs | ||
rCmd.timeout = config.GetFlagDuration(rCmd.Cmd, config.HTTPTIMEOUT) | ||
rCmd.user = config.GetBsFlagString(rCmd.Cmd, config.CURVEBS_USER) | ||
rCmd.src = config.GetBsFlagString(rCmd.Cmd, config.CURVEBS_SRC) | ||
rCmd.dest = config.GetBsFlagString(rCmd.Cmd, config.CURVEBS_DEST) | ||
rCmd.lazy = config.GetBsFlagBool(rCmd.Cmd, config.CURVEBS_LAZY) | ||
return nil | ||
} | ||
|
||
func (rCmd *RecoverCmd) RunCommand(cmd *cobra.Command, args []string) error { | ||
c := newCloneOrRecover(rCmd.snapshotAddrs, rCmd.timeout, rCmd.user, rCmd.src, rCmd.dest, rCmd.lazy) | ||
if err := c.CreateRecover(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (rCmd *RecoverCmd) Print(cmd *cobra.Command, args []string) error { | ||
return output.FinalCmdOutput(&rCmd.FinalCurveCmd, rCmd) | ||
} | ||
|
||
func (rCmd *RecoverCmd) ResultPlainOutput() error { | ||
return output.FinalCmdOutputPlain(&rCmd.FinalCurveCmd) | ||
} | ||
|
||
func (rCmd *RecoverCmd) AddFlags() { | ||
config.AddBsSnapshotCloneFlagOption(rCmd.Cmd) | ||
config.AddHttpTimeoutFlag(rCmd.Cmd) | ||
config.AddBsUserOptionFlag(rCmd.Cmd) | ||
config.AddBsSrcOptionFlag(rCmd.Cmd) | ||
config.AddBsDestOptionFlag(rCmd.Cmd) | ||
config.AddBsLazyOptionFlag(rCmd.Cmd) | ||
} | ||
|
||
func NewCreateVolumeRecoverCommand() *RecoverCmd { | ||
rCmd := &RecoverCmd{ | ||
FinalCurveCmd: basecmd.FinalCurveCmd{ | ||
Use: "recover", | ||
Short: "create volume recover tasks in curvebs cluster", | ||
Example: recoverExample, | ||
}, | ||
} | ||
basecmd.NewFinalCurveCli(&rCmd.FinalCurveCmd, rCmd) | ||
return rCmd | ||
} | ||
|
||
func NewRecoverCommand() *cobra.Command { | ||
return NewCreateVolumeRecoverCommand().Cmd | ||
} |
Oops, something went wrong.