github.com/vmware/govmomi@v0.51.0/cli/volume/snapshot/rm.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package snapshot 6 7 import ( 8 "context" 9 "errors" 10 "flag" 11 "fmt" 12 "io" 13 "text/tabwriter" 14 15 "github.com/vmware/govmomi/cli" 16 "github.com/vmware/govmomi/cli/flags" 17 "github.com/vmware/govmomi/cns" 18 "github.com/vmware/govmomi/cns/types" 19 ) 20 21 type rm struct { 22 *flags.ClientFlag 23 *flags.OutputFlag 24 } 25 26 func init() { 27 cli.Register("volume.snapshot.rm", &rm{}) 28 } 29 30 func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) { 31 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 32 cmd.ClientFlag.Register(ctx, f) 33 34 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 35 cmd.OutputFlag.Register(ctx, f) 36 } 37 38 func (cmd *rm) Process(ctx context.Context) error { 39 if err := cmd.ClientFlag.Process(ctx); err != nil { 40 return err 41 } 42 return cmd.OutputFlag.Process(ctx) 43 } 44 45 func (cmd *rm) Usage() string { 46 return "[SNAP_ID VOL_ID]..." 47 } 48 49 func (cmd *rm) Description() string { 50 return `Remove snapshot SNAP_ID from volume VOL_ID. 51 52 Use a list of [SNAP_ID VOL_ID] pairs to remove multiple snapshots at once. 53 54 Examples: 55 govc volume.snapshot.rm f75989dc-95b9-4db7-af96-8583f24bc59d df86393b-5ae0-4fca-87d0-b692dbc67d45 56 govc volume.snapshot.rm $(govc volume.snapshot.ls -i df86393b-5ae0-4fca-87d0-b692dbc67d45) 57 govc volume.snapshot.rm $(govc volume.snapshot.create -i df86393b-5ae0-4fca-87d0-b692dbc67d45 my-snapshot) 58 govc volume.snapshot.rm $(govc volume.snapshot.ls -i $(govc volume.ls -i))` 59 } 60 61 type rmResult struct { 62 VolumeResults []*types.CnsSnapshotDeleteResult `json:"volumeResults"` 63 cmd *rm 64 } 65 66 func (r *rmResult) Write(w io.Writer) error { 67 var err error = nil 68 tw := tabwriter.NewWriter(r.cmd.Out, 2, 0, 2, ' ', 0) 69 for _, s := range r.VolumeResults { 70 fmt.Fprintf(tw, "%s\t%s", s.SnapshotId.Id, s.VolumeId.Id) 71 if s.Fault != nil { 72 if err == nil { 73 err = errors.New(s.Fault.LocalizedMessage) 74 } 75 fmt.Fprintf(tw, "\t%s", s.Fault.LocalizedMessage) 76 } 77 fmt.Fprintln(tw) 78 } 79 tw.Flush() 80 return err 81 } 82 83 func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error { 84 if len(f.Args()) < 2 || len(f.Args())%2 != 0 { 85 return flag.ErrHelp 86 } 87 88 c, err := cmd.CnsClient() 89 if err != nil { 90 return err 91 } 92 93 result := rmResult{cmd: cmd} 94 95 for i := 0; i < len(f.Args()); i += 2 { 96 spec := types.CnsSnapshotDeleteSpec{ 97 VolumeId: types.CnsVolumeId{ 98 Id: f.Arg(i + 1), 99 }, 100 SnapshotId: types.CnsSnapshotId{ 101 Id: f.Arg(i), 102 }, 103 } 104 105 task, err := c.DeleteSnapshots(ctx, []types.CnsSnapshotDeleteSpec{spec}) 106 if err != nil { 107 return err 108 } 109 110 info, err := cns.GetTaskInfo(ctx, task) 111 if err != nil { 112 return err 113 } 114 115 res, err := cns.GetTaskResult(ctx, info) 116 if err != nil { 117 return err 118 } 119 120 sdr := res.(*types.CnsSnapshotDeleteResult) 121 if sdr.Fault != nil { 122 if len(f.Args()) == 2 { 123 return errors.New(sdr.Fault.LocalizedMessage) 124 } 125 } 126 127 result.VolumeResults = append(result.VolumeResults, sdr) 128 } 129 130 return cmd.WriteResult(&result) 131 }