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