From 0f2c184d03cadbcbcf0295f69c6df2a9a60fb5d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Mon, 17 Sep 2012 18:56:04 +0200 Subject: [PATCH] appflow: Bug #1444: CLI can use names as range arguments --- flow/client/bin/appflow | 14 +++++---- flow/client/bin/appflow-template | 12 +++++--- flow/client/lib/appflow_client.rb | 48 +++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/flow/client/bin/appflow b/flow/client/bin/appflow index 8879f8377c..ada9e29764 100755 --- a/flow/client/bin/appflow +++ b/flow/client/bin/appflow @@ -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], diff --git a/flow/client/bin/appflow-template b/flow/client/bin/appflow-template index b3e98387fe..669146a8b0 100755 --- a/flow/client/bin/appflow-template +++ b/flow/client/bin/appflow-template @@ -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], diff --git a/flow/client/lib/appflow_client.rb b/flow/client/lib/appflow_client.rb index c727ecf0b0..946af978ca 100644 --- a/flow/client/lib/appflow_client.rb +++ b/flow/client/lib/appflow_client.rb @@ -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