vuser2010
Contributor
Contributor

Finding DataCenter Name from a vm ref

Jump to solution

Hi,

When I receive VmCreatedEvent I receive new vm ref. While retrieving new VMs property, I would like to find "Datacenter name". I am not able to traverse from VM to Datacenter. Can someone help me with this?

I am trying following traversal (vSphere SDK for Java):

PropertySpec dcSpec = new PropertySpec();

        dcSpec.setType("Datacenter");

        dcSpec.setPathSet(new String[] {"name"});

        hostPropSpec.setAll(false);

SelectionSpec recurseParents = new SelectionSpec();
        recurseParents.setName("parent2parent");
        TraversalSpec dcTraversalSpec = new TraversalSpec();
        dcTraversalSpec.setType("VirtualMachine");
        dcTraversalSpec.setPath("parent");
        dcTraversalSpec.setName(recurseParents.getName());
        dcTraversalSpec.setSelectSet(new SelectionSpec[] { recurseParents });

  PropertyFilterSpec pfSpec = new PropertyFilterSpec();

        pfSpec.setPropSet(new PropertySpec[] {dcSpec});

        pfSpec.setObjectSet(new ObjectSpec[] {oSpec });

        return getService().retrieveProperties(getContent().getPropertyCollector(), new PropertyFilterSpec[] {pfSpec });

this is not working.

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
vijayagce
Enthusiast
Enthusiast

Hi,

I have c# code for getting datacenter name from vm ref. Here i have explained how i got datacenter using vm ref in c#. I think it may helpful u to get equivalent in java.

steps:

1) get vm ref using findbyip method by sending datacenter name as null.

                               ManagedObjectReference VMObject = _service.FindByIp(_sic.searchIndex, null, clientIp, true);

2) get the vmparent as moref using getdynamicprop

                               ManagedObjectReference vmParent = (ManagedObjectReference)GetDynamicProp(VMObject, "parent");

3) get datacenter name as moref  using getdynamicprop

                               ManagedObjectReference dataCenter = (ManagedObjectReference)GetDynamicProp(vmParent, "parent");

let me know if u need getdynamicprop function explanation. I think it will be in vijava src.

Thanks,

Vijaya

View solution in original post

0 Kudos
2 Replies
Steve_Jin
Expert
Expert

You may want to try VMware sponsored open source project vijava API here: http://vijava.sf.net. With the API, you don't get the VirtualMachine instance from its moref with the MorUtil utility class. Once you have that say vm, you can simple code like this:

String datacenterName = vm.getParent().getParent().getName();

Before you use the API, read the 5 minute get started tutorial: http://vijava.sourceforge.net/doc/getstarted/tutorial.htm

For more details on the inventory structure, check here: http://www.doublecloud.org/2010/03/vsphere-inventory-structure-deep-dive/

Good luck!

Steve, author of VMware VI and vSphere SDK by Prentice Hall

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
vijayagce
Enthusiast
Enthusiast

Hi,

I have c# code for getting datacenter name from vm ref. Here i have explained how i got datacenter using vm ref in c#. I think it may helpful u to get equivalent in java.

steps:

1) get vm ref using findbyip method by sending datacenter name as null.

                               ManagedObjectReference VMObject = _service.FindByIp(_sic.searchIndex, null, clientIp, true);

2) get the vmparent as moref using getdynamicprop

                               ManagedObjectReference vmParent = (ManagedObjectReference)GetDynamicProp(VMObject, "parent");

3) get datacenter name as moref  using getdynamicprop

                               ManagedObjectReference dataCenter = (ManagedObjectReference)GetDynamicProp(vmParent, "parent");

let me know if u need getdynamicprop function explanation. I think it will be in vijava src.

Thanks,

Vijaya

0 Kudos