Easy vSphere Web Apps with Grails and the VI Java API


A few weeks ago I wrote a post with a very similar title, “Easy VMware Development with VI Java API and Groovy.”  Today I want to expand on that a little bit and show you a cool way to quickly stand up web apps for VMware vSphere using Grails.    What is Grails?  If you’re familiar with the popular Ruby on Rails web application framework, then you can think of Grails as the Java (well, Groovy actually) equivalent of Rails.  From the Grails official website …

Grails is an advanced and innovative open source web application platform that delivers new levels of developer productivity by applying principles like Convention over Configuration. Grails helps development teams embrace agile methodologies, deliver quality applications in reduced amounts of time, and focus on what really matters: creating high quality, easy to use applications that delight users.

What does all this mean?  The short and sweet answer is Grails will take care of all the pain-in-the-a$$ “stuff” required to get a web app up and running.  A good analogy would be cake mix.


Let me explain.  If you wanted to bake a cake, you could go buy the eggs, flower, milk, sugar (and whatever else goes into a cake) and then spend all day in the kitchen putting it all together.  Or, you could simply buy a box of cake mix, follow some simple instructions and have exactly what you wanted in the first place, but with a fraction of the time and effort. It’s very much the same kind of thing with Grails.  If I wanted to build a web application, I could go set up all the necessary pieces (e.g. database, web server, directory structure, security, etc.) and then spend a good amount of time assembling all the pieces together, all before I ever write a single line of code for my application.  Or, I could simply let Grails do all the heavy lifting and take care of all that “stuff,” freeing me to focus on writing code for my web app.

And when I say web app, I don’t mean a few HTML pages and a web server.  I’m talking about powerful, database driven, dynamic web applications.  I’m talking about about things like ecommerce websites, such as Amazon and Ebay, or social networking sites like Twitter and Facebook.  It’s these kinds of web applications that I’m referring to which Grails can help us stand up quickly.  So does that mean you’ll be able to have your very own Twitter by simply reading this blog post and installing Grails?  Uh, no.   Cake mix doesn’t bake itself.  You still have some work to do, just far less than doing everything from scratch.

Having said that, I am going to show how to very quickly stand up an application that will pull real time info from vSphere and display it on a web page.  Ready?  Here we go …

  1. If you haven’t already done so, download, install and properly configure Grails for your environment.
  2. Create a home for your web apps.  For me, I created /home/asweemer/MyApps.
  3. In the directory you just created, execute the command grails create-app <app name>.  In this case, we’re going to create an app called HelloVM, so I executed grails create-app HelloVM in /home/asweemer/MyApps.  This will spit out a bunch of stuff on your screen as Grails is doing all sorts of things for you, like creating a directory structure and putting “starter files” in place for you.
  4. Now let’s enter into the HelloVM directory that was just created for us.  In this directory, enter the command grails create-controller HelloVM.  (IMPORTANT NOTE:  from here forward, we need to execute all Grails commands from the root app directory, in this case, ~/MyApps/HelloVM),
  5. Now execute the command grails create-view HelloVM.  After this command is finished executing, make sure there is an index.gsp file in ~/MyApps/HelloVM/grails-app/views/helloVM.  If there’s not one there, just create an empty file (e.g. touch ~/MyApps/HelloVM/grails-app/views/helloVM/index.gsp).  I thought the grails create-view command would do this for you, but for some reason it didn’t on my system.  And the file needs to be there (even if it’s blank) or else we’ll get an error later.
  6. Believe it or not, that should be all we need from a Grails perspective (well, for this quick demo anyway).  Now it’s time to write some code that will connect to vCenter (or ESX), retrieve a list of VMs and display the results on a webpage.   Not surprisingly, this is where VI Java API comes in.  If you read the last post, then you should already have downloaded the VI Java API.  If not, download the VI Java API.
  7. Unzip the VI Java API package and place the two .jar files in the ~/MyApps/HelloVM/lib directory.
  8. With your favorite text editor, open the HelloVMController.groovy file, which Grails created for us and will be located in either ~/MyApps/HelloVM/grails-app/controllers/ or ~/MyApps/HelloVM/grails-app/controllers/hellovm, depending on which version of Grails you’re running.  If you’re running the latest version of Grails, it will be located in the latter.  When you open the file, you should see some text Grails has already created for you.  We need to add our application logic here.
  9. Using the HelloVM example from my previous post as a staring point (actually using the slightly more Groovy-fied version from Guillaume Laforge), I’ve made a few minor modifications so the code will properly run on Grails.  So copy the code below and modify your HelloVMController.groovy file so it looks like this …
    package hellovm

    import java.net.URL
    import com.vmware.vim25.*
    import com.vmware.vim25.mo.*

    class HelloVMController {
    def index = {
    def si = new ServiceInstance("https://<ip address of vCenter or ESX>/sdk".toURL(), "administrator", "password", true)
    def rootFolder = si.rootFolder
    render "Root folder: ${rootFolder.name}"
    new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine").each {
    render """
    Hello ${it.name}
    Guest OS: ${it.config.guestFullName}
    Multiple snapshots supported?: ${it.capability.isMultipleSnapshotsSupported()}

  10. Save the file and the only thing we have left to do now is start the application server.  To do this, from the ~/MyApps/HelloVM directory execute grails run-app.  Again Grails will spit out a bunch of things on the screen as it’s doing things like compiling your code and starting an application server.  Once Grails is ready, you should see Server running.  Browse to http://localhost:8080/HelloVM.  Opening a web browser and going to that URL should reveal a default “Welcome to Grails” web page with a section titled “Available Controllers.”  We only created one controller, so you should only see one link.  Clicking on that link will execute our code and tell us what VMs we have running.  Here’s a screenshot of what I get on my system …

    Now there’s a lot of things in the 10 steps above that I didn’t go into detail about.  You may be wondering, “what is a Controller?” or “what is a View?” or “why did he change println to render?” or about 50 other things that I (in)conveniently skipped over.  Sorry, but in the interest of brevity and time, I’m going to have to save that for a later post.  In the meantime, do some Google searches and see if you can figure out the answers to those questions on your own.  Or just check back here in a week or so :)

    Thanks again to everyone who left comments on the last post.  As usual, feel free to let me know if you have any questions about Groovy, Grails or the VI Java API.  I’d also really like to see some of your own unique code examples that uses Grails and the VI Java API to to manage and automate vSphere.


  1. Glad to see yet another great post soon after the Groovy one. I just posted a blog to introduce your tutorial, as well as a tech note I wrote a while back on developing a simple yet complete vSphere Client plug-in using Java and Tomcat. With these two, people can develop vSphere Client plug-in with Grails.

  2. Hi Steve,

    Thanks again for promoting this post on your blog, and thanks for the info about your tech note. I'm reading it now and it's really good!

    Looking forward to meeting you at Tech Summit.


Comments are closed.