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