我正在使用下面的代码将配置的 NIC 添加DistributedVirtualSwitch
到现有 VM(通过 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])
我收到以下异常:
switchUuid=network_objref.config.distributedVirtualSwitch.uuid AttributeError: 'NoneType' 对象没有属性 'uuid'
在检查 Vcenter Managed Objects(通过mob)后,似乎
某些DistributedVirtualPortgroup
对象引用确实具有该 ( VmwareDistributedVirtualSwitch
) 属性,而其他对象引用具有该属性Unset
。
我尝试了多种方法来解决这个问题,例如:
设置:
switchUuid=None
产生:TypeError: Required field "switchUuid" not provided (not @optional)
设置:
dswitch_port_connection = None
产生:TypeError: Required field "port" not provided (not @optional)
注意:当我VMware WebClient
用于配置上述内容时,它可以完美运行。
问题:如何使添加这样的 NIC 工作?