github.com/vmware/govmomi@v0.37.1/govc/device/cdrom/add.go (about) 1 /* 2 Copyright (c) 2014-2016 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 cdrom 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 24 "github.com/vmware/govmomi/govc/cli" 25 "github.com/vmware/govmomi/govc/flags" 26 ) 27 28 type add struct { 29 *flags.VirtualMachineFlag 30 31 controller string 32 } 33 34 func init() { 35 cli.Register("device.cdrom.add", &add{}) 36 } 37 38 func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) { 39 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 40 cmd.VirtualMachineFlag.Register(ctx, f) 41 42 f.StringVar(&cmd.controller, "controller", "", "IDE controller name") 43 } 44 45 func (cmd *add) Description() string { 46 return `Add CD-ROM device to VM. 47 48 Examples: 49 govc device.cdrom.add -vm $vm 50 govc device.ls -vm $vm | grep ide- 51 govc device.cdrom.add -vm $vm -controller ide-200 52 govc device.info cdrom-*` 53 } 54 55 func (cmd *add) Process(ctx context.Context) error { 56 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 57 return err 58 } 59 return nil 60 } 61 62 func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error { 63 vm, err := cmd.VirtualMachine() 64 if err != nil { 65 return err 66 } 67 68 if vm == nil { 69 return flag.ErrHelp 70 } 71 72 devices, err := vm.Device(ctx) 73 if err != nil { 74 return err 75 } 76 77 c, err := devices.FindIDEController(cmd.controller) 78 if err != nil { 79 return err 80 } 81 82 d, err := devices.CreateCdrom(c) 83 if err != nil { 84 return err 85 } 86 87 err = vm.AddDevice(ctx, d) 88 if err != nil { 89 return err 90 } 91 92 // output name of device we just created 93 devices, err = vm.Device(ctx) 94 if err != nil { 95 return err 96 } 97 98 devices = devices.SelectByType(d) 99 100 name := devices.Name(devices[len(devices)-1]) 101 102 fmt.Println(name) 103 104 return nil 105 }