vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.02k stars 923 forks source link

Sample: find a VM using FindByDatastorePath #48

Open hartsock opened 10 years ago

hartsock commented 10 years ago

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.

michaelrice commented 10 years ago

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

chinmaya-n commented 1 year ago

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.

prziborowski commented 1 year ago

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.