From 8b7c02b9145de812266b0e7d585394234b4d0ddc Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Thu, 20 Oct 2011 10:53:47 +0200 Subject: [PATCH] Feature #789: Symbolize the keys from resource data hashes. This means accessing properties of a resource should primarily be done with a symbol name: vdc[:HOSTS] instead of vdc["HOSTS"]. All client code updated accordingly. In practice, and to easy the task of developers it will still work if Strings are used (.to_sym performed by default). --- .../lib/cli/ozones_helper/vdc_helper.rb | 24 +++++++-------- .../lib/cli/ozones_helper/zones_helper.rb | 30 +++++++++---------- src/ozones/Client/lib/zona/OZonesElement.rb | 10 +++---- src/ozones/Client/lib/zona/OZonesJSON.rb | 16 +++++----- src/ozones/Client/lib/zona/OZonesPool.rb | 8 ++--- src/ozones/Client/lib/zona/VDCElement.rb | 8 ++--- src/ozones/Client/lib/zona/VDCPool.rb | 2 +- src/ozones/Client/lib/zona/ZoneElement.rb | 4 +-- src/ozones/Client/lib/zona/ZonePool.rb | 2 +- 9 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb b/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb index c4a394e799..796edcea5e 100644 --- a/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb +++ b/src/ozones/Client/lib/cli/ozones_helper/vdc_helper.rb @@ -62,7 +62,7 @@ class VDCHelper < OZonesHelper::OZHelper vdc = Zona::OZonesJSON.parse_json(rc.body, @vdc_str.upcase) end - hosts = vdc['HOSTS'].split(',').collect!{|x| x.to_i} + hosts = vdc[:HOSTS].split(',').collect!{|x| x.to_i} host_array.concat(hosts).uniq! new_host = host_array.join(',') @@ -90,7 +90,7 @@ class VDCHelper < OZonesHelper::OZHelper vdc = Zona::OZonesJSON.parse_json(rc.body, @vdc_str.upcase) end - hosts = vdc['HOSTS'].split(',').collect!{|x| x.to_i} + hosts = vdc[:HOSTS].split(',').collect!{|x| x.to_i} new_host = (hosts - host_array).join(',') template = "ID=#{id}\nHOSTS=#{new_host}\n" @@ -112,12 +112,12 @@ class VDCHelper < OZonesHelper::OZHelper CLIHelper.print_header(str_h1 % ["VDC #{vdc['name']} INFORMATION"]) - puts str % ["ID ", vdc['ID'].to_s] - puts str % ["NAME ", vdc['NAME'].to_s] - puts str % ["GROUP_ID ", vdc['GROUP_ID'].to_s] - puts str % ["ZONEID ", vdc['ZONES_ID'].to_s] - puts str % ["VDCADMIN ", vdc['VDCADMINNAME'].to_s] - puts str % ["HOST IDs ", vdc['HOSTS'].to_s] + puts str % ["ID ", vdc[:ID].to_s] + puts str % ["NAME ", vdc[:NAME].to_s] + puts str % ["GROUP_ID ", vdc[:GROUP_ID].to_s] + puts str % ["ZONEID ", vdc[:ZONES_ID].to_s] + puts str % ["VDCADMIN ", vdc[:VDCADMINNAME].to_s] + puts str % ["HOST IDs ", vdc[:HOSTS].to_s] puts return 0 @@ -126,21 +126,21 @@ class VDCHelper < OZonesHelper::OZHelper def format_pool(pool, options) st=CLIHelper::ShowTable.new(nil) do column :ID, "Identifier for VDC", :size=>4 do |d,e| - d["ID"] + d[:ID] end column :NAME, "Name of the VDC", :right, :size=>15 do |d,e| - d["NAME"] + d[:NAME] end column :ZONEID, "Id of the Zone where it belongs", :right, :size=>40 do |d,e| - d["ZONES_ID"] + d[:ZONES_ID] end default :ID, :NAME, :ZONEID end - st.show(pool[@vdc_str.upcase], options) + st.show(pool[:VDC], options) return 0 end diff --git a/src/ozones/Client/lib/cli/ozones_helper/zones_helper.rb b/src/ozones/Client/lib/cli/ozones_helper/zones_helper.rb index a56029f7e6..f59e94b6dc 100644 --- a/src/ozones/Client/lib/cli/ozones_helper/zones_helper.rb +++ b/src/ozones/Client/lib/cli/ozones_helper/zones_helper.rb @@ -36,17 +36,17 @@ class ZonesHelper < OZonesHelper::OZHelper str_h1="%-61s" str="%-15s: %-20s" - CLIHelper.print_header(str_h1 % ["ZONE #{zone['NAME']} INFORMATION"]) + CLIHelper.print_header(str_h1 % ["ZONE #{zone[:NAME]} INFORMATION"]) - puts str % ["ID ", zone['ID'].to_s] - puts str % ["NAME ", zone['NAME'].to_s] - puts str % ["ZONE ADMIN ",zone['ONENAME'].to_s] - puts str % ["ZONE PASS ", zone['ONEPASS'].to_s] - puts str % ["ENDPOINT ", zone['ENDPOINT'].to_s] - puts str % ["# VDCS ", zone['VDCS'].size.to_s] + puts str % ["ID ", zone[:ID].to_s] + puts str % ["NAME ", zone[:NAME].to_s] + puts str % ["ZONE ADMIN ",zone[:ONENAME].to_s] + puts str % ["ZONE PASS ", zone[:ONEPASS].to_s] + puts str % ["ENDPOINT ", zone[:ENDPOINT].to_s] + puts str % ["# VDCS ", zone[:VDCS].size.to_s] puts - if zone['VDCS'].size == 0 + if zone[:VDCS].size == 0 return [0, zone] end @@ -54,17 +54,17 @@ class ZonesHelper < OZonesHelper::OZHelper st=CLIHelper::ShowTable.new(nil) do column :ID, "Identifier for VDC", :size=>4 do |d,e| - d["ID"] + d[:ID] end column :NAME, "Name of the VDC", :right, :size=>15 do |d,e| - d["NAME"] + d[:NAME] end default :ID, :NAME end - st.show(zone["VDCS"], options) + st.show(zone[:VDCS], options) return [0, zone] end @@ -72,20 +72,20 @@ class ZonesHelper < OZonesHelper::OZHelper def format_pool(pool, options) st=CLIHelper::ShowTable.new(nil) do column :ID, "Identifier for Zone", :size=>4 do |d,e| - d["ID"] + d[:ID] end column :NAME, "Name of the Zone", :right, :size=>15 do |d,e| - d["NAME"] + d[:NAME] end column :ENDPOINT, "Endpoint of the Zone", :right, :size=>40 do |d,e| - d["ENDPOINT"] + d[:ENDPOINT] end default :ID, :NAME, :ENDPOINT end - st.show(pool[@zone_str.upcase], options) + st.show(pool[:ZONE], options) return 0 end diff --git a/src/ozones/Client/lib/zona/OZonesElement.rb b/src/ozones/Client/lib/zona/OZonesElement.rb index e1ed76bd81..3716487991 100644 --- a/src/ozones/Client/lib/zona/OZonesElement.rb +++ b/src/ozones/Client/lib/zona/OZonesElement.rb @@ -30,8 +30,8 @@ module Zona @client = client @json_hash = hash - @pe_id = self["ID"] ? self["ID"].to_i : nil - @name = self["NAME"] ? self["NAME"] : nil + @pe_id = self[:ID] ? self[:ID].to_i : nil + @name = self[:NAME] ? self[:NAME] : nil end # Retrieves details about an object and fills in @@ -48,8 +48,8 @@ module Zona rc = nil - @pe_id = self["ID"] ? self["ID"].to_i : nil - @name = self["NAME"] ? self["NAME"] : nil + @pe_id = self[:ID] ? self[:ID].to_i : nil + @name = self[:NAME] ? self[:NAME] : nil end rc @@ -72,7 +72,7 @@ module Zona if !Zona.is_error?(rc) initialize_json(rc.body,kind.upcase) - @pe_id = self["ID"].to_i + @pe_id = self[:ID].to_i rc = nil end rc diff --git a/src/ozones/Client/lib/zona/OZonesJSON.rb b/src/ozones/Client/lib/zona/OZonesJSON.rb index 62e1ebfd19..ef82ab3be3 100644 --- a/src/ozones/Client/lib/zona/OZonesJSON.rb +++ b/src/ozones/Client/lib/zona/OZonesJSON.rb @@ -25,15 +25,17 @@ module Zona # Build an element description hash. # @param [String] json_str JSON description of the element - # @param [String] root_element root element of the JSON object + # @param [#to_sym] root_element root element of the JSON object # @return [Hash,Zona::Error] The parsed JSON hash, or Error def self.build_json(json_str, root_element) begin - parser = JSON.parser.new(json_str, {:symbolize_names => false}) + parser = JSON.parser.new(json_str, {:symbolize_names => true}) hash = parser.parse - if hash.has_key?(root_element) - return hash[root_element] + root_sym = root_element.to_sym + + if hash.has_key?(root_sym) + return hash[root_sym] end Error.new("Error parsing JSON:\ root element not present") @@ -74,7 +76,7 @@ module Zona # Initializes an instance with a JSON description # @param [String] json_str JSON description - # @param [String] root_element root element in the element description + # @param [#to_sym] root_element root element in the element description def initialize_json(json_str, root_element) rc = OZonesJSON.build_json(json_str,root_element) @json_hash = rc @@ -85,10 +87,10 @@ module Zona end # Accesses the value of a JSON element key - # @param [String] key + # @param [#to_sym] key # @return [String] Value def [](key) - @json_hash[key] + @json_hash[key.to_sym] end diff --git a/src/ozones/Client/lib/zona/OZonesPool.rb b/src/ozones/Client/lib/zona/OZonesPool.rb index 7c86132c5e..6994737df6 100644 --- a/src/ozones/Client/lib/zona/OZonesPool.rb +++ b/src/ozones/Client/lib/zona/OZonesPool.rb @@ -23,15 +23,15 @@ module Zona protected # Initializes a Pool instance - # @param [String] pool pool name tag - # @param [String] pool pool elements name tag + # @param [#to_sym] pool pool name tag + # @param [#to_sym] pool pool elements name tag # @param [Zona::Client] client OZones Client def initialize(pool,element,client) super(nil) @client = client - @pool_name = pool.upcase - @element_name = element.upcase + @pool_name = pool.to_sym + @element_name = element.to_sym end # Produces a new Pool element with the provided description diff --git a/src/ozones/Client/lib/zona/VDCElement.rb b/src/ozones/Client/lib/zona/VDCElement.rb index 43f1081876..150b3ba9af 100644 --- a/src/ozones/Client/lib/zona/VDCElement.rb +++ b/src/ozones/Client/lib/zona/VDCElement.rb @@ -35,7 +35,7 @@ module Zona else json = '{"VDC":{}}' end - OZonesJSON.build_json(json,"VDC") + OZonesJSON.build_json(json,:VDC) end # Initializes a VDC object instance @@ -50,7 +50,7 @@ module Zona # the information hash # @return [Zona::Error] nil or Error def info - super(VDC_KIND,"VDC") + super(VDC_KIND,:VDC) end # Allocates a new element from a hash description @@ -86,7 +86,7 @@ module Zona return Error.new('VDC not info-ed') if !@json_hash # array of hosts, integers - hosts = self["HOSTS"].split(',').collect!{|x| x.to_i} + hosts = self[:HOSTS].split(',').collect!{|x| x.to_i} hosts.concat(hosts_array).uniq! new_hosts = hosts.join(',') @@ -107,7 +107,7 @@ module Zona def delhosts(hosts_array) return Error.new('VDC not info-ed') if !@json_hash - hosts = self["HOSTS"].split(',').collect!{|x| x.to_i} + hosts = self[:HOSTS].split(',').collect!{|x| x.to_i} new_hosts = (hosts - hosts_array).join(',') template = {:VDC => {:ID => @pe_id, :HOSTS => new_hosts}} diff --git a/src/ozones/Client/lib/zona/VDCPool.rb b/src/ozones/Client/lib/zona/VDCPool.rb index ab1894b429..1024ef78ac 100644 --- a/src/ozones/Client/lib/zona/VDCPool.rb +++ b/src/ozones/Client/lib/zona/VDCPool.rb @@ -27,7 +27,7 @@ module Zona # Initializes a VDC Pool instance # @param [Zona::Client] client OZones Client def initialize(client) - super("VDC_POOL", "VDC", client) + super(:VDC_POOL, :VDC, client) end # Produces a new VDC element with the provided description diff --git a/src/ozones/Client/lib/zona/ZoneElement.rb b/src/ozones/Client/lib/zona/ZoneElement.rb index 69df5baf75..4cbd5a89e3 100644 --- a/src/ozones/Client/lib/zona/ZoneElement.rb +++ b/src/ozones/Client/lib/zona/ZoneElement.rb @@ -33,7 +33,7 @@ module Zona else json = '{"ZONE":{}}' end - OZonesJSON.build_json(json,"ZONE") + OZonesJSON.build_json(json,:ZONE) end # Initializes a Zone object instance @@ -48,7 +48,7 @@ module Zona # the information hash # @return [Zona::Error] nil or Error def info - super(ZONE_KIND,"ZONE") + super(ZONE_KIND,:ZONE) end # Allocates a new element from a hash description diff --git a/src/ozones/Client/lib/zona/ZonePool.rb b/src/ozones/Client/lib/zona/ZonePool.rb index 0a50e0c7e4..8b503fa792 100644 --- a/src/ozones/Client/lib/zona/ZonePool.rb +++ b/src/ozones/Client/lib/zona/ZonePool.rb @@ -27,7 +27,7 @@ module Zona # Initializes a Zone Pool instance # @param [Zona::Client] client OZones Client def initialize(client) - super("ZONE_POOL", "ZONE", client) + super(:ZONE_POOL, :ZONE, client) end # Produces a new Zone element with the provided description