pyvmomi-community-samples
pyvmomi-community-samples copied to clipboard
Sample: find a VM using FindByDatastorePath
Demonstrate how to figure out a Virtual Machine's datastore path during create, save the path, and then use that path to find the VM again later. This can be a valuable tool in systems that are focused on Datastore and storage as their primary focus in creating, managing, and moving around Virtual Machines.
In particular the Nova driver on OpenStack does this a lot and could use some clean samples around that work flow.
Working on this now.
- Where do you want to save the datastore path?
- If the VirtualMachine is on multiple datastores how do you want to handle that?
- Do you want the example to be made of multiple files where 1 does the create and store, then another reads that stored info and does a find vm by datastore path?
Thanks
Any example on how this is done? I'm stuck wondering how to create the datacenter object as FindByDatastorePath needs that as an argument (Python vSphere SDK). Any help is greatly appreciated. Lack of clear documentation for the Python SDK is frustrating.
Any example on how this is done? I'm stuck wondering how to create the datacenter object as
FindByDatastorePathneeds that as an argument (Python vSphere SDK). Any help is greatly appreciated. Lack of clear documentation for the Python SDK is frustrating.
A lot of pyVmomi works with an inventory view that you can navigate after you log in and retrieve a ServiceInstance.
The content property of service instance (either access content or call the method RetrieveContent()) has a lot of managers with methods and also has a rootFolder object which you could think of from the UI as the root object.
From there you will have a list of either Datacenter folders, or Datacenters themselves.
So if you know the datacenter name, you could look for it that way, such as:
dcName = "US East"
vmxPath = "[datastoreName] path/to/file.vmx"
si = SmartConnect(...)
searchIndex = si.content.searchIndex
for dc in si.content.rootFolder:
if dc.name == dcName:
vm = searchIndex.FindByDatastorePath(dc, vmxPath)
if vm is not None:
print("Found VM " + vm.name)
break
There should be a number of samples for connecting. https://developer.vmware.com/apis/358/vsphere/doc/vim.ServiceInstance.html gives some graphical details about the ServiceInstance and inventory.