From 32e10726d45429eb3e35e95a409d7d2bf04f40d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 12 Jul 2011 19:30:00 +0200 Subject: [PATCH] Bug: Image and VNet creation with an already used name reported a DB error, instead of a nice reason like Templates --- include/ImagePool.h | 14 ++++++++++++ include/VirtualNetworkPool.h | 14 ++++++++++++ src/image/ImagePool.cc | 16 ++++++++++++++ src/vnm/VirtualNetworkPool.cc | 41 +++++++++++++++++++++++++++++++++-- 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/include/ImagePool.h b/include/ImagePool.h index 1162653c09..d69a2f3a60 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -78,6 +78,20 @@ public: return static_cast(PoolSQL::get(oid,lock)); }; + /** + * Gets an object from the pool (if needed the object is loaded from the + * database). + * @param name of the object + * @param uid id of owner + * @param lock locks the object if true + * + * @return a pointer to the object, 0 in case of failure + */ + Image * get(const string& name, int uid, bool lock) + { + return static_cast(PoolSQL::get(name,uid,lock)); + }; + /** * Update a particular Image * @param image pointer to Image diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index 776297601b..e77438acd0 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -69,6 +69,20 @@ public: return static_cast(PoolSQL::get(oid,lock)); }; + /** + * Gets an object from the pool (if needed the object is loaded from the + * database). + * @param name of the object + * @param uid id of owner + * @param lock locks the object if true + * + * @return a pointer to the object, 0 in case of failure + */ + VirtualNetwork * get(const string& name, int uid, bool lock) + { + return static_cast(PoolSQL::get(name,uid,lock)); + }; + //-------------------------------------------------------------------------- // Virtual Network DB access functions //-------------------------------------------------------------------------- diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 5dfb6c6915..bafdfd4abb 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -64,6 +64,7 @@ int ImagePool::allocate ( string& error_str) { Image * img; + Image * img_aux = 0; string name; ostringstream oss; @@ -77,6 +78,14 @@ int ImagePool::allocate ( goto error_name; } + // Check for duplicates + img_aux = get(name,uid,false); + + if( img_aux != 0 ) + { + goto error_duplicated; + } + // --------------------------------------------------------------------- // Insert the Object in the pool & Register the image in the repository // --------------------------------------------------------------------- @@ -100,6 +109,13 @@ int ImagePool::allocate ( error_name: oss << "NAME cannot be empty."; + goto error_common; + +error_duplicated: + oss << "NAME is already taken by IMAGE " + << img_aux->get_oid() << "."; + +error_common: delete img; *oid = -1; diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index 30f5350c37..eaa5294079 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -78,13 +78,50 @@ int VirtualNetworkPool::allocate ( int * oid, string& error_str) { - VirtualNetwork * vn; - string name; + VirtualNetwork * vn; + VirtualNetwork * vn_aux = 0; + string name; + ostringstream oss; + + vn = new VirtualNetwork(uid, gid, uname, gname, vn_template); + + // Check name + vn->get_template_attribute("NAME", name); + + if ( name.empty() ) + { + goto error_name; + } + + // Check for duplicates + vn_aux = get(name,uid,false); + + if( vn_aux != 0 ) + { + goto error_duplicated; + } vn = new VirtualNetwork(uid, gid, uname, gname, vn_template); *oid = PoolSQL::allocate(vn, error_str); + return *oid; + +error_name: + oss << "NAME cannot be empty."; + + goto error_common; + +error_duplicated: + oss << "NAME is already taken by NET " + << vn_aux->get_oid() << "."; + +error_common: + delete vn; + + *oid = -1; + error_str = oss.str(); + return *oid; }