Skip to content

Commit

Permalink
Merge pull request vmware#4 from rreubenur/issue103
Browse files Browse the repository at this point in the history
Create change_vm_vif.py
  • Loading branch information
michaelrice committed Dec 9, 2014
2 parents 0dbbb1d + 7503b9a commit e798adb
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions samples/change_vm_vif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
Written by Reubenur Rahman
Github: https://github.com/rreubenur/
This code is released under the terms of the Apache 2
http://www.apache.org/licenses/LICENSE-2.0.html
Example script to change the network of the Virtual Machine NIC
"""

import atexit

from tools import cli
from tools import tasks
from pyVim import connect
from pyVmomi import vim, vmodl


def get_obj(content, vimtype, name):
"""
Get the vsphere object associated with a given text name
"""
obj = None
container = content.viewManager.CreateContainerView(content.rootFolder,
vimtype, True)
for view in container.view:
if view.name == name:
obj = view
break
return obj


def get_args():
"""Get command line args from the user.
"""

parser = cli.build_arg_parser()

parser.add_argument('-v', '--vm_uuid',
required=False,
action='store',
help='Virtual machine uuid')

parser.add_argument('-n', '--network_name',
required=False,
action='store',
help='Name of the network/portgroup')

parser.add_argument('-d', '--is_VDS',
action="store_true",
default=False,
help='The provided network is in VSS or VDS')

args = parser.parse_args()

cli.prompt_for_password(args)
return args


def main():
"""
Simple command-line program for changing network virtual machines NIC.
"""

args = get_args()

try:
service_instance = connect.SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))

atexit.register(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
vm = content.searchIndex.FindByUuid(None, args.vm_uuid, True)
# This code is for changing only one Interface. For multiple Interface
# Iterate through a loop of network names.
device_change = []
for device in vm.config.hardware.device:
if isinstance(device, vim.vm.device.VirtualEthernetCard):
nicspec = vim.vm.device.VirtualDeviceSpec()
nicspec.operation = \
vim.vm.device.VirtualDeviceSpec.Operation.edit
nicspec.device = device
nicspec.device.wakeOnLanEnabled = True

if not args.is_VDS:
nicspec.device.backing = \
vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
nicspec.device.backing.network = \
get_obj(content, [vim.Network], args.network_name)
nicspec.device.backing.deviceName = args.network_name
else:
network = get_obj(content,
[vim.dvs.DistributedVirtualPortgroup],
args.network_name)
dvs_port_connection = vim.dvs.PortConnection()
dvs_port_connection.portgroupKey = network.key
dvs_port_connection.switchUuid = \
network.config.distributedVirtualSwitch.uuid
nicspec.device.backing = \
vim.vm.device.VirtualEthernetCard. \
DistributedVirtualPortBackingInfo()
nicspec.device.backing.port = dvs_port_connection

nicspec.device.connectable = \
vim.vm.device.VirtualDevice.ConnectInfo()
nicspec.device.connectable.startConnected = True
nicspec.device.connectable.allowGuestControl = True
device_change.append(nicspec)
break

config_spec = vim.vm.ConfigSpec(deviceChange=device_change)
task = vm.ReconfigVM_Task(config_spec)
tasks.wait_for_tasks(service_instance, [task])
print "Successfully changed network"

except vmodl.MethodFault as error:
print "Caught vmodl fault : " + error.msg
return -1

return 0

# Start program
if __name__ == "__main__":
main()

0 comments on commit e798adb

Please sign in to comment.