1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-13 13:17:39 +03:00

bug #734: Translate resource names for regular users

This commit is contained in:
Daniel Molina 2011-08-02 19:06:43 +02:00
parent 98b495080b
commit 33b563da8c
8 changed files with 85 additions and 96 deletions

View File

@ -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

View File

@ -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

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|

View File

@ -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|