Skip to content
This repository was archived by the owner on Mar 26, 2020. It is now read-only.

Commit 030846b

Browse files
committed
snapshot/delete: Implement snapshot delete all for a label
With this PR, snapshot delete all can be performed on a volume name, label or both volume name and lable name. If both volume name and lable name has not given, then it will delete all snapshots in the cluster Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
1 parent 43159f5 commit 030846b

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

glustercli/cmd/snapshot-delete.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/gluster/glusterd2/pkg/api"
7-
86
log "github.com/sirupsen/logrus"
97
"github.com/spf13/cobra"
108
)
@@ -27,18 +25,22 @@ var (
2725
)
2826

2927
var (
30-
snapshotDeleteAllCmd = &cobra.Command{
31-
Use: "all [volname]",
28+
flagSnapshotDeleteAllVolume string
29+
flagSnapshotDeleteAllLabel string
30+
snapshotDeleteAllCmd = &cobra.Command{
31+
Use: "all",
3232
Short: snapshotDeleteAllHelpShort,
3333
Long: snapshotDeleteAllHelpLong,
34-
Args: cobra.MaximumNArgs(1),
34+
Args: cobra.MaximumNArgs(0),
3535
Run: snapshotDeleteAllCmdRun,
3636
}
3737
)
3838

3939
func init() {
4040
snapshotCmd.AddCommand(snapshotDeleteCmd)
4141
snapshotDeleteCmd.AddCommand(snapshotDeleteAllCmd)
42+
snapshotDeleteAllCmd.Flags().StringVar(&flagSnapshotDeleteAllVolume, "volume", "", "Deletes all snapshots of a given volume")
43+
snapshotDeleteAllCmd.Flags().StringVar(&flagSnapshotDeleteAllLabel, "label", "", "Deletes all snapshots attached to the label")
4244
}
4345

4446
func snapshotDelete(snapname string) error {
@@ -60,21 +62,40 @@ func snapshotDeleteCmdRun(cmd *cobra.Command, args []string) {
6062
}
6163
}
6264

65+
func deleteAllSnapshots(snapList []string) {
66+
for _, snap := range snapList {
67+
if err := snapshotDelete(snap); err != nil {
68+
fmt.Printf("Failed to delete snapshot %s \n", snap)
69+
}
70+
}
71+
}
72+
6373
func snapshotDeleteAllCmdRun(cmd *cobra.Command, args []string) {
64-
var snaps api.SnapListResp
6574
var err error
66-
volname := ""
67-
if len(args) > 0 {
68-
volname = args[0]
75+
volname := flagSnapshotDeleteAllVolume
76+
labelname := flagSnapshotDeleteAllLabel
77+
78+
if labelname != "" {
79+
info, err := client.LabelInfo(labelname)
80+
if err != nil {
81+
failure("Failed to get all snapshots", err, 1)
82+
}
83+
fmt.Printf("Deleting All snapshots of label %s \n", labelname)
84+
deleteAllSnapshots(info.SnapList)
85+
if volname != "" {
86+
//If volume falg has not given then we are done with
87+
//delete all.
88+
return
89+
}
6990
}
7091

71-
snaps, err = client.SnapshotList(volname)
92+
snaps, err := client.SnapshotList(volname)
7293
if err != nil {
7394
failure("Failed to get all snapshots", err, 1)
7495
}
7596

7697
if len(snaps) == 0 {
77-
fmt.Printf("There are no snapshots to delete \n")
98+
fmt.Printf("There are no more snapshots to delete \n")
7899
return
79100
}
80101

0 commit comments

Comments
 (0)