github.com/vmware/govmomi@v0.43.0/govc/vm/dataset/update.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 dataset 18 19 import ( 20 "context" 21 "errors" 22 "flag" 23 24 "github.com/vmware/govmomi/govc/cli" 25 "github.com/vmware/govmomi/govc/flags" 26 "github.com/vmware/govmomi/vapi/vm/dataset" 27 ) 28 29 type update struct { 30 *flags.VirtualMachineFlag 31 32 description string 33 host dataset.Access 34 guest dataset.Access 35 omitFromSnapshotAndClone *bool 36 } 37 38 func init() { 39 cli.Register("vm.dataset.update", &update{}) 40 } 41 42 func FindDataSetId(ctx context.Context, mgr *dataset.Manager, vmId string, nameOrId string) (string, error) { 43 l, err := mgr.ListDataSets(ctx, vmId) 44 if err != nil { 45 return "", err 46 } 47 for _, summary := range l { 48 if nameOrId == summary.DataSet || nameOrId == summary.Name { 49 return summary.DataSet, nil 50 } 51 } 52 return nameOrId, nil 53 } 54 55 func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) { 56 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 57 cmd.VirtualMachineFlag.Register(ctx, f) 58 f.StringVar(&cmd.description, "d", "", "Description") 59 f.StringVar((*string)(&cmd.host), "host-access", "", hostAccessUsage()) 60 f.StringVar((*string)(&cmd.guest), "guest-access", "", guestAccessUsage()) 61 f.Var(flags.NewOptionalBool(&cmd.omitFromSnapshotAndClone), "omit-from-snapshot", "Omit the data set from snapshots and clones of the VM") 62 } 63 64 func (cmd *update) Process(ctx context.Context) error { 65 return cmd.VirtualMachineFlag.Process(ctx) 66 } 67 68 func (cmd *update) Usage() string { 69 return "NAME" 70 } 71 72 func (cmd *update) Description() string { 73 return `Update data set. 74 75 Examples: 76 govc vm.dataset.update -vm $vm -d "New description." -guest-access READ_ONLY com.example.project2 77 govc vm.dataset.update -vm $vm -omit-from-snapshot=false com.example.project3` 78 } 79 80 func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error { 81 if f.NArg() != 1 { 82 return flag.ErrHelp 83 } 84 85 vm, err := cmd.VirtualMachineFlag.VirtualMachine() 86 if err != nil { 87 return err 88 } 89 if vm == nil { 90 return flag.ErrHelp 91 } 92 vmId := vm.Reference().Value 93 94 if cmd.host != "" && !validateDataSetAccess(cmd.host) { 95 return errors.New("please specify valid host access") 96 } 97 if cmd.guest != "" && !validateDataSetAccess(cmd.guest) { 98 return errors.New("please specify valid guest access") 99 } 100 101 c, err := cmd.RestClient() 102 if err != nil { 103 return err 104 } 105 mgr := dataset.NewManager(c) 106 107 id, err := FindDataSetId(ctx, mgr, vmId, f.Arg(0)) 108 if err != nil { 109 return err 110 } 111 112 // Update only the fields which the user asked for 113 updateSpec := dataset.UpdateSpec{} 114 if cmd.description != "" { 115 updateSpec.Description = &cmd.description 116 } 117 if cmd.host != "" { 118 updateSpec.Host = &cmd.host 119 } 120 if cmd.guest != "" { 121 updateSpec.Guest = &cmd.guest 122 } 123 if cmd.omitFromSnapshotAndClone != nil { 124 updateSpec.OmitFromSnapshotAndClone = cmd.omitFromSnapshotAndClone 125 } 126 127 err = mgr.UpdateDataSet(ctx, vmId, id, &updateSpec) 128 if err != nil { 129 return err 130 } 131 132 return nil 133 }