1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-22 22:03:39 +03:00

Feature #407: Create default groups 'oneadmin' and 'users'. Make regular groups start from ID 100. Fix tests.

Feature #591: Add group_pool table to the DB migration script.
This commit is contained in:
Carlos Martín 2011-05-13 12:51:23 +02:00
parent e9b0bf3eea
commit 211eed3289
7 changed files with 76 additions and 33 deletions

View File

@ -31,9 +31,10 @@ public:
~GroupPool(){};
/**
* Cluster name for the default cluster
* Names for the default groups
*/
static const string DEFAULT_GROUP_NAME;
static const string ONEADMIN_GROUP_NAME;
static const string USERS_GROUP_NAME;
/* ---------------------------------------------------------------------- */
/* Methods for DB management */

View File

@ -164,6 +164,12 @@ protected:
int dump(ostringstream& oss, const string& elem_name,
const char * table, const string& where);
/**
* Last object ID assigned to an object. It must be initialized by the
* target pool.
*/
int lastOID;
/**
* Returns the value of the last identifier assigned by the pool
*/
@ -172,6 +178,11 @@ protected:
return lastOID;
};
/**
* Inserts the last oid into the pool_control table
*/
void update_lastOID();
private:
pthread_mutex_t mutex;
@ -183,12 +194,6 @@ private:
*/
static const unsigned int MAX_POOL_SIZE;
/**
* Last object ID assigned to an object. It must be initialized by the
* target pool.
*/
int lastOID;
/**
* Tablename for this pool
*/
@ -258,11 +263,6 @@ private:
return key.str();
};
/**
* Inserts the last oid into the pool_control table
*/
void update_lastOID();
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */

View File

@ -50,7 +50,7 @@ protected:
need_im(false), need_tm(false),
need_lcm(false), need_dm(false),
need_rm(false), need_hm(false),
need_authm(false)
need_authm(false), need_imagem(false)
{};
virtual ~NebulaTest(){};

View File

