5 Easy Steps Using vSphere Java API

In my previous blogs, I have introduced the vSphere API object model, vSphere Java API architecture. I assume you’ve run through the 5 minute Getting Started Tutorials with HelloWorld sample.

Now, let’s take a look on how to use the API in general.

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.

1. Always starts with a ServiceInstance with URL/username/password, or URL/sessionID. For example,

ServiceInstance si = new ServiceInstance(new URL(urlStr), username, password, true);

2. From the ServiceInstance object, you can:

  • Get its properties like capability:

Capability cap = si.getCapability();

  • Get root folder object of the inventory tree.

Folder rootFolder = si.getRootFolder();

  • Since property content (ServiceContent) only holds AboutInfo and ManagedObjectReferences to all different manager, we move the these info up by providing methods like getAboutInfo and various get*() methods to get “manager” objects (including SearchIndex) directly. For example,

SearchIndex searchIndex = si.getSearchIndex();
EventManager em = si.getEventManager();

3. With the manager objects returned from ServiceInstance object, you can do many different things. For example: list all the events with a VM by using EventManager.

4. From the root Folder or any ManagedEntity in the inventory,

  • You can navigate to the sub-nodes in the inventory tree:

rootFolder.getChildEntity();

  • You can use InventoryNavigator class to search based on your criteria.
  • All the items you see in the inventory tree are of type ManagedEntity.
  • You can test the exact type of a ManagedEntity

if(me instanceof VirtualMachine){ }

  • You can call the methods defined on the exact subtypes of the ManagedEntity.

Task task = ((VirtualMachine)me).powerOffVM_Task();

5. Lastly, don’t forget to  logout the session when you are done. Exiting your application without logging out may hurt your server performance. (see, vSphere API best practice #9, and tips on session management).

si.getServerConnection().logout();

To get help, you can find exact description of each managed object and its properties/methods in the vSphere SDK API reference. Just look for the managed object type and its method with the same name. Please skip the first _this parameter and the rest are the same except the difference of real type and ManagedObjectReference. The description should be applicable despite the difference.

You will find some samples inside the com.vmware.vim25.mo.samples package. As you will find out, the samples using this APIs are much shorter and more readable than the ones using Web Service interfaces.

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

2 Comments

  1. Nikhil
    Posted July 25, 2015 at 9:47 pm | Permalink

    The samples link is not working.. I am getting connection was reset error.

  2. Posted July 26, 2015 at 2:44 pm | Permalink

    Thanks Nikhil, the SF.net is updating its infrastructure. It may have broken the links. Try it later. Sorry about the inconvenience.
    Steve

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.