appflow: Bug #1444: CLI can use names as range arguments

This commit is contained in:
Carlos Martín authored and Javi Fontan committed 2012-09-18 17:42:54 +02:00
1 parent 70000e6829
commit 0f2c184d03
3 files changed
+65 -9

No files matched your search

+9 -5
View File
@@ -236,6 +236,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Service.rname_to_id(arg, "SERVICE")
end
set :format, :serviceid_list, Service.list_to_id_desc("SERVICE") do |arg|
Service.list_to_id(arg, "SERVICE")
end
#
# List
#
@@ -326,7 +330,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Delete a given service
EOT
command :delete, delete_desc, :range do
command :delete, delete_desc, [:range, :serviceid_list] do
client = Service::Client.new(
options[:username],
options[:password],
@@ -346,7 +350,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Shutdown a service
EOT
command :shutdown, shutdown_desc, :range do
command :shutdown, shutdown_desc, [:range, :serviceid_list] do
client = Service::Client.new(
options[:username],
options[:password],
@@ -364,7 +368,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Changes the service group
EOT
command :chgrp, chgrp_desc, :range, :groupid do
command :chgrp, chgrp_desc, [:range, :serviceid_list], :groupid do
client = Service::Client.new(
options[:username],
options[:password],
@@ -385,7 +389,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Changes the service owner and group
EOT
command :chown, chown_desc, :range, :userid, [:groupid, nil] do
command :chown, chown_desc, [:range, :serviceid_list], :userid, [:groupid, nil] do
client = Service::Client.new(
options[:username],
options[:password],
@@ -407,7 +411,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Changes the service permissions
EOT
command :chmod, chmod_desc, :range, :octet do
command :chmod, chmod_desc, [:range, :serviceid_list], :octet do
client = Service::Client.new(
options[:username],
options[:password],
+8 -4
View File
@@ -150,6 +150,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Service.rname_to_id(arg, "SERVICE TEMPLATE")
end
set :format, :templateid_list, Service.list_to_id_desc("SERVICE TEMPLATE") do |arg|
Service.list_to_id(arg, "SERVICE TEMPLATE")
end
#
# List
#
@@ -261,7 +265,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Delete a given Service Template
EOT
command :delete, delete_desc, :range do
command :delete, delete_desc, [:range, :templateid_list] do
client = Service::Client.new(
options[:username],
options[:password],
@@ -310,7 +314,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Changes the service template group
EOT
command :chgrp, chgrp_desc, :range, :groupid do
command :chgrp, chgrp_desc, [:range, :templateid_list], :groupid do
client = Service::Client.new(
options[:username],
options[:password],
@@ -331,7 +335,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Changes the service template owner and group
EOT
command :chown, chown_desc, :range, :userid, [:groupid, nil] do
command :chown, chown_desc, [:range, :templateid_list], :userid, [:groupid, nil] do
client = Service::Client.new(
options[:username],
options[:password],
@@ -353,7 +357,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Changes the service template permissions
EOT
command :chmod, chmod_desc, :range, :octet do
command :chmod, chmod_desc, [:range, :templateid_list], :octet do
client = Service::Client.new(
options[:username],
options[:password],
+48
View File
@@ -148,6 +148,10 @@ module Service
end
def self.name_to_id(name, pool, ename)
if pool['DOCUMENT_POOL']['DOCUMENT'].nil?
return -1, "#{ename} named #{name} not found."
end
objects = pool['DOCUMENT_POOL']['DOCUMENT'].select {|object| object['NAME'] == name }
if objects.length>0
@@ -163,6 +167,50 @@ module Service
return 0, result
end
def self.list_to_id(names, poolname)
client = Service::Client.new(nil, nil, nil)
# client = Service::Client.new(
# options[:username],
# options[:password],
# options[:server],
# USER_AGENT)
resource_path = case poolname
when "SERVICE" then "/service"
when "SERVICE TEMPLATE" then "/service_template"
end
response = client.get(resource_path)
if CloudClient::is_error?(response)
return -1, "OpenNebula #{poolname} name not found," <<
" use the ID instead"
end
pool = JSON.parse(response.body)
result = names.split(',').collect { |name|
if name.match(/^[0123456789]+$/)
name.to_i
else
rc = name_to_id(name, pool, poolname)
if rc.first == -1
return rc[0], rc[1]
end
rc[1]
end
}
return 0, result
end
def self.list_to_id_desc(poolname)
"Comma-separated list of OpenNebula #{poolname} names or ids"
end
# Perform an action on several resources
# @param [Array] ids resources ids
# @param [Block] block action to be performed