Ok, after much head scratching and looking at the pysphere sources to better understand the odd vmware api I've finally figured out how to do this. I'm posting how I did this below. There may be a more elegant way but this works for me.
Basically, to create a VM with a nic on a distributed switch you need two pieces of info. The distributed virtual switches uuid and the portgroups key. The script below allows you to just give the portgroups name and it will find the portgroup key and the switches uuid. You then specify those when adding the nic to the VM.
#!/usr/bin/env python
import sys
from pysphere import VIServer, VIProperty, MORTypes
from pysphere.resources import VimService_services as VI
from pysphere.vi_task import VITask
# CONNECTION PARAMTERS
server = "
vcr40.mgt.host.com"
user = 'username'
password = 'password'
datacentername = "Dev_Datacenter"
hostname = "
esx14.mgt.hawaii.edu"
network_name = "dv_172_16_1_0_24"
# CONNECT TO THE SERVER
s = VIServer()
s.connect(server, user, password)
# GET INITIAL PROPERTIES AND OBJECTS
dcmor = [k for k,v in s.get_datacenters().items() if v==datacentername][0]
dcprops = VIProperty(s, dcmor)
# networkFolder managed object reference
nfmor = dcprops.networkFolder._obj
dvpg_mors = s._retrieve_properties_traversal(property_names=['name','key'],
from_node=nfmor, obj_type='DistributedVirtualPortgroup')
# Get the portgroup managed object.
dvpg_mor = None
for dvpg in dvpg_mors:
if dvpg_mor:
break
for p in dvpg.PropSet:
if p.Name == "name" and p.Val == network_name:
dvpg_mor = dvpg
if dvpg_mor:
break
if dvpg_mor == None:
print "Didnt find the dvpg %s, exiting now" % (network_name)
sys.exit()
# Get the portgroup key
portgroupKey = None
for p in dvpg_mor.PropSet:
if p.Name == "key":
portgroupKey = p.Val
# Grab the dvswitch uuid and portgroup properties
dvswitch_mors = s._retrieve_properties_traversal(property_names=['uuid','portgroup'],
from_node=nfmor, obj_type='DistributedVirtualSwitch')
dvswitch_mor = None
# Get the appropriate dvswitches managed object
for dvswitch in dvswitch_mors:
if dvswitch_mor:
break
for p in dvswitch.PropSet:
if p.Name == "portgroup":
pg_mors = p.Val.ManagedObjectReference
for pg_mor in pg_mors:
if dvswitch_mor:
break
key_mor = s._get_object_properties(pg_mor, property_names=['key'])
for key in key_mor.PropSet:
if key.Val == portgroupKey:
dvswitch_mor = dvswitch
# Get the switches uuid
dvswitch_uuid = None
for p in dvswitch_mor.PropSet:
if p.Name == "uuid":
dvswitch_uuid = p.Val
Then when you add the nic to the VM you specify it like so:
nic_spec = config.new_deviceChange()
if network_name:
nic_spec.set_element_operation("add")
if nic_type == "e1000":
nic_ctlr = VI.ns0.VirtualE1000_Def("nic_ctlr").pyclass()
elif nic_type == "e1000e":
nic_ctlr = VI.ns0.VirtualE1000e_Def("nic_ctlr").pyclass()
elif nic_type == "pcnet32":
nic_ctlr = VI.ns0.VirtualPCNet32_Def("nic_ctlr").pyclass()
elif nic_type == "vmxnet":
nic_ctlr = VI.ns0.VirtualVmxnet_Def("nic_ctlr").pyclass()
elif nic_type == "vmxnet2":
nic_ctlr = VI.ns0.VirtualVmxnet2_Def("nic_ctlr").pyclass()
elif nic_type == "vmxnet3":
nic_ctlr = VI.ns0.VirtualVmxnet3_Def("nic_ctlr").pyclass()
if network_type == "standard":
# Standard switch
nic_backing = VI.ns0.VirtualEthernetCardNetworkBackingInfo_Def("nic_backing").pyclass()
nic_backing.set_element_deviceName(network_name)
elif network_type == "dvs":
nic_backing_port = VI.ns0.DistributedVirtualSwitchPortConnection_Def("nic_backing_port").pyclass()
nic_backing_port.set_element_switchUuid(dvswitch_uuid)
nic_backing_port.set_element_portgroupKey(portgroupKey)
nic_backing = VI.ns0.VirtualEthernetCardDistributedVirtualPortBackingInfo_Def("nic_backing").pyclass()
nic_backing.set_element_port(nic_backing_port)
nic_ctlr.set_element_addressType("generated")
nic_ctlr.set_element_backing(nic_backing)
nic_ctlr.set_element_key(4)
nic_spec.set_element_device(nic_ctlr)
devices.append(nic_spec)
Hope that helps someone in the future!
Romeo
On Tuesday, February 26, 2013 2:11:08 PM UTC-10, Romeo Theriault wrote: