github.com/vmware/govmomi@v0.43.0/govc/vm/network/add.go (about) 1 /* 2 Copyright (c) 2014-2015 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 network 18 19 import ( 20 "context" 21 "errors" 22 "flag" 23 "fmt" 24 25 "github.com/vmware/govmomi/govc/cli" 26 "github.com/vmware/govmomi/govc/flags" 27 ) 28 29 type add struct { 30 *flags.VirtualMachineFlag 31 *flags.NetworkFlag 32 } 33 34 func init() { 35 cli.Register("vm.network.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 cmd.NetworkFlag, ctx = flags.NewNetworkFlag(ctx) 42 cmd.NetworkFlag.Register(ctx, f) 43 } 44 45 func (cmd *add) Description() string { 46 return `Add network adapter to VM. 47 48 Examples: 49 govc vm.network.add -vm $vm -net "VM Network" -net.adapter e1000e 50 govc vm.network.add -vm $vm -net SwitchName/PortgroupName 51 govc device.info -vm $vm ethernet-*` 52 } 53 54 func (cmd *add) Process(ctx context.Context) error { 55 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 56 return err 57 } 58 if err := cmd.NetworkFlag.Process(ctx); err != nil { 59 return err 60 } 61 return nil 62 } 63 64 func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error { 65 vm, err := cmd.VirtualMachineFlag.VirtualMachine() 66 if err != nil { 67 return err 68 } 69 70 if vm == nil { 71 return errors.New("please specify a vm") 72 } 73 74 // Set network if specified as extra argument. 75 if f.NArg() > 0 { 76 err = cmd.NetworkFlag.Set(f.Arg(0)) 77 if err != nil { 78 return fmt.Errorf("couldn't set specified network %v", 79 err) 80 } 81 } 82 83 net, err := cmd.NetworkFlag.Device() 84 if err != nil { 85 return err 86 } 87 88 return vm.AddDevice(ctx, net) 89 }