From 21fe3116818cfceda4cb84b02662b3f475eb8ce9 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 24 Jul 2018 11:41:41 +0200 Subject: [PATCH] F #2183: Use pager for list commands (e.g. onevm list). Support for custom ordering (by ID) in list API calls, this can be set per user, group and default in oned.conf --- include/ClusterPool.h | 6 +- include/DatastorePool.h | 6 +- include/DocumentPool.h | 6 +- include/GroupPool.h | 4 +- include/HostPool.h | 6 +- include/ImagePool.h | 7 +- include/MarketPlaceAppPool.h | 5 +- include/MarketPlacePool.h | 5 +- include/PoolSQL.h | 18 +- include/SecurityGroupPool.h | 6 +- include/UserPool.h | 4 +- include/VMGroupPool.h | 6 +- include/VMTemplatePool.h | 6 +- include/VdcPool.h | 6 +- include/VirtualMachinePool.h | 6 +- include/VirtualNetworkPool.h | 6 +- include/VirtualRouterPool.h | 6 +- include/ZonePool.h | 6 +- share/etc/oned.conf | 18 +- src/cli/cli_helper.rb | 23 ++- src/cli/one_helper.rb | 268 +++++++++++++++++++++---- src/group/GroupPool.cc | 8 +- src/nebula/NebulaTemplate.cc | 2 + src/oca/ruby/opennebula/pool.rb | 53 +++-- src/oca/ruby/opennebula/xml_element.rb | 27 +-- src/pool/PoolSQL.cc | 7 +- src/rm/RequestManagerPoolInfoFilter.cc | 21 +- src/um/UserPool.cc | 8 +- 28 files changed, 418 insertions(+), 132 deletions(-) diff --git a/include/ClusterPool.h b/include/ClusterPool.h index 898de8f4e7..31853beca2 100644 --- a/include/ClusterPool.h +++ b/include/ClusterPool.h @@ -186,13 +186,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "CLUSTER_POOL", Cluster::table, where, - limit); + limit, desc); }; /** diff --git a/include/DatastorePool.h b/include/DatastorePool.h index 3b791603f5..de8040b599 100644 --- a/include/DatastorePool.h +++ b/include/DatastorePool.h @@ -131,13 +131,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "DATASTORE_POOL", Datastore::table, where, - limit); + limit, desc); }; /** diff --git a/include/DocumentPool.h b/include/DocumentPool.h index 285f94d047..0043c07891 100644 --- a/include/DocumentPool.h +++ b/include/DocumentPool.h @@ -82,13 +82,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "DOCUMENT_POOL", Document::table, where, - limit); + limit, desc); }; /** diff --git a/include/GroupPool.h b/include/GroupPool.h index e9cdfcaedb..5a5b957fad 100644 --- a/include/GroupPool.h +++ b/include/GroupPool.h @@ -148,10 +148,12 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit); + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc); private: diff --git a/include/HostPool.h b/include/HostPool.h index 69a7041efe..65ccd8728d 100644 --- a/include/HostPool.h +++ b/include/HostPool.h @@ -221,12 +221,14 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { - return PoolSQL::dump(oss, "HOST_POOL", Host::table, where, limit); + return PoolSQL::dump(oss, "HOST_POOL", Host::table, where, limit, desc); }; /** diff --git a/include/ImagePool.h b/include/ImagePool.h index 621bffbea2..2da8850144 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -134,12 +134,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { - return PoolSQL::dump(oss, "IMAGE_POOL", Image::table, where, limit); + return PoolSQL::dump(oss, "IMAGE_POOL", Image::table, where, limit, + desc); } /** diff --git a/include/MarketPlaceAppPool.h b/include/MarketPlaceAppPool.h index 3d998ae45c..9db95fff4e 100644 --- a/include/MarketPlaceAppPool.h +++ b/include/MarketPlaceAppPool.h @@ -122,14 +122,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ int dump(std::ostringstream& oss, const std::string& where, - const std::string& limit) + const std::string& limit, bool desc) { return PoolSQL::dump(oss, "MARKETPLACEAPP_POOL", MarketPlaceApp::table, - where, limit); + where, limit, desc); }; /** Update a particular MarketPlaceApp diff --git a/include/MarketPlacePool.h b/include/MarketPlacePool.h index daaf6b0605..07826c9c3c 100644 --- a/include/MarketPlacePool.h +++ b/include/MarketPlacePool.h @@ -102,14 +102,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ int dump(std::ostringstream& oss, const std::string& where, - const std::string& limit) + const std::string& limit, bool desc) { return PoolSQL::dump(oss, "MARKETPLACE_POOL", MarketPlace::table, where, - limit); + limit, desc); }; /** diff --git a/include/PoolSQL.h b/include/PoolSQL.h index 275f19508d..e5f5e67f28 100644 --- a/include/PoolSQL.h +++ b/include/PoolSQL.h @@ -168,12 +168,13 @@ public: * query * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where) + int dump(ostringstream& oss, const string& where, bool desc) { - return dump(oss, where, ""); + return dump(oss, where, "", desc); } /** @@ -182,12 +183,13 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ virtual int dump(ostringstream& oss, const string& where, - const string& limit) = 0; + const string& limit, bool desc) = 0; // ------------------------------------------------------------------------- // Function to generate dump filters @@ -276,6 +278,7 @@ protected: * @param table Pool table name * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ @@ -283,7 +286,8 @@ protected: const string& elem_name, const char * table, const string& where, - const string& limit); + const string& limit, + bool desc); /** * Dumps the pool in XML format. A filter can be also added to the @@ -292,15 +296,17 @@ protected: * @param elem_name Name of the root xml pool name * @param table Pool table name * @param where filter for the objects, defaults to all + * @param desc descending order of pool elements * * @return 0 on success */ int dump(ostringstream& oss, const string& elem_name, const char * table, - const string& where) + const string& where, + bool desc) { - return dump(oss, elem_name, table, where, ""); + return dump(oss, elem_name, table, where, "", desc); } /** diff --git a/include/SecurityGroupPool.h b/include/SecurityGroupPool.h index f7df849acc..243a046392 100644 --- a/include/SecurityGroupPool.h +++ b/include/SecurityGroupPool.h @@ -96,13 +96,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "SECURITY_GROUP_POOL", SecurityGroup::table, - where, limit); + where, limit, desc); }; /** diff --git a/include/UserPool.h b/include/UserPool.h index 170dc8a394..d5ff328edb 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -178,10 +178,12 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit); + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc); /** * Name for the OpenNebula core authentication process diff --git a/include/VMGroupPool.h b/include/VMGroupPool.h index e965395e90..bb685a6db0 100644 --- a/include/VMGroupPool.h +++ b/include/VMGroupPool.h @@ -100,13 +100,15 @@ public: * @param os the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ int dump(std::ostringstream& os, const std::string& where, - const std::string& limit) + const std::string& limit, bool desc) { - return PoolSQL::dump(os, "VM_GROUP_POOL", VMGroup::table, where, limit); + return PoolSQL::dump(os, "VM_GROUP_POOL", VMGroup::table, where, limit, + desc); }; /** diff --git a/include/VMTemplatePool.h b/include/VMTemplatePool.h index a60a8e006d..881647d00f 100644 --- a/include/VMTemplatePool.h +++ b/include/VMTemplatePool.h @@ -73,13 +73,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "VMTEMPLATE_POOL", VMTemplate::table, where, - limit); + limit, desc); }; /** diff --git a/include/VdcPool.h b/include/VdcPool.h index 55d6848612..c4818eaec2 100644 --- a/include/VdcPool.h +++ b/include/VdcPool.h @@ -91,12 +91,14 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { - return PoolSQL::dump(oss, "VDC_POOL", Vdc::table, where, limit); + return PoolSQL::dump(oss, "VDC_POOL", Vdc::table, where, limit, desc); }; /** diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index 5c4a83f95e..a8f2902ed2 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -246,13 +246,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "VM_POOL", VirtualMachine::table, where, - limit); + limit, desc); }; /** diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index 4043e9d379..591485541b 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -133,13 +133,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "VNET_POOL", VirtualNetwork::table, where, - limit); + limit, desc); } /** diff --git a/include/VirtualRouterPool.h b/include/VirtualRouterPool.h index b26b49fb35..8af87cc10f 100644 --- a/include/VirtualRouterPool.h +++ b/include/VirtualRouterPool.h @@ -84,13 +84,15 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { return PoolSQL::dump(oss, "VROUTER_POOL", VirtualRouter::table, where, - limit); + limit, desc); }; /** diff --git a/include/ZonePool.h b/include/ZonePool.h index b78ab12063..3f9643db44 100644 --- a/include/ZonePool.h +++ b/include/ZonePool.h @@ -92,12 +92,14 @@ public: * @param oss the output stream to dump the pool contents * @param where filter for the objects, defaults to all * @param limit parameters used for pagination + * @param desc descending order of pool elements * * @return 0 on success */ - int dump(ostringstream& oss, const string& where, const string& limit) + int dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { - return PoolSQL::dump(oss, "ZONE_POOL", Zone::table, where, limit); + return PoolSQL::dump(oss, "ZONE_POOL", Zone::table, where, limit, desc); }; /** diff --git a/share/etc/oned.conf b/share/etc/oned.conf index b53f95613b..38faa242b3 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -103,6 +103,16 @@ VNC_PORTS = [ # RESERVED = "6800, 6801, 6810:6820, 9869" ] +#******************************************************************************* +# API configuration attributes +#------------------------------------------------------------------------------- +# VM_SUBMIT_ON_HOLD: Forces VMs to be created on hold state instead of pending. +# Values: YES or NO. +# API_LIST_ORDER: Sets order (by ID) of elements in list API calls. +# Values: ASC (ascending order) or DESC (descending order) +#******************************************************************************* +# +#API_LIST_ORDER = "DESC" #VM_SUBMIT_ON_HOLD = "NO" #******************************************************************************* @@ -313,14 +323,6 @@ VXLAN_IDS = [ # vd KVM virtual disk # # DEFAULT_CDROM_DEVICE_PREFIX: Same as above but for CDROM devices. -# -# DEFAULT_IMAGE_PERSISTENT: Control the default value for the PERSISTENT -# attribute on image creation (oneimage clone, onevm disk-saveas). If blank -# images will inherit the persistent attribute from the base image. -# -# DEFAULT_IMAGE_PERSISTENT_NEW: Control the default value for the PERSISTENT -# attribute on image creation (oneimage create). By default images are no -# persistent if not set. #******************************************************************************* #DATASTORE_LOCATION = /var/lib/one/datastores diff --git a/src/cli/cli_helper.rb b/src/cli/cli_helper.rb index eb17e05194..dbb144560c 100644 --- a/src/cli/cli_helper.rb +++ b/src/cli/cli_helper.rb @@ -132,16 +132,21 @@ module CLIHelper # Print header def CLIHelper.print_header(str, underline=true) if $stdout.tty? - scr_bold - scr_underline if underline - print str - scr_restore + print_tty_header(str, underline) else print str end puts end + # Pretty print header + def CLIHelper.print_tty_header(str, underline=true) + scr_bold + scr_underline if underline + print str + scr_restore + end + module HashWithSearch def dsearch(path) stems=path.split('/') @@ -256,10 +261,18 @@ module CLIHelper end end + def print_tty_header + CLIHelper.print_tty_header(header_str) + puts + end + private def print_table(data, options) - CLIHelper.print_header(header_str) if !options[:csv] + if !options[:csv] && !options[:noheader] + CLIHelper.print_header(header_str) + end + data ? print_data(data, options) : puts end diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 9d33bc643b..09da351e47 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -16,6 +16,9 @@ require 'cli_helper' +require 'open3' +require 'io/console' + begin require 'opennebula' rescue Exception => e @@ -505,51 +508,243 @@ EOT end end + #----------------------------------------------------------------------- + # List pool functions + #----------------------------------------------------------------------- + def start_pager + pager = ENV['ONE_PAGER'] || 'less' + + # Start pager, defaults to less + p_r, p_w = IO.pipe + + lpid = fork do + $stdin.reopen(p_r) + + p_r.close + p_w.close + + Kernel.select [$stdin] + + exec([pager, pager]) + end + + # Send listing to pager pipe + $stdout.close + $stdout = p_w.dup + + p_w.close + p_r.close + + return lpid + end + + def stop_pager(lpid) + $stdout.close + + Process.wait(lpid) + end + + def print_page(pool, options) + page = nil + + if options[:xml] + elements = 0 + page = "" + + pool.each {|e| + elements += 1 + page << e.to_xml(true) << "\n" + } + else + + pname = pool.pool_name + ename = pool.element_name + + page = pool.to_hash + elems = page["#{pname}"]["#{ename}"] + + if elems.class == Array + elements = elems.length + else + elements = 1 + end + end + + return elements, page + end + + #----------------------------------------------------------------------- + # List the pool in table form, it uses pagination for interactive + # output + #----------------------------------------------------------------------- + def list_pool_table(table, pool, options, filter_flag) + if $stdout.isatty + size = $stdout.winsize[0] - 1 + + # ----------- First page, check if pager is needed ------------- + rc = pool.get_page(size, 0) + ps = "" + + return -1, rc.message if OpenNebula.is_error?(rc) + + elements, hash = print_page(pool, options) + + ppid = -1 + + if elements >= size + ppid = start_pager + end + + table.show(hash, options) + + if elements < size + return 0 + end + + # ------- Rest of the pages in the pool, piped to pager -------- + current = size + + options[:noheader] = true + + loop do + rc = pool.get_page(size, current) + + return -1, rc.message if OpenNebula.is_error?(rc) + + current += size + + elements, hash = print_page(pool, options) + + table.show(hash, options) + + $stdout.flush + + break if elements < size + end + + stop_pager(ppid) + else + array = pool.get_hash + return -1, array.message if OpenNebula.is_error?(array) + + rname = self.class.rname + elements = array["#{rname}_POOL"][rname] + + if options[:ids] && elements + elements.reject! do |element| + !options[:ids].include?(element['ID'].to_i) + end + end + + table.show(array, options) + end + + return 0 + end + + #----------------------------------------------------------------------- + # List pool in XML format, pagination is used in interactive output + #----------------------------------------------------------------------- + def list_pool_xml(pool, options, filter_flag) + if $stdout.isatty + size = $stdout.winsize[0] - 1 + + # ----------- First page, check if pager is needed ------------- + rc = pool.get_page(size, 0) + ps = "" + + return -1, rc.message if OpenNebula.is_error?(rc) + + pname = pool.pool_name + + elements, page = print_page(pool, options) + + ppid = -1 + + if elements >= size + ppid = start_pager + end + + puts "<#{pname}>" + + puts page + + if elements < size + puts "" + return 0 + end + + # ------- Rest of the pages in the pool, piped to pager -------- + current = size + + loop do + rc = pool.get_page(size, current) + + return -1, rc.message if OpenNebula.is_error?(rc) + + current += size + + elements, page = print_page(pool, options) + + puts page + + $stdout.flush + + break if elements < size + end + + puts "" + + stop_pager(ppid) + else + rc = pool.info + + return -1, rc.message if OpenNebula.is_error?(rc) + + puts pool.to_xml(true) + end + + return 0 + end + + #----------------------------------------------------------------------- + # List pool table in top-like form + #----------------------------------------------------------------------- + def list_pool_top(table, pool, options) + table.top(options) { + array = pool.get_hash + + return -1, array.message if OpenNebula.is_error?(array) + + array + } + + return 0 + end + def list_pool(options, top=false, filter_flag=nil) - if options[:describe] - table = format_pool(options) + table = format_pool(options) + if options[:describe] table.describe_columns + return 0 end filter_flag ||= OpenNebula::Pool::INFO_ALL - pool = factory_pool(filter_flag) + pool = factory_pool(filter_flag) - if options[:xml] - # TODO: use paginated functions - rc=pool.info - return -1, rc.message if OpenNebula.is_error?(rc) - return 0, pool.to_xml(true) + if top + return list_pool_top(table, pool, options) + elsif options[:xml] + return list_pool_xml(pool, options, filter_flag) else - table = format_pool(options) - - if top - table.top(options) { - array=pool.get_hash - return -1, array.message if OpenNebula.is_error?(array) - - array - } - else - array=pool.get_hash - return -1, array.message if OpenNebula.is_error?(array) - - rname=self.class.rname - elements=array["#{rname}_POOL"][rname] - if options[:ids] && elements - elements.reject! do |element| - !options[:ids].include?(element['ID'].to_i) - end - end - - table.show(array, options) - end - - return 0 + return list_pool_table(table, pool, options, filter_flag) end + + return 0 end def show_resource(id, options) @@ -834,8 +1029,11 @@ EOT end end - def OpenNebulaHelper.time_to_str(time, print_seconds=true, print_hours=true, print_years=false) - value=time.to_i + def OpenNebulaHelper.time_to_str(time, print_seconds=true, + print_hours=true, print_years=false) + + value = time.to_i + if value==0 value='-' else diff --git a/src/group/GroupPool.cc b/src/group/GroupPool.cc index 192457d44c..d8d55de693 100644 --- a/src/group/GroupPool.cc +++ b/src/group/GroupPool.cc @@ -222,7 +222,8 @@ int GroupPool::drop(PoolObjectSQL * objsql, string& error_msg) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int GroupPool::dump(ostringstream& oss, const string& where, const string& limit) +int GroupPool::dump(ostringstream& oss, const string& where, + const string& limit, bool desc) { int rc; string def_quota_xml; @@ -241,6 +242,11 @@ int GroupPool::dump(ostringstream& oss, const string& where, const string& limit cmd << " ORDER BY oid"; + if ( desc == true ) + { + cmd << " DESC"; + } + if ( !limit.empty() ) { cmd << " LIMIT " << limit; diff --git a/src/nebula/NebulaTemplate.cc b/src/nebula/NebulaTemplate.cc index 5bd5c4e182..8351bcfadf 100644 --- a/src/nebula/NebulaTemplate.cc +++ b/src/nebula/NebulaTemplate.cc @@ -383,6 +383,7 @@ void OpenNebulaTemplate::set_conf_default() # DB # SCRIPTS_REMOTE_DIR # VM_SUBMIT_ON_HOLD +# API_LIST_ORDER # VNC_PORTS #******************************************************************************* */ @@ -401,6 +402,7 @@ void OpenNebulaTemplate::set_conf_default() set_conf_single("LISTEN_ADDRESS", "0.0.0.0"); set_conf_single("SCRIPTS_REMOTE_DIR", "/var/tmp/one"); set_conf_single("VM_SUBMIT_ON_HOLD", "NO"); + set_conf_single("API_LIST_ORDER", "DESC"); //DB CONFIGURATION vvalue.insert(make_pair("BACKEND","sqlite")); diff --git a/src/oca/ruby/opennebula/pool.rb b/src/oca/ruby/opennebula/pool.rb index fda31a8aa8..99e6a83e85 100644 --- a/src/oca/ruby/opennebula/pool.rb +++ b/src/oca/ruby/opennebula/pool.rb @@ -24,6 +24,9 @@ module OpenNebula include Enumerable alias_method :each_with_xpath, :each + attr_reader :pool_name + attr_reader :element_name + PAGINATED_POOLS=%w{VM_POOL IMAGE_POOL TEMPLATE_POOL VN_POOL DOCUMENT_POOL SECGROUP_POOL} @@ -132,12 +135,13 @@ module OpenNebula # xml_method:: _String_ the name of the XML-RPC method # args:: _Array_ with additional arguments for the info call # [return] nil in case of success or an Error object - def xmlrpc_info(xml_method,*args) - rc = @client.call(xml_method,*args) + def xmlrpc_info(xml_method, *args) + rc = @client.call(xml_method, *args) if !OpenNebula.is_error?(rc) - initialize_xml(rc,@pool_name) - rc = nil + initialize_xml(rc, @pool_name) + + rc = nil end return rc @@ -179,14 +183,17 @@ module OpenNebula if OpenNebula.pool_page_size && allow_paginated && ( ( size && size >= 2 ) || !size ) + size = OpenNebula.pool_page_size if !size - hash=info_paginated(size) + hash = info_paginated(size) return hash if OpenNebula.is_error?(hash) + { @pool_name => { @element_name => hash } } else - rc=info + rc = info return rc if OpenNebula.is_error?(rc) + to_hash end end @@ -195,14 +202,15 @@ module OpenNebula # # size:: _Integer_ size of each page def info_paginated(size) - array=Array.new - current=0 + array = Array.new + current = 0 - parser=ParsePoolSax.new(@pool_name, @element_name) + parser = ParsePoolSax.new(@pool_name, @element_name) while true - a=@client.call("#{@pool_name.delete('_').downcase}.info", - @user_id, current, -size, -1) + a = @client.call("#{@pool_name.delete('_').downcase}.info", + @user_id, current, -size, -1) + return a if OpenNebula.is_error?(a) a_array=parser.parse(a) @@ -214,9 +222,30 @@ module OpenNebula end array.compact! - array=nil if array.length == 0 + array = nil if array.length == 0 array end + + # Gets a hash from a info page from pool + # size:: nil => default page size + # > 0 => page size + # current first element of the page + # hash:: return page as a hash + def get_page(size, current) + rc = nil + + if PAGINATED_POOLS.include?(@pool_name) + size = OpenNebula.pool_page_size if (!size || size == 0) + rc = @client.call("#{@pool_name.delete('_').downcase}.info", + @user_id, current, -size, -1) + + initialize_xml(rc, @pool_name) + else + rc = info + end + + return rc + end end end diff --git a/src/oca/ruby/opennebula/xml_element.rb b/src/oca/ruby/opennebula/xml_element.rb index a89dae5765..e2e7c02776 100644 --- a/src/oca/ruby/opennebula/xml_element.rb +++ b/src/oca/ruby/opennebula/xml_element.rb @@ -430,30 +430,5 @@ module OpenNebula return attr end end - - # The XMLUtilsPool module provides an abstraction of the underlying - # XML parser engine. It provides XML-related methods for the Pools - class XMLPool < XMLElement - - def initialize(xml=nil) - super(xml) - end - - #Executes the given block for each element of the Pool - #block:: _Block_ - def each_element(block) - if NOKOGIRI - @xml.xpath( - "#{@element_name}").each {|pelem| - block.call self.factory(pelem) - } - else - @xml.elements.each( - "#{@element_name}") {|pelem| - block.call self.factory(pelem) - } - end - end - end - end + diff --git a/src/pool/PoolSQL.cc b/src/pool/PoolSQL.cc index 93d82021c1..d790259d31 100644 --- a/src/pool/PoolSQL.cc +++ b/src/pool/PoolSQL.cc @@ -214,7 +214,7 @@ PoolObjectSQL * PoolSQL::get(const string& name, int ouid) /* -------------------------------------------------------------------------- */ int PoolSQL::dump(ostringstream& oss, const string& elem_name, const char* table, - const string& where, const string& limit) + const string& where, const string& limit, bool desc) { ostringstream cmd; @@ -227,6 +227,11 @@ int PoolSQL::dump(ostringstream& oss, const string& elem_name, const char* table cmd << " ORDER BY oid"; + if ( desc == true ) + { + cmd << " DESC"; + } + if ( !limit.empty() ) { cmd << " LIMIT " << limit; diff --git a/src/rm/RequestManagerPoolInfoFilter.cc b/src/rm/RequestManagerPoolInfoFilter.cc index b43b9e0d11..b059594691 100644 --- a/src/rm/RequestManagerPoolInfoFilter.cc +++ b/src/rm/RequestManagerPoolInfoFilter.cc @@ -438,8 +438,11 @@ void RequestManagerPoolInfoFilter::dump( const string& or_clause) { ostringstream oss; - string where_string, limit_clause; - int rc; + + std::string where_string, limit_clause; + std::string desc; + + int rc; if ( filter_flag < GROUP ) { @@ -466,7 +469,11 @@ void RequestManagerPoolInfoFilter::dump( oss.str(""); } - rc = pool->dump(oss, where_string, limit_clause); + Nebula::instance().get_configuration_attribute(att.uid, att.gid, + "API_LIST_ORDER", desc); + + rc = pool->dump(oss, where_string, limit_clause, + one_util::toupper(desc) == "DESC"); if ( rc != 0 ) { @@ -528,10 +535,14 @@ void VirtualNetworkPoolInfo::request_execute( /* ---------------------------------------------------------------------- */ /* Get the VNET pool */ /* ---------------------------------------------------------------------- */ - ostringstream pool_oss; + std::string desc; - int rc = pool->dump(pool_oss, where_string.str(), limit_clause.str()); + Nebula::instance().get_configuration_attribute(att.uid, att.gid, + "API_LIST_ORDER", desc); + + int rc = pool->dump(pool_oss, where_string.str(), limit_clause.str(), + one_util::toupper(desc) == "DESC"); if ( rc != 0 ) { diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index dff678fe7c..e21f205fd2 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -1275,7 +1275,8 @@ int UserPool::authorize(AuthRequest& ar) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int UserPool::dump(ostringstream& oss, const string& where, const string& limit) +int UserPool::dump(ostringstream& oss, const string& where, const string& limit, + bool desc) { int rc; string def_quota_xml; @@ -1294,6 +1295,11 @@ int UserPool::dump(ostringstream& oss, const string& where, const string& limit) cmd << " ORDER BY oid"; + if ( desc == true ) + { + cmd << " DESC"; + } + if ( !limit.empty() ) { cmd << " LIMIT " << limit;