|
| 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 | +/* |
| 18 | + * Project: CurveCli |
| 19 | + * Created Date: 2023-11-10 |
| 20 | + * Author: ZackSoul |
| 21 | + */ |
| 22 | +package snapshot |
| 23 | + |
| 24 | +import ( |
| 25 | + "time" |
| 26 | + |
| 27 | + cmderror "github.com/opencurve/curve/tools-v2/internal/error" |
| 28 | + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" |
| 29 | + snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot" |
| 30 | + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" |
| 31 | + listSnapshot "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/snapshot" |
| 32 | + stopSnapShot "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/stop/snapshot" |
| 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 delete snapshot --path /test111/test222 --user root --snapshotFailed=false` |
| 40 | +) |
| 41 | + |
| 42 | +type SnapShotCommand struct { |
| 43 | + basecmd.FinalCurveCmd |
| 44 | + snapshotAddrs []string |
| 45 | + timeout time.Duration |
| 46 | + |
| 47 | + user string |
| 48 | + file string |
| 49 | + uuid string |
| 50 | + failed bool |
| 51 | +} |
| 52 | + |
| 53 | +var _ basecmd.FinalCurveCmdFunc = (*SnapShotCommand)(nil) |
| 54 | + |
| 55 | +func NewSnapShotCommand() *cobra.Command { |
| 56 | + return NewDeleteSnapShotCommand().Cmd |
| 57 | +} |
| 58 | + |
| 59 | +func NewDeleteSnapShotCommand() *SnapShotCommand { |
| 60 | + snapShotCommand := &SnapShotCommand{ |
| 61 | + FinalCurveCmd: basecmd.FinalCurveCmd{ |
| 62 | + Use: "snapshot", |
| 63 | + Short: "delete snapshot in curvebs", |
| 64 | + Example: snapshotExample, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + basecmd.NewFinalCurveCli(&snapShotCommand.FinalCurveCmd, snapShotCommand) |
| 69 | + return snapShotCommand |
| 70 | +} |
| 71 | + |
| 72 | +func (sCmd *SnapShotCommand) AddFlags() { |
| 73 | + config.AddBsSnapshotCloneFlagOption(sCmd.Cmd) |
| 74 | + config.AddHttpTimeoutFlag(sCmd.Cmd) |
| 75 | + config.AddBsUserOptionFlag(sCmd.Cmd) |
| 76 | + config.AddBsSnapshotIDOptionFlag(sCmd.Cmd) |
| 77 | + config.AddBsPathOptionFlag(sCmd.Cmd) |
| 78 | + config.AddBsSnapshotFailedOptionFlag(sCmd.Cmd) |
| 79 | +} |
| 80 | + |
| 81 | +func (sCmd *SnapShotCommand) Init(cmd *cobra.Command, args []string) error { |
| 82 | + snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd) |
| 83 | + if err.TypeCode() != cmderror.CODE_SUCCESS { |
| 84 | + sCmd.Error = err |
| 85 | + return err.ToError() |
| 86 | + } |
| 87 | + sCmd.snapshotAddrs = snapshotAddrs |
| 88 | + sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT) |
| 89 | + sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER) |
| 90 | + sCmd.file = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_PATH) |
| 91 | + sCmd.uuid = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOT_ID) |
| 92 | + sCmd.failed = config.GetBsFlagBool(sCmd.Cmd, config.CURVEBS_SNAPSHOT_FAILED) |
| 93 | + header := []string{ |
| 94 | + cobrautil.ROW_SNAPSHOT_ID, |
| 95 | + cobrautil.ROW_SNAPSHOT_NAME, |
| 96 | + cobrautil.ROW_RESULT, |
| 97 | + cobrautil.ROW_REASON, |
| 98 | + } |
| 99 | + sCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice( |
| 100 | + sCmd.Header, []string{cobrautil.ROW_FILE}, |
| 101 | + )) |
| 102 | + sCmd.SetHeader(header) |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (sCmd *SnapShotCommand) Print(cmd *cobra.Command, args []string) error { |
| 107 | + return output.FinalCmdOutput(&sCmd.FinalCurveCmd, sCmd) |
| 108 | +} |
| 109 | + |
| 110 | +func (sCmd *SnapShotCommand) RunCommand(cmd *cobra.Command, args []string) error { |
| 111 | + params := map[string]any{ |
| 112 | + snapshotutil.QueryAction: snapshotutil.ActionGetFileSnapshotList, |
| 113 | + snapshotutil.QueryUser: sCmd.user, |
| 114 | + snapshotutil.QueryFile: sCmd.file, |
| 115 | + snapshotutil.QueryLimit: snapshotutil.Limit, |
| 116 | + snapshotutil.QueryOffset: snapshotutil.Offset, |
| 117 | + } |
| 118 | + if sCmd.failed { |
| 119 | + params[snapshotutil.QueryStatus] = snapshotutil.ErrSnaphshot |
| 120 | + } |
| 121 | + if sCmd.uuid != snapshotutil.DefaultSnapID { |
| 122 | + params[snapshotutil.QueryUUID] = sCmd.uuid |
| 123 | + } |
| 124 | + snapshotsList, err := listSnapshot.ListSnapShot(sCmd.snapshotAddrs, sCmd.timeout, params) |
| 125 | + if err != nil { |
| 126 | + sCmd.Error = err |
| 127 | + return sCmd.Error.ToError() |
| 128 | + } |
| 129 | + rows := make([]map[string]string, 0) |
| 130 | + if len(snapshotsList) == 0 { |
| 131 | + rows = append(rows, stopSnapShot.EmptyOutPut()) |
| 132 | + } else { |
| 133 | + for _, snapshot := range snapshotsList { |
| 134 | + row := make(map[string]string) |
| 135 | + err := DeleteSnapShot(sCmd.snapshotAddrs, sCmd.timeout, snapshot) |
| 136 | + row[cobrautil.ROW_SNAPSHOT_ID] = snapshot.UUID |
| 137 | + row[cobrautil.ROW_SNAPSHOT_NAME] = snapshot.Name |
| 138 | + if err.TypeCode() == cmderror.CODE_SUCCESS { |
| 139 | + row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS |
| 140 | + row[cobrautil.ROW_REASON] = "null" |
| 141 | + } else { |
| 142 | + row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_FAILED |
| 143 | + row[cobrautil.ROW_REASON] = err.ToError().Error() |
| 144 | + } |
| 145 | + rows = append(rows, row) |
| 146 | + } |
| 147 | + } |
| 148 | + list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{cobrautil.ROW_SNAPSHOT_NAME, cobrautil.ROW_SNAPSHOT_ID}) |
| 149 | + var emptyList [][]string = [][]string{} |
| 150 | + if len(snapshotsList) == 0 { |
| 151 | + sCmd.TableNew.AppendBulk(emptyList) |
| 152 | + sCmd.Result = rows |
| 153 | + sCmd.Error = cmderror.Success() |
| 154 | + return nil |
| 155 | + } |
| 156 | + sCmd.TableNew.AppendBulk(list) |
| 157 | + sCmd.Result = rows |
| 158 | + sCmd.Error = cmderror.Success() |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func (sCmd *SnapShotCommand) ResultPlainOutput() error { |
| 163 | + return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd) |
| 164 | +} |
| 165 | + |
| 166 | +func DeleteSnapShot(addrs []string, timeout time.Duration, snapshot *snapshotutil.SnapshotInfo) *cmderror.CmdError { |
| 167 | + params := map[string]any{ |
| 168 | + snapshotutil.QueryAction: snapshotutil.ActionDeleteSnapshot, |
| 169 | + snapshotutil.QueryUser: snapshot.User, |
| 170 | + snapshotutil.QueryUUID: snapshot.UUID, |
| 171 | + snapshotutil.QueryFile: snapshot.File, |
| 172 | + } |
| 173 | + subUri := snapshotutil.NewQuerySubUri(params) |
| 174 | + metric := basecmd.NewMetric(addrs, subUri, timeout) |
| 175 | + _, err := basecmd.QueryMetric(metric) |
| 176 | + return err |
| 177 | +} |
0 commit comments