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:

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.

package com.vmware.vim25.mo.samples
import java.net.URL
import org.apache.log4j.Logger
import com.vmware.vim25.mo._
object HelloVM {
  val log = Logger.getLogger(getClass)
  def sayHello(url: String, user: String, pwd: String) {
    log.info("Starting")
    val si = new ServiceInstance(new URL(url), user, pwd, true)
    log.info("ServiceInstance created")
    val rootFolder = si.getRootFolder
    log.info("Root folder: " + rootFolder.getName)
    log.info("Searching for VMs")
    getVMs(rootFolder) match {
      case firstVm :: others =>
        log.info("Hello " + firstVm.getName)
        log.info("GuestOS: " + firstVm.getConfig.getGuestFullName)
        log.info("Multiple snapshots supported: " + firstVm.getCapability.isMultipleSnapshotsSupported)
      case _ => log.warn("No VMs found")
    }
    si.getServerConnection.logout()
  }
  private def getVMs(rootFolder: Folder): List[VirtualMachine] =
    new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine") match {
      case null =>
        log.error("searchManagedEntities returned null")
        List[VirtualMachine]()
      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>

  • 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.