How to access references to files on a vsphere datastore


#1

I’m trying to create a button/dialog that will allow a user to select an ISO image from a vSphere datastore and mount it to the VM in question.

If the the root abject is a vm, I’d like to be able to filter a list of all eligible ISO files on datastores mountable to the VM, by traversing the datastores visible to the vm’s host and gathering a list of files ending in .iso. I could then further filter this by using tags from the datastores to separate access for multi-tenant environments.

Rather than using some brittle external dependency, I’d prefer to locate the ISO file from the information that is available in the vmdb. I notice that once a datastore has been smart state analysed one can drill down into the files via the “All Files” box under “Content” from the datastores web ui view. But how do I find this information in the vmdb? In my lab, I’ve tried traversing everything likely looking under $evm.vmdb(:storage).first but nothing resembling this list of files is evident.

Any clues?


#2

@gmccullough @eclarizio @mkanoor Please review


#3

Hi @Stephen_McKay,

For objects to be exposed to automate they require a service model and relationships need to be exposed in these service models to allow you to traverse between models.

For your use case the Storage service model exists but the StorageFile model does not and the relationship between the models is also not exposed.

I created a PR to add these items that you can apply to your environment and test with:

This will allow you to traverse from the VM in the root object to the host, storages and finally the storage files.

That would look something like this:

vm.host.storages.first.storage_files

However, If you are only looking for ISO files it might make more sense to perform a query against the StorageFile model to limit your results.

To return ISO files on the storages related to the VM’s host you could do this:

storage_ids = vm.host.storages.collect(&:id)
iso_files = $evm.vmdb(:storage_file).where("ext_name = 'iso' and storage_id IN (?)", storage_ids)

Support for the where command was added in PR #6046. If that is not supported in your environment try:

iso_file = $evm.vmdb(:storage_file).find_all_by_ext_name_and_storage_id('iso', storage_ids)