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

Merge branch 'master' of git.opennebula.org:faf-71

This commit is contained in:
Carlos Martín 2014-01-22 13:21:59 +01:00
commit 09f204e242
16 changed files with 460 additions and 499 deletions

View File

@ -20,7 +20,7 @@
#include "PoolSQL.h"
#include "ObjectCollection.h"
#include "User.h"
#include "GroupQuotas.h"
#include "QuotasSQL.h"
using namespace std;
@ -55,7 +55,7 @@ public:
int from_xml(const string &xml_str);
/**
* Adds this user's ID to the set.
* Adds this user's ID to the set.
* @param id of the user to be added to the group
* @return 0 on success
*/

View File

@ -1,58 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#ifndef GROUP_QUOTAS_H_
#define GROUP_QUOTAS_H_
#include "Quotas.h"
class GroupQuotas : public Quotas
{
public:
GroupQuotas():Quotas(
"/QUOTAS/DATASTORE_QUOTA",
"/QUOTAS/NETWORK_QUOTA",
"/QUOTAS/IMAGE_QUOTA",
"/QUOTAS/VM_QUOTA"){};
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * db_table;
static const char * db_oid_column;
protected:
const char * table() const
{
return db_table;
};
const char * table_names() const
{
return db_names;
};
const char * table_oid_column() const
{
return db_oid_column;
};
};
#endif /*GROUP_QUOTAS_H_*/

View File

@ -21,29 +21,12 @@
#include "QuotaNetwork.h"
#include "QuotaVirtualMachine.h"
#include "QuotaImage.h"
#include "ObjectSQL.h"
class ObjectXML;
class Quotas : public ObjectSQL
class Quotas
{
public:
Quotas(const char * _ds_xpath,
const char * _net_xpath,
const char * _img_xpath,
const char * _vm_xpath):
datastore_quota(false),
network_quota(false),
image_quota(false),
vm_quota(false),
ds_xpath(_ds_xpath),
net_xpath(_net_xpath),
img_xpath(_img_xpath),
vm_xpath(_vm_xpath)
{};
virtual ~Quotas(){};
/**
* Different quota types
*/
@ -177,14 +160,6 @@ public:
*/
string& to_xml(string& xml) const;
/**
* Generates a string representation of the quotas in XML format, enclosed
* in the QUOTAS tag
* @param xml the string to store the XML
* @return the same xml string to use it in << compounds
*/
string& to_xml_db(string& xml) const;
/**
* Builds quota object from an ObjectXML
* @param object_xml pointer to the ObjectXML
@ -192,9 +167,6 @@ public:
*/
int from_xml(ObjectXML * object_xml);
// TODO: remove previous method, leave this one only
int from_xml(const string& xml);
/**
* Delete VM related usage (network, image and compute) from quota counters.
* for the given user and group
@ -229,51 +201,6 @@ public:
*/
static void quota_del(QuotaType type, int uid, int gid, Template * tmpl);
//--------------------------------------------------------------------------
// Database
//--------------------------------------------------------------------------
/**
* Reads the ObjectSQL (identified with its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select(SqlDB * db);
/**
* Writes the Quotas in the database.
* @param db pointer to the db
* @return 0 on success
*/
int insert(SqlDB *db, string& error_str)
{
return insert_replace(db, false, error_str);
};
/**
* Writes/updates the Quotas fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB *db)
{
string error_str;
return insert_replace(db, true, error_str);
}
/**
* Removes the Quotas from the database.
* @param db pointer to the db
* @return 0 on success
*/
int drop(SqlDB * db);
/**
* User/Group oid. Must be set before a DB write operation
*/
int oid;
protected:
/**
* This is an specialized constructor only for derived Quotas classes.
@ -284,7 +211,6 @@ protected:
const char * _img_xpath,
const char * _vm_xpath,
bool is_deafult):
oid(-1),
datastore_quota(is_deafult),
network_quota(is_deafult),
image_quota(is_deafult),
@ -292,23 +218,9 @@ protected:
ds_xpath(_ds_xpath),
net_xpath(_net_xpath),
img_xpath(_img_xpath),
vm_xpath(_vm_xpath)
{};
vm_xpath(_vm_xpath){};
virtual const char * table() const
{
return 0;
};
virtual const char * table_names() const
{
return 0;
};
virtual const char * table_oid_column() const
{
return 0;
};
virtual ~Quotas(){};
private:
//--------------------------------------------------------------------------
@ -358,28 +270,6 @@ private:
* Path for the vm quota object
*/
const char * vm_xpath;
//--------------------------------------------------------------------------
// Database
//--------------------------------------------------------------------------
/**
* Callback function to read a Quotas object (Quotas::select)
* @param num the number of columns read from the DB
* @para names the column names
* @para vaues the column values
* @return 0 on success
*/
int select_cb(void *nil, int num, char **values, char **names);
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @param error_str Returns the error reason, if any
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace, string& error_str);
};
#endif /*QUOTABLE_H_*/

