1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-04-01 06:50:25 +03:00

Feature #2632: Extract group quotas to another table

This commit is contained in:
Carlos Martín 2014-01-16 17:08:08 +01:00
parent d5d904b114
commit c2f1a6d1c4
7 changed files with 296 additions and 43 deletions

View File

@ -20,7 +20,7 @@
#include "PoolSQL.h"
#include "ObjectCollection.h"
#include "User.h"
#include "Quotas.h"
#include "GroupQuotas.h"
using namespace std;
@ -97,7 +97,7 @@ public:
/**
* Object quotas, provides set and check interface
*/
Quotas quota;
GroupQuotas quota;
private:
@ -114,10 +114,7 @@ private:
Group(int id, const string& name):
PoolObjectSQL(id,GROUP,name,-1,-1,"","",table),
ObjectCollection("USERS"),
quota("/GROUP/DATASTORE_QUOTA",
"/GROUP/NETWORK_QUOTA",
"/GROUP/IMAGE_QUOTA",
"/GROUP/VM_QUOTA")
quota()
{
// Allow users in this group to see it
group_u = 1;
@ -156,31 +153,61 @@ private:
*/
static int bootstrap(SqlDB * db)
{
ostringstream oss(Group::db_bootstrap);
int rc;
return db->exec(oss);
ostringstream oss_group(Group::db_bootstrap);
ostringstream oss_quota(GroupQuotas::db_bootstrap);
rc = db->exec(oss_group);
rc += db->exec(oss_quota);
return rc;
};
/**
* Reads the Group (identified with its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select(SqlDB * db);
/**
* Reads the Group (identified with its OID) from the database.
* @param db pointer to the db
* @param name of the group
* @param uid of the owner
*
* @return 0 on success
*/
int select(SqlDB * db, const string& name, int uid);
/**
* Reads the Group quotas from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select_quotas(SqlDB * db);
/**
* Drops the group from the database
* @param db pointer to the db
* @return 0 on success
*/
int drop(SqlDB *db);
/**
* Writes the Group 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);
}
int insert(SqlDB *db, string& error_str);
/**
* Writes/updates the Group's 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);
}
int update(SqlDB *db);
/**
* Function to print the Group object into a string in

View File

@ -150,20 +150,7 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "GROUP_POOL", Group::table, where);
};
protected:
/**
* Adds the default quotas xml element, right after all the
* pool objects
*
* @param oss The output stream to dump the xml contents
*/
virtual void add_extra_xml(ostringstream& oss);
int dump(ostringstream& oss, const string& where);
private:
@ -175,6 +162,15 @@ private:
{
return new Group(-1,"");
};
/**
* Callback function to get output in XML format
* @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 dump_cb(void * _oss, int num, char **values, char **names);
};
#endif /*GROUP_POOL_H_*/

58
include/GroupQuotas.h Normal file
View File

@ -0,0 +1,58 @@
/* -------------------------------------------------------------------------- */
/* 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

@ -37,6 +37,103 @@ const char * Group::db_bootstrap = "CREATE TABLE IF NOT EXISTS group_pool ("
/* Group :: Database Access Functions */
/* ************************************************************************ */
int Group::select(SqlDB * db)
{
int rc;
rc = PoolObjectSQL::select(db);
if ( rc != 0 )
{
return rc;
}
return select_quotas(db);
}
/* -------------------------------------------------------------------------- */
int Group::select(SqlDB * db, const string& name, int uid)
{
int rc;
rc = PoolObjectSQL::select(db,name,uid);
if ( rc != 0 )
{
return rc;
}
return select_quotas(db);
}
/* -------------------------------------------------------------------------- */
int Group::select_quotas(SqlDB * db)
{
quota.oid = oid;
return quota.select(db);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Group::drop(SqlDB * db)
{
int rc;
rc = PoolObjectSQL::drop(db);
if ( rc == 0 )
{
rc += quota.drop(db);
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Group::insert(SqlDB *db, string& error_str)
{
int rc;
rc = insert_replace(db, false, error_str);
quota.oid = oid;
if (rc == 0)
{
rc = quota.insert(db, error_str);
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Group::update(SqlDB *db)
{
int rc;
string error_str;
rc = insert_replace(db, true, error_str);
quota.oid = oid;
if (rc == 0)
{
rc = quota.update(db);
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Group::insert_replace(SqlDB *db, bool replace, string& error_str)
{
ostringstream oss;
@ -147,20 +244,16 @@ string& Group::to_xml_extended(string& xml, bool extended) const
{
ostringstream oss;
string collection_xml;
string quota_xml;
set<pair<int,int> >::const_iterator it;
ObjectCollection::to_xml(collection_xml);
quota.to_xml(quota_xml);
oss <<
"<GROUP>" <<
"<ID>" << oid << "</ID>" <<
"<NAME>" << name << "</NAME>" <<
collection_xml <<
quota_xml;
collection_xml;
for (it = providers.begin(); it != providers.end(); it++)
{
@ -173,8 +266,11 @@ string& Group::to_xml_extended(string& xml, bool extended) const
if (extended)
{
string quota_xml;
string def_quota_xml;
oss << Nebula::instance().get_default_group_quota().to_xml(def_quota_xml);
oss << quota.to_xml(quota_xml)
<< Nebula::instance().get_default_group_quota().to_xml(def_quota_xml);
}
oss << "</GROUP>";
@ -219,9 +315,6 @@ int Group::from_xml(const string& xml)
ObjectXML::free_nodes(content);
content.clear();
// Quotas
rc += quota.from_xml(this);
// Set of resource providers
ObjectXML::get_nodes("/GROUP/RESOURCE_PROVIDER", content);

View File

@ -169,10 +169,57 @@ int GroupPool::drop(PoolObjectSQL * objsql, string& error_msg)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void GroupPool::add_extra_xml(ostringstream& oss)
int GroupPool::dump(ostringstream& oss, const string& where)
{
string def_quota_xml;
int rc;
string def_quota_xml;
ostringstream cmd;
cmd << "SELECT " << Group::table << ".body, "
<< GroupQuotas::db_table << ".body"
<< " FROM " << Group::table << " LEFT JOIN " << GroupQuotas::db_table
<< " ON " << Group::table << ".oid=" << GroupQuotas::db_table << ".group_oid";
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
cmd << " ORDER BY oid";
oss << "<GROUP_POOL>";
set_callback(static_cast<Callbackable::Callback>(&GroupPool::dump_cb),
static_cast<void *>(&oss));
rc = db->exec(cmd, this);
unset_callback();
oss << Nebula::instance().get_default_group_quota().to_xml(def_quota_xml);
oss << "</GROUP_POOL>";
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int GroupPool::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
if ( (!values[0]) || (num != 2) )
{
return -1;
}
*oss << values[0] << values[1];
return 0;
}
/* -------------------------------------------------------------------------- */

31
src/group/GroupQuotas.cc Normal file
View File

@ -0,0 +1,31 @@
/* -------------------------------------------------------------------------- */
/* 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,7 +23,8 @@ lib_name='nebula_group'
# Sources to generate the library
source_files=[
'GroupPool.cc',
'Group.cc'
'Group.cc',
'GroupQuotas.cc'
]
# Build library