使用pysphere将新端口组添加到vmware虚拟交换机

时间:2014-07-09 18:14:44

标签: python vmware

我尝试使用pysphere自动将新端口组添加到ESXi主机。我使用以下代码片段:

from pysphere import MORTypes
from pysphere import VIServer, VIProperty
from pysphere.resources import VimService_services as VI 

s = VIServer()
s.connect(vcenter, user, password)

host_system = s.get_hosts().keys()[17]
prop = VIProperty(s, host_system)
propname = prop.configManager._obj.get_element_networkSystem()
vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0]
network_system = VIMor(propname, MORTypes.HostServiceSystem)

def add_port_group(name, vlan_id, vswitch, network_system):
    request = VI.AddPortGroupRequestMsg()
    _this = request.new__this(network_system)
    _this.set_attribute_type(network_system.get_attribute_type())
    request.set_element__this(_this)
    portgrp = request.new_portgrp()
    portgrp.set_element_name(name)
    portgrp.set_element_vlanId(vlan_id)
    portgrp.set_element_vswitchName(vswitch)
    portgrp.set_element_policy(portgrp.new_policy())
    request.set_element_portgrp(portgrp)
    s._proxy.AddPortGroup(request) 

但是,当我尝试运行它时,我收到以下错误:

>>> add_port_group(name, vlan_id, vswitch, network_system)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 12, in add_port_group
  File "/usr/lib/python2.6/site-packages/pysphere-0.1.8-    py2.6.egg/pysphere/resources/VimService_services.py", line 4344, in AddPortGroup
    response = self.binding.Receive(AddPortGroupResponseMsg.typecode)
  File "/usr/lib/python2.6/site-packages/pysphere-0.1.8-    py2.6.egg/pysphere/ZSI/client.py", line 545, in Receive
    return _Binding.Receive(self, replytype, **kw)
  File "/usr/lib/python2.6/site-packages/pysphere-0.1.8-    py2.6.egg/pysphere/ZSI/client.py", line 464, in Receive
    raise FaultException(msg)
pysphere.ZSI.FaultException: The object has already been deleted or has not been completely created

我试图为&#34; vswitch&#34;交换不同的值。和#34; network_system&#34;,但我没有取得任何成功。有没有人试图成功地与pysphere做类似的事情?

我可以通过Powershell完成我需要的东西,这表明它不是vmware问题,但我不想在这种特殊情况下使用Powershell。

1 个答案:

答案 0 :(得分:1)

我在我们的一个vSpheres上尝试了你的代码。 您似乎将对象传递给set_element_vswitchName而不是名称。也许这会有所帮助:

vswitch = prop.configManager.networkSystem.networkInfo.vswitch[0].name