Ruby OCA API 2.0

This page contains the OpenNebula Cloud API Specification for Ruby. It has been designed as a wrapper for the XML-RPC methods, with some basic helpers. This means that you should be familiar with the XML-RPC API and the XML formats returned by the OpenNebula core. As stated in the XML-RPC documentation, you can download the XML Schemas (XSD) here.

Rdoc

You can consult the Rdoc online.

Usage

You can download the oca gem by: <xterm> $ sudo gem install oca </xterm>

Then you can use the functionality of the gem including the following lines in the code:

##############################################################################
# Required libraries
##############################################################################
require 'rubygems'
require 'oca'

You can also use the Ruby OCA included in the OpenNebula distribution by adding the OpenNebula Ruby library path to the search path:

##############################################################################
# Environment Configuration
##############################################################################
ONE_LOCATION=ENV["ONE_LOCATION"]

if !ONE_LOCATION
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
else
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end

$: << RUBY_LIB_LOCATION

##############################################################################
# Required libraries
##############################################################################
require 'OpenNebula'

Code sample

This is a small code snippet. As you can see, the code flow would be as follows:

  • Create a new Client, setting up the authorization string. You only need to create it once.
  • Get the VirtualMachine pool that contains the VirtualMachines owned by this User.
  • You can perform “actions” over these objects right away, like myVNet.delete();. In this example all the VirtualMachines will be shut down.
#!/usr/bin/env ruby
 
##############################################################################
# Required libraries
##############################################################################
require 'rubygems'
require 'oca'
 
include OpenNebula
 
# OpenNebula credentials
CREDENTIALS = "oneuser:onepass"
# XML_RPC endpoint where OpenNebula is listening
ENDPOINT    = "http://localhost:2633/RPC2"
 
client = Client.new(CREDENTIALS, ENDPOINT)
 
vm_pool = VirtualMachinePool.new(client, -1)
 
rc = vm_pool.info
if OpenNebula.is_error?(rc)
     puts rc.message
     exit -1
end
 
vm_pool.each do |vm|
     rc = vm.shutdown
     if OpenNebula.is_error?(rc)
          puts "Virtual Machine #{vm.id}: #{rc.message}"
     else
          puts "Virtual Machine #{vm.id}: Shutting down"
     end
end
 
exit 0