I am using the code below in order to add a NIC configured with DistributedVirtualSwitch
to an existing VM (via pyVmomi):
def __AddNIC(si, vmconf_dict, network_name):
vm = __get_vm(si, vmconf_dict)
print " Network label : " + network_name
devices = []
nicspec = vim.vm.device.VirtualDeviceSpec()
nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
nicspec.device = vim.vm.device.VirtualVmxnet3()
nicspec.device.wakeOnLanEnabled = True
nicspec.device.deviceInfo = vim.Description()
nicspec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nicspec.device.connectable.startConnected = True
nicspec.device.connectable.allowGuestControl = True
network_objref = _get_mor_by_property(si, vim.dvs.DistributedVirtualPortgroup, network_name)
dswitch_port_connection = vim.dvs.PortConnection(
portgroupKey=network_objref.key,
switchUuid=network_objref.config.distributedVirtualSwitch.uuid
)
nicspec.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
nicspec.device.backing.port = dswitch_port_connection
devices.append(nicspec)
vmconf = vim.vm.ConfigSpec(deviceChange=devices)
task = vm.ReconfigVM_Task(vmconf)
tasks.wait_for_tasks(si, [task])
I'm getting the following exception:
switchUuid=network_objref.config.distributedVirtualSwitch.uuid AttributeError: 'NoneType' object has no attribute 'uuid'
After examination of Vcenter Managed Objects(via mob) it appears that
some of the DistributedVirtualPortgroup
object references does have that (VmwareDistributedVirtualSwitch
) property, while others have this property Unset
.
I've tried multiple ways to work around that, such as:
Setting:
switchUuid=None
which yielded:
TypeError: Required field "switchUuid" not provided (not @optional)
Setting:
dswitch_port_connection = None
which yielded:
TypeError: Required field "port" not provided (not @optional)
Note: When I'm using VMware WebClient
to configure the above it works perfectly.
Question: how can I make adding a NIC like this work?
Eventually after severe experimentation, Setting:
dswitch_port_connection = '<Unset>'
Solved the issue and resulted in adding NIC with configured but unconnected DVS.
Check your user permissions. Do you have System.Read privileges on the portgroup?
Distributed virtual switch that the portgroup is defined on. This property should always be set unless the user's setting does not have System.Read privilege on the object referred to by this property.
https://github.com/vmware/pyvmomi/blob/master/docs/vim/DistributedVirtualSwitch.rst
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With