While working on something that was completely random, I started to play around a bit with the Ruby programming language. I had a few questions and I reached out to a buddy of mine who is Ruby developer and in doing so I was introduced to a very cool Ruby gem called HTTParty. For those of you who are not familiar with Ruby gems, it is a packaged library or application for Ruby, similar to an rpm or pkg file. The neat thing about HTTParty is that it makes working with REST APIs extremely easy and processing XML request/responses into Ruby objects without too much effort.
With the bit of knowledge I gained from my buddy, I wanted to see how easy it would be to apply this to the vCloud API using Ruby and HTTParty. To my surprise, it was actually pretty easy!
Disclaimer: I am not a developer nor even a Ruby novice. This is probably the second or third time I have used Ruby.
Before getting started, you will need to have the following installed on your system:
The following vCloud APIs are implemented in the sample below:
- Query Service API for looking up vCloud vApps
- PowerOn vApp
- PowerOff vApp
- Shutdown vApp
- Suspend vApp
- Reset vApp
- Create Snapshot for vApp
- Remove Snapshot for vApp
To demonstrate the use of Ruby & HTTParty, I created a very simple Ruby script called vcd.rb which primarily deals with vApp operations.
To use the script, you will need to create a file called config-vcd.yml which contains the credentials to your vCloud Director instance. Here is what the file should look like:
:username: administrator@system
:password: supersecretpassword
:site: https://vcd.primp-industries.com
Note: This example assumes you are running vCloud Director 5.1 (but sample can be modified to support 1.5)
Here is an example of listing all vApps for the given Organization you are logging into:
Here is an example of listing a specific vApp:
Here is an example of performing several operations on the vApp:
At the end of this little exercise, I was pretty impressed at how easy it was to work with REST APIs using Ruby and HTTParty. I think the toughest part for me personally was getting used to Ruby's syntax which I was not familiar with. I also extended this exercise to the vCloud Networking and Security REST APIs which I will share in another blog post.
Useful Resources:
- http://www.railstips.org/blog/archives/2008/07/29/it-s-an-httparty-and-everyone-is-invited/
- http://blogs.vmware.com/vsphere/2012/03/exploring-the-vcloud-rest-api-part-1.html
- http://blogs.vmware.com/vsphere/2012/03/exploring-the-vcloud-rest-api-part-2.html
Simon Fredsted says
If you can't find the vcd.rb script, it's because it's moved to Github. The URL is: https://github.com/lamw/vghetto-scripts