From 1e1cb17e3a4ad532b60738793e69551fb080cdd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 21 Mar 2011 19:01:40 +0100 Subject: [PATCH] Features #539, #527, #407: Basic consistency checks for User, Cluster and VNet creation --- src/cluster/ClusterPool.cc | 47 +++++++++++++++++++++++--------------- src/um/UserPool.cc | 41 +++++++++++++++++++++------------ src/vnm/VirtualNetwork.cc | 26 +++++++++++++-------- 3 files changed, 72 insertions(+), 42 deletions(-) diff --git a/src/cluster/ClusterPool.cc b/src/cluster/ClusterPool.cc index a5846b2486..ef1280e8ef 100644 --- a/src/cluster/ClusterPool.cc +++ b/src/cluster/ClusterPool.cc @@ -57,30 +57,41 @@ ClusterPool::ClusterPool(SqlDB * db):PoolSQL(db, Cluster::table) int ClusterPool::allocate(int * oid, string name, string& error_str) { - Cluster * cluster; - Cluster * cluster_aux; + Cluster * cluster; + ostringstream oss; + + if ( name.empty() ) + { + goto error_name; + } // Check for duplicates - cluster_aux = get(name, false); + cluster = get(name, false); - if( cluster_aux != 0 ) + if( cluster != 0 ) { - ostringstream oss; - - oss << "NAME is already taken by CLUSTER " - << cluster_aux->get_oid() << "."; - error_str = oss.str(); - - *oid = -1; + goto error_duplicated; } - else - { - // Build a new Cluster object - cluster = new Cluster(-1, name); - // Insert the Object in the pool - *oid = PoolSQL::allocate(cluster, error_str); - } + // Build a new Cluster object + cluster = new Cluster(-1, name); + + // Insert the Object in the pool + *oid = PoolSQL::allocate(cluster, error_str); + + return *oid; + + +error_name: + oss << "NAME cannot be empty."; + goto error_common; + +error_duplicated: + oss << "NAME is already taken by CLUSTER " << cluster->get_oid() << "."; + +error_common: + *oid = -1; + error_str = oss.str(); return *oid; } diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index 99c814c297..f21ee4efe1 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -120,26 +120,39 @@ int UserPool::allocate ( string& error_str) { User * user; + ostringstream oss; + + if ( username.empty() ) + { + goto error_name; + } user = get(username,false); - if ( user !=0) + if ( user !=0 ) { - ostringstream oss; - - oss << "NAME is already taken by USER " << user->get_oid() << "."; - error_str = oss.str(); - - *oid = -1; + goto error_duplicated; } - else - { - // Build a new User object - user = new User(-1, username, password, enabled); - // Insert the Object in the pool - *oid = PoolSQL::allocate(user, error_str); - } + // Build a new User object + user = new User(-1, username, password, enabled); + + // Insert the Object in the pool + *oid = PoolSQL::allocate(user, error_str); + + return *oid; + + +error_name: + oss << "NAME cannot be empty."; + goto error_common; + +error_duplicated: + oss << "NAME is already taken by USER " << user->get_oid() << "."; + +error_common: + *oid = -1; + error_str = oss.str(); return *oid; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 7dd18572c8..6280f7ae58 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -228,9 +228,13 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) { type = VirtualNetwork::FIXED; } + else if ( s_type.empty() ) + { + goto error_type_defined; + } else { - goto error_type; + goto error_wrong_type; } // ------------ NAME ---------------------- @@ -343,32 +347,34 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) return 0; -error_type: - ose << "Wrong type in template for Virtual Network, id: "; +error_type_defined: + ose << "No TYPE in template for Virtual Network."; + goto error_common; + +error_wrong_type: + ose << "Wrong type \""<< s_type <<"\" in template for Virtual Network."; goto error_common; error_name: - ose << "No NAME in template for Virtual Network, id: "; + ose << "No NAME in template for Virtual Network."; goto error_common; error_bridge: - ose << "No BRIDGE in template for Virtual Network, id: "; + ose << "No BRIDGE in template for Virtual Network."; goto error_common; error_update: - ose << "Can not update Virtual Network, id: "; + ose << "Can not update Virtual Network."; goto error_common; error_addr: - ose << "Network address is not defined, id: "; + ose << "No NETWORK_ADDRESS in template for Virtual Network."; goto error_common; error_null_leases: - ose << "Error getting Virtual Network leases, id: "; + ose << "Error getting Virtual Network leases."; error_common: - ose << oid << "."; - error_str = ose.str(); NebulaLog::log("VNM", Log::ERROR, ose); return -1;