From 33b563da8c9e263b8833d946a30a238f95ccb5a7 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 2 Aug 2011 19:06:43 +0200 Subject: [PATCH] bug #734: Translate resource names for regular users --- src/cli/one_helper.rb | 135 ++++++++++++-------------- src/cli/one_helper/onegroup_helper.rb | 4 +- src/cli/one_helper/oneuser_helper.rb | 2 +- src/cli/oneimage | 8 +- src/cli/onetemplate | 8 +- src/cli/oneuser | 4 +- src/cli/onevm | 12 +-- src/cli/onevnet | 8 +- 8 files changed, 85 insertions(+), 96 deletions(-) diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 42f1d81ee0..e4c60e7756 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -151,14 +151,6 @@ EOT ######################################################################## # Id translation ######################################################################## - def uid_to_str(uid, options={}) - rid_to_str(:users, uid, options) - end - - def gid_to_str(gid, options={}) - rid_to_str(:groups, gid, options) - end - def user_name(resource, options={}) if options[:numeric] resource['UID'] @@ -174,17 +166,17 @@ EOT resource['GNAME'] end end + ######################################################################## # Formatters for arguments ######################################################################## def to_id(name) return 0, name if name.match(/^[0123456789]+$/) - user_flag = -2 - pool = factory_pool(user_flag) + pool = get_pool poolname = self.class.rname - self.class.id_to_name(name, pool, poolname) + OneHelper.name_to_id(name, pool, poolname) end def self.to_id_desc @@ -192,18 +184,22 @@ EOT end def list_to_id(names) - user_flag = -2 - pool = factory_pool(user_flag) - - rc = pool.info - return -1, rc.message if OpenNebula.is_error?(rc) + pool = get_pool + poolname = self.class.rname result = names.split(',').collect { |name| - rc = to_id(name) - unless rc.first==0 - return rc + if name.match(/^[0123456789]+$/) + name + else + rc = OneHelper.name_to_id(name, pool, poolname) + + if rc.first==-1 + return -1, "OpenNebula #{self.rname} #{name} " << + "not found, use the ID instead" + end + + rc[1] end - rc[1] } return 0, result @@ -213,20 +209,36 @@ EOT "Comma-separated list of OpenNebula #{self.rname} names or ids" end + def self.name_to_id(name, pool, ename) + objects=pool.select {|object| object.name==name } + + if objects.length>0 + if objects.length>1 + return -1, "There are multiple #{ename}s with name #{name}." + else + result = objects.first.id + end + else + return -1, "#{ename} named #{name} not found." + end + + return 0, result + end + def filterflag_to_i(str) filter_flag = case str - when "a", "all" then OpenNebula::Pool::INFO_ALL - when "m", "mine" then OpenNebula::Pool::INFO_MINE + when "a", "all" then OpenNebula::Pool::INFO_ALL + when "m", "mine" then OpenNebula::Pool::INFO_MINE when "g", "group" then OpenNebula::Pool::INFO_GROUP else if str.match(/^[0123456789]+$/) str.to_i else - user = translation_hash[:users].select { |k,v| v==str } - if user.length > 0 - user.first.first.to_i + rc = OpenNebulaHelper.rname_to_id(str, "USER") + if rc.first==-1 + return rc else - OpenNebula::Pool::INFO_GROUP + rc[1] end end end @@ -255,26 +267,6 @@ EOT end end - def self.id_to_name(name, pool, ename) - rc = pool.info - return -1, rc.message if OpenNebula.is_error?(rc) - - objects=pool.select {|object| object.name==name } - - if objects.length>0 - if objects.length>1 - return -1, - "There are multiple #{ename}s with name #{name}." - else - result = objects.first.id - end - else - return -1, "#{ename} named #{name} not found." - end - - return 0, result - end - private def retrieve_resource(id) @@ -302,50 +294,47 @@ EOT phash end - def translation_hash - @translation_hash ||= { - :users => generate_resource_translation(UserPool), - :groups => generate_resource_translation(GroupPool) - } - end + def get_pool + user_flag = OpenNebula::Pool::INFO_ALL + pool = factory_pool(user_flag) - def generate_resource_translation(pool) - p = pool.new(@client) - p.info + rc = pool.info + if OpenNebula.is_error?(rc) + user_flag = OpenNebula::Pool::INFO_GROUP + pool = factory_pool(user_flag) - hash = Hash.new - p.each { |r| hash[r["ID"]]=r["NAME"] } - hash - end - - def rid_to_str(resource, id, options) - if options[:numeric] - id - else - if name = translation_hash[resource][id] - name - else - id + rc = pool.info + if OpenNebula.is_error?(rc) + return -1, "OpenNebula #{self.rname} name not found," << + " use the ID instead" end end + + pool end end - def OpenNebulaHelper.name_to_id(name, poolname, user_flag=-2) + def OpenNebulaHelper.rname_to_id(name, poolname) return 0, name.to_i if name.match(/^[0123456789]+$/) client = OpenNebula::Client.new - # TBD user_flag + pool = case poolname - when "HOST" then OpenNebula::HostPool.new(client) + when "HOST" then OpenNebula::HostPool.new(client) when "GROUP" then OpenNebula::GroupPool.new(client) when "USER" then OpenNebula::UserPool.new(client) end - OneHelper.id_to_name(name, pool, poolname) + rc = pool.info + if OpenNebula.is_error?(rc) + return -1, "OpenNebula #{self.rname} name not found," << + " use the ID instead" + end + + OneHelper.name_to_id(name, pool, poolname) end - def OpenNebulaHelper.name_to_id_desc(poolname) + def OpenNebulaHelper.rname_to_id_desc(poolname) "OpenNebula #{poolname} name or id" end diff --git a/src/cli/one_helper/onegroup_helper.rb b/src/cli/one_helper/onegroup_helper.rb index 4dd71390f1..592132a6c5 100644 --- a/src/cli/one_helper/onegroup_helper.rb +++ b/src/cli/one_helper/onegroup_helper.rb @@ -100,9 +100,9 @@ class OneGroupHelper < OpenNebulaHelper::OneHelper puts CLIHelper.print_header(str_h1 % "USERS", false) - CLIHelper.print_header("%-15s %-20s" % ["ID","NAME"]) + CLIHelper.print_header("%-15s" % ["ID"]) group.user_ids.each do |uid| - puts "%-15s %-20s" % [uid, self.uid_to_str(uid.to_s)] + puts "%-15s" % [uid] end end diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index abf112b0fa..f98ad75e73 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -89,7 +89,7 @@ class OneUserHelper < OpenNebulaHelper::OneHelper end column :GROUP, "Group of the User", :left, :size=>8 do |d| - helper.gid_to_str(d["GID"], options) + helper.group_name(d, options) end column :PASSWORD, "Password of the User", :size=>50 do |d| diff --git a/src/cli/oneimage b/src/cli/oneimage index 74c361d020..8bc019d045 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -48,12 +48,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## # Formatters for arguments ######################################################################## - set :format, :groupid, OpenNebulaHelper.name_to_id_desc("GROUP") do |arg| - OpenNebulaHelper.name_to_id(arg, "GROUP") + set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg| + OpenNebulaHelper.rname_to_id(arg, "GROUP") end - set :format, :userid, OpenNebulaHelper.name_to_id_desc("USER") do |arg| - OpenNebulaHelper.name_to_id(arg, "USER") + set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg| + OpenNebulaHelper.rname_to_id(arg, "USER") end set :format, :imageid, OneImageHelper.to_id_desc do |arg| diff --git a/src/cli/onetemplate b/src/cli/onetemplate index e197def2a1..1e412a15d0 100755 --- a/src/cli/onetemplate +++ b/src/cli/onetemplate @@ -48,12 +48,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## # Formatters for arguments ######################################################################## - set :format, :groupid, OpenNebulaHelper.name_to_id_desc("GROUP") do |arg| - OpenNebulaHelper.name_to_id(arg, "GROUP") + set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg| + OpenNebulaHelper.rname_to_id(arg, "GROUP") end - set :format, :userid, OpenNebulaHelper.name_to_id_desc("USER") do |arg| - OpenNebulaHelper.name_to_id(arg, "USER") + set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg| + OpenNebulaHelper.rname_to_id(arg, "USER") end set :format, :templateid, OneTemplateHelper.to_id_desc do |arg| diff --git a/src/cli/oneuser b/src/cli/oneuser index ffb4346786..d1ee7fcfe1 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -64,8 +64,8 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## # Formatters for arguments ######################################################################## - set :format, :groupid, OpenNebulaHelper.name_to_id_desc("GROUP") do |arg| - OpenNebulaHelper.name_to_id(arg, "GROUP") + set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg| + OpenNebulaHelper.rname_to_id(arg, "GROUP") end set :format, :userid, OneUserHelper.to_id_desc do |arg| diff --git a/src/cli/onevm b/src/cli/onevm index 9a1102b450..b6d7014d45 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -44,16 +44,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## # Formatters for arguments ######################################################################## - set :format, :hostid, OpenNebulaHelper.name_to_id_desc("HOST") do |arg| - OpenNebulaHelper.name_to_id(arg, "HOST") + set :format, :hostid, OpenNebulaHelper.rname_to_id_desc("HOST") do |arg| + OpenNebulaHelper.rname_to_id(arg, "HOST") end - set :format, :groupid, OpenNebulaHelper.name_to_id_desc("GROUP") do |arg| - OpenNebulaHelper.name_to_id(arg, "GROUP") + set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg| + OpenNebulaHelper.rname_to_id(arg, "GROUP") end - set :format, :userid, OpenNebulaHelper.name_to_id_desc("USER") do |arg| - OpenNebulaHelper.name_to_id(arg, "USER") + set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg| + OpenNebulaHelper.rname_to_id(arg, "USER") end set :format, :vmid, OneVMHelper.to_id_desc do |arg| diff --git a/src/cli/onevnet b/src/cli/onevnet index 5376291da9..f152a103df 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -44,12 +44,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## # Formatters for arguments ######################################################################## - set :format, :groupid, OpenNebulaHelper.name_to_id_desc("GROUP") do |arg| - OpenNebulaHelper.name_to_id(arg, "GROUP") + set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg| + OpenNebulaHelper.rname_to_id(arg, "GROUP") end - set :format, :userid, OpenNebulaHelper.name_to_id_desc("USER") do |arg| - OpenNebulaHelper.name_to_id(arg, "USER") + set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg| + OpenNebulaHelper.rname_to_id(arg, "USER") end set :format, :vnetid, OneVNetHelper.to_id_desc do |arg|