228
include/QuotasSQL.h Normal file
View File

@ -0,0 +1,228 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#ifndef GROUP_QUOTAS_H_
#define GROUP_QUOTAS_H_
#include "Quotas.h"
#include "ObjectSQL.h"
class QuotasSQL : public Quotas, ObjectSQL
{
public:
/**
* Reads the ObjectSQL (identified with its OID) from the database.
* @param oid the Group/User oid
* @param db pointer to the db
* @return 0 on success
*/
int select(int _oid, SqlDB *db)
{
oid = _oid;
return select(db);
};
/**
* Writes the Quotas in the database.
* @param db pointer to the db
* @return 0 on success
*/
int insert(int _oid, SqlDB *db, string& error_str)
{
oid = _oid;
return insert(db, error_str);
};
/**
* Writes/updates the Quotas fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
int update(int _oid, SqlDB *db)
{
oid = _oid;
return update(db);
}
/**
* Removes the Quotas from the database.
* @param db pointer to the db
* @return 0 on success
*/
int drop(SqlDB * db);
protected:
QuotasSQL(const char * ds_xpath,
const char * net_xpath,
const char * img_xpath,
const char * vm_xpath):
Quotas(ds_xpath, net_xpath, img_xpath, vm_xpath, false),
ObjectSQL(),
oid(-1){};
virtual ~QuotasSQL(){};
virtual const char * table() const = 0;
virtual const char * table_names() const = 0;
virtual const char * table_oid_column() const = 0;
int from_xml(const string& xml);
private:
/**
* User/Group oid. Must be set before a DB write operation
*/
int oid;
/**
* Reads the ObjectSQL (identified with its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select(SqlDB * db);
/**
* Writes the Quotas in the database.
* @param db pointer to the db
* @return 0 on success
*/
int insert(SqlDB *db, string& error_str)
{
return insert_replace(db, false, error_str);
};
/**
* Writes/updates the Quotas fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB *db)
{
string error_str;
return insert_replace(db, true, error_str);
}
/**
* Callback function to read a Quotas object (Quotas::select)
* @param num the number of columns read from the DB
* @para names the column names
* @para vaues the column values
* @return 0 on success
*/
int select_cb(void *nil, int num, char **values, char **names);
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @param error_str Returns the error reason, if any
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace, string& error_str);
/**
* Generates a string representation of the quotas in XML format, enclosed
* in the QUOTAS tag
* @param xml the string to store the XML
* @return the same xml string to use it in << compounds
*/
string& to_xml_db(string& xml) const;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
class GroupQuotas : public QuotasSQL
{
public:
GroupQuotas():QuotasSQL(
"/QUOTAS/DATASTORE_QUOTA",
"/QUOTAS/NETWORK_QUOTA",
"/QUOTAS/IMAGE_QUOTA",
"/QUOTAS/VM_QUOTA"){};
virtual ~GroupQuotas(){};
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * db_table;
static const char * db_oid_column;
protected:
const char * table() const
{
return db_table;
};
const char * table_names() const
{
return db_names;
};
const char * table_oid_column() const
{
return db_oid_column;
};
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
class UserQuotas : public QuotasSQL
{
public:
UserQuotas():QuotasSQL(
"/QUOTAS/DATASTORE_QUOTA",
"/QUOTAS/NETWORK_QUOTA",
"/QUOTAS/IMAGE_QUOTA",
"/QUOTAS/VM_QUOTA"){};
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * db_table;
static const char * db_oid_column;
protected:
const char * table() const
{
return db_table;
};
const char * table_names() const
{
return db_names;
};
const char * table_oid_column() const
{
return db_oid_column;
};
};
#endif /*QUOTAS_SQL_H_*/

View File

@ -19,8 +19,10 @@
#include "PoolSQL.h"
#include "UserTemplate.h"
#include "UserQuotas.h"
#include "ObjectCollection.h"
#include "QuotasSQL.h"
class UserQuotas;
using namespace std;
@ -408,13 +410,6 @@ protected:
static const char * table;
/**
* Reads the User quotas from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select_quotas(SqlDB * db);
/**
* Writes the User in the database.
* @param db pointer to the db

View File

@ -1,58 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#ifndef USER_QUOTAS_H_
#define USER_QUOTAS_H_
#include "Quotas.h"
class UserQuotas : public Quotas
{
public:
UserQuotas():Quotas(
"/QUOTAS/DATASTORE_QUOTA",
"/QUOTAS/NETWORK_QUOTA",
"/QUOTAS/IMAGE_QUOTA",
"/QUOTAS/VM_QUOTA"){};
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * db_table;
static const char * db_oid_column;
protected:
const char * table() const
{
return db_table;
};
const char * table_names() const
{
return db_names;
};
const char * table_oid_column() const
{
return db_oid_column;
};
};
#endif /*USER_QUOTAS_H_*/

View File

@ -48,7 +48,7 @@ int Group::select(SqlDB * db)
return rc;
}
return select_quotas(db);
return quota.select(oid, db);
}
/* -------------------------------------------------------------------------- */
@ -64,15 +64,7 @@ int Group::select(SqlDB * db, const string& name, int uid)
return rc;
}
return select_quotas(db);
}
/* -------------------------------------------------------------------------- */
int Group::select_quotas(SqlDB * db)
{
quota.oid = oid;
return quota.select(db);
return quota.select(oid, db);
}
/* -------------------------------------------------------------------------- */
@ -101,11 +93,9 @@ int Group::insert(SqlDB *db, string& error_str)
rc = insert_replace(db, false, error_str);
quota.oid = oid;
if (rc == 0)
{
rc = quota.insert(db, error_str);
rc = quota.insert(oid, db, error_str);
}
return rc;
@ -121,11 +111,9 @@ int Group::update(SqlDB *db)
rc = insert_replace(db, true, error_str);
quota.oid = oid;
if (rc == 0)
{
rc = quota.update(db);
rc = quota.update(oid, db);
}
return rc;

