github.com/vmware/govmomi@v0.51.0/cli/volume/snapshot/create.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 "time" 15 16 "github.com/vmware/govmomi/cli" 17 "github.com/vmware/govmomi/cli/flags" 18 "github.com/vmware/govmomi/cns" 19 "github.com/vmware/govmomi/cns/types" 20 ) 21 22 type create struct { 23 *flags.ClientFlag 24 *flags.OutputFlag 25 26 id bool 27 } 28 29 func init() { 30 cli.Register("volume.snapshot.create", &create{}) 31 } 32 33 func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) { 34 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 35 cmd.ClientFlag.Register(ctx, f) 36 37 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 38 cmd.OutputFlag.Register(ctx, f) 39 40 f.BoolVar(&cmd.id, "i", false, "Output snapshot ID and volume ID only") 41 } 42 43 func (cmd *create) Process(ctx context.Context) error { 44 if err := cmd.ClientFlag.Process(ctx); err != nil { 45 return err 46 } 47 return cmd.OutputFlag.Process(ctx) 48 } 49 50 func (cmd *create) Usage() string { 51 return "[ID] [DESC]" 52 } 53 54 func (cmd *create) Description() string { 55 return `Create snapshot of volume ID with description DESC. 56 57 Examples: 58 govc volume.snapshot.create df86393b-5ae0-4fca-87d0-b692dbc67d45 my-snapshot 59 govc volume.snapshot.create -i df86393b-5ae0-4fca-87d0-b692dbc67d45 my-snapshot` 60 } 61 62 type createResult struct { 63 VolumeResults *types.CnsSnapshotCreateResult `json:"volumeResults"` 64 cmd *create 65 } 66 67 func (r *createResult) Write(w io.Writer) error { 68 tw := tabwriter.NewWriter(r.cmd.Out, 2, 0, 2, ' ', 0) 69 if r.cmd.id { 70 fmt.Fprintf(tw, "%s\t%s", r.VolumeResults.Snapshot.SnapshotId.Id, 71 r.VolumeResults.Snapshot.VolumeId.Id) 72 } else { 73 fmt.Fprintf(tw, "%s\t%s\t%s\t%s", r.VolumeResults.Snapshot.SnapshotId.Id, 74 r.VolumeResults.Snapshot.Description, r.VolumeResults.Snapshot.VolumeId.Id, 75 r.VolumeResults.Snapshot.CreateTime.Format(time.Stamp)) 76 } 77 fmt.Fprintln(tw) 78 return tw.Flush() 79 } 80 81 func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error { 82 if len(f.Args()) != 2 { 83 return flag.ErrHelp 84 } 85 86 c, err := cmd.CnsClient() 87 if err != nil { 88 return err 89 } 90 91 spec := types.CnsSnapshotCreateSpec{ 92 VolumeId: types.CnsVolumeId{ 93 Id: f.Arg(0), 94 }, 95 Description: f.Arg(1), 96 } 97 98 task, err := c.CreateSnapshots(ctx, []types.CnsSnapshotCreateSpec{spec}) 99 if err != nil { 100 return err 101 } 102 103 info, err := cns.GetTaskInfo(ctx, task) 104 if err != nil { 105 return err 106 } 107 108 res, err := cns.GetTaskResult(ctx, info) 109 if err != nil { 110 return err 111 } 112 113 scr := res.(*types.CnsSnapshotCreateResult) 114 if scr.CnsSnapshotOperationResult.Fault != nil { 115 return errors.New(scr.CnsSnapshotOperationResult.Fault.LocalizedMessage) 116 } 117 118 return cmd.WriteResult(&createResult{VolumeResults: scr, cmd: cmd}) 119 }