From e4b34a7e4e175da871b796396d054bf36ae2de7f Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Mon, 5 Mar 2012 11:32:36 +0100 Subject: [PATCH] Ozones: enable raw JSON output from CLI commands --- src/ozones/Client/bin/onevdc | 5 ++- src/ozones/Client/bin/onezone | 44 ++++++++++++++-------- src/ozones/Client/lib/cli/ozones_helper.rb | 34 ++++++++++++++--- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src/ozones/Client/bin/onevdc b/src/ozones/Client/bin/onevdc index b584c7fb6f..1fb688a2b2 100755 --- a/src/ozones/Client/bin/onevdc +++ b/src/ozones/Client/bin/onevdc @@ -61,11 +61,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do helper.create_resource(args[0], options) end - command :show, 'Show information of a particular VDC', :vdcid do + command :show, 'Show information of a particular VDC', :vdcid, + :options => OZonesHelper::JSON do helper.show_resource(args[0],options) end - command :list, 'Lists VDCs in the pool' do + command :list, 'Lists VDCs in the pool', :options => OZonesHelper::JSON do helper.list_pool(options) end diff --git a/src/ozones/Client/bin/onezone b/src/ozones/Client/bin/onezone index af922e7bcb..5eae35012e 100755 --- a/src/ozones/Client/bin/onezone +++ b/src/ozones/Client/bin/onezone @@ -71,8 +71,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do onezone show 4 host EOT - command :show, show_desc, :zoneid, [:resource, nil] do - zone=helper.show_resource(args[0],options)[1] + command :show, show_desc, :zoneid, [:resource, nil], + :options => OZonesHelper::JSON do + zone = helper.show_resource(args[0],options)[1] + + #manually print here + if options[:json] + puts zone + end + + if !args[1] then next 0 end case args[1] when "host" @@ -87,35 +95,41 @@ cmd=CommandParser::CmdParser.new(ARGV) do aux_helper = OneTemplateHelper.new when "user" aux_helper = OneUserHelper.new - else - puts "\n:!: Pool #{args[1]} doesn't exist or is not supported\n\n" - next 0 + else + puts "\n:!: Pool #{args[1]} doesn't exist or is not supported\n\n" + next 0 end - pool_hash_data = helper.get_resource_pool("zone", zone[:ID], args[1]) + pool_hash_data = helper.get_resource_pool("zone", args[0], args[1], + options) - if pool_hash_data[0] != 0 - puts "\nError retrieving information for pool #{args[1]}. Reason: " + pool_hash_data[1] + "\n\n" + if pool_hash_data[0] != 0 + puts "\nError retrieving information for pool #{args[1]}. Reason: " + pool_hash_data[1] + "\n\n" next 0 - end + end - if !pool_hash_data[1] - next 0 + if !pool_hash_data[1] + next 0 + end + + if options[:json] + puts pool_hash_data[1] + next 0 end if pool_hash_data[1].is_a?(Hash) pool_hash_data[1]=[Hash.transform_keys_to_strings(pool_hash_data[1])] - else - pool_hash_data[1].each{|hash| hash.replace(Hash.transform_keys_to_strings(hash))} + else + pool_hash_data[1].each{|hash| hash.replace(Hash.transform_keys_to_strings(hash))} end table = aux_helper.format_pool(options) - table.show(pool_hash_data[1]) + table.show(pool_hash_data[1]) 0 end - command :list, 'Lists Zones in the pool' do + command :list, 'Lists Zones in the pool', :options=>OZonesHelper::JSON do helper.list_pool(options) end diff --git a/src/ozones/Client/lib/cli/ozones_helper.rb b/src/ozones/Client/lib/cli/ozones_helper.rb index 6ce320b5e8..cf13653354 100644 --- a/src/ozones/Client/lib/cli/ozones_helper.rb +++ b/src/ozones/Client/lib/cli/ozones_helper.rb @@ -18,6 +18,15 @@ require 'zona' module OZonesHelper + #Specific ozones CLI options + + JSON={ + :name => "json", + :short => "-j", + :large => "--json", + :description => "Show the resource in JSON format" + } + class OZHelper def initialize(user=nil, pass=nil, endpoint_str=nil, timeout=nil, debug_flag=true) @@ -45,8 +54,13 @@ module OZonesHelper if Zona::is_error?(rc) [-1, rc.message] else - pool=Zona::OZonesJSON.parse_json(rc.body, kind.upcase + "_POOL") - format_pool(pool, options) + if options[:json] + [0, rc.body] + else + resource_str = kind.upcase + "_POOL" + pool=Zona::OZonesJSON.parse_json(rc.body, resource_str) + format_pool(pool, options) + end end end @@ -56,18 +70,26 @@ module OZonesHelper if Zona::is_error?(rc) [-1, rc.message] else - resource=Zona::OZonesJSON.parse_json(rc.body, kind.upcase) - format_resource(resource, options) + if options[:json] + [0, rc.body] + else + resource=Zona::OZonesJSON.parse_json(rc.body, kind.upcase) + format_resource(resource, options) + end end end - def get_resource_pool(kind, id, pool) + def get_resource_pool(kind, id, pool, options) rc = @client.get_resource_pool(kind, id, pool) if Zona::is_error?(rc) [-1, rc.message] else - [0 , Zona::OZonesJSON.parse_json(rc.body, pool.upcase+"_POOL")[pool.upcase.to_sym]] + if options[:json] + [0, rc.body] + else + [0 , Zona::OZonesJSON.parse_json(rc.body, pool.upcase+"_POOL")[pool.upcase.to_sym]] + end end end