PyVmomi add NIC with unconnected dvs ('config.distributedVirtualSwitch' is Unset)
<p>I am using the code below in order to add a NIC configured with <code>DistributedVirtualSwitch</code> to an existing VM (via pyVmomi):</p>

<pre><code>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])
</code></pre>

<p>I'm getting the following <strong>exception</strong>:</p>

<blockquote>
  <p>switchUuid=network_objref.config.distributedVirtualSwitch.uuid
      AttributeError: 'NoneType' object has no attribute 'uuid'</p>
</blockquote>

<p>After examination of Vcenter Managed Objects(via <a href="https://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.wssdk.pg.doc_50%2FPG_ChB_Using_MOB.20.1.html" rel="nofollow noreferrer">mob</a>) it appears that
<strong>some of the <em><code>DistributedVirtualPortgroup</code></em> object references does have that (<em><code>VmwareDistributedVirtualSwitch</code></em>) property, while others have this property <em><code>Unset</code></em>.</strong></p>

<p><a href="https://i.stack.imgur.com/odkFn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/odkFn.png" alt="VmwareDistributedVirtualSwitch Set"></a>
<br><br></p>

<p><a href="https://i.stack.imgur.com/1AgmB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1AgmB.png" alt="VmwareDistributedVirtualSwitch Unset"></a></p>

<p>I've tried multiple ways to work around that, such as:</p>

<ul>
<li><p>Setting:
<code>switchUuid=None</code> which yielded:</p>

<blockquote>
  <p><code>TypeError: Required field "switchUuid" not provided (not @optional)</code></p>
</blockquote></li>
<li><p>Setting:
<code>dswitch_port_connection = None</code> which yielded:</p>

<blockquote>
  <p><code>TypeError: Required field "port" not provided (not @optional)</code></p>
</blockquote></li>
</ul>

<p><strong>Note:</strong> When I'm using <code>VMware WebClient</code> to configure the above it works perfectly.</p>

<p><strong>Question:</strong> how can I make adding a NIC like this work?</p>