mirror of
https://github.com/OpenNebula/one.git
synced 2025-01-08 21:17:43 +03:00
Feature #1099: Add new one.vmpool.accounting XMLRPC method
This commit is contained in:
parent
42872f9c44
commit
c178601623
@ -62,6 +62,12 @@ public:
|
||||
*/
|
||||
string& to_xml(string& xml) const;
|
||||
|
||||
// ----------------------------------------
|
||||
// DataBase implementation variables
|
||||
// ----------------------------------------
|
||||
|
||||
static const char * table;
|
||||
|
||||
private:
|
||||
friend class VirtualMachine;
|
||||
friend class VirtualMachinePool;
|
||||
@ -70,8 +76,6 @@ private:
|
||||
// DataBase implementation variables
|
||||
// ----------------------------------------
|
||||
|
||||
static const char * table;
|
||||
|
||||
static const char * db_names;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
|
@ -154,6 +154,19 @@ public:
|
||||
*/
|
||||
virtual int dump(ostringstream& oss, const string& where) = 0;
|
||||
|
||||
/**
|
||||
* Dumps the output of the custom sql query into an xml
|
||||
*
|
||||
* @param oss The output stream to dump the xml contents
|
||||
* @param root_elem_name Name of the root xml element name
|
||||
* @param sql_query The SQL query to execute
|
||||
*
|
||||
* @return 0 on success
|
||||
*/
|
||||
int custom_dump(ostringstream& oss,
|
||||
const string& root_elem_name,
|
||||
ostringstream& sql_query);
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
|
@ -56,11 +56,22 @@ protected:
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
|
||||
void dump(RequestAttributes& att,
|
||||
void generate_where_string(
|
||||
RequestAttributes& att,
|
||||
int filter_flag,
|
||||
int start_id,
|
||||
int end_id,
|
||||
const string& and_clause,
|
||||
const string& or_clause,
|
||||
ostringstream& where_string);
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
|
||||
void dump(RequestAttributes& att,
|
||||
int filter_flag,
|
||||
int start_id,
|
||||
int start_id,
|
||||
int end_id,
|
||||
const string& and_clause,
|
||||
const string& and_clause,
|
||||
const string& or_clause);
|
||||
};
|
||||
|
||||
@ -98,6 +109,31 @@ public:
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
class VirtualMachinePoolAccounting : public RequestManagerPoolInfoFilter
|
||||
{
|
||||
public:
|
||||
|
||||
VirtualMachinePoolAccounting():
|
||||
RequestManagerPoolInfoFilter("VirtualMachinePoolAccounting",
|
||||
"Returns the virtual machine history records",
|
||||
"A:siii")
|
||||
{
|
||||
Nebula& nd = Nebula::instance();
|
||||
pool = nd.get_vmpool();
|
||||
auth_object = PoolObjectSQL::VM;
|
||||
};
|
||||
|
||||
~VirtualMachinePoolAccounting(){};
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
|
||||
void request_execute(
|
||||
xmlrpc_c::paramList const& paramList, RequestAttributes& att);
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
class TemplatePoolInfo : public RequestManagerPoolInfoFilter
|
||||
{
|
||||
public:
|
||||
|
@ -704,6 +704,13 @@ public:
|
||||
static void set_auth_request(int uid,
|
||||
AuthRequest& ar,
|
||||
VirtualMachineTemplate *tmpl);
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// DataBase implementation (Public)
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
static const char * table;
|
||||
|
||||
private:
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@ -940,8 +947,6 @@ protected:
|
||||
// DataBase implementation
|
||||
// *************************************************************************
|
||||
|
||||
static const char * table;
|
||||
|
||||
static const char * db_names;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
|
@ -143,7 +143,7 @@ public:
|
||||
int dump(ostringstream& oss, const string& where)
|
||||
{
|
||||
return PoolSQL::dump(oss, "VM_POOL", VirtualMachine::table, where);
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
/**
|
||||
|
@ -479,14 +479,8 @@ int PoolSQL::dump(ostringstream& oss,
|
||||
const char * table,
|
||||
const string& where)
|
||||
{
|
||||
int rc;
|
||||
ostringstream cmd;
|
||||
|
||||
oss << "<" << elem_name << ">";
|
||||
|
||||
set_callback(static_cast<Callbackable::Callback>(&PoolSQL::dump_cb),
|
||||
static_cast<void *>(&oss));
|
||||
|
||||
cmd << "SELECT body FROM " << table;
|
||||
|
||||
if ( !where.empty() )
|
||||
@ -496,9 +490,26 @@ int PoolSQL::dump(ostringstream& oss,
|
||||
|
||||
cmd << " ORDER BY oid";
|
||||
|
||||
rc = db->exec(cmd, this);
|
||||
return custom_dump(oss, elem_name, cmd);
|
||||
}
|
||||
|
||||
oss << "</" << elem_name << ">";
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int PoolSQL::custom_dump(ostringstream& oss,
|
||||
const string& root_elem_name,
|
||||
ostringstream& sql_query)
|
||||
{
|
||||
int rc;
|
||||
|
||||
oss << "<" << root_elem_name << ">";
|
||||
|
||||
set_callback(static_cast<Callbackable::Callback>(&PoolSQL::dump_cb),
|
||||
static_cast<void *>(&oss));
|
||||
|
||||
rc = db->exec(sql_query, this);
|
||||
|
||||
oss << "</" << root_elem_name << ">";
|
||||
|
||||
unset_callback();
|
||||
|
||||
|
@ -243,6 +243,7 @@ void RequestManager::register_xml_methods()
|
||||
xmlrpc_c::methodPtr vm_migrate(new VirtualMachineMigrate());
|
||||
xmlrpc_c::methodPtr vm_action(new VirtualMachineAction());
|
||||
xmlrpc_c::methodPtr vm_savedisk(new VirtualMachineSaveDisk());
|
||||
xmlrpc_c::methodPtr vm_pool_acct(new VirtualMachinePoolAccounting());
|
||||
|
||||
// VirtualNetwork Methods
|
||||
xmlrpc_c::methodPtr vn_addleases(new VirtualNetworkAddLeases());
|
||||
@ -348,6 +349,7 @@ void RequestManager::register_xml_methods()
|
||||
RequestManagerRegistry.addMethod("one.vm.chmod", vm_chmod);
|
||||
|
||||
RequestManagerRegistry.addMethod("one.vmpool.info", vm_pool_info);
|
||||
RequestManagerRegistry.addMethod("one.vmpool.accounting", vm_pool_acct);
|
||||
|
||||
/* VM Template related methods*/
|
||||
RequestManagerRegistry.addMethod("one.template.update", template_update);
|
||||
|
@ -91,6 +91,74 @@ void VirtualMachinePoolInfo::request_execute(
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
void VirtualMachinePoolAccounting::request_execute(
|
||||
xmlrpc_c::paramList const& paramList,
|
||||
RequestAttributes& att)
|
||||
{
|
||||
int filter_flag = xmlrpc_c::value_int(paramList.getInt(1));
|
||||
int time_start = xmlrpc_c::value_int(paramList.getInt(2));
|
||||
int time_end = xmlrpc_c::value_int(paramList.getInt(3));
|
||||
|
||||
ostringstream oss;
|
||||
ostringstream cmd;
|
||||
ostringstream vmpool_where;
|
||||
int rc;
|
||||
|
||||
if ( filter_flag < MINE )
|
||||
{
|
||||
failure_response(XML_RPC_API,
|
||||
request_error("Incorrect filter_flag",""),
|
||||
att);
|
||||
return;
|
||||
}
|
||||
|
||||
cmd << "SELECT " << History::table << ".body FROM " << History::table
|
||||
<< " INNER JOIN " << VirtualMachine::table
|
||||
<< " WHERE vid=oid";
|
||||
|
||||
generate_where_string(att, filter_flag, -1, -1,
|
||||
"", "", vmpool_where);
|
||||
|
||||
if ( !vmpool_where.str().empty() ) //TODO: better empty check?
|
||||
{
|
||||
cmd << " AND " << vmpool_where;
|
||||
}
|
||||
|
||||
if ( time_start != -1 || time_end != -1 )
|
||||
{
|
||||
if ( time_start != -1 )
|
||||
{
|
||||
cmd << " AND (etime > " << time_start << " OR etime = 0)";
|
||||
}
|
||||
|
||||
if ( time_end != -1 )
|
||||
{
|
||||
cmd << " AND stime < " << time_end;
|
||||
}
|
||||
}
|
||||
|
||||
cmd << " GROUP BY vid,seq";
|
||||
|
||||
// ------------------------------------------
|
||||
// Dump the history records
|
||||
// ------------------------------------------
|
||||
|
||||
rc = pool->custom_dump(oss, "HISTORY_RECORDS", cmd);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
failure_response(INTERNAL,request_error("Internal Error",""), att);
|
||||
return;
|
||||
}
|
||||
|
||||
success_response(oss.str(), att);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
void HostPoolInfo::request_execute(
|
||||
xmlrpc_c::paramList const& paramList,
|
||||
RequestAttributes& att)
|
||||
@ -141,19 +209,16 @@ void ClusterPoolInfo::request_execute(
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
void RequestManagerPoolInfoFilter::dump(
|
||||
void RequestManagerPoolInfoFilter::generate_where_string(
|
||||
RequestAttributes& att,
|
||||
int filter_flag,
|
||||
int start_id,
|
||||
int end_id,
|
||||
const string& and_clause,
|
||||
const string& or_clause)
|
||||
const string& or_clause,
|
||||
ostringstream& where_string)
|
||||
{
|
||||
set<int>::iterator it;
|
||||
|
||||
ostringstream oss;
|
||||
bool empty = true;
|
||||
ostringstream where_string;
|
||||
|
||||
ostringstream uid_filter;
|
||||
ostringstream id_filter;
|
||||
@ -162,16 +227,6 @@ void RequestManagerPoolInfoFilter::dump(
|
||||
string acl_str;
|
||||
string id_str;
|
||||
|
||||
int rc;
|
||||
|
||||
if ( filter_flag < MINE )
|
||||
{
|
||||
failure_response(XML_RPC_API,
|
||||
request_error("Incorrect filter_flag",""),
|
||||
att);
|
||||
return;
|
||||
}
|
||||
|
||||
Nebula& nd = Nebula::instance();
|
||||
AclManager* aclm = nd.get_aclm();
|
||||
bool all;
|
||||
@ -308,12 +363,39 @@ void RequestManagerPoolInfoFilter::dump(
|
||||
|
||||
where_string << "(" << or_clause << ")";
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
void RequestManagerPoolInfoFilter::dump(
|
||||
RequestAttributes& att,
|
||||
int filter_flag,
|
||||
int start_id,
|
||||
int end_id,
|
||||
const string& and_clause,
|
||||
const string& or_clause)
|
||||
{
|
||||
ostringstream oss;
|
||||
ostringstream where_string;
|
||||
int rc;
|
||||
|
||||
if ( filter_flag < MINE )
|
||||
{
|
||||
failure_response(XML_RPC_API,
|
||||
request_error("Incorrect filter_flag",""),
|
||||
att);
|
||||
return;
|
||||
}
|
||||
|
||||
generate_where_string(att, filter_flag, start_id, end_id,
|
||||
and_clause, or_clause, where_string);
|
||||
|
||||
// ------------------------------------------
|
||||
// Get the pool
|
||||
// ------------------------------------------
|
||||
|
||||
rc = pool->dump(oss,where_string.str());
|
||||
rc = pool->dump(oss, where_string.str());
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
|
@ -25,10 +25,11 @@
|
||||
|
||||
const char * History::table = "history";
|
||||
|
||||
const char * History::db_names = "vid, seq, body";
|
||||
const char * History::db_names = "vid, seq, body, stime, etime";
|
||||
|
||||
const char * History::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
|
||||
"history (vid INTEGER, seq INTEGER, body TEXT, PRIMARY KEY(vid,seq))";
|
||||
"history (vid INTEGER, seq INTEGER, body TEXT, "
|
||||
"stime INTEGER, etime INTEGER,PRIMARY KEY(vid,seq))";
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
@ -168,7 +169,9 @@ int History::insert_replace(SqlDB *db, bool replace)
|
||||
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
|
||||
<< oid << ","
|
||||
<< seq << ","
|
||||
<< "'" << sql_xml << "')";
|
||||
<< "'" << sql_xml << "',"
|
||||
<< stime << ","
|
||||
<< etime << ")";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
@ -264,6 +267,7 @@ string& History::to_xml(string& xml) const
|
||||
|
||||
oss <<
|
||||
"<HISTORY>" <<
|
||||
"<OID>" << oid << "</OID>" <<
|
||||
"<SEQ>" << seq << "</SEQ>" <<
|
||||
"<HOSTNAME>" << hostname << "</HOSTNAME>"<<
|
||||
"<HID>" << hid << "</HID>" <<
|
||||
|
Loading…
Reference in New Issue
Block a user