|
| 1 | +/* |
| 2 | +* Copyright (c) 2023 NetEase Inc. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | + */ |
| 16 | +/* |
| 17 | +* Project: CurveCli |
| 18 | +* Created Date: 2023-09-23 |
| 19 | +* Author: baytan0720 |
| 20 | + */ |
| 21 | + |
| 22 | +package snapshot |
| 23 | + |
| 24 | +import ( |
| 25 | + "encoding/json" |
| 26 | + "fmt" |
| 27 | + "time" |
| 28 | + |
| 29 | + cmderror "github.com/opencurve/curve/tools-v2/internal/error" |
| 30 | + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" |
| 31 | + snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot" |
| 32 | + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" |
| 33 | + "github.com/opencurve/curve/tools-v2/pkg/config" |
| 34 | + "github.com/opencurve/curve/tools-v2/pkg/output" |
| 35 | + "github.com/spf13/cobra" |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + snapshotExample = `$ curve bs create volume snapshot --user root --filename test --snapshotname snap-test` |
| 40 | +) |
| 41 | + |
| 42 | +type SnapshotCmd struct { |
| 43 | + basecmd.FinalCurveCmd |
| 44 | + snapshotAddrs []string |
| 45 | + timeout time.Duration |
| 46 | + |
| 47 | + user string |
| 48 | + fileName string |
| 49 | + snapshotName string |
| 50 | +} |
| 51 | + |
| 52 | +var _ basecmd.FinalCurveCmdFunc = (*SnapshotCmd)(nil) |
| 53 | + |
| 54 | +func NewCommand() *cobra.Command { |
| 55 | + return NewSnapshotCmd().Cmd |
| 56 | +} |
| 57 | + |
| 58 | +func NewSnapshotCmd() *SnapshotCmd { |
| 59 | + sCmd := &SnapshotCmd{ |
| 60 | + FinalCurveCmd: basecmd.FinalCurveCmd{ |
| 61 | + Use: "snapshot", |
| 62 | + Short: "create volume snapshot in curvebs cluster", |
| 63 | + Example: snapshotExample, |
| 64 | + }, |
| 65 | + } |
| 66 | + basecmd.NewFinalCurveCli(&sCmd.FinalCurveCmd, sCmd) |
| 67 | + return sCmd |
| 68 | +} |
| 69 | + |
| 70 | +func (sCmd *SnapshotCmd) AddFlags() { |
| 71 | + config.AddBsSnapshotCloneFlagOption(sCmd.Cmd) |
| 72 | + config.AddHttpTimeoutFlag(sCmd.Cmd) |
| 73 | + config.AddBsUserRequiredFlag(sCmd.Cmd) |
| 74 | + config.AddBsFileNameRequiredFlag(sCmd.Cmd) |
| 75 | + config.AddBsSnapshotNameRequiredFlag(sCmd.Cmd) |
| 76 | +} |
| 77 | + |
| 78 | +func (sCmd *SnapshotCmd) Init(cmd *cobra.Command, args []string) error { |
| 79 | + snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd) |
| 80 | + if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 { |
| 81 | + return err.ToError() |
| 82 | + } |
| 83 | + sCmd.snapshotAddrs = snapshotAddrs |
| 84 | + sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT) |
| 85 | + sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER) |
| 86 | + sCmd.fileName = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_FILENAME) |
| 87 | + sCmd.snapshotName = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOTNAME) |
| 88 | + sCmd.SetHeader([]string{cobrautil.ROW_USER, cobrautil.ROW_FILE_NAME, cobrautil.ROW_SNAPSHOTNAME}) |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func (sCmd *SnapshotCmd) RunCommand(cmd *cobra.Command, args []string) error { |
| 93 | + params := map[string]any{ |
| 94 | + snapshotutil.QueryAction: snapshotutil.ActionCreateSnapshot, |
| 95 | + snapshotutil.QueryUser: sCmd.user, |
| 96 | + snapshotutil.QueryFile: sCmd.fileName, |
| 97 | + snapshotutil.QueryName: sCmd.snapshotName, |
| 98 | + } |
| 99 | + subUri := snapshotutil.NewQuerySubUri(params) |
| 100 | + metric := basecmd.NewMetric(sCmd.snapshotAddrs, subUri, sCmd.timeout) |
| 101 | + result, err := basecmd.QueryMetric(metric) |
| 102 | + if err.TypeCode() != cmderror.CODE_SUCCESS { |
| 103 | + return err.ToError() |
| 104 | + } |
| 105 | + resp := snapshotutil.Response{} |
| 106 | + if err := json.Unmarshal([]byte(result), &resp); err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + row := make(map[string]string) |
| 110 | + row[cobrautil.ROW_USER] = sCmd.user |
| 111 | + row[cobrautil.ROW_FILE_NAME] = sCmd.fileName |
| 112 | + row[cobrautil.ROW_SNAPSHOTNAME] = sCmd.snapshotName |
| 113 | + |
| 114 | + if resp.Code != snapshotutil.ResultSuccess { |
| 115 | + return fmt.Errorf("create snapshot fail, requestId: %s, code: %s, error: %s", resp.RequestId, resp.Code, resp.Message) |
| 116 | + } |
| 117 | + |
| 118 | + sCmd.TableNew.Append(cobrautil.Map2List(row, sCmd.Header)) |
| 119 | + sCmd.Result = row |
| 120 | + sCmd.Error = cmderror.Success() |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (sCmd *SnapshotCmd) Print(cmd *cobra.Command, args []string) error { |
| 125 | + return output.FinalCmdOutput(&sCmd.FinalCurveCmd, sCmd) |
| 126 | +} |
| 127 | + |
| 128 | +func (sCmd *SnapshotCmd) ResultPlainOutput() error { |
| 129 | + return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd) |
| 130 | +} |
0 commit comments