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
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
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')
return false
end
@ -105,21 +105,15 @@ module PuppetX
entry.each do |attribute, values|
attr = attribute.to_s
if values.is_a? Array and values.size > 1
entry_data[attr] = []
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
value_data = []
Array(values).flatten.each do |v|
value_data << v.chomp
end
entry_data[attr] = value_data
end
data << entry_data
end
Puppet.debug(data)
return data
end