1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

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).
This commit is contained in:
Hector Sanjuan 2011-10-20 10:53:47 +02:00
parent de9423c69c
commit 8b7c02b914
9 changed files with 53 additions and 51 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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