1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-27 13:57:23 +03:00

F #4937: Adds XML-RPC method to execute & replicate SQL commands

This commit is contained in:
Ruben S. Montero 2017-05-25 17:48:29 +02:00
parent c8c0306742
commit 631abcdd1f
4 changed files with 97 additions and 11 deletions

View File

@ -29,11 +29,8 @@ using namespace std;
class RequestManagerSystem: public Request
{
protected:
RequestManagerSystem( const string& method_name,
const string& help,
const string& params)
:Request(method_name,params,help)
{};
RequestManagerSystem(const string& method_name, const string& help,
const string& params) :Request(method_name,params,help) {};
~RequestManagerSystem(){};
@ -82,6 +79,24 @@ public:
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class SystemSql: public RequestManagerSystem
{
public:
SystemSql():RequestManagerSystem("one.system.sql",
"Executes and replicates SQL commands on the DB backend","A:ssb")
{
auth_op = AuthRequest::ADMIN;
};
~SystemSql(){};
void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att);
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class UserQuotaInfo : public RequestManagerSystem
{
public:

View File

@ -29,7 +29,8 @@ module OpenNebula
:groupquotainfo => "groupquota.info",
:groupquotaupdate => "groupquota.update",
:version => "system.version",
:config => "system.config"
:config => "system.config",
:sql => "system.sql"
}
#######################################################################
@ -46,6 +47,17 @@ module OpenNebula
# XML-RPC Methods
#######################################################################
# Executes and replicates SQL commands on OpenNebula DB
# @param [String] Sql string
# @param [Boolean] True to replicate command on a federation. To
# operate on federated tables
# @return [Integer, OpenNebula::Error] Sql execution result in case
# of success, Error otherwise
def sql_command(sql, federate)
return @client.call(SYSTEM_METHODS[:sql], sql, federate)
end
#
# Gets the oned version
#
# @return [String, OpenNebula::Error] the oned version in case

View File

@ -446,6 +446,7 @@ void RequestManager::register_xml_methods()
// System Methods
xmlrpc_c::methodPtr system_version(new SystemVersion());
xmlrpc_c::methodPtr system_config(new SystemConfig());
xmlrpc_c::methodPtr system_sql(new SystemSql());
// Rename Methods
xmlrpc_c::methodPtr vm_rename(new VirtualMachineRename());
@ -1063,6 +1064,7 @@ void RequestManager::register_xml_methods()
/* System related methods */
RequestManagerRegistry.addMethod("one.system.version", system_version);
RequestManagerRegistry.addMethod("one.system.config", system_config);
RequestManagerRegistry.addMethod("one.system.sql", system_sql);
};
/* -------------------------------------------------------------------------- */

View File

@ -25,11 +25,6 @@ using namespace std;
void SystemVersion::request_execute(xmlrpc_c::paramList const& paramList,
RequestAttributes& att)
{
// TODO: request_execute will not be executed if the session string
// is not authenticated in Request::execute.
// Should we make the version call accessible even
// if no user is provided?
success_response(Nebula::instance().code_version(), att);
return;
@ -57,6 +52,68 @@ void SystemConfig::request_execute(xmlrpc_c::paramList const& paramList,
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
void SystemSql::request_execute(xmlrpc_c::paramList const& paramList,
RequestAttributes& att)
{
std::string sql = xmlrpc_c::value_string(paramList.getString(1));
bool federate = xmlrpc_c::value_boolean(paramList.getBoolean(2));
Nebula& nd = Nebula::instance();
LogDB * logdb = nd.get_logdb();
SqlDB * db;
if ( att.uid != 0 )
{
att.resp_id = -1;
failure_response(AUTHORIZATION, att);
return;
}
if ( federate )
{
if (nd.is_federation_slave())
{
att.resp_msg = "SQL command has to be executed on a master zone";
att.resp_id = - 1;
failure_response(ACTION, att);
return;
}
db = new FedLogDB(logdb);
}
else
{
db = logdb;
}
std::ostringstream oss(sql);
int rc = db->exec_wr(oss);
if ( federate )
{
delete db;
}
if ( rc == 0 )
{
success_response(0, att);
}
else
{
att.resp_id = rc;
failure_response(ACTION, att);
}
return;
}
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
void UserQuotaInfo::request_execute(xmlrpc_c::paramList const& paramList,
RequestAttributes& att)
{