As I mentioned in a previous blog, vSphere(VI) Java API can be used in any JVM languages/frameworks. We have samples in Jython, Groovy, Grail. This weekend I got a sample in JRuby shared by our community member Martin Jackson in the API forum. Thanks Martin!
I think it would be fun to share it with you. If you can write Ruby code, you can take advantage of VI Java API for managing and automating vSphere as well. If you have samples leveraging the API to share, I am happy to hear about it.
Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.
Now, let us take a look at Martin’s sample code ported from a VI Java API sample.
# VI Java API Hello using JRuby require 'java' require 'dom4j-1.6.1.jar' require 'vijava2120100824.jar' # Only using a single class from this package so I'll just import it import java.net.URL # Creating ViJava Module so I don't have to import each com.vmware.vim25.mo class individually. module VIJava include_package "com.vmware.vim25.mo" end si = VIJava::ServiceInstance.new(URL.new("https://192.168.1.9/sdk"), "root", "password", true) rootFolder = si.getRootFolder() vms = VIJava::InventoryNavigator.new(rootFolder).searchManagedEntities("VirtualMachine") puts "Hello " + vms.getName() si.getServerConnection().logout()