From ae53d43789ef4ee0965074705c480e65607632ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 10 May 2011 18:45:15 +0200 Subject: [PATCH 01/88] Feature #407: Base implementation for groups. So far groups can be managed, but resources can't be associated to a group. Tasks done: * New basic onegroup command * RequestManager XML-RPC methods * New group pool * New GROUP authorization symbol * Basic GroupPool tests * Ruby OCA methods --- SConstruct | 3 + include/AuthManager.h | 3 +- include/Group.h | 109 ++++++++++++ include/GroupPool.h | 133 +++++++++++++++ include/Nebula.h | 16 +- include/RequestManager.h | 118 ++++++++++++- include/test/NebulaTest.h | 7 +- install.sh | 7 +- share/test/do_tests.sh | 3 +- src/authm/AuthManager.cc | 1 + src/cli/client_utilities.rb | 4 + src/cli/onegroup | 186 ++++++++++++++++++++ src/cluster/test/SConstruct | 1 + src/group/Group.cc | 189 +++++++++++++++++++++ src/group/GroupPool.cc | 118 +++++++++++++ src/group/SConstruct | 30 ++++ src/group/test/GroupPoolTest.cc | 233 ++++++++++++++++++++++++++ src/group/test/SConstruct | 57 +++++++ src/host/test/SConstruct | 1 + src/lcm/test/SConstruct | 1 + src/nebula/Nebula.cc | 4 + src/nebula/SConstruct | 1 + src/oca/ruby/OpenNebula.rb | 2 + src/oca/ruby/OpenNebula/Group.rb | 87 ++++++++++ src/oca/ruby/OpenNebula/GroupPool.rb | 52 ++++++ src/rm/RequestManager.cc | 20 +++ src/rm/RequestManagerGroupAllocate.cc | 122 ++++++++++++++ src/rm/RequestManagerGroupDelete.cc | 144 ++++++++++++++++ src/rm/RequestManagerGroupInfo.cc | 107 ++++++++++++ src/rm/RequestManagerGroupPoolInfo.cc | 100 +++++++++++ src/rm/SConstruct | 4 + src/test/Nebula.cc | 14 +- src/test/NebulaTest.cc | 7 + 33 files changed, 1875 insertions(+), 9 deletions(-) create mode 100644 include/Group.h create mode 100644 include/GroupPool.h create mode 100755 src/cli/onegroup create mode 100644 src/group/Group.cc create mode 100644 src/group/GroupPool.cc create mode 100644 src/group/SConstruct create mode 100644 src/group/test/GroupPoolTest.cc create mode 100644 src/group/test/SConstruct create mode 100644 src/oca/ruby/OpenNebula/Group.rb create mode 100644 src/oca/ruby/OpenNebula/GroupPool.rb create mode 100644 src/rm/RequestManagerGroupAllocate.cc create mode 100644 src/rm/RequestManagerGroupDelete.cc create mode 100644 src/rm/RequestManagerGroupInfo.cc create mode 100644 src/rm/RequestManagerGroupPoolInfo.cc diff --git a/SConstruct b/SConstruct index 3bf25cec2b..bff501eaf8 100644 --- a/SConstruct +++ b/SConstruct @@ -58,6 +58,7 @@ main_env.Append(LIBPATH=[ cwd+'/src/sql', cwd+'/src/host', cwd+'/src/cluster', + cwd+'/src/group', cwd+'/src/mad', cwd+'/src/nebula', cwd+'/src/pool', @@ -187,6 +188,7 @@ build_scripts=[ 'src/template/SConstruct', 'src/host/SConstruct', 'src/cluster/SConstruct', + 'src/group/SConstruct', 'src/mad/SConstruct', 'src/nebula/SConstruct', 'src/pool/SConstruct', @@ -233,6 +235,7 @@ if testing=='yes': 'src/common/test/SConstruct', 'src/host/test/SConstruct', 'src/cluster/test/SConstruct', + 'src/group/test/SConstruct', 'src/image/test/SConstruct', 'src/lcm/test/SConstruct', 'src/pool/test/SConstruct', diff --git a/include/AuthManager.h b/include/AuthManager.h index e456f02fee..0bb58e23dc 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -295,7 +295,8 @@ public: IMAGE, USER, CLUSTER, - TEMPLATE + TEMPLATE, + GROUP }; /** diff --git a/include/Group.h b/include/Group.h new file mode 100644 index 0000000000..a1bd086304 --- /dev/null +++ b/include/Group.h @@ -0,0 +1,109 @@ +/* ------------------------------------------------------------------------ */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------*/ + +#ifndef GROUP_H_ +#define GROUP_H_ + +#include "PoolSQL.h" + +using namespace std; + +/** + * The Group class. + */ +class Group : public PoolObjectSQL +{ +public: + /** + * Function to write a Group on an output stream + */ + friend ostream& operator<<(ostream& os, Group& group); + + /** + * Function to print the Group object into a string in XML format + * @param xml the resulting XML string + * @return a reference to the generated string + */ + string& to_xml(string& xml) const; + + /** + * Rebuilds the object from an xml formatted string + * @param xml_str The xml-formatted string + * + * @return 0 on success, -1 otherwise + */ + int from_xml(const string &xml_str); + +private: + + // ------------------------------------------------------------------------- + // Friends + // ------------------------------------------------------------------------- + + friend class GroupPool; + + // ************************************************************************* + // Constructor + // ************************************************************************* + + Group(int id, int uid, const string& name); + + virtual ~Group(); + + // ************************************************************************* + // DataBase implementation (Private) + // ************************************************************************* + + static const char * db_names; + + static const char * db_bootstrap; + + static const char * table; + + /** + * Execute an INSERT or REPLACE Sql query. + * @param db The SQL DB + * @param replace Execute an INSERT or a REPLACE + * @return 0 one success + */ + int insert_replace(SqlDB *db, bool replace); + + /** + * Bootstraps the database table(s) associated to the Group + */ + static void bootstrap(SqlDB * db) + { + ostringstream oss(Group::db_bootstrap); + + db->exec(oss); + }; + + /** + * Writes the Group in the database. + * @param db pointer to the db + * @return 0 on success + */ + int insert(SqlDB *db, string& error_str); + + /** + * Writes/updates the Group's data fields in the database. + * @param db pointer to the db + * @return 0 on success + */ + int update(SqlDB *db); +}; + +#endif /*GROUP_H_*/ diff --git a/include/GroupPool.h b/include/GroupPool.h new file mode 100644 index 0000000000..b4b667b338 --- /dev/null +++ b/include/GroupPool.h @@ -0,0 +1,133 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#ifndef GROUP_POOL_H_ +#define GROUP_POOL_H_ + +#include "Group.h" +#include "SqlDB.h" + +using namespace std; + + +class GroupPool : public PoolSQL +{ +public: + GroupPool(SqlDB * db); + + ~GroupPool(){}; + + /** + * Cluster name for the default cluster + */ + static const string DEFAULT_GROUP_NAME; + + /* ---------------------------------------------------------------------- */ + /* Methods for DB management */ + /* ---------------------------------------------------------------------- */ + + /** + * Allocates a new group, writting it in the pool database. No memory is + * allocated for the object. + * @param uid user id (the owner of the Object) + * @param name Group name + * @param oid the id assigned to the Group + * @param error_str Returns the error reason, if any + * + * @return the oid assigned to the object, -1 in case of failure + */ + int allocate(int uid, + string name, + int * oid, + string& error_str); + + /** + * Function to get a group from the pool, if the object is not in memory + * it is loaded from the DB + * @param oid group unique id + * @param lock locks the group mutex + * @return a pointer to the group, 0 if the group could not be loaded + */ + Group * get(int oid, bool lock) + { + 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 + */ + Group * get(const string& name, int uid, bool lock) + { + return static_cast(PoolSQL::get(name,uid,lock)); + }; + + /** Update a particular Group + * @param user pointer to Group + * @return 0 on success + */ + int update(Group * group) + { + return group->update(db); + }; + + /** + * Drops the Group from the data base. The object mutex SHOULD be + * locked. + * @param group a pointer to the object + * @return 0 on success. + */ + int drop(Group * group); + + /** + * Bootstraps the database table(s) associated to the Group pool + */ + static void bootstrap(SqlDB * _db) + { + Group::bootstrap(_db); + }; + + /** + * Dumps the Group pool in XML format. A filter can be also added to the + * query + * @param oss the output stream to dump the pool contents + * @param where filter for the objects, defaults to all + * + * @return 0 on success + */ + int dump(ostringstream& oss, const string& where) + { + return PoolSQL::dump(oss, "GROUP_POOL", Group::table, where); + }; + +private: + + /** + * Factory method to produce objects + * @return a pointer to the new object + */ + PoolObjectSQL * create() + { + return new Group(-1,-1,""); + }; +}; + +#endif /*GROUP_POOL_H_*/ diff --git a/include/Nebula.h b/include/Nebula.h index a677815583..8a814387be 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -26,6 +26,7 @@ #include "HostPool.h" #include "UserPool.h" #include "VMTemplatePool.h" +#include "GroupPool.h" #include "VirtualMachineManager.h" #include "LifeCycleManager.h" @@ -82,6 +83,11 @@ public: return cpool; }; + GroupPool * get_gpool() + { + return gpool; + }; + VMTemplatePool * get_tpool() { return tpool; @@ -240,8 +246,8 @@ private: // ----------------------------------------------------------------------- Nebula():nebula_configuration(0),db(0),vmpool(0),hpool(0),vnpool(0), - upool(0),ipool(0),cpool(0),tpool(0),lcm(0),vmm(0),im(0),tm(0),dm(0), - rm(0),hm(0),authm(0),imagem(0) + upool(0),ipool(0),cpool(0),gpool(0),tpool(0),lcm(0),vmm(0),im(0),tm(0), + dm(0),rm(0),hm(0),authm(0),imagem(0) { const char * nl = getenv("ONE_LOCATION"); @@ -306,6 +312,11 @@ private: delete cpool; } + if ( gpool != 0) + { + delete gpool; + } + if ( tpool != 0) { delete tpool; @@ -403,6 +414,7 @@ private: UserPool * upool; ImagePool * ipool; ClusterPool * cpool; + GroupPool * gpool; VMTemplatePool * tpool; // --------------------------------------------------------------- diff --git a/include/RequestManager.h b/include/RequestManager.h index e604581e30..4718b6be74 100644 --- a/include/RequestManager.h +++ b/include/RequestManager.h @@ -25,6 +25,7 @@ #include "ImagePool.h" #include "ClusterPool.h" #include "VMTemplatePool.h" +#include "GroupPool.h" #include #include @@ -48,11 +49,12 @@ public: ImagePool * _ipool, ClusterPool * _cpool, VMTemplatePool * _tpool, + GroupPool * _gpool, int _port, string _xml_log_file) :vmpool(_vmpool),hpool(_hpool),vnpool(_vnpool),upool(_upool), - ipool(_ipool),cpool(_cpool),tpool(_tpool),port(_port),socket_fd(-1), - xml_log_file(_xml_log_file) + ipool(_ipool),cpool(_cpool),tpool(_tpool),gpool(_gpool),port(_port), + socket_fd(-1),xml_log_file(_xml_log_file) { am.addListener(this); }; @@ -141,6 +143,11 @@ private: */ VMTemplatePool * tpool; + /** + * Pointer to the Group Pool, to access groups + */ + GroupPool * gpool; + /** * Port number where the connection will be open */ @@ -987,6 +994,113 @@ private: }; + /* ---------------------------------------------------------------------- */ + /* Group Interface */ + /* ---------------------------------------------------------------------- */ + + class GroupAllocate: public xmlrpc_c::method + { + public: + GroupAllocate( + UserPool * _upool, + GroupPool * _gpool): + upool(_upool), + gpool(_gpool) + { + _signature="A:ss"; + _help="Allocates a group in the pool"; + }; + + ~GroupAllocate(){}; + + void execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retvalP); + + private: + UserPool * upool; + GroupPool * gpool; + }; + + /* ---------------------------------------------------------------------- */ + + class GroupInfo: public xmlrpc_c::method + { + public: + GroupInfo( + UserPool * _upool, + GroupPool * _gpool): + upool(_upool), + gpool(_gpool) + { + _signature="A:si"; + _help="Returns group information"; + }; + + ~GroupInfo(){}; + + void execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retvalP); + + private: + UserPool * upool; + GroupPool * gpool; + }; + + /* ---------------------------------------------------------------------- */ + + class GroupDelete: public xmlrpc_c::method + { + public: + GroupDelete( + UserPool * _upool, + GroupPool * _gpool): + upool(_upool), + gpool(_gpool) + { + _signature="A:si"; + _help="Deletes a group from the pool"; + }; + + ~GroupDelete(){}; + + void execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retvalP); + + private: + UserPool * upool; + GroupPool * gpool; + }; + + /* ---------------------------------------------------------------------- */ + + class GroupPoolInfo: public xmlrpc_c::method + { + public: + GroupPoolInfo( + UserPool * _upool, + GroupPool * _gpool): + upool(_upool), + gpool(_gpool) + { + _signature="A:s"; + _help="Returns the group pool information"; + }; + + ~GroupPoolInfo(){}; + + void execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retvalP); + + private: + UserPool * upool; + GroupPool * gpool; + }; + + /* ---------------------------------------------------------------------- */ /* Virtual Network Interface */ /* ---------------------------------------------------------------------- */ diff --git a/include/test/NebulaTest.h b/include/test/NebulaTest.h index 52c551799d..8785f0b8e1 100644 --- a/include/test/NebulaTest.h +++ b/include/test/NebulaTest.h @@ -45,7 +45,8 @@ protected: NebulaTest():mysql(false), need_host_pool(false), need_vm_pool(false), need_vnet_pool(false), need_image_pool(false), need_user_pool(false), need_cluster_pool(false), - need_template_pool(false),need_vmm(false), + need_template_pool(false), need_group_pool(false), + need_vmm(false), need_im(false), need_tm(false), need_lcm(false), need_dm(false), need_rm(false), need_hm(false), @@ -66,6 +67,7 @@ public: bool need_user_pool; bool need_cluster_pool; bool need_template_pool; + bool need_group_pool; bool need_vmm; bool need_im; @@ -104,6 +106,8 @@ public: virtual VMTemplatePool* create_tpool(SqlDB* db); + virtual GroupPool* create_gpool(SqlDB* db); + // ------------------------------------------------------------------------ // Managers // ------------------------------------------------------------------------ @@ -134,6 +138,7 @@ public: ImagePool * ipool, ClusterPool * cpool, VMTemplatePool * tpool, + GroupPool * gpool, string log_file); virtual HookManager* create_hm(VirtualMachinePool * vmpool); diff --git a/install.sh b/install.sh index 6a4fcc63c1..b2283b3547 100755 --- a/install.sh +++ b/install.sh @@ -365,6 +365,7 @@ BIN_FILES="src/nebula/oned \ src/cli/oneimage \ src/cli/onecluster \ src/cli/onetemplate \ + src/cli/onegroup \ share/scripts/one \ src/authm_mad/oneauth" @@ -411,6 +412,8 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \ src/oca/ruby/OpenNebula/ClusterPool.rb \ src/oca/ruby/OpenNebula/Template.rb \ src/oca/ruby/OpenNebula/TemplatePool.rb \ + src/oca/ruby/OpenNebula/Group.rb \ + src/oca/ruby/OpenNebula/GroupPool.rb \ src/oca/ruby/OpenNebula/XMLUtils.rb" @@ -733,7 +736,8 @@ CLI_BIN_FILES="src/cli/onevm \ src/cli/oneuser \ src/cli/oneimage \ src/cli/onecluster \ - src/cli/onetemplate" + src/cli/onetemplate \ + src/cli/onegroup" #----------------------------------------------------------------------------- # Sunstone files @@ -843,6 +847,7 @@ MAN_FILES="share/man/oneauth.8.gz \ share/man/onevm.8.gz \ share/man/onevnet.8.gz \ share/man/onetemplate.8.gz \ + share/man/onegroup.8.gz \ share/man/econe-describe-images.8.gz \ share/man/econe-describe-instances.8.gz \ share/man/econe-register.8.gz \ diff --git a/share/test/do_tests.sh b/share/test/do_tests.sh index 1e18096337..f29c181ca4 100755 --- a/share/test/do_tests.sh +++ b/share/test/do_tests.sh @@ -18,7 +18,8 @@ TESTS="$TWD_DIR/vnm/test \ $TWD_DIR/um/test \ $TWD_DIR/lcm/test \ $TWD_DIR/pool/test \ - $TWD_DIR/vm_template/test" + $TWD_DIR/vm_template/test \ + $TWD_DIR/group/test" #------------------------------------------------------------------------------- # COMMAND LINE PARSING diff --git a/src/authm/AuthManager.cc b/src/authm/AuthManager.cc index cc88d57de5..ad83f3681a 100644 --- a/src/authm/AuthManager.cc +++ b/src/authm/AuthManager.cc @@ -87,6 +87,7 @@ void AuthRequest::add_auth(Object ob, case USER: oss << "USER:" ; break; case CLUSTER: oss << "CLUSTER:" ; break; case TEMPLATE: oss << "TEMPLATE:" ; break; + case GROUP: oss << "GROUP:" ; break; } if (op == CREATE || op == INSTANTIATE) //encode the ob_id, it is a template diff --git a/src/cli/client_utilities.rb b/src/cli/client_utilities.rb index 90e7307bd1..36ef1ff549 100644 --- a/src/cli/client_utilities.rb +++ b/src/cli/client_utilities.rb @@ -321,6 +321,10 @@ def get_template_id(name) get_entity_id(name, OpenNebula::TemplatePool) end +def get_group_id(name) + get_entity_id(name, OpenNebula::GroupPool) +end + def str_running_time(data) stime=Time.at(data["STIME"].to_i) if data["ETIME"]=="0" diff --git a/src/cli/onegroup b/src/cli/onegroup new file mode 100755 index 0000000000..5a6670c0aa --- /dev/null +++ b/src/cli/onegroup @@ -0,0 +1,186 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + +$: << RUBY_LIB_LOCATION + + +require 'OpenNebula' +require 'client_utilities' +require 'command_parse' + + +ShowTableUP={ + :id => { + :name => "ID", + :desc => "ONE identifier for group", + :size => 4, + :proc => lambda {|d,e| + d.id + } + }, + :uid => { + :name => "OWNER", + :desc => "Owner of the group", + :size => 5, + :proc => lambda {|d,e| + d.uid + } + }, + :name => { + :name => "NAME", + :desc => "Name of the group", + :size => 16, + :proc => lambda {|d,e| + d.name + } + }, + + :default => [:id, :uid, :name] +} + +class UPShow + def initialize + @grouppool=OpenNebula::GroupPool.new(get_one_client) + @table=ShowTable.new(ShowTableUP) + end + + def header_up_small + scr_bold + scr_underline + print @table.header_str + scr_restore + puts "" + end + + def list_short(options=nil) + res=@grouppool.info + if options + @table.columns=options[:columns] if options[:columns] + end + + if OpenNebula.is_error?(res) + result=res + else + result=res + header_up_small + + puts @table.data_str(@grouppool, options) + result + end + end +end + +class OneUPParse < CommandParse + + COMMANDS_HELP=<<-EOT + +Description: + +This command enables the OpenNebula administrator to manage groups. + + +Commands: + +* create (Creates a new group) + onegroup create groupname + +* delete (Removes a group) + onegroup delete + +* list (Lists all the groups in the pool) + onegroup list + +EOT + + def text_commands + COMMANDS_HELP + end + + def text_command_name + "onegroup" + end + + def list_options + table=ShowTable.new(ShowTableUP) + table.print_help + end + +end + +oneup_opts=OneUPParse.new([:list, :xml]) +oneup_opts.parse(ARGV) +ops=oneup_opts.options + +result=[false, "Unknown error"] + +command=ARGV.shift + +case command +when "create" + check_parameters("create", 1) + group=OpenNebula::Group.new(OpenNebula::Group.build_xml, get_one_client) + result=group.allocate(ARGV[0]) + + if is_successful?(result) + puts "ID: " + group.id.to_s if ops[:verbose] + exit 0 + end + +when "delete" + check_parameters("delete", 1) + args=expand_args(ARGV) + + args.each do |param| + group_id=get_group_id(param) + group=OpenNebula::Group.new( + OpenNebula::Group.build_xml(group_id), get_one_client) + result=group.delete + if !OpenNebula.is_error?(result) + puts "Group deleted" if ops[:verbose] + break + end + end + +when "list" + if !ops[:xml] + uplist=UPShow.new + ops[:columns]=ops[:list] if ops[:list] + result=uplist.list_short(ops) + else + grouppool=OpenNebula::GroupPool.new(get_one_client) + grouppool.info + puts grouppool.to_xml(true) + end + +else + oneup_opts.print_help + exit -1 +end + +if OpenNebula.is_error?(result) + puts "Error: " + result.message + exit -1 +end diff --git a/src/cluster/test/SConstruct b/src/cluster/test/SConstruct index 3f31210fa3..c9823986fd 100644 --- a/src/cluster/test/SConstruct +++ b/src/cluster/test/SConstruct @@ -40,6 +40,7 @@ env.Prepend(LIBS=[ 'nebula_template', 'nebula_vm', 'nebula_vmtemplate', + 'nebula_group', 'nebula_vnm', 'nebula_image', 'nebula_pool', diff --git a/src/group/Group.cc b/src/group/Group.cc new file mode 100644 index 0000000000..b5e6da986a --- /dev/null +++ b/src/group/Group.cc @@ -0,0 +1,189 @@ +/* ------------------------------------------------------------------------ */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* ------------------------------------------------------------------------ */ + +#include +#include + +#include +#include + +#include "Group.h" + + +const char * Group::table = "group_pool"; + +const char * Group::db_names = "oid, name, body, uid"; + +const char * Group::db_bootstrap = "CREATE TABLE IF NOT EXISTS group_pool (" + "oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, " + "UNIQUE(name))"; + + +/* ************************************************************************ */ +/* Group :: Constructor/Destructor */ +/* ************************************************************************ */ + +Group::Group(int id, int uid, const string& name): + PoolObjectSQL(id,name,uid,table){}; + +Group::~Group(){}; + +/* ************************************************************************ */ +/* Group :: Database Access Functions */ +/* ************************************************************************ */ + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Group::insert(SqlDB *db, string& error_str) +{ + int rc; + + rc = insert_replace(db, false); + + if ( rc != 0 ) + { + error_str = "Error inserting Group in DB."; + } + + return rc; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Group::update(SqlDB *db) +{ + int rc; + + rc = insert_replace(db, true); + + return rc; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Group::insert_replace(SqlDB *db, bool replace) +{ + ostringstream oss; + + int rc; + string xml_body; + + char * sql_name; + char * sql_xml; + + // Update the Group + + sql_name = db->escape_str(name.c_str()); + + if ( sql_name == 0 ) + { + goto error_name; + } + + sql_xml = db->escape_str(to_xml(xml_body).c_str()); + + if ( sql_xml == 0 ) + { + goto error_body; + } + + if(replace) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } + + // Construct the SQL statement to Insert or Replace + + oss <<" INTO "<exec(oss); + + db->free_str(sql_name); + db->free_str(sql_xml); + + return rc; + +error_body: + db->free_str(sql_name); +error_name: + return -1; +} + +/* ************************************************************************ */ +/* Group :: Misc */ +/* ************************************************************************ */ + +ostream& operator<<(ostream& os, Group& group) +{ + string group_str; + + os << group.to_xml(group_str); + + return os; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +string& Group::to_xml(string& xml) const +{ + ostringstream oss; + + oss << + "" << + "" << oid << "" << + "" << uid << "" << + "" << name << "" << + ""; + + xml = oss.str(); + + return xml; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Group::from_xml(const string& xml) +{ + int rc = 0; + + // Initialize the internal XML object + update_from_str(xml); + + // Get class base attributes + rc += xpath(oid, "/GROUP/ID", -1); + rc += xpath(uid, "/GROUP/UID", -1); + rc += xpath(name,"/GROUP/NAME", "not_found"); + + if (rc != 0) + { + return -1; + } + + return 0; +} diff --git a/src/group/GroupPool.cc b/src/group/GroupPool.cc new file mode 100644 index 0000000000..de2c2ca5ee --- /dev/null +++ b/src/group/GroupPool.cc @@ -0,0 +1,118 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "GroupPool.h" +#include "Nebula.h" +#include "NebulaLog.h" + +#include + +const string GroupPool::DEFAULT_GROUP_NAME = "default"; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +GroupPool::GroupPool(SqlDB * db):PoolSQL(db, Group::table) +{ + // lastOID is set in PoolSQL::init_cb + if (get_lastOID() == -1) + { + int rc; + Group * group; + string error_str; + + // Build a new Group object + group = new Group(0, 0, GroupPool::DEFAULT_GROUP_NAME); + + // Insert the Object in the pool + rc = PoolSQL::allocate(group, error_str); + + if(rc != 0) + { + ostringstream oss; + + oss << "Error trying to create default group: " << error_str; + NebulaLog::log("GROUP",Log::ERROR,oss); + + throw runtime_error(oss.str()); + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int GroupPool::allocate(int uid, string name, int * oid, string& error_str) +{ + Group * group; + ostringstream oss; + + if ( name.empty() ) + { + goto error_name; + } + + // Check for duplicates + group = get(name, uid, false); + + if( group != 0 ) + { + goto error_duplicated; + } + + // Build a new Group object + group = new Group(-1, uid, name); + + // Insert the Object in the pool + *oid = PoolSQL::allocate(group, error_str); + + return *oid; + + +error_name: + oss << "NAME cannot be empty."; + goto error_common; + +error_duplicated: + oss << "NAME is already taken by GROUP " << group->get_oid() << "."; + +error_common: + *oid = -1; + error_str = oss.str(); + + return *oid; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int GroupPool::drop(Group * group) +{ + int rc; + + // Return error if group is 'default' + if( group->get_oid() == 0 ) + { + NebulaLog::log("GROUP",Log::WARNING, + "Default group cannot be deleted."); + + return -1; + } + + rc = group->drop(db); + + return rc; +} diff --git a/src/group/SConstruct b/src/group/SConstruct new file mode 100644 index 0000000000..d9a3e06b8a --- /dev/null +++ b/src/group/SConstruct @@ -0,0 +1,30 @@ +# SConstruct for src/group + +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +Import('env') + +lib_name='nebula_group' + +# Sources to generate the library +source_files=[ + 'GroupPool.cc', + 'Group.cc' +] + +# Build library +env.StaticLibrary(lib_name, source_files) diff --git a/src/group/test/GroupPoolTest.cc b/src/group/test/GroupPoolTest.cc new file mode 100644 index 0000000000..76f8962a5d --- /dev/null +++ b/src/group/test/GroupPoolTest.cc @@ -0,0 +1,233 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include +#include +#include + +#include "GroupPool.h" +#include "PoolTest.h" + +using namespace std; + +const int uids[] = {0,1,2}; +const string names[] = {"First name", "Second name"}; + +const string xmls[] = +{ + "10First name", + "21Second name" +}; + +const string group_xml_dump = + "00default15group_a35group_c45group_d"; + +/* ************************************************************************* */ +/* ************************************************************************* */ + + +#include "NebulaTest.h" + +class NebulaTestGroup: public NebulaTest +{ +public: + NebulaTestGroup():NebulaTest() + { + NebulaTest::the_tester = this; + + need_group_pool= true; + } +}; + +class GroupPoolTest : public PoolTest +{ + CPPUNIT_TEST_SUITE (GroupPoolTest); + + // Not all tests from PoolTest can be used. Because + // of the initial default group added to the DB, the + // oid_assignment would fail. + CPPUNIT_TEST (get_from_cache); + CPPUNIT_TEST (get_from_db); + CPPUNIT_TEST (wrong_get); + CPPUNIT_TEST (drop_and_get); + + CPPUNIT_TEST (duplicates); + CPPUNIT_TEST (dump); + + CPPUNIT_TEST_SUITE_END (); + +protected: + + NebulaTestGroup * tester; + GroupPool * gpool; + + + + void bootstrap(SqlDB* db) + { + // setUp overwritten + }; + + PoolSQL* create_pool(SqlDB* db) + { + // setUp overwritten + return gpool; + }; + + int allocate(int index) + { + int oid; + string err; + + return gpool->allocate(uids[index], names[index], &oid, err); + }; + + void check(int index, PoolObjectSQL* obj) + { + Group * group = static_cast(obj); + + CPPUNIT_ASSERT( obj != 0 ); + + string xml_str = ""; + string name = group->get_name(); + + CPPUNIT_ASSERT( name == names[index] ); + + // Get the xml + group->to_xml(xml_str); + +// A little help for debugging +/* + if( xml_str != xmls[index] ) + { + cout << endl << xml_str << endl << "========" + << endl << xmls[index]; + } +//*/ + CPPUNIT_ASSERT( xml_str == xmls[index]); + }; + +private: + + +public: + GroupPoolTest() + { + xmlInitParser(); + }; + + ~GroupPoolTest() + { + xmlCleanupParser(); + }; + + void setUp() + { + create_db(); + + tester = new NebulaTestGroup(); + + Nebula& neb = Nebula::instance(); + neb.start(); + + gpool = neb.get_gpool(); + + pool = gpool; + }; + + void tearDown() + { + delete_db(); + + delete tester; + }; + + /* ********************************************************************* */ + /* ********************************************************************* */ + + void duplicates() + { + int rc, oid; + string err; + GroupPool * gpool = static_cast(pool); + + // Allocate a group + rc = gpool->allocate(uids[0], names[0], &oid, err); + CPPUNIT_ASSERT( oid == 1 ); + CPPUNIT_ASSERT( oid == rc ); + + // Try to allocate twice the same group, should fail + rc = gpool->allocate(uids[0], names[0], &oid, err); + CPPUNIT_ASSERT( rc == -1 ); + CPPUNIT_ASSERT( oid == rc ); + + // Try again, this time with different uid. Should fail, groups can't + // repeat names + rc = gpool->allocate(uids[1], names[0], &oid, err); + CPPUNIT_ASSERT( rc == -1 ); + CPPUNIT_ASSERT( oid == rc ); + } + + /* ********************************************************************* */ + + void dump() + { + Group * group; + int oid, rc; + ostringstream oss; + string err; + + // Allocate some groups + rc = gpool->allocate(5, "group_a", &oid, err); + CPPUNIT_ASSERT( rc == 1 ); + + rc = gpool->allocate(5, "group_b", &oid, err); + CPPUNIT_ASSERT( rc == 2 ); + + rc = gpool->allocate(5, "group_c", &oid, err); + CPPUNIT_ASSERT( rc == 3 ); + + rc = gpool->allocate(5, "group_d", &oid, err); + CPPUNIT_ASSERT( rc == 4 ); + + // Drop one of them + group = gpool->get(2, false); + CPPUNIT_ASSERT( group != 0 ); + + rc = gpool->drop(group); + CPPUNIT_ASSERT( rc == 0 ); + + // dump the pool + rc = gpool->dump(oss,""); +/* + if( oss.str() != group_xml_dump ) + { + cout << endl << oss.str() << endl << "========" + << endl << group_xml_dump; + } +//*/ + CPPUNIT_ASSERT( oss.str() == group_xml_dump ); + + } +}; + +/* ************************************************************************* */ +/* ************************************************************************* */ + +int main(int argc, char ** argv) +{ + return PoolTest::main(argc, argv, GroupPoolTest::suite()); +} diff --git a/src/group/test/SConstruct b/src/group/test/SConstruct new file mode 100644 index 0000000000..fb71f43fb5 --- /dev/null +++ b/src/group/test/SConstruct @@ -0,0 +1,57 @@ +# -------------------------------------------------------------------------- +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# -------------------------------------------------------------------------- + +Import('env') + +env.Prepend(LIBS=[ + 'nebula_cluster', + 'nebula_host', + 'nebula_pool', + 'nebula_template', + 'nebula_xml', + 'nebula_log', + 'nebula_common', + 'nebula_sql', + +### TODO: delete not needed + 'nebula_core_test', + 'nebula_host', + 'nebula_cluster', + 'nebula_xml', + 'nebula_vmm', + 'nebula_im', + 'nebula_rm', + 'nebula_tm', + 'nebula_um', + 'nebula_mad', + 'nebula_template', + 'nebula_vm', + 'nebula_vmtemplate', + 'nebula_group', + 'nebula_vnm', + 'nebula_image', + 'nebula_pool', + 'nebula_hm', + 'nebula_authm', + 'nebula_common', + 'nebula_lcm', + 'nebula_dm', + 'nebula_sql', + 'nebula_log', + 'crypto' +]) + +env.Program('test','GroupPoolTest.cc') diff --git a/src/host/test/SConstruct b/src/host/test/SConstruct index c839508487..c474d19dd4 100644 --- a/src/host/test/SConstruct +++ b/src/host/test/SConstruct @@ -32,6 +32,7 @@ env.Prepend(LIBS=[ 'nebula_template', 'nebula_vm', 'nebula_vmtemplate', + 'nebula_group', 'nebula_vnm', 'nebula_image', 'nebula_pool', diff --git a/src/lcm/test/SConstruct b/src/lcm/test/SConstruct index 5fbcbc1687..4ef32700c3 100644 --- a/src/lcm/test/SConstruct +++ b/src/lcm/test/SConstruct @@ -31,6 +31,7 @@ env.Prepend(LIBS=[ 'nebula_template', 'nebula_vm', 'nebula_vmtemplate', + 'nebula_group', 'nebula_vnm', 'nebula_image', 'nebula_pool', diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index fa8fc57ab1..c7b382bd4c 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -235,6 +235,7 @@ void Nebula::start() UserPool::bootstrap(db); ImagePool::bootstrap(db); ClusterPool::bootstrap(db); + GroupPool::bootstrap(db); VMTemplatePool::bootstrap(db); } catch (exception&) @@ -275,6 +276,8 @@ void Nebula::start() cpool = new ClusterPool(db); + gpool = new GroupPool(db); + tpool = new VMTemplatePool(db); } catch (exception&) @@ -443,6 +446,7 @@ void Nebula::start() ipool, cpool, tpool, + gpool, rm_port, log_location + "one_xmlrpc.log"); } diff --git a/src/nebula/SConstruct b/src/nebula/SConstruct index 3cd71f38a4..47358a5aa1 100644 --- a/src/nebula/SConstruct +++ b/src/nebula/SConstruct @@ -41,6 +41,7 @@ env.Prepend(LIBS=[ 'nebula_dm', 'nebula_tm', 'nebula_um', + 'nebula_group', 'nebula_authm', 'nebula_mad', 'nebula_template', diff --git a/src/oca/ruby/OpenNebula.rb b/src/oca/ruby/OpenNebula.rb index f051799ae7..08ac79b870 100644 --- a/src/oca/ruby/OpenNebula.rb +++ b/src/oca/ruby/OpenNebula.rb @@ -39,6 +39,8 @@ require 'OpenNebula/Cluster' require 'OpenNebula/ClusterPool' require 'OpenNebula/Template' require 'OpenNebula/TemplatePool' +require 'OpenNebula/Group' +require 'OpenNebula/GroupPool' module OpenNebula diff --git a/src/oca/ruby/OpenNebula/Group.rb b/src/oca/ruby/OpenNebula/Group.rb new file mode 100644 index 0000000000..3f381e031c --- /dev/null +++ b/src/oca/ruby/OpenNebula/Group.rb @@ -0,0 +1,87 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'OpenNebula/Pool' + +module OpenNebula + class Group < PoolElement + # --------------------------------------------------------------------- + # Constants and Class Methods + # --------------------------------------------------------------------- + GROUP_METHODS = { + :info => "group.info", + :allocate => "group.allocate", + :delete => "group.delete", + } + + # Creates a Group description with just its identifier + # this method should be used to create plain Group objects. + # +id+ the id of the user + # + # Example: + # group = Group.new(Group.build_xml(3),rpc_client) + # + def Group.build_xml(pe_id=nil) + if pe_id + group_xml = "#{pe_id}" + else + group_xml = "" + end + + XMLElement.build_xml(group_xml,'GROUP') + end + + # --------------------------------------------------------------------- + # Class constructor + # --------------------------------------------------------------------- + def initialize(xml, client) + super(xml,client) + + @client = client + end + + # --------------------------------------------------------------------- + # XML-RPC Methods for the Group Object + # --------------------------------------------------------------------- + + # Retrieves the information of the given Group. + def info() + super(GROUP_METHODS[:info], 'GROUP') + end + + # Allocates a new Group in OpenNebula + # + # +groupname+ A string containing the name of the Group. + def allocate(groupname) + super(GROUP_METHODS[:allocate], groupname) + end + + # Deletes the Group + def delete() + super(GROUP_METHODS[:delete]) + end + + # --------------------------------------------------------------------- + # Helpers to get information + # --------------------------------------------------------------------- + + # Returns the owner (numeric value) + def uid + self['UID'].to_i + end + + end +end diff --git a/src/oca/ruby/OpenNebula/GroupPool.rb b/src/oca/ruby/OpenNebula/GroupPool.rb new file mode 100644 index 0000000000..591889bd4f --- /dev/null +++ b/src/oca/ruby/OpenNebula/GroupPool.rb @@ -0,0 +1,52 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'OpenNebula/Pool' + +module OpenNebula + class GroupPool < Pool + # --------------------------------------------------------------------- + # Constants and Class attribute accessors + # --------------------------------------------------------------------- + + GROUP_POOL_METHODS = { + :info => "grouppool.info" + } + + # --------------------------------------------------------------------- + # Class constructor & Pool Methods + # --------------------------------------------------------------------- + + # +client+ a Client object that represents a XML-RPC connection + def initialize(client) + super('GROUP_POOL','GROUP',client) + end + + # Factory method to create User objects + def factory(element_xml) + OpenNebula::Group.new(element_xml,@client) + end + + # --------------------------------------------------------------------- + # XML-RPC Methods for the User Object + # --------------------------------------------------------------------- + + # Retrieves all the Groups in the pool. + def info() + super(GROUP_POOL_METHODS[:info]) + end + end +end \ No newline at end of file diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 9f2c9f64f4..8db0911c22 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -288,6 +288,18 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr clusterpool_info(new RequestManager::ClusterPoolInfo(upool,cpool)); + xmlrpc_c::methodPtr group_allocate(new + RequestManager::GroupAllocate(upool,gpool)); + + xmlrpc_c::methodPtr group_info(new + RequestManager::GroupInfo(upool,gpool)); + + xmlrpc_c::methodPtr group_delete(new + RequestManager::GroupDelete(upool,gpool)); + + xmlrpc_c::methodPtr grouppool_info(new + RequestManager::GroupPoolInfo(upool,gpool)); + xmlrpc_c::methodPtr vn_allocate(new RequestManager::VirtualNetworkAllocate(vnpool,upool)); @@ -392,6 +404,14 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.clusterpool.info", clusterpool_info); + /* Group related methods */ + + RequestManagerRegistry.addMethod("one.group.allocate", group_allocate); + RequestManagerRegistry.addMethod("one.group.info", group_info); + RequestManagerRegistry.addMethod("one.group.delete", group_delete); + + RequestManagerRegistry.addMethod("one.grouppool.info", grouppool_info); + /* Network related methods*/ RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate); diff --git a/src/rm/RequestManagerGroupAllocate.cc b/src/rm/RequestManagerGroupAllocate.cc new file mode 100644 index 0000000000..51a6364975 --- /dev/null +++ b/src/rm/RequestManagerGroupAllocate.cc @@ -0,0 +1,122 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "RequestManager.h" +#include "NebulaLog.h" + +#include "AuthManager.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void RequestManager::GroupAllocate::execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retval) +{ + string session; + string error_str; + + string name; + int id; + + const string method_name = "GroupAllocate"; + + int rc, uid; + ostringstream oss; + + /* -- RPC specific vars -- */ + vector arrayData; + xmlrpc_c::value_array * arrayresult; + + NebulaLog::log("ReM",Log::DEBUG,"GroupAllocate method invoked"); + + // Get the parameters + session = xmlrpc_c::value_string(paramList.getString(0)); + name = xmlrpc_c::value_string(paramList.getString(1)); + + //Authenticate the user + uid = GroupAllocate::upool->authenticate(session); + + if ( uid == -1 ) + { + goto error_authenticate; + } + + //Authorize the operation + if ( uid != 0 ) // uid == 0 means oneadmin + { + AuthRequest ar(uid); + + ar.add_auth(AuthRequest::GROUP, + -1, + AuthRequest::CREATE, + uid, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + // Perform the allocation in the pool + rc = GroupAllocate::gpool->allocate(uid, name, &id, error_str); + + if ( rc == -1 ) + { + goto error_allocate; + } + + // All nice, return the new id to client + arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS + arrayData.push_back(xmlrpc_c::value_int(id)); + arrayresult = new xmlrpc_c::value_array(arrayData); + // Copy arrayresult into retval mem space + *retval = *arrayresult; + // and get rid of the original + delete arrayresult; + + return; + +error_authenticate: + oss.str(authenticate_error(method_name)); + goto error_common; + +error_authorize: + oss.str(authorization_error(method_name, "CREATE", "GROUP", uid, -1)); + goto error_common; + +error_allocate: + oss << action_error(method_name, "CREATE", "GROUP", -2, 0); + oss << " " << error_str; + goto error_common; + +error_common: + + arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + NebulaLog::log("ReM",Log::ERROR,oss); + + xmlrpc_c::value_array arrayresult_error(arrayData); + + *retval = arrayresult_error; + + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerGroupDelete.cc b/src/rm/RequestManagerGroupDelete.cc new file mode 100644 index 0000000000..615b025484 --- /dev/null +++ b/src/rm/RequestManagerGroupDelete.cc @@ -0,0 +1,144 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "RequestManager.h" +#include "NebulaLog.h" + +#include "AuthManager.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void RequestManager::GroupDelete::execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retval) +{ + string session; + + int id; + Group * group; + ostringstream oss; + int rc; + int owner; + + const string method_name = "GroupDelete"; + + /* -- RPC specific vars -- */ + vector arrayData; + xmlrpc_c::value_array * arrayresult; + + NebulaLog::log("ReM",Log::DEBUG,"GroupDelete method invoked"); + + // Get the parameters + session = xmlrpc_c::value_string(paramList.getString(0)); + id = xmlrpc_c::value_int (paramList.getInt(1)); + + //Authenticate the user + rc = GroupDelete::upool->authenticate(session); + + if ( rc == -1 ) + { + goto error_authenticate; + } + + // Get template from the pool + group = GroupDelete::gpool->get(id,true); + + if ( group == 0 ) + { + goto error_get; + } + + owner = group->get_uid(); + + group->unlock(); + + //Authorize the operation + if ( rc != 0 ) // rc == 0 means oneadmin + { + AuthRequest ar(rc); + + ar.add_auth(AuthRequest::GROUP, + id, + AuthRequest::DELETE, + owner, + false); + + if (UserPool::authorize(ar) == -1) + { + goto error_authorize; + } + } + + // Perform the deletion from the pool + group = GroupDelete::gpool->get(id,true); + + if ( group == 0 ) + { + goto error_get; + } + + rc = GroupDelete::gpool->drop(group); + + group->unlock(); + + if ( rc != 0 ) + { + goto error_delete; + } + + // Return success + arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS + arrayresult = new xmlrpc_c::value_array(arrayData); + + // Copy arrayresult into retval mem space + *retval = *arrayresult; + // and get rid of the original + delete arrayresult; + + return; + +error_authenticate: + oss.str(authenticate_error(method_name)); + goto error_common; + +error_authorize: + oss.str(authorization_error(method_name, "DELETE", "GROUP", rc, id)); + goto error_common; + +error_get: + oss.str(get_error(method_name, "GROUP", id)); + goto error_common; + +error_delete: + oss.str(action_error(method_name, "DELETE", "GROUP", id, rc)); + goto error_common; + +error_common: + NebulaLog::log ("Rem",Log::ERROR,oss); + + arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + xmlrpc_c::value_array arrayresult_error(arrayData); + + *retval = arrayresult_error; + + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerGroupInfo.cc b/src/rm/RequestManagerGroupInfo.cc new file mode 100644 index 0000000000..f48bfbac20 --- /dev/null +++ b/src/rm/RequestManagerGroupInfo.cc @@ -0,0 +1,107 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "RequestManager.h" +#include "NebulaLog.h" + +#include "AuthManager.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void RequestManager::GroupInfo::execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retval) +{ + string session; + + Group * group; + ostringstream oss; + + int id; + int rc; + + const string method_name = "GroupInfo"; + + + /* -- RPC specific vars -- */ + vector arrayData; + xmlrpc_c::value_array * arrayresult; + + NebulaLog::log("ReM",Log::DEBUG,"GroupInfo method invoked"); + + // Get the parameters + session = xmlrpc_c::value_string(paramList.getString(0)); + id = xmlrpc_c::value_int (paramList.getInt(1)); + + //Authenticate the user + rc = GroupInfo::upool->authenticate(session); + + if ( rc == -1 ) + { + goto error_authenticate; + } + + // TODO: perform authorization with INFO ? + + // Get the group + group = GroupInfo::gpool->get(id,true); + + if ( group == 0 ) + { + goto error_get; + } + + oss << *group; + + group->unlock(); + + // All nice, return the group info to the client + arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + // Copy arrayresult into retval mem space + arrayresult = new xmlrpc_c::value_array(arrayData); + *retval = *arrayresult; + + delete arrayresult; // and get rid of the original + + return; + +error_authenticate: + oss.str(authenticate_error(method_name)); + goto error_common; + +error_get: + oss.str(get_error(method_name, "GROUP", id)); + goto error_common; + +error_common: + + arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + NebulaLog::log("ReM",Log::ERROR,oss); + + xmlrpc_c::value_array arrayresult_error(arrayData); + + *retval = arrayresult_error; + + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/rm/RequestManagerGroupPoolInfo.cc b/src/rm/RequestManagerGroupPoolInfo.cc new file mode 100644 index 0000000000..234700228a --- /dev/null +++ b/src/rm/RequestManagerGroupPoolInfo.cc @@ -0,0 +1,100 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "RequestManager.h" +#include "NebulaLog.h" + +#include "AuthManager.h" + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void RequestManager::GroupPoolInfo::execute( + xmlrpc_c::paramList const& paramList, + xmlrpc_c::value * const retval) +{ + string session; + ostringstream oss; + int rc; + + const string method_name = "GroupPoolInfo"; + + /* -- RPC specific vars -- */ + vector arrayData; + xmlrpc_c::value_array * arrayresult; + + NebulaLog::log("ReM",Log::DEBUG,"GroupPoolInfo method invoked"); + + // Get the parameters + session = xmlrpc_c::value_string(paramList.getString(0)); + + //Authenticate the user + rc = GroupPoolInfo::upool->authenticate(session); + + if ( rc == -1 ) + { + goto error_authenticate; + } + + // TODO: perform authorization with INFO? + // Add flags, like "groups I belong to", "groups I own" + + // Dump the pool + rc = GroupPoolInfo::gpool->dump(oss, ""); + + if ( rc != 0 ) + { + goto error_dump; + } + + //All nice, return the info to the client + arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + arrayresult = new xmlrpc_c::value_array(arrayData); + + // Copy arrayresult into retval mem space + *retval = *arrayresult; + + // and get rid of the original + delete arrayresult; + + return; + +error_authenticate: + oss.str(authenticate_error(method_name)); + goto error_common; + +error_dump: + oss.str(get_error(method_name, "GROUP", -1)); + goto error_common; + +error_common: + + arrayData.push_back(xmlrpc_c::value_boolean(false)); // FAILURE + arrayData.push_back(xmlrpc_c::value_string(oss.str())); + + NebulaLog::log("ReM",Log::ERROR,oss); + + xmlrpc_c::value_array arrayresult_error(arrayData); + + *retval = arrayresult_error; + + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/rm/SConstruct b/src/rm/SConstruct index 11c674e9ea..60658f55c1 100644 --- a/src/rm/SConstruct +++ b/src/rm/SConstruct @@ -50,6 +50,10 @@ source_files=[ 'RequestManagerClusterInfo.cc', 'RequestManagerClusterPoolInfo.cc', 'RequestManagerClusterRemove.cc', + 'RequestManagerGroupAllocate.cc', + 'RequestManagerGroupDelete.cc', + 'RequestManagerGroupInfo.cc', + 'RequestManagerGroupPoolInfo.cc', 'RequestManagerVirtualNetworkAllocate.cc', 'RequestManagerVirtualNetworkInfo.cc', 'RequestManagerVirtualNetworkPoolInfo.cc', diff --git a/src/test/Nebula.cc b/src/test/Nebula.cc index 0c07de18cf..3bbfdc4083 100644 --- a/src/test/Nebula.cc +++ b/src/test/Nebula.cc @@ -87,6 +87,11 @@ void Nebula::start() delete tpool; } + if ( gpool != 0) + { + delete gpool; + } + if ( vmm != 0) { delete vmm; @@ -171,6 +176,8 @@ void Nebula::start() UserPool::bootstrap(db); ImagePool::bootstrap(db); ClusterPool::bootstrap(db); + VMTemplatePool::bootstrap(db); + GroupPool::bootstrap(db); } catch (exception&) { @@ -222,6 +229,11 @@ void Nebula::start() { tpool = tester->create_tpool(db); } + + if (tester->need_group_pool) + { + gpool = tester->create_gpool(db); + } } catch (exception&) { @@ -367,7 +379,7 @@ void Nebula::start() try { rm = tester->create_rm(vmpool,hpool,vnpool,upool,ipool,cpool,tpool, - log_location + "one_xmlrpc.log"); + gpool,log_location + "one_xmlrpc.log"); } catch (bad_alloc&) { diff --git a/src/test/NebulaTest.cc b/src/test/NebulaTest.cc index c0a6b04808..32574623c0 100644 --- a/src/test/NebulaTest.cc +++ b/src/test/NebulaTest.cc @@ -57,6 +57,11 @@ VMTemplatePool* NebulaTest::create_tpool(SqlDB* db) return new VMTemplatePool(db); } +GroupPool* NebulaTest::create_gpool(SqlDB* db) +{ + return new GroupPool(db); +} + // ----------------------------------------------------------- // Managers // ----------------------------------------------------------- @@ -116,6 +121,7 @@ RequestManager* NebulaTest::create_rm( ImagePool * ipool, ClusterPool * cpool, VMTemplatePool * tpool, + GroupPool * gpool, string log_file) { int rm_port = 2633; @@ -127,6 +133,7 @@ RequestManager* NebulaTest::create_rm( ipool, cpool, tpool, + gpool, rm_port, log_file); } From 09c93c75280c0f923ce4d91d936b331b49b37c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 12 May 2011 17:10:35 +0200 Subject: [PATCH 02/88] Feature #407: Remove usernames from owned resources, only user-id is stored now. --- include/Image.h | 8 +-- include/ImagePool.h | 6 +-- include/VMTemplate.h | 8 +-- include/VMTemplatePool.h | 4 +- include/VirtualMachine.h | 11 +--- include/VirtualMachinePool.h | 5 +- include/VirtualNetwork.h | 9 ---- include/VirtualNetworkPool.h | 12 ++--- include/test/PoolTest.h | 39 ++++++++++++++ src/image/Image.cc | 6 +-- src/image/ImagePool.cc | 3 +- src/image/test/ImagePoolTest.cc | 30 +++++------ src/lcm/test/LifeCycleManagerTest.cc | 2 +- src/rm/RequestManagerAllocate.cc | 24 --------- src/rm/RequestManagerImageAllocate.cc | 25 +-------- src/rm/RequestManagerSaveDisk.cc | 20 +------ src/rm/RequestManagerTemplateAllocate.cc | 24 --------- .../RequestManagerVirtualNetworkAllocate.cc | 25 +-------- src/vm/VirtualMachine.cc | 4 -- src/vm/VirtualMachinePool.cc | 3 +- src/vm/test/VirtualMachinePoolTest.cc | 49 ++++++++--------- src/vm_template/VMTemplate.cc | 4 -- src/vm_template/VMTemplatePool.cc | 3 +- src/vm_template/test/VMTemplatePoolTest.cc | 26 ++++----- src/vnm/VirtualNetwork.cc | 7 +-- src/vnm/VirtualNetworkPool.cc | 3 +- src/vnm/test/VirtualNetworkPoolTest.cc | 53 +++++++++---------- 27 files changed, 131 insertions(+), 282 deletions(-) diff --git a/include/Image.h b/include/Image.h index 1ce8c795c8..6095c88f0d 100644 --- a/include/Image.h +++ b/include/Image.h @@ -271,11 +271,6 @@ private: // Image Description // ------------------------------------------------------------------------- - /** - * Image owner's name - */ - string user_name; - /** * Type of the Image */ @@ -346,8 +341,7 @@ protected: // Constructor // ************************************************************************* - Image(int uid, - const string& user_name, + Image(int uid, ImageTemplate* img_template); virtual ~Image(); diff --git a/include/ImagePool.h b/include/ImagePool.h index 9dae46d2ed..3b9a35ebac 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -47,15 +47,15 @@ public: /** * Function to allocate a new Image object * @param uid the user id of the image's owner - * @param stemplate template associated with the image + * @param img_template template associated with the image * @param oid the id assigned to the Image + * @param error_str Returns the error reason, if any * @return the oid assigned to the object, * -1 in case of failure * -2 in case of template parse failure */ int allocate ( int uid, - string user_name, ImageTemplate * img_template, int * oid, string& error_str); @@ -179,7 +179,7 @@ private: */ PoolObjectSQL * create() { - return new Image(-1,"",0); + return new Image(-1,0); }; }; diff --git a/include/VMTemplate.h b/include/VMTemplate.h index 7a92818bda..817560a851 100644 --- a/include/VMTemplate.h +++ b/include/VMTemplate.h @@ -104,11 +104,6 @@ private: // VMTemplate Attributes // ------------------------------------------------------------------------- - /** - * Owner's name - */ - string user_name; - /** * Public scope of the VMTemplate */ @@ -154,8 +149,7 @@ protected: // ************************************************************************* // Constructor // ************************************************************************* - VMTemplate(int id, int uid, string _user_name, - VirtualMachineTemplate * _template_contents); + VMTemplate(int id, int uid, VirtualMachineTemplate * _template_contents); ~VMTemplate(); diff --git a/include/VMTemplatePool.h b/include/VMTemplatePool.h index 92660f6226..39c387adb0 100644 --- a/include/VMTemplatePool.h +++ b/include/VMTemplatePool.h @@ -35,7 +35,6 @@ public: * Allocates a new object, writting it in the pool database. No memory is * allocated for the object. * @param uid user id (the owner of the Template) - * @param user_name Owner's user name * @param template_contents a VM Template object * @param oid the id assigned to the Template * @param error_str Returns the error reason, if any @@ -43,7 +42,6 @@ public: * @return the oid assigned to the object, -1 in case of failure */ int allocate(int uid, - string user_name, VirtualMachineTemplate * template_contents, int * oid, string& error_str); @@ -139,7 +137,7 @@ private: */ PoolObjectSQL * create() { - return new VMTemplate(-1,-1,"", 0); + return new VMTemplate(-1,-1, 0); }; }; diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 6295b100ca..ce56a4bab5 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -668,14 +668,6 @@ private: // Virtual Machine Attributes // ************************************************************************* - // ------------------------------------------------------------------------- - // Identification variables - // ------------------------------------------------------------------------- - /** - * Owner's name - */ - string user_name; - // ------------------------------------------------------------------------- // VM Scheduling & Managing Information // ------------------------------------------------------------------------- @@ -855,8 +847,7 @@ protected: // Constructor //************************************************************************** - VirtualMachine(int id, int uid, string _user_name, - VirtualMachineTemplate * _vm_template); + VirtualMachine(int id, int uid, VirtualMachineTemplate * _vm_template); virtual ~VirtualMachine(); diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index cedf998473..4fefedc640 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -41,7 +41,6 @@ public: /** * Function to allocate a new VM object * @param uid user id (the owner of the VM) - * @param user_name Owner's user name * @param vm_template a VM Template object describing the VM * @param oid the id assigned to the VM (output) * @param error_str Returns the error reason, if any @@ -51,7 +50,6 @@ public: */ int allocate ( int uid, - string user_name, VirtualMachineTemplate * vm_template, int * oid, string& error_str, @@ -146,7 +144,6 @@ public: * pool * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all - * @param extended condition to include history and username data * @param state include only VMs in this state. -1 means any state, * except DONE * @@ -161,7 +158,7 @@ private: */ PoolObjectSQL * create() { - return new VirtualMachine(-1,-1,"", 0); + return new VirtualMachine(-1,-1, 0); }; }; diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 5df5114888..b1575a3551 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -187,14 +187,6 @@ private: // Virtual Network Private Attributes // ************************************************************************* - // ------------------------------------------------------------------------- - // Identification variables - // ------------------------------------------------------------------------- - /** - * Owner's name - */ - string user_name; - // ------------------------------------------------------------------------- // Binded physical attributes // ------------------------------------------------------------------------- @@ -269,7 +261,6 @@ private: //************************************************************************** VirtualNetwork(int uid, - string _user_name, VirtualNetworkTemplate * _vn_template = 0); ~VirtualNetwork(); diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index 09e908cf93..3483c2a192 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -44,14 +44,14 @@ public: * @param uid user identifier * @param vn_template a VirtualNetworkTemplate describing the VNET * @param oid the id assigned to the VM (output) + * @param error_str Returns the error reason, if any * @return oid on success, -1 error */ int allocate ( - int uid, - string user_name, - VirtualNetworkTemplate * vn_template, - int * oid, - string& error_str); + int uid, + VirtualNetworkTemplate * vn_template, + int * oid, + string& error_str); /** * Function to get a VN from the pool, if the object is not in memory @@ -154,7 +154,7 @@ private: */ PoolObjectSQL * create() { - return new VirtualNetwork(0,"",0); + return new VirtualNetwork(0, 0); }; }; diff --git a/include/test/PoolTest.h b/include/test/PoolTest.h index 1e5afe32d9..d3983f869f 100644 --- a/include/test/PoolTest.h +++ b/include/test/PoolTest.h @@ -68,6 +68,45 @@ protected: PoolTest():pool(0){}; virtual ~PoolTest(){}; + /** + * Replaces all elements, padding them with 0 + */ + string fix_regtimes(string& xml) + { + return fix_time(xml, "REGTIME"); + } + + string fix_stimes(string& xml) + { + return fix_time(xml, "STIME"); + } + + string fix_time(string& xml, string elem_name) + { + string start = "<" + elem_name + ">"; + string end = ""; + + string replacement = "0000000000"; + unsigned int pos = 0; + unsigned int end_pos = 0; + + unsigned int length; + + pos = xml.find(start, pos+1); + + while( pos != xml.npos ) + { + end_pos = xml.find(end, pos); + length = end_pos - pos - start.size(); + + xml.replace( pos+start.size(), length, replacement, 0, length); + + pos = xml.find(start, pos+2); + } + + return xml; + } + public: void setUp() diff --git a/src/image/Image.cc b/src/image/Image.cc index 58d6cda259..53d4e30abb 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -34,11 +34,9 @@ /* Image :: Constructor/Destructor */ /* ************************************************************************ */ -Image::Image(int _uid, - const string& _user_name, +Image::Image(int _uid, ImageTemplate * _image_template): PoolObjectSQL(-1,"",_uid,table), - user_name(_user_name), type(OS), regtime(time(0)), source("-"), @@ -335,7 +333,6 @@ string& Image::to_xml(string& xml) const "" << "" << oid << "" << "" << uid << "" << - "" << user_name << "" << "" << name << "" << "" << type << "" << "" << public_img << "" << @@ -369,7 +366,6 @@ int Image::from_xml(const string& xml) // Get class base attributes rc += xpath(oid, "/IMAGE/ID", -1); rc += xpath(uid, "/IMAGE/UID", -1); - rc += xpath(user_name, "/IMAGE/USERNAME", "not_found"); rc += xpath(name, "/IMAGE/NAME", "not_found"); rc += xpath(int_type, "/IMAGE/TYPE", 0); diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index b59c8470a9..85210c8952 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -56,7 +56,6 @@ ImagePool::ImagePool(SqlDB * db, int ImagePool::allocate ( int uid, - string user_name, ImageTemplate* img_template, int * oid, string& error_str) @@ -66,7 +65,7 @@ int ImagePool::allocate ( string name; ostringstream oss; - img = new Image(uid, user_name, img_template); + img = new Image(uid, img_template); // Check name img->get_template_attribute("NAME", name); diff --git a/src/image/test/ImagePoolTest.cc b/src/image/test/ImagePoolTest.cc index c801177e94..dfadbe640d 100644 --- a/src/image/test/ImagePoolTest.cc +++ b/src/image/test/ImagePoolTest.cc @@ -24,7 +24,6 @@ using namespace std; const int uids[] = {0,1,2}; -const string user_names[] = {"A user","B user","C user"}; const string names[] = {"Image one", "Second Image", "The third image"}; @@ -50,21 +49,20 @@ const string templates[] = const string xmls[] = { - "00A userImage one0010000000000-40", + "00Image one0010000000000-40", - "11B userSecond Image0100000000000-40", + "11Second Image0100000000000-40", - "02C userThe third image0000000000000-40" + "02The third image0000000000000-40" }; // This xml dump result has the STIMEs modified to 0000000000 const string xml_dump = -"00A userImage one0010000000000-4011B userSecond Image0100000000000-4022C userThe third image0000000000000-40"; +"00Image one0010000000000-4011Second Image0100000000000-4022The third image0000000000000-40"; const string xml_dump_where = -"00A userImage one0010000000000-4011B userSecond Image0100000000000-40"; +"00Image one0010000000000-4011Second Image0100000000000-40"; -const string replacement = "0000000000"; class ImagePoolFriend : public ImagePool { @@ -90,7 +88,7 @@ public: if( rc == 0 ) { - return ImagePool::allocate(uid, user_names[uid], img_template, oid, err); + return ImagePool::allocate(uid, img_template, oid, err); } else { @@ -167,7 +165,7 @@ protected: // Get the xml and replace the REGTIME to 0, so we can compare // it. ((Image*)obj)->to_xml(xml_str); - xml_str.replace( xml_str.find("")+9, 10, replacement); + fix_regtimes(xml_str); /* if( xml_str != xmls[index] ) @@ -868,17 +866,14 @@ public: CPPUNIT_ASSERT(rc == 0); string result = oss.str(); - - result.replace(157, 10, replacement); - result.replace(1076, 10, replacement); - result.replace(1535, 10, replacement); + fix_regtimes(result); /* if( result != xml_dump ) { cout << endl << result << endl << xml_dump << endl; } -*/ +//*/ CPPUNIT_ASSERT( result == xml_dump ); } @@ -904,15 +899,14 @@ public: CPPUNIT_ASSERT(rc == 0); string result = oss.str(); - result.replace(157, 10, replacement); - result.replace(1076, 10, replacement); - + fix_regtimes(result); +/* if( result != xml_dump_where ) { cout << endl << result << endl << xml_dump_where << endl; } - +//*/ CPPUNIT_ASSERT( result == xml_dump_where ); } diff --git a/src/lcm/test/LifeCycleManagerTest.cc b/src/lcm/test/LifeCycleManagerTest.cc index 23672f7c21..70d0adbc42 100644 --- a/src/lcm/test/LifeCycleManagerTest.cc +++ b/src/lcm/test/LifeCycleManagerTest.cc @@ -185,7 +185,7 @@ private: if( rc == 0 ) { - return vmpool->allocate(uids[index], "username", vm_template, &oid, + return vmpool->allocate(uids[index], vm_template, &oid, err, false); } else diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 2de7a853ac..06f34ffd59 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -29,7 +29,6 @@ void RequestManager::VirtualMachineAllocate::execute( string session; string str_template; string error_str; - string user_name; string template_id_str = "TEMPLATE_ID";; const string method_name = "VirtualMachineAllocate"; @@ -44,7 +43,6 @@ void RequestManager::VirtualMachineAllocate::execute( VirtualMachineTemplate * vm_template; VirtualMachineTemplate * vm_template_aux; - User * user; VMTemplate * registered_template; bool using_template_pool; int template_owner; @@ -198,26 +196,10 @@ void RequestManager::VirtualMachineAllocate::execute( } } - //-------------------------------------------------------------------------- - // Get the User Name - //-------------------------------------------------------------------------- - - user = VirtualMachineAllocate::upool->get(uid,true); - - if ( user == 0 ) - { - goto error_user_get; - } - - user_name = user->get_name(); - - user->unlock(); - //-------------------------------------------------------------------------- // Allocate the VirtualMAchine //-------------------------------------------------------------------------- rc = VirtualMachineAllocate::vmpool->allocate(uid, - user_name, vm_template, &vid, error_str, @@ -244,12 +226,6 @@ error_template_get: delete vm_template; goto error_common; -error_user_get: - oss.str(get_error(method_name, "USER", uid)); - - delete vm_template; - goto error_common; - error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; diff --git a/src/rm/RequestManagerImageAllocate.cc b/src/rm/RequestManagerImageAllocate.cc index 8e435f57c5..05fa520585 100644 --- a/src/rm/RequestManagerImageAllocate.cc +++ b/src/rm/RequestManagerImageAllocate.cc @@ -31,10 +31,8 @@ void RequestManager::ImageAllocate::execute( string session; string str_template; string error_str; - string user_name; ImageTemplate * img_template = 0; - User * user; int iid; int uid; @@ -101,27 +99,11 @@ void RequestManager::ImageAllocate::execute( } } - //-------------------------------------------------------------------------- - // Get the User Name - //-------------------------------------------------------------------------- - - user = ImageAllocate::upool->get(uid,true); - - if ( user == 0 ) - { - goto error_user_get; - } - - user_name = user->get_name(); - - user->unlock(); - //-------------------------------------------------------------------------- // Allocate the Image //-------------------------------------------------------------------------- - rc = ImageAllocate::ipool->allocate(uid,user_name, - img_template,&iid, error_str); + rc = ImageAllocate::ipool->allocate(uid, img_template, &iid, error_str); if ( rc < 0 ) { @@ -148,11 +130,6 @@ void RequestManager::ImageAllocate::execute( return; -error_user_get: - oss.str(get_error(method_name, "USER", uid)); - delete img_template; - goto error_common; - error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; diff --git a/src/rm/RequestManagerSaveDisk.cc b/src/rm/RequestManagerSaveDisk.cc index 7b62095cf8..aea514ea97 100644 --- a/src/rm/RequestManagerSaveDisk.cc +++ b/src/rm/RequestManagerSaveDisk.cc @@ -34,7 +34,6 @@ void RequestManager::VirtualMachineSaveDisk::execute( string img_name; int vm_owner; - string user_name; int rc; int uid; @@ -47,7 +46,6 @@ void RequestManager::VirtualMachineSaveDisk::execute( VirtualMachine * vm; Image * image; ImageTemplate * img_template; - User * user; Image * source_img; int source_img_id; @@ -79,17 +77,6 @@ void RequestManager::VirtualMachineSaveDisk::execute( goto error_authenticate; } - user = VirtualMachineSaveDisk::upool->get(uid,true); - - if ( user == 0 ) - { - goto error_user_get; - } - - user_name = user->get_name(); - - user->unlock(); - //------------------------------------------------------------------------- // Check that the image does not exist & prepare the template //------------------------------------------------------------------------- @@ -148,8 +135,7 @@ void RequestManager::VirtualMachineSaveDisk::execute( //-------------------------------------------------------------------------- // Create the image //-------------------------------------------------------------------------- - rc = VirtualMachineSaveDisk::ipool->allocate(uid,user_name,img_template, - &iid,estr); + rc = VirtualMachineSaveDisk::ipool->allocate(uid, img_template, &iid,estr); if ( rc < 0 ) { @@ -245,10 +231,6 @@ error_vm_get_disk_id: vm->unlock(); goto error_common; -error_user_get: - oss.str(get_error(method_name, "USER", uid)); - goto error_common; - error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; diff --git a/src/rm/RequestManagerTemplateAllocate.cc b/src/rm/RequestManagerTemplateAllocate.cc index 6ae07c1a44..2c0b98ecf0 100644 --- a/src/rm/RequestManagerTemplateAllocate.cc +++ b/src/rm/RequestManagerTemplateAllocate.cc @@ -29,7 +29,6 @@ void RequestManager::TemplateAllocate::execute( string session; string str_template; string error_str; - string user_name; const string method_name = "TemplateAllocate"; @@ -42,7 +41,6 @@ void RequestManager::TemplateAllocate::execute( xmlrpc_c::value_array * arrayresult; VirtualMachineTemplate * template_contents; - User * user; char * error_msg = 0; @@ -94,26 +92,10 @@ void RequestManager::TemplateAllocate::execute( } } - //-------------------------------------------------------------------------- - // Get the User Name - //-------------------------------------------------------------------------- - - user = TemplateAllocate::upool->get(uid,true); - - if ( user == 0 ) - { - goto error_user_get; - } - - user_name = user->get_name(); - - user->unlock(); - //-------------------------------------------------------------------------- // Allocate the VMTemplate //-------------------------------------------------------------------------- rc = TemplateAllocate::tpool->allocate(uid, - user_name, template_contents, &oid, error_str); @@ -135,12 +117,6 @@ void RequestManager::TemplateAllocate::execute( return; -error_user_get: - oss.str(get_error(method_name, "USER", uid)); - - delete template_contents; - goto error_common; - error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; diff --git a/src/rm/RequestManagerVirtualNetworkAllocate.cc b/src/rm/RequestManagerVirtualNetworkAllocate.cc index 6d66296c8c..fafb957701 100644 --- a/src/rm/RequestManagerVirtualNetworkAllocate.cc +++ b/src/rm/RequestManagerVirtualNetworkAllocate.cc @@ -29,12 +29,10 @@ void RequestManager::VirtualNetworkAllocate::execute( { string session; string name; - string user_name; string str_template; string error_str; VirtualNetworkTemplate * vn_template; - User * user; int nid; int uid; @@ -99,25 +97,10 @@ void RequestManager::VirtualNetworkAllocate::execute( } } - //-------------------------------------------------------------------------- - // Get the User Name - //-------------------------------------------------------------------------- - - user = VirtualNetworkAllocate::upool->get(uid,true); - - if ( user == 0 ) - { - goto error_user_get; - } - - user_name = user->get_name(); - - user->unlock(); - //-------------------------------------------------------------------------- // Allocate the Virtual Network //-------------------------------------------------------------------------- - rc = vnpool->allocate(uid,user_name,vn_template,&nid,error_str); + rc = vnpool->allocate(uid,vn_template,&nid,error_str); if ( rc < 0 ) { @@ -136,12 +119,6 @@ void RequestManager::VirtualNetworkAllocate::execute( return; -error_user_get: - oss.str(get_error(method_name, "USER", uid)); - - delete vn_template; - goto error_common; - error_authenticate: oss.str(authenticate_error(method_name)); goto error_common; diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 83b0af12d2..d9f8b007f8 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -38,10 +38,8 @@ VirtualMachine::VirtualMachine(int id, int _uid, - string _user_name, VirtualMachineTemplate * _vm_template): PoolObjectSQL(id,"",_uid,table), - user_name(_user_name), last_poll(0), state(INIT), lcm_state(LCM_INIT), @@ -1106,7 +1104,6 @@ string& VirtualMachine::to_xml(string& xml) const oss << "" << "" << oid << "" << "" << uid << "" - << "" << user_name << "" << "" << name << "" << "" << last_poll << "" << "" << state << "" @@ -1146,7 +1143,6 @@ int VirtualMachine::from_xml(const string &xml_str) // Get class base attributes rc += xpath(oid, "/VM/ID", -1); rc += xpath(uid, "/VM/UID", -1); - rc += xpath(user_name, "/VM/USERNAME", "not_found"); rc += xpath(name, "/VM/NAME", "not_found"); rc += xpath(last_poll, "/VM/LAST_POLL",0); diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index bd8006f8f1..51da344537 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -167,7 +167,6 @@ VirtualMachinePool::VirtualMachinePool(SqlDB * db, int VirtualMachinePool::allocate ( int uid, - string user_name, VirtualMachineTemplate * vm_template, int * oid, string& error_str, @@ -178,7 +177,7 @@ int VirtualMachinePool::allocate ( // ------------------------------------------------------------------------ // Build a new Virtual Machine object // ------------------------------------------------------------------------ - vm = new VirtualMachine(-1, uid, user_name, vm_template); + vm = new VirtualMachine(-1, uid, vm_template); if (on_hold == true) { diff --git a/src/vm/test/VirtualMachinePoolTest.cc b/src/vm/test/VirtualMachinePoolTest.cc index a1c7e7010e..3c4dc52f5d 100644 --- a/src/vm/test/VirtualMachinePoolTest.cc +++ b/src/vm/test/VirtualMachinePoolTest.cc @@ -25,7 +25,6 @@ using namespace std; const int uids[] = {123, 261, 123}; -const string user_names[] = {"A user","B user","C user"}; const string names[] = {"VM one", "Second VM", "VM one"}; @@ -47,21 +46,21 @@ const string templates[] = const string xmls[] = { - "0123A userVM one00123VM one010000000000000000", - "1261B userSecond VM0" + "1261Second VM0" "1000000000000<" "/ETIME>0000", - "0123A userVM one00123VM one010000000000000000