Earlier this year, when I first thought to test out the VI Java API, I estimated I would spend several hours learning the API just to write a simple test program. I was surprised when in less than two hours, I had created my first java program for my ESXi server. Granted it’s a simple program, but the ease and speed of creating something that interacts with my ESXi server was impressive.
The program allows you to connect to a ESX server; once logged-on, it displays all the virtual machines you have installed on the server. You then have the option to perform several management operations such as power on, power off, suspend, or restart on any virtual machine. You can download the sample code from the download section.
The VI SDK has a set of management interfaces to the VMware Infrastructure. Everything is centered on Web Services and the VI SDK uses SOAP (Simple Object Access Protocol) as the underlying protocol. The advantage of web services is that it overcomes platform and language dependency. Unfortunately, one issue with web services is that it’s procedural with only methods defined. It doesn’t have built in object orientation. However, the VI Java API can take full advantage of object-oriented programming and compile time–type checking.
The VI SDK object model contains objects called managed objects that represent server-side objects that can be either a manageable entity (eg. virtual machine) or a service (eg. authentication service). One of the things that really helped me when using the VI Java API was the fact that there is a ManagedObject type. It captures all the common properties and behaviors of managed objects and makes things a lot easier to understand and accomplish when just getting started. This object type is not available in the VI SDK.
In the VI SDK, ManagedObjectReference is used to represent managed objects. Further, unlike the norm in an object-oriented programming language, another managed object called PropertyCollector is used to obtain properties of a managed object. The VI Java API hides the PropertyCollector object behind getter methods. This a how a clean API really should be; the programmer shouldn’t have to worry about functionality of other objects to retrieve data for a manged object.
To see just how easy it is to implement some of the functionality look at my code using some of the core API calls below. My methods for such activities as powering on or off a virtual machine are literally one line of code within try/catch blocks. Once the virtual machine managed object is obtained, it’s a piece of cake from there.