Here are some examples of several implementations of xml-rpc clients to invoke OpenNebula to execute a Submit action (one.vmallocate), which submits the template with the description of a VM.
This sample submits a string containing a definition of a virtual machine (template) and gets the resulting vid, for more information on XML-RPC go to http://xmlrpc-c.sourceforge.net/, we used xmlrpc-c-1.06.21 version for this test.
#include <cstdlib> #include <string> #include <iostream> #include <xmlrpc-c/girerr.hpp> #include <xmlrpc-c/base.hpp> #include <xmlrpc-c/client_simple.hpp> #include <map> using namespace std; int main(int argc, char **) { if (argc-1 > 0) { cerr << "This program has no arguments" << endl; exit(1); } try { xmlrpc_env env; string const serverUrl("http://localhost:8080/RPC2"); string const methodAllocate("one.vmallocate"); xmlrpc_c::clientSimple myClient; xmlrpc_c::value resultSUBMIT; xmlrpc_env_init(&env); myClient.call(serverUrl, methodAllocate, "ss", &resultSUBMIT,"SESSION-GOLA&4H9109KVFSG", "MEMORY=345 CPU=4 DISK=[FILE=\"img\",TYPE=cd]" "DISK=[FILE=\"../f\"]"); xmlrpc_c::value_array resultArray = xmlrpc_c::value_array(resultSUBMIT); vector<xmlrpc_c::value> const paramArrayValue(resultArray.vectorValueValue()); //check posible Errors: xmlrpc_c::value * firstvalue; firstvalue = &(static_cast<xmlrpc_c::value>(paramArrayValue[0])); xmlrpc_c::value_boolean * status = &(static_cast<xmlrpc_c::value_boolean>(*firstvalue)); xmlrpc_c::value * secondvalue; secondvalue = &(static_cast<xmlrpc_c::value>(paramArrayValue[1])); xmlrpc_c::value_string * valueS = &(static_cast<xmlrpc_c::value_string>(*secondvalue)); if(static_cast<bool>(*status)) { //Success, returns the id assigned to the VM: cout << "vmid returned: " << static_cast<string>(*valueS) << endl; return 0; } else{ //Failure: string error_value=static_cast<string>(*valueS); if (error_value.find("Error inserting",0)!=string::npos ) cout << "Error inserting VM in the database" << endl; else if (error_value.find("Error parsing",0)!=string::npos ) cout << "Error parsing VM template" << endl; else cout << "Unknown error " << static_cast<string>(*valueS) << endl; }; } catch (girerr::error const error) { cerr << "Client threw error: " << error.what() << endl; //"Client threw error:" return 20; } catch (std::exception const e) { cerr << "Client threw unexpected error." << endl; //Unexpected error: return 999; } return 0; }
This is the same sample but in ruby, using the xmlrpc gems, doc on ruby xml-rpc can be found at http://www.ruby-doc.org/stdlib/libdoc/xmlrpc/rdoc.
#!/usr/bin/env ruby require 'xmlrpc/client' require 'pp' server_url="http://localhost:2633/RPC2" server=XMLRPC::Client.new2(server_url) begin response=server.call("one.vmallocate", "SESSION-GOLA&4H910", "MEMORY=345" "CPU=4 DISK=[FILE=\"img\",TYPE=cd] DISK=[FILE=\"../f\"]") response<<nil if response.length<2 pp response rescue Exception => e [false, e.message] end
This sample submits a vm description in a template to OpenNebula, it implements the xmlrpc client library of Apache, more information at http://ws.apache.org/xmlrpc/.
import org.apache.xmlrpc.XmlRpcClient; import java.util.Vector; public class XmlRpcTest { public static void main( String args[] ) throws Exception { XmlRpcClient client = new XmlRpcClient( "http://localhost:2633/RPC2" ); Vector params = new Vector(); params.addElement("SESSION-GOLA&4H910"); params.addElement("MEMORY=345 CPU=4 DISK=[FILE=\"img\",TYPE=cd]" "DISK=[FILE=\"../f\"]"); Object result = client.execute( "one.vmallocate", params ); if ( result != null ) System.out.println( result.toString() ); } }