How to Get a Managed Object With Its ID Like task-id?

The open source VI Java API has implemented typing which makes it much easier to use and possible to catch errors in compile time. To use these types, you have to get hold of these objects. Normally you don’t need to worry about this because you navigate the system from the top ServiceInstance and get the managed objects through VI Java API calls.

There are some rare cases in which you get the object id first, for example, a task id as asked in the VI Java API forum. A bit more common case is when you develop vSphere Client plug-ins. The URL string your web application gets has object id and type for a virtual machine or host, etc.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

Although coming from different use cases, it’s the same for API usage. You have two choices:

1.  If you know both the type and id of the managed object, you can simply follow the following code. Make sure you type the name of the managed object correctly. Note that you can also replace the last line of code in first approach with the code of second approach.

ManagedObjectReference mor = new ManagedObjectReference();
mor.setType("Task");
mor.setVal("task-123");
Task task = new Task(si.getServerConnection(), mor);

2.  If you have ManagedObjectReference object instead (more common if you use API to drill down some properties to their sub-properties), you can simply use a utility as the following. Note that you have to cast the type by yourself, meaning as a programmer you have to know the exact type before hand. To play safer, you can test the type before you cast.

Task task1 = (Task) MorUtil.createExactManagedObject(si.getServerConnection(), mor);
// or if you know the object will be a ManagedEntity
VirtualMachine vm = (VirtualMachine) MorUtil.createExactManagedObject(si.getServerConnection(), mor);

Note that the si variable in both code snippets is a reference to a ServiceInstance object. Because getServerConnection() is defined in ManagedObject type, so you can use any reference to a VirtualMachine, HostSystem, etc.

This entry was posted in vSphere API and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. amogh
    Posted January 11, 2013 at 12:51 am | Permalink

    how to get VM’s UUID,TokenId via VIJAVA??,
    and how to get vcenter uuid,tokenId?
    plz give me sample code

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.