Fix FrozenError on Focal

This change ensures that a hash passed to a function is not directly
modified, to avoid FrozenError.

Change-Id: Ie9242d7ff5cac206ae65526e08cd096425fa4232
This commit is contained in:
Takashi Kajinami
2021-03-25 20:14:23 +09:00
parent 6efb2bfb62
commit 39c5618009
2 changed files with 23 additions and 16 deletions

View File

@@ -52,15 +52,15 @@ Puppet::Functions.create_function(:os_database_connection) do
# support previous charset option on the function. Setting charset will
# override charset if passed in via the extra parameters
extra = {}
if v.include?('extra')
extra.merge!(v['extra'])
end
if v.include?('charset')
if v.include?('extra')
v['extra'].merge!({ 'charset' => v['charset'] })
else
v['extra'] = { 'charset' => v['charset'] }
end
extra.merge!({ 'charset' => v['charset'] })
end
parts[:query] = v['extra'].map{ |k,v| "#{k}=#{v}" }.join('&') if v.include?('extra')
parts[:query] = extra.map{ |k,v| "#{k}=#{v}" }.join('&') if ! extra.empty?
parts[:scheme] = v['dialect']

View File

@@ -86,16 +86,21 @@ Puppet::Functions.create_function(:os_transport_url) do
raise(ArgumentError, 'os_transport_url(): Wrong number of arguments')
end
v = args[0]
klass = v.class
v_raw = args[0]
klass = v_raw.class
unless klass == Hash
raise(Puppet::ParseError, "os_transport_url(): Requires an hash, got #{klass}")
end
v = {}
# type checking for the parameter hash
v.keys.each do |key|
v[key] = v[key].to_s if key == 'port'
v_raw.keys.each do |key|
if key == 'port'
v[key] = v_raw[key].to_s
else
v[key] = v_raw[key]
end
klass = (key == 'hosts') ? Array : String
klass = (key == 'query') ? Hash : klass
unless (v[key].class == klass) or (v[key] == :undef)
@@ -156,6 +161,11 @@ Puppet::Functions.create_function(:os_transport_url) do
parts[:path] = "/#{v['virtual_host']}" if v.include?('virtual_host')
query = {}
if v.include?('query')
query.merge!(v['query'])
end
# support previous ssl option on the function. Setting ssl will
# override ssl if passed in via the query parameters
if v.include?('ssl')
@@ -169,14 +179,11 @@ Puppet::Functions.create_function(:os_transport_url) do
# str2bool or bool2num legacy functions using call_function.
ssl_str = _str2bool(v['ssl'])
ssl_val = _bool2num(v['ssl'])
if v.include?('query')
v['query'].merge!({ 'ssl' => ssl_val })
else
v['query'] = { 'ssl' => ssl_val }
end
query.merge!({ 'ssl' => ssl_val })
end
parts[:query] = v['query'].map{ |k,val| "#{k}=#{val}" }.join('&') if v.include?('query')
parts[:query] = query.map{ |k,val| "#{k}=#{val}" }.join('&') if ! query.empty?
url_parts = []
url_parts << parts[:transport]