pyvmomi-community-samples icon indicating copy to clipboard operation
pyvmomi-community-samples copied to clipboard

Sample: find a VM using FindByDatastorePath

Open hartsock opened this issue 11 years ago • 3 comments

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.

hartsock avatar Jun 02 '14 14:06 hartsock

Working on this now.

  1. Where do you want to save the datastore path?
  2. If the VirtualMachine is on multiple datastores how do you want to handle that?
  3. 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

michaelrice avatar Jun 11 '14 23:06 michaelrice

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.

chinmaya-n avatar Feb 20 '23 19:02 chinmaya-n

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.

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.

prziborowski avatar Feb 22 '23 10:02 prziborowski