Always return an array for the values

Previously, its impossible to know if the results you are working with
in the puppet manifest are in string or array form without counting
them.  This work ensures that an array is always returned, even if there
is only one item returned.

This is useful in situations where an attribute is commonly both
multi-valued and single-valued to avoid complext manifest code.
This commit is contained in:
Zach Leslie 2015-11-10 21:10:06 +00:00 committed by Zach Leslie
parent f50421744e
commit bf64a51a42

View File

@ -88,10 +88,10 @@ module PuppetX
end_time = Time.now end_time = Time.now
time_delta = sprintf('%.3f', end_time - start_time) time_delta = sprintf('%.3f', end_time - start_time)
Puppet.debug("Searching #{@base} for #{@attributes} using #{@filter} took #{time_delta} seconds and returned #{entries.length} results") Puppet.debug("ldapquery(): Searching #{@base} for #{@attributes} using #{@filter} took #{time_delta} seconds and returned #{entries.length} results")
return entries return entries
rescue Exception => e rescue Exception => e
Puppet.debug('There was an error searching LDAP #{e.message}') Puppet.debug("There was an error searching LDAP #{e.message}")
Puppet.debug('Returning false') Puppet.debug('Returning false')
return false return false
end end
@ -105,21 +105,15 @@ module PuppetX
entry.each do |attribute, values| entry.each do |attribute, values|
attr = attribute.to_s attr = attribute.to_s
value_data = []
if values.is_a? Array and values.size > 1 Array(values).flatten.each do |v|
entry_data[attr] = [] value_data << v.chomp
values.each do |v|
entry_data[attr] << v.chomp
end
elsif values.is_a? Array and values.size == 1
entry_data[attr] = values[0].chomp
else
entry_data[attr] = values.chomp
end end
entry_data[attr] = value_data
end end
data << entry_data data << entry_data
end end
Puppet.debug(data)
return data return data
end end