diff --git a/include/DocumentPool.h b/include/DocumentPool.h index d08111713a..32b33b7c37 100644 --- a/include/DocumentPool.h +++ b/include/DocumentPool.h @@ -61,10 +61,10 @@ public: int * oid, std::string& error_str) { - *oid = PoolSQL::allocate( - new Document(-1, uid, gid, uname, gname, umask, type, - std::move(template_contents)), - error_str); + Document doc {-1, uid, gid, uname, gname, umask, type, + std::move(template_contents)}; + + *oid = PoolSQL::allocate(doc, error_str); return *oid; } diff --git a/include/GroupPool.h b/include/GroupPool.h index 06c72362c6..88550373d9 100644 --- a/include/GroupPool.h +++ b/include/GroupPool.h @@ -42,7 +42,7 @@ public: /** * Identifier for the oneadmin group */ - static const int ONEADMIN_ID; + static constexpr int ONEADMIN_ID = 0; /** * Default name for the users group @@ -52,7 +52,7 @@ public: /** * Identifier for the user group */ - static const int USERS_ID; + static constexpr int USERS_ID = 1; /* ---------------------------------------------------------------------- */ /* Methods for DB management */ diff --git a/include/PoolSQL.h b/include/PoolSQL.h index 96993b6dea..7038f38298 100644 --- a/include/PoolSQL.h +++ b/include/PoolSQL.h @@ -43,7 +43,7 @@ public: */ PoolSQL(SqlDB * _db, const char * _table); - virtual ~PoolSQL(); + virtual ~PoolSQL() = default; /** * Allocates a new object, writting it in the pool database. No memory is @@ -52,7 +52,7 @@ public: * @return the oid assigned to the object or -1 in case of failure */ virtual int allocate( - PoolObjectSQL *objsql, + PoolObjectSQL &objsql, std::string& error_str); /** diff --git a/include/RequestManagerAllocateDB.h b/include/RequestManagerAllocateDB.h index 3ddf46094f..8678393552 100644 --- a/include/RequestManagerAllocateDB.h +++ b/include/RequestManagerAllocateDB.h @@ -47,9 +47,9 @@ protected: return; } - PoolObjectSQL * obj = create(xml); + auto obj = std::unique_ptr(create(xml)); - int rc = pool->allocate(obj, att.resp_msg); + int rc = pool->allocate(*obj, att.resp_msg); if ( rc == -1 ) { diff --git a/include/VirtualRouterPool.h b/include/VirtualRouterPool.h index 85e1a7c2c6..3ba43f19df 100644 --- a/include/VirtualRouterPool.h +++ b/include/VirtualRouterPool.h @@ -55,9 +55,8 @@ public: int * oid, std::string& error_str) { - *oid = PoolSQL::allocate( - new VirtualRouter(-1, uid, gid, uname, gname, umask, move(template_contents)), - error_str); + VirtualRouter vr{-1, uid, gid, uname, gname, umask, move(template_contents)}; + *oid = PoolSQL::allocate(vr, error_str); return *oid; } diff --git a/src/cluster/ClusterPool.cc b/src/cluster/ClusterPool.cc index 5f1f1194dc..408f63d311 100644 --- a/src/cluster/ClusterPool.cc +++ b/src/cluster/ClusterPool.cc @@ -98,43 +98,34 @@ error_bootstrap: int ClusterPool::allocate(string name, int * oid, string& error_str) { - Cluster * cluster; - - ostringstream oss; - - int db_oid; + *oid = -1; // Check name if ( !PoolObjectSQL::name_is_valid(name, error_str) ) { - goto error_name; + return *oid; } // Check for duplicates - db_oid = exist(name); + const auto db_oid = exist(name); if( db_oid != -1 ) { - goto error_duplicated; + ostringstream oss; + + oss << "NAME is already taken by CLUSTER " << db_oid << "."; + error_str = oss.str(); + + return *oid; } // Build a new Cluster object - cluster = new Cluster(-1, name, 0, vnc_conf); + Cluster cluster {-1, name, 0, vnc_conf}; // Insert the Object in the pool *oid = PoolSQL::allocate(cluster, error_str); return *oid; - - -error_duplicated: - oss << "NAME is already taken by CLUSTER " << db_oid << "."; - error_str = oss.str(); - -error_name: - *oid = -1; - - return *oid; } /* -------------------------------------------------------------------------- */ diff --git a/src/datastore/Datastore.cc b/src/datastore/Datastore.cc index 809c3dbd03..a372b42723 100644 --- a/src/datastore/Datastore.cc +++ b/src/datastore/Datastore.cc @@ -16,7 +16,6 @@ #include "Datastore.h" #include "DatastorePool.h" -#include "GroupPool.h" #include "NebulaLog.h" #include "Nebula.h" #include "VirtualMachineDisk.h" diff --git a/src/datastore/DatastorePool.cc b/src/datastore/DatastorePool.cc index baddd1ad8e..84fe2d43fa 100644 --- a/src/datastore/DatastorePool.cc +++ b/src/datastore/DatastorePool.cc @@ -193,33 +193,34 @@ int DatastorePool::allocate( const set &cluster_ids, string& error_str) { - Datastore * ds; - - int db_oid; - string name; - - ostringstream oss; - - ds = new Datastore(uid, gid, uname, gname, umask, - move(ds_template), cluster_ids); + Datastore ds {uid, gid, uname, gname, umask, + move(ds_template), cluster_ids}; // ------------------------------------------------------------------------- // Check name & duplicates // ------------------------------------------------------------------------- - ds->get_template_attribute("NAME", name); + string name; + ds.get_template_attribute("NAME", name); + + *oid = -1; if ( !PoolObjectSQL::name_is_valid(name, error_str) ) { - goto error_name; + return *oid; } // Check for duplicates - db_oid = exist(name); + const auto db_oid = exist(name); if( db_oid != -1 ) { - goto error_duplicated; + ostringstream oss; + + oss << "NAME is already taken by DATASTORE " << db_oid << "."; + error_str = oss.str(); + + return *oid; } *oid = PoolSQL::allocate(ds, error_str); @@ -235,16 +236,6 @@ int DatastorePool::allocate( } } - return *oid; - -error_duplicated: - oss << "NAME is already taken by DATASTORE " << db_oid << "."; - error_str = oss.str(); - -error_name: - delete ds; - *oid = -1; - return *oid; } diff --git a/src/group/GroupPool.cc b/src/group/GroupPool.cc index e04461d757..e2e6625ebb 100644 --- a/src/group/GroupPool.cc +++ b/src/group/GroupPool.cc @@ -32,10 +32,7 @@ using namespace std; /* -------------------------------------------------------------------------- */ const string GroupPool::ONEADMIN_NAME = "oneadmin"; -const int GroupPool::ONEADMIN_ID = 0; - const string GroupPool::USERS_NAME = "users"; -const int GroupPool::USERS_ID = 1; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -44,9 +41,6 @@ GroupPool::GroupPool(SqlDB * db, bool is_slave, const vector& restricted_attrs) : PoolSQL(db, one_db::group_table) { - ostringstream oss; - string error_str; - //Federation slaves do not need to init the pool if (is_slave) { @@ -56,28 +50,36 @@ GroupPool::GroupPool(SqlDB * db, bool is_slave, //lastOID is set in PoolSQL::init_cb if (get_lastOID() == -1) { - int rc; - Group * group; - // Build the default oneadmins & users group - group = new Group(ONEADMIN_ID, ONEADMIN_NAME); + Group group_oneadmin {ONEADMIN_ID, ONEADMIN_NAME}; - rc = PoolSQL::allocate(group, error_str); + string error_str; + auto rc = PoolSQL::allocate(group_oneadmin, error_str); if (rc < 0) { - goto error_groups; + ostringstream oss; + + oss << "Error trying to create default group: " << error_str; + NebulaLog::log("GROUP", Log::ERROR, oss); + + throw runtime_error(oss.str()); } - group = new Group(USERS_ID, USERS_NAME); + Group group {USERS_ID, USERS_NAME}; - group->sunstone_views("cloud", "cloud", "groupadmin", "groupadmin,cloud"); + group.sunstone_views("cloud", "cloud", "groupadmin", "groupadmin,cloud"); rc = PoolSQL::allocate(group, error_str); if (rc < 0) { - goto error_groups; + ostringstream oss; + + oss << "Error trying to create default group: " << error_str; + NebulaLog::log("GROUP", Log::ERROR, oss); + + throw runtime_error(oss.str()); } set_lastOID(99); @@ -87,12 +89,6 @@ GroupPool::GroupPool(SqlDB * db, bool is_slave, GroupTemplate::parse_restricted(restricted_attrs); return; - -error_groups: - oss << "Error trying to create default group: " << error_str; - NebulaLog::log("GROUP", Log::ERROR, oss); - - throw runtime_error(oss.str()); } /* -------------------------------------------------------------------------- */ @@ -100,11 +96,7 @@ error_groups: int GroupPool::allocate(string name, int * oid, string& error_str) { - int db_oid; - - Group * group; - - ostringstream oss; + *oid = -1; if (Nebula::instance().is_federation_slave()) { @@ -112,38 +104,34 @@ int GroupPool::allocate(string name, int * oid, string& error_str) "GroupPool::allocate called, but this " "OpenNebula is a federation slave"); - return -1; + return *oid; } // Check name if ( !PoolObjectSQL::name_is_valid(name, error_str) ) { - goto error_name; + return *oid; } // Check for duplicates - db_oid = exist(name); + const auto db_oid = exist(name); if( db_oid != -1 ) { - goto error_duplicated; + ostringstream oss; + + oss << "NAME is already taken by GROUP " << db_oid << "."; + error_str = oss.str(); + + return *oid; } // Build a new Group object - group = new Group(-1, name); + Group group{-1, name}; // Insert the Object in the pool *oid = PoolSQL::allocate(group, error_str); - return *oid; - -error_duplicated: - oss << "NAME is already taken by GROUP " << db_oid << "."; - error_str = oss.str(); - -error_name: - *oid = -1; - return *oid; } diff --git a/src/hm/HookPool.cc b/src/hm/HookPool.cc index ecfb5d454b..3e31623d4f 100644 --- a/src/hm/HookPool.cc +++ b/src/hm/HookPool.cc @@ -23,45 +23,28 @@ using namespace std; int HookPool::allocate(unique_ptr