From bf64a51a420f8819c1b30dd0b29853c09e874c15 Mon Sep 17 00:00:00 2001 From: Zach Leslie Date: Tue, 10 Nov 2015 21:10:06 +0000 Subject: [PATCH] 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. --- lib/puppet_x/ldapquery.rb | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/puppet_x/ldapquery.rb b/lib/puppet_x/ldapquery.rb index aeadf22..6f02408 100644 --- a/lib/puppet_x/ldapquery.rb +++ b/lib/puppet_x/ldapquery.rb @@ -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