How You Can Use VI Java API in Scala: A Quick Sample

As I blogged before, you can use open source VI Java API in all JVM languages like Jython, Groovy, JRuby and frameworks like Grail. Today Dave Briccetti (@dcbriccetti) sent me a sample showing how to use it in Scala. Dave has been a happy user of the API since he started to work at VMware as an independent consultant more than a year ago.

Here is the code sample:

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.

import org.apache.log4j.Logger
object HelloVM {
  val log = Logger.getLogger(getClass)
  def sayHello(url: String, user: String, pwd: String) {"Starting")
    val si = new ServiceInstance(new URL(url), user, pwd, true)"ServiceInstance created")
    val rootFolder = si.getRootFolder"Root folder: " + rootFolder.getName)"Searching for VMs")
    getVMs(rootFolder) match {
      case firstVm :: others =>"Hello " + firstVm.getName)"GuestOS: " + firstVm.getConfig.getGuestFullName)"Multiple snapshots supported: " + firstVm.getCapability.isMultipleSnapshotsSupported)
      case _ => log.warn("No VMs found")
  private def getVMs(rootFolder: Folder): List[VirtualMachine] =
    new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine") match {
      case null =>
        log.error("searchManagedEntities returned null")
      case vms => vms map(_.asInstanceOf[VirtualMachine]) toList

If you have not yet installed Scala, please download it here. For VI Java API, download it here. Optionally you can use Maven with the pom.xml provided in this zipped project from Dave. Thanks Dave!

BTW, please leave a comment if you find we miss a JVM language you are interested.

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

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>


    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__

    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.