mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-17 18:33:32 +09:00
Feature #1611: Add persistence to default quotas, in a new DB table
This commit is contained in:
1 parent
dad6caf0ca
commit
855d34f912
4 files changed
+223
-7
No files matched your search
+87
-5
@@ -18,8 +18,10 @@
|
||||
#define DEFAULT_QUOTAS_H_
|
||||
|
||||
#include "Quotas.h"
|
||||
#include "SqlDB.h"
|
||||
#include "ObjectSQL.h"
|
||||
|
||||
class DefaultQuotas : public Quotas
|
||||
class DefaultQuotas : public Quotas, ObjectSQL
|
||||
{
|
||||
public:
|
||||
DefaultQuotas(
|
||||
@@ -54,14 +56,94 @@ public:
|
||||
string& to_xml(string& xml) const;
|
||||
|
||||
/**
|
||||
* Builds quota object from an ObjectXML
|
||||
* @param xml The xml-formatted string
|
||||
* @return 0 if success
|
||||
* Writes the quotas in the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
int from_xml(const string& xml);
|
||||
int insert(SqlDB *db, string& error_str)
|
||||
{
|
||||
return insert_replace(db, false, error_str);
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes/updates the quotas data 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);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads the Quotas from the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
int select(SqlDB *db);
|
||||
|
||||
/**
|
||||
* Bootstraps the database table(s) associated to the default quotas
|
||||
* @return 0 on success
|
||||
*/
|
||||
static int bootstrap(SqlDB * db);
|
||||
|
||||
private:
|
||||
const char * root_elem;
|
||||
|
||||
// *************************************************************************
|
||||
// DataBase implementation
|
||||
// *************************************************************************
|
||||
|
||||
static const char * db_names;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
|
||||
static const char * table;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Callback function to unmarshall a PoolObjectSQL
|
||||
* @param num the number of columns read from the DB
|
||||
* @param names the column names
|
||||
* @param vaues the column values
|
||||
* @return 0 on success
|
||||
*/
|
||||
int select_cb(void *nil, int num, char **values, char **names)
|
||||
{
|
||||
if ( (!values[0]) || (num != 1) )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return from_xml(values[0]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds quota object from an ObjectXML
|
||||
* @param xml The xml-formatted string
|
||||
* @return 0 on success
|
||||
*/
|
||||
int from_xml(const string& xml);
|
||||
|
||||
/**
|
||||
* Removes the Quotas from the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
int drop(SqlDB * db)
|
||||
{
|
||||
return -1; // Not supported
|
||||
};
|
||||
};
|
||||
|
||||
#endif /*DEFAULT_QUOTAS_H_*/
|
||||
+18
-2
@@ -320,7 +320,15 @@ public:
|
||||
|
||||
int set_default_user_quota(Template *tmpl, string& error)
|
||||
{
|
||||
return default_user_quota.set(tmpl, error);
|
||||
int rc;
|
||||
rc = default_user_quota.set(tmpl, error);
|
||||
|
||||
if ( rc == 0 )
|
||||
{
|
||||
rc = default_user_quota.update(db);
|
||||
}
|
||||
|
||||
return rc;
|
||||
};
|
||||
|
||||
const DefaultQuotas& get_default_group_quota()
|
||||
@@ -330,7 +338,15 @@ public:
|
||||
|
||||
int set_default_group_quota(Template *tmpl, string& error)
|
||||
{
|
||||
return default_group_quota.set(tmpl, error);
|
||||
int rc;
|
||||
rc = default_group_quota.set(tmpl, error);
|
||||
|
||||
if ( rc == 0 )
|
||||
{
|
||||
rc = default_group_quota.update(db);
|
||||
}
|
||||
|
||||
return rc;
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
@@ -245,6 +245,8 @@ void Nebula::start()
|
||||
rc += ClusterPool::bootstrap(db);
|
||||
rc += DocumentPool::bootstrap(db);
|
||||
|
||||
rc += DefaultQuotas::bootstrap(db);
|
||||
|
||||
// Create the versioning table only if bootstrap went well
|
||||
if ( rc == 0 )
|
||||
{
|
||||
@@ -340,6 +342,9 @@ void Nebula::start()
|
||||
tpool = new VMTemplatePool(db);
|
||||
|
||||
dspool = new DatastorePool(db);
|
||||
|
||||
default_user_quota.select(db);
|
||||
default_group_quota.select(db);
|
||||
}
|
||||
catch (exception&)
|
||||
{
|
||||
@@ -681,6 +686,7 @@ int Nebula::bootstrap()
|
||||
{
|
||||
int rc;
|
||||
ostringstream oss;
|
||||
string error;
|
||||
|
||||
oss << "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, "
|
||||
"last_oid BIGINT UNSIGNED)";
|
||||
@@ -700,6 +706,9 @@ int Nebula::bootstrap()
|
||||
|
||||
rc += db->exec(oss);
|
||||
|
||||
rc += default_user_quota.insert(db, error);
|
||||
rc += default_group_quota.insert(db, error);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,15 @@
|
||||
#include "DefaultQuotas.h"
|
||||
#include "ObjectXML.h"
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
const char * DefaultQuotas::table = "system_attributes";
|
||||
const char * DefaultQuotas::db_names = "name, body";
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
string& DefaultQuotas::to_xml(string& xml) const
|
||||
{
|
||||
ostringstream oss;
|
||||
@@ -91,3 +100,103 @@ int DefaultQuotas::from_xml(const string& xml)
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int DefaultQuotas::select(SqlDB *db)
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
int rc;
|
||||
|
||||
set_callback(
|
||||
static_cast<Callbackable::Callback>(&DefaultQuotas::select_cb));
|
||||
|
||||
oss << "SELECT body FROM " << table << " WHERE name = '" << root_elem << "'";
|
||||
|
||||
rc = db->exec(oss, this);
|
||||
|
||||
unset_callback();
|
||||
|
||||
if (rc != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int DefaultQuotas::insert_replace(SqlDB *db, bool replace, string& error_str)
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
int rc;
|
||||
string xml_body;
|
||||
|
||||
char * sql_xml;
|
||||
|
||||
sql_xml = db->escape_str(to_xml(xml_body).c_str());
|
||||
|
||||
if ( sql_xml == 0 )
|
||||
{
|
||||
goto error_body;
|
||||
}
|
||||
/*
|
||||
if ( validate_xml(sql_xml) != 0 )
|
||||
{
|
||||
goto error_xml;
|
||||
}
|
||||
*/
|
||||
if ( replace )
|
||||
{
|
||||
oss << "REPLACE";
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << "INSERT";
|
||||
}
|
||||
|
||||
// Construct the SQL statement to Insert or Replace
|
||||
|
||||
oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
|
||||
<< "'" << root_elem << "',"
|
||||
<< "'" << sql_xml << "')";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
db->free_str(sql_xml);
|
||||
|
||||
return rc;
|
||||
/*
|
||||
error_xml:
|
||||
db->free_str(sql_xml);
|
||||
|
||||
error_str = "Error transforming the Quotas to XML.";
|
||||
|
||||
goto error_common;
|
||||
*/
|
||||
error_body:
|
||||
goto error_generic;
|
||||
|
||||
error_generic:
|
||||
error_str = "Error inserting Quotas in DB.";
|
||||
error_common:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int DefaultQuotas::bootstrap(SqlDB * db)
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
oss << "CREATE TABLE IF NOT EXISTS system_attributes ("
|
||||
"name VARCHAR(128) PRIMARY KEY, body TEXT)";
|
||||
|
||||
return db->exec(oss);
|
||||
}
|
||||
Reference in new issue
Block a user