diff --git a/include/HostPool.h b/include/HostPool.h index d9cde67bb0..834b2b52fa 100644 --- a/include/HostPool.h +++ b/include/HostPool.h @@ -67,7 +67,19 @@ public: { return static_cast(PoolSQL::get(oid,lock)); }; - + + /** + * Function to get a Host from the pool, if the object is not in memory + * it is loaded from the DB + * @param hostname + * @param lock locks the Host mutex + * @return a pointer to the Host, 0 if the Host could not be loaded + */ + Host * get(string name, bool lock) + { + return static_cast(PoolSQL::get(name,-1,lock)); + }; + /** * Bootstraps the database table(s) associated to the Host pool */ diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index 9773408871..6df3fb827a 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -144,18 +144,32 @@ int HostPool::allocate ( { Host * host; - // Build a new Host object + host = get(hostname,false); - host = new Host(-1, - hostname, - im_mad_name, - vmm_mad_name, - tm_mad_name, - ClusterPool::DEFAULT_CLUSTER_NAME); + if ( host !=0) + { + ostringstream oss; - // Insert the Object in the pool + oss << "NAME is already taken by HOST " << host->get_oid() << "."; + error_str = oss.str(); - *oid = PoolSQL::allocate(host, error_str); + *oid = -1; + } + else + { + // Build a new Host object + + host = new Host(-1, + hostname, + im_mad_name, + vmm_mad_name, + tm_mad_name, + ClusterPool::DEFAULT_CLUSTER_NAME); + + // Insert the Object in the pool + + *oid = PoolSQL::allocate(host, error_str); + } return *oid; } diff --git a/src/vnm/test/VirtualNetworkPoolTest.cc b/src/vnm/test/VirtualNetworkPoolTest.cc index d3d6900b41..93340f9432 100644 --- a/src/vnm/test/VirtualNetworkPoolTest.cc +++ b/src/vnm/test/VirtualNetworkPoolTest.cc @@ -409,12 +409,12 @@ public: CPPUNIT_ASSERT( rc == oid_0 ); CPPUNIT_ASSERT( rc == 0 ); - // Allocate the same vnet twice, with the same user ID + // Allocate the same vnet twice, with the same user ID. Should fail rc = vnpool->allocate(uids[0], user_names[0], templates[0], &oid_1); CPPUNIT_ASSERT( rc == oid_1 ); CPPUNIT_ASSERT( rc == -1 ); - // Same VNet, with different user ID + // Same VNet, with different user ID. Should succeed rc = vnpool->allocate(uids[1], user_names[1], templates[0], &oid_2); CPPUNIT_ASSERT( rc == oid_2 ); CPPUNIT_ASSERT( rc == 1 ); @@ -425,7 +425,7 @@ public: CPPUNIT_ASSERT( rc == 2 ); - // Make sure the table contains only one vnet with name[0] + // Make sure the table contains two vnets with name[0] vector results; int ret; const char * table = "network_pool";