@ -20,7 +20,8 @@
#include <stdexcept>
const string GroupPool::DEFAULT_GROUP_NAME = "default";
const string GroupPool::ONEADMIN_GROUP_NAME = "oneadmin";
const string GroupPool::USERS_GROUP_NAME = "users";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -34,13 +35,13 @@ GroupPool::GroupPool(SqlDB * db):PoolSQL(db, Group::table)
Group * group;
string error_str;
// Build a new Group object
group = new Group(0, 0, GroupPool::DEFAULT_GROUP_NAME);
// Build the default groups
group = new Group(0, 0, GroupPool::ONEADMIN_GROUP_NAME);
// Insert the Object in the pool
rc = PoolSQL::allocate(group, error_str);
if(rc != 0)
if(rc < 0)
{
ostringstream oss;
@ -49,6 +50,26 @@ GroupPool::GroupPool(SqlDB * db):PoolSQL(db, Group::table)
throw runtime_error(oss.str());
}
group = new Group(1, 0, GroupPool::USERS_GROUP_NAME);
// Insert the Object in the pool
rc = PoolSQL::allocate(group, error_str);
if(rc < 0)
{
ostringstream oss;
oss << "Error trying to create default group: " << error_str;
NebulaLog::log("GROUP",Log::ERROR,oss);
throw runtime_error(oss.str());
}
// First 100 group Ids are reserved for system groups.
// Regular ones start from ID 100
lastOID=99;
update_lastOID();
}
}
@ -103,11 +124,11 @@ int GroupPool::drop(Group * group)
{
int rc;
// Return error if group is 'default'
if( group->get_oid() == 0 )
// Return error if the group is a default one.
if( group->get_oid() < 100 )
{
NebulaLog::log("GROUP",Log::WARNING,
"Default group cannot be deleted.");
NebulaLog::log("GROUP",Log::ERROR,
"Groups with ID less than 100 cannot be deleted.");
return -1;
}

View File

@ -28,13 +28,12 @@ const string names[] = {"First name", "Second name"};
const string xmls[] =
{
"<GROUP><ID>1</ID><UID>0</UID><NAME>First name</NAME></GROUP>",
"<GROUP><ID>2</ID><UID>1</UID><NAME>Second name</NAME></GROUP>"
"<GROUP><ID>100</ID><UID>0</UID><NAME>First name</NAME></GROUP>",
"<GROUP><ID>101</ID><UID>1</UID><NAME>Second name</NAME></GROUP>"
};
const string group_xml_dump =
"<GROUP_POOL><GROUP><ID>0</ID><UID>0</UID><NAME>default</NAME></GROUP><GROUP><ID>1</ID><UID>5</UID><NAME>group_a</NAME></GROUP><GROUP><ID>3</ID><UID>5</UID><NAME>group_c</NAME></GROUP><GROUP><ID>4</ID><UID>5</UID><NAME>group_d</NAME></GROUP></GROUP_POOL>";
"<GROUP_POOL><GROUP><ID>0</ID><UID>0</UID><NAME>oneadmin</NAME></GROUP><GROUP><ID>1</ID><UID>0</UID><NAME>users</NAME></GROUP><GROUP><ID>100</ID><UID>5</UID><NAME>group_a</NAME></GROUP><GROUP><ID>102</ID><UID>5</UID><NAME>group_c</NAME></GROUP><GROUP><ID>103</ID><UID>5</UID><NAME>group_d</NAME></GROUP></GROUP_POOL>";
/* ************************************************************************* */
/* ************************************************************************* */
@ -57,7 +56,7 @@ class GroupPoolTest : public PoolTest
CPPUNIT_TEST_SUITE (GroupPoolTest);
// Not all tests from PoolTest can be used. Because
// of the initial default group added to the DB, the
// of the initial default groups added to the DB, the
// oid_assignment would fail.
CPPUNIT_TEST (get_from_cache);
CPPUNIT_TEST (get_from_db);
@ -166,7 +165,7 @@ public:
// Allocate a group
rc = gpool->allocate(uids[0], names[0], &oid, err);
CPPUNIT_ASSERT( oid == 1 );
CPPUNIT_ASSERT( oid == 100 );
CPPUNIT_ASSERT( oid == rc );
// Try to allocate twice the same group, should fail
@ -192,19 +191,19 @@ public:
// Allocate some groups
rc = gpool->allocate(5, "group_a", &oid, err);
CPPUNIT_ASSERT( rc == 1 );
CPPUNIT_ASSERT( rc == 100 );
rc = gpool->allocate(5, "group_b", &oid, err);
CPPUNIT_ASSERT( rc == 2 );
CPPUNIT_ASSERT( rc == 101 );
rc = gpool->allocate(5, "group_c", &oid, err);
CPPUNIT_ASSERT( rc == 3 );
CPPUNIT_ASSERT( rc == 102 );
rc = gpool->allocate(5, "group_d", &oid, err);
CPPUNIT_ASSERT( rc == 4 );
CPPUNIT_ASSERT( rc == 103 );
// Drop one of them
group = gpool->get(2, false);
group = gpool->get(101, false);
CPPUNIT_ASSERT( group != 0 );
rc = gpool->drop(group);

View File

@ -255,6 +255,11 @@ class Migrator < MigratorBase
@db.run "CREATE TABLE db_versioning (oid INTEGER PRIMARY KEY, version INTEGER, timestamp INTEGER, comment VARCHAR(256));"
@db.run "CREATE TABLE template_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, public INTEGER);"
# The group pool has two default ones
@db.run "CREATE TABLE group_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, UNIQUE(name));"
@db.run "INSERT INTO group_pool VALUES(0,'oneadmin','<GROUP><ID>0</ID><UID>0</UID><NAME>oneadmin</NAME></GROUP>',0);"
@db.run "INSERT INTO group_pool VALUES(1,'users','<GROUP><ID>1</ID><UID>0</UID><NAME>users</NAME></GROUP>',0);"
# New pool_control table contains the last_oid used, must be rebuilt
@db.run "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, last_oid BIGINT UNSIGNED)"
@ -266,6 +271,10 @@ class Migrator < MigratorBase
end
end
# First 100 group Ids are reserved for system groups.
# Regular ones start from ID 100
@db.run "INSERT INTO pool_control (tablename, last_oid) VALUES ('group_pool', 99);"
return true
end

View File

@ -170,6 +170,7 @@ void Nebula::start()
NebulaLog::log("ONE",Log::INFO,"Bootstraping OpenNebula database.");
bootstrap();
VirtualMachinePool::bootstrap(db);
HostPool::bootstrap(db);
VirtualNetworkPool::bootstrap(db);
@ -498,3 +499,15 @@ void Nebula::start()
authm->load_mads(0);
}
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Nebula::bootstrap()
{
ostringstream oss;
oss << "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, "
"last_oid BIGINT UNSIGNED)";
db->exec(oss);
}