VMware Cloud Community
abuzzi_cisco
Contributor
Contributor
Jump to solution

pyVmomi: derive datacenter information from vim.VirtualMachine object

Hello,

in a pyVmomi script I do get all VMs objects using following snippet code:

    [...]
    content = vc[vcenter]["si"].RetrieveContent()
    objview = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
    vmList = objview.view
    objview.Destroy()
    [...]

then I save in a file all the vmPathName informations (ie: vmList[0].summary.config.vmPathName).

Later the script need to locate back the vim.VirtualMachine object from the vmPathName information available in the saved file.

For this I plan to use SearchIndex.FindByDatastorePath but it requires to specify the datacenter information as well.

The issue is therefore how to derive the datacenter information for the given vim.VirtualMachine object and store it accordingly along with the vmPathName.

Unfortunately I've been unable to derive datacenter information from the vim.VirtualMachine object....

Any help/suggestion is really appreciated!

Thx,

A.

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
abuzzi_cisco
Contributor
Contributor
Jump to solution

Hello,

I changed the get vm logic. Now I iterate among the DC/CL/hosts.

Here the snippet in case anyone would be interested as well.

    content = vc[vcenter]["si"].RetrieveContent()

    objview = content.viewManager.CreateContainerView(content.rootFolder, [vim.Datacenter],True)

    dcList = objview.view

    objview.Destroy()

    for dc in dcList:

       for cl in dc.hostFolder.childEntity:

         for host in cl.host:

           for vm in host.vm:

                [...]

                uuid=vm.summary.config.uuid

                vmdb[uuid]={};

                vmdb[uuid]["name"]=vm.summary.config.name

                vmdb[uuid]["vmPathName"]=vm.summary.config.vmPathName

                vmdb[uuid]["powerState"]=vm.summary.runtime.powerState

                vmdb[uuid]["vcenter"]=vcenter

                vmdb[uuid]["datacenter"]=dc.name

                [...]

Thx,
A

View solution in original post

Reply
0 Kudos
3 Replies
abuzzi_cisco
Contributor
Contributor
Jump to solution

Hello,

I changed the get vm logic. Now I iterate among the DC/CL/hosts.

Here the snippet in case anyone would be interested as well.

    content = vc[vcenter]["si"].RetrieveContent()

    objview = content.viewManager.CreateContainerView(content.rootFolder, [vim.Datacenter],True)

    dcList = objview.view

    objview.Destroy()

    for dc in dcList:

       for cl in dc.hostFolder.childEntity:

         for host in cl.host:

           for vm in host.vm:

                [...]

                uuid=vm.summary.config.uuid

                vmdb[uuid]={};

                vmdb[uuid]["name"]=vm.summary.config.name

                vmdb[uuid]["vmPathName"]=vm.summary.config.vmPathName

                vmdb[uuid]["powerState"]=vm.summary.runtime.powerState

                vmdb[uuid]["vcenter"]=vcenter

                vmdb[uuid]["datacenter"]=dc.name

                [...]

Thx,
A

Reply
0 Kudos
tburge
Contributor
Contributor
Jump to solution

Any way I can possibly get a look at all of your code? I've coded something similar and I'm wondering about comparisons of execution speed and such.

Thanks!

VCIX6-DCV VCP7-CMA VCAP6-DTM Deploy
Reply
0 Kudos
PadraicBaker
Contributor
Contributor
Jump to solution

Just in case somebody were searching for this, here is a simple solution with only the VirtualMachine object.

def get_vmdatacenter(vm):
    dc_type = vim.Datacenter
    obj = vm.parent
    while not isinstance(obj, dc_type):
        obj = obj.parent
    return obj

 

Or, it could be performed in place, without defining a function.

obj = vm.parent
while not isinstance(obj, vim.Datacenter):
    obj = obj.parent
datacenter = obj

 

Reply
0 Kudos