View File

@ -1,31 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "GroupQuotas.h"
/* ************************************************************************** */
/* GroupQuotas :: Database Access Functions */
/* ************************************************************************** */
const char * GroupQuotas::db_table = "group_quotas";
const char * GroupQuotas::db_names = "group_oid, body";
const char * GroupQuotas::db_oid_column = "group_oid";
const char * GroupQuotas::db_bootstrap =
"CREATE TABLE IF NOT EXISTS group_quotas ("
"group_oid INTEGER PRIMARY KEY, body MEDIUMTEXT)";

View File

@ -23,8 +23,7 @@ lib_name='nebula_group'
# Sources to generate the library
source_files=[
'GroupPool.cc',
'Group.cc',
'GroupQuotas.cc'
'Group.cc'
]
# Build library

View File

@ -260,7 +260,7 @@ bool Request::basic_authorization(int oid,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
bool Request::user_quota_authorization (Template * tmpl,
bool Request::user_quota_authorization (Template * tmpl,
Quotas::QuotaType qtype,
RequestAttributes& att,
string& error_str)
@ -279,7 +279,7 @@ bool Request::user_quota_authorization (Template * tmpl,
return false;
}
Quotas default_user_quotas = nd.get_default_user_quota();
DefaultQuotas default_user_quotas = nd.get_default_user_quota();
rc = user->quota.quota_check(qtype, tmpl, default_user_quotas, error_str);
@ -304,7 +304,7 @@ bool Request::user_quota_authorization (Template * tmpl,
/* -------------------------------------------------------------------------- */
bool Request::group_quota_authorization (Template * tmpl,
bool Request::group_quota_authorization (Template * tmpl,
Quotas::QuotaType qtype,
RequestAttributes& att,
string& error_str)
@ -323,7 +323,7 @@ bool Request::group_quota_authorization (Template * tmpl,
return false;
}
Quotas default_group_quotas = nd.get_default_group_quota();
DefaultQuotas default_group_quotas = nd.get_default_group_quota();
rc = group->quota.quota_check(qtype, tmpl, default_group_quotas, error_str);
@ -348,7 +348,7 @@ bool Request::group_quota_authorization (Template * tmpl,
/* -------------------------------------------------------------------------- */
void Request::user_quota_rollback(Template * tmpl,
void Request::user_quota_rollback(Template * tmpl,
Quotas::QuotaType qtype,
RequestAttributes& att)
{
@ -373,7 +373,7 @@ void Request::user_quota_rollback(Template * tmpl,
/* -------------------------------------------------------------------------- */
void Request::group_quota_rollback(Template * tmpl,
void Request::group_quota_rollback(Template * tmpl,
Quotas::QuotaType qtype,
RequestAttributes& att)
{
@ -458,8 +458,8 @@ bool Request::quota_authorization(
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Request::quota_rollback(Template * tmpl,
Quotas::QuotaType qtype,
void Request::quota_rollback(Template * tmpl,
Quotas::QuotaType qtype,
RequestAttributes& att)
{
// uid/gid == -1 means do not update user/group
@ -480,7 +480,7 @@ void Request::quota_rollback(Template * tmpl,
void Request::failure_response(ErrorCode ec, const string& str_val,
RequestAttributes& att)
{
{
vector<xmlrpc_c::value> arrayData;
arrayData.push_back(xmlrpc_c::value_boolean(false));
@ -496,7 +496,7 @@ void Request::failure_response(ErrorCode ec, const string& str_val,
/* -------------------------------------------------------------------------- */
void Request::success_response(int id, RequestAttributes& att)
{
{
vector<xmlrpc_c::value> arrayData;
arrayData.push_back(xmlrpc_c::value_boolean(true));
@ -512,7 +512,7 @@ void Request::success_response(int id, RequestAttributes& att)
/* -------------------------------------------------------------------------- */
void Request::success_response(const string& val, RequestAttributes& att)
{
{
vector<xmlrpc_c::value> arrayData;
arrayData.push_back(xmlrpc_c::value_boolean(true));
@ -635,7 +635,7 @@ string Request::request_error (const string &err_desc, const string &err_detail)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string Request::allocate_error(PoolObjectSQL::ObjectType obj,
string Request::allocate_error(PoolObjectSQL::ObjectType obj,
const string& error)
{
ostringstream oss;

View File

@ -136,8 +136,8 @@ bool RequestManagerVirtualMachine::quota_resize_authorization(
UserPool* upool = nd.get_upool();
GroupPool* gpool = nd.get_gpool();
Quotas user_dquotas = nd.get_default_user_quota();
Quotas group_dquotas = nd.get_default_group_quota();
DefaultQuotas user_dquotas = nd.get_default_user_quota();
DefaultQuotas group_dquotas = nd.get_default_group_quota();
if (vm_perms.uid != UserPool::ONEADMIN_ID)
{

View File

@ -91,33 +91,6 @@ string& Quotas::to_xml(string& xml) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Quotas::to_xml_db(string& xml) const
{
ostringstream oss;
oss << "<QUOTAS>"
<< "<ID>" << oid << "</ID>"
<< to_xml(xml)
<< "</QUOTAS>";
xml = oss.str();
return xml;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Quotas::from_xml(const string& xml)
{
ObjectXML obj_xml(xml);
return from_xml(&obj_xml);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Quotas::from_xml(ObjectXML * object_xml)
{
vector<xmlNodePtr> content;
@ -162,8 +135,6 @@ int Quotas::from_xml(ObjectXML * object_xml)
object_xml->free_nodes(content);
object_xml->xpath(oid, "/QUOTAS/ID", -1);
return rc;
}
@ -310,123 +281,3 @@ void Quotas::quota_del(QuotaType type, int uid, int gid, Template * tmpl)
}
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Quotas::select(SqlDB * db)
{
ostringstream oss;
int rc;
set_callback(static_cast<Callbackable::Callback>(&Quotas::select_cb));
oss << "SELECT body FROM " << table()
<< " WHERE " << table_oid_column() << " = " << oid;
rc = db->exec(oss,this);
unset_callback();
if (rc != 0)
{
goto error_id;
}
return 0;
error_id:
oss.str("");
oss << "Error getting quotas for user/group " << oid;
NebulaLog::log("ONE", Log::ERROR, oss);
return -1;
}
/* -------------------------------------------------------------------------- */
int Quotas::select_cb(void *nil, int num, char **values, char **names)
{
if ( (!values[0]) || (num != 1) )
{
return -1;
}
return from_xml(values[0]);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Quotas::insert_replace(SqlDB *db, bool replace, string& error_str)
{
ostringstream oss;
int rc;
string xml_quota;
char * sql_quota_xml;
// Quota fields
sql_quota_xml = db->escape_str(to_xml_db(xml_quota).c_str());
if ( sql_quota_xml == 0 )
{
goto error_quota_body;
}
if ( ObjectXML::validate_xml(sql_quota_xml) != 0 )
{
goto error_quota_xml;
}
// Construct the SQL statement to Insert or Replace
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
oss << " INTO " << table() << " ("<< table_names() <<") VALUES ("
<< oid << ","
<< "'" << sql_quota_xml << "')";
rc = db->exec(oss);
db->free_str(sql_quota_xml);
return rc;
error_quota_xml:
db->free_str(sql_quota_xml);
goto error_common;
error_quota_body:
error_str = "Error transforming the Quotas to XML.";
goto error_common;
error_common:
error_str = "Error transforming the Quotas to XML.";
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Quotas::drop(SqlDB *db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table()
<< " WHERE " << table_oid_column() << " = " << oid;
rc = db->exec(oss);
return rc;
}

200
src/um/QuotasSQL.cc Normal file
View File

@ -0,0 +1,200 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "QuotasSQL.h"
#include "Nebula.h"
#include "ObjectXML.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& QuotasSQL::to_xml_db(string& xml) const
{
ostringstream oss;
oss << "<QUOTAS>"
<< "<ID>" << oid << "</ID>"
<< Quotas::to_xml(xml)
<< "</QUOTAS>";
xml = oss.str();
return xml;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int QuotasSQL::from_xml(const string& xml)
{
ObjectXML obj_xml(xml);
int rc = Quotas::from_xml(&obj_xml);
obj_xml.xpath(oid, "/QUOTAS/ID", -1);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int QuotasSQL::select(SqlDB * db)
{
ostringstream oss;
int rc;
set_callback(static_cast<Callbackable::Callback>(&QuotasSQL::select_cb));
oss << "SELECT body FROM " << table()
<< " WHERE " << table_oid_column() << " = " << oid;
rc = db->exec(oss,this);
unset_callback();
if (rc != 0)
{
goto error_id;
}
return 0;
error_id:
oss.str("");
oss << "Error getting quotas for user/group " << oid;
NebulaLog::log("ONE", Log::ERROR, oss);
return -1;
}
/* -------------------------------------------------------------------------- */
int QuotasSQL::select_cb(void *nil, int num, char **values, char **names)
{
if ( (!values[0]) || (num != 1) )
{
return -1;
}
return from_xml(values[0]);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int QuotasSQL::insert_replace(SqlDB *db, bool replace, string& error_str)
{
ostringstream oss;
int rc;
string xml_quota;
char * sql_quota_xml;
// Quota fields
sql_quota_xml = db->escape_str(to_xml_db(xml_quota).c_str());
if ( sql_quota_xml == 0 )
{
goto error_quota_body;
}
if ( ObjectXML::validate_xml(sql_quota_xml) != 0 )
{
goto error_quota_xml;
}
// Construct the SQL statement to Insert or Replace
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
oss << " INTO " << table() << " ("<< table_names() <<") VALUES ("
<< oid << ","
<< "'" << sql_quota_xml << "')";
rc = db->exec(oss);
db->free_str(sql_quota_xml);
return rc;
error_quota_xml:
db->free_str(sql_quota_xml);
goto error_common;
error_quota_body:
error_str = "Error transforming the Quotas to XML.";
goto error_common;
error_common:
error_str = "Error transforming the Quotas to XML.";
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int QuotasSQL::drop(SqlDB *db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table()
<< " WHERE " << table_oid_column() << " = " << oid;
rc = db->exec(oss);
return rc;
}
/* ************************************************************************** */
/* UserQuotas :: Database Access Functions */
/* ************************************************************************** */
const char * UserQuotas::db_table = "user_quotas";
const char * UserQuotas::db_names = "user_oid, body";
const char * UserQuotas::db_oid_column = "user_oid";
const char * UserQuotas::db_bootstrap =
"CREATE TABLE IF NOT EXISTS user_quotas ("
"user_oid INTEGER PRIMARY KEY, body MEDIUMTEXT)";
/* ************************************************************************** */
/* GroupQuotas :: Database Access Functions */
/* ************************************************************************** */
const char * GroupQuotas::db_table = "group_quotas";
const char * GroupQuotas::db_names = "group_oid, body";
const char * GroupQuotas::db_oid_column = "group_oid";
const char * GroupQuotas::db_bootstrap =
"CREATE TABLE IF NOT EXISTS group_quotas ("
"group_oid INTEGER PRIMARY KEY, body MEDIUMTEXT)";

View File

@ -31,7 +31,7 @@ source_files=[
'QuotaImage.cc',
'Quotas.cc',
'DefaultQuotas.cc',
'UserQuotas.cc'
'QuotasSQL.cc'
]
# Build library

View File

@ -58,7 +58,7 @@ int User::select(SqlDB * db)
return rc;
}
return select_quotas(db);
return quota.select(oid, db);
}
/* -------------------------------------------------------------------------- */
@ -74,15 +74,7 @@ int User::select(SqlDB * db, const string& name, int uid)
return rc;
}
return select_quotas(db);
}
/* -------------------------------------------------------------------------- */
int User::select_quotas(SqlDB * db)
{
quota.oid = oid;
return quota.select(db);
return quota.select(oid, db);
}
/* -------------------------------------------------------------------------- */
@ -111,11 +103,9 @@ int User::insert(SqlDB *db, string& error_str)
rc = insert_replace(db, false, error_str);
quota.oid = oid;
if (rc == 0)
{
rc = quota.insert(db, error_str);
rc = quota.insert(oid, db, error_str);
}
return rc;
@ -131,11 +121,9 @@ int User::update(SqlDB *db)
rc = insert_replace(db, true, error_str);
quota.oid = oid;
if (rc == 0)
{
rc = quota.update(db);
rc = quota.update(oid, db);
}
return rc;

View File

@ -1,31 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "UserQuotas.h"
/* ************************************************************************** */
/* UserQuotas :: Database Access Functions */
/* ************************************************************************** */
const char * UserQuotas::db_table = "user_quotas";
const char * UserQuotas::db_names = "user_oid, body";
const char * UserQuotas::db_oid_column = "user_oid";
const char * UserQuotas::db_bootstrap =
"CREATE TABLE IF NOT EXISTS user_quotas ("
"user_oid INTEGER PRIMARY KEY, body MEDIUMTEXT)";