Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyVmomi add NIC with unconnected dvs ('config.distributedVirtualSwitch' is Unset)

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.

VmwareDistributedVirtualSwitch Set

VmwareDistributedVirtualSwitch 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?

like image 407
Vano Avatar asked Feb 15 '16 11:02

Vano


2 Answers

Eventually after severe experimentation, Setting:

dswitch_port_connection = '<Unset>'

Solved the issue and resulted in adding NIC with configured but unconnected DVS.

like image 149
Vano Avatar answered Nov 15 '22 23:11

Vano


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

like image 2
Luke Exton Avatar answered Nov 16 '22 01:11

Luke Exton