From 351ad626396f0f2d0767ba8a100dc09f6abbb3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 8 Mar 2011 18:06:15 +0100 Subject: [PATCH] Feature #407: Let vnets have duplicated names if they are owned by different users --- src/vnm/VirtualNetwork.cc | 2 +- src/vnm/VirtualNetworkPool.cc | 22 +++++++++++++++++++++- src/vnm/test/VirtualNetworkPoolTest.cc | 19 ++++++++++--------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 64ac8a3387..32beabb9e8 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -74,7 +74,7 @@ const char * VirtualNetwork::db_names = "oid, name, body, uid"; const char * VirtualNetwork::db_bootstrap = "CREATE TABLE IF NOT EXISTS" " network_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256)," - " body TEXT, uid INTEGER, UNIQUE(name))"; + " body TEXT, uid INTEGER, UNIQUE(name,uid))"; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index d0ddab04b3..854ec9f658 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -77,10 +77,30 @@ int VirtualNetworkPool::allocate ( string& error_str) { VirtualNetwork * vn; + VirtualNetwork * vn_aux; + string name; vn = new VirtualNetwork(uid, user_name, vn_template); - *oid = PoolSQL::allocate(vn, error_str); + // Check for duplicates + vn->get_template_attribute("NAME", name); + vn_aux = get(name,uid,false); + + if( vn_aux != 0 ) + { + ostringstream oss; + + oss << "NAME is already taken by NET " << vn_aux->get_oid() << "."; + error_str = oss.str(); + + *oid = -1; + + delete vn; + } + else + { + *oid = PoolSQL::allocate(vn, error_str); + } return *oid; } diff --git a/src/vnm/test/VirtualNetworkPoolTest.cc b/src/vnm/test/VirtualNetworkPoolTest.cc index f538064ec2..d3d6900b41 100644 --- a/src/vnm/test/VirtualNetworkPoolTest.cc +++ b/src/vnm/test/VirtualNetworkPoolTest.cc @@ -399,7 +399,7 @@ public: void duplicates() { - int rc, oid_0, oid_1; + int rc, oid_0, oid_1, oid_2, oid_3; VirtualNetworkPoolFriend * vnpool = static_cast(pool); VirtualNetwork * vnet; @@ -414,15 +414,15 @@ public: CPPUNIT_ASSERT( rc == oid_1 ); CPPUNIT_ASSERT( rc == -1 ); - // With different user ID - rc = vnpool->allocate(uids[1], user_names[1], templates[0], &oid_1); - CPPUNIT_ASSERT( rc == oid_1 ); - CPPUNIT_ASSERT( rc == -1 ); + // Same VNet, with different user ID + rc = vnpool->allocate(uids[1], user_names[1], templates[0], &oid_2); + CPPUNIT_ASSERT( rc == oid_2 ); + CPPUNIT_ASSERT( rc == 1 ); // Insert a different template, with the same user ID - rc = vnpool->allocate(uids[1], user_names[1], templates[1], &oid_1); - CPPUNIT_ASSERT( rc == oid_1 ); - CPPUNIT_ASSERT( rc == 1 ); + rc = vnpool->allocate(uids[1], user_names[1], templates[1], &oid_3); + CPPUNIT_ASSERT( rc == oid_3 ); + CPPUNIT_ASSERT( rc == 2 ); // Make sure the table contains only one vnet with name[0] @@ -434,8 +434,9 @@ public: ret = pool->search(results, table, where); CPPUNIT_ASSERT(ret == 0); - CPPUNIT_ASSERT(results.size() == 1); + CPPUNIT_ASSERT(results.size() == 2); CPPUNIT_ASSERT(results.at(0) == oid_0); + CPPUNIT_ASSERT(results.at(1) == oid_2); // Get the vnet and check it, to make sure the user id was not rewritten vnet = vnpool->get(oid_0, false);