From 9d8632bdd30b2d8077a9ca52b859018fc0807fc6 Mon Sep 17 00:00:00 2001 From: Stephen Gran Date: Sat, 14 Nov 2009 17:57:20 +0000 Subject: [PATCH] Try to generalize the ldap lookups into a single module Signed-off-by: Stephen Gran --- .../puppet/parser/functions/allnodeinfo.rb | 25 +-------------- .../lib/puppet/parser/functions/ldapinfo.rb | 32 +++++++++++++++++++ .../lib/puppet/parser/functions/nodeinfo.rb | 17 +--------- 3 files changed, 34 insertions(+), 40 deletions(-) create mode 100644 files/etc/puppet/lib/puppet/parser/functions/ldapinfo.rb diff --git a/files/etc/puppet/lib/puppet/parser/functions/allnodeinfo.rb b/files/etc/puppet/lib/puppet/parser/functions/allnodeinfo.rb index 922f60422..0b072de77 100644 --- a/files/etc/puppet/lib/puppet/parser/functions/allnodeinfo.rb +++ b/files/etc/puppet/lib/puppet/parser/functions/allnodeinfo.rb @@ -1,28 +1,5 @@ module Puppet::Parser::Functions newfunction(:allnodeinfo, :type => :rvalue) do |attributes| - - unless attributes.include?('hostname') - attributes << 'hostname' - end - - ldap = LDAP::SSLConn.new('db.debian.org', 636) - - results = [] - filter = '(hostname=*)' - begin - ldap.search2('ou=hosts,dc=debian,dc=org', LDAP::LDAP_SCOPE_SUBTREE, filter, attrs=attributes, false, 0, 0, s_attr="hostname").each do |x| - # If a returned value doesn't have all the attributes we're searching for, skip - # We'll skip if the array is empty, but we also seem to get back a nil object for empty attributes sometimes - next if attributes.any?{ |a| not x[a] or x[a].empty? } - results << x - end - rescue LDAP::ResultError - raise Puppet::ParseError, "LDAP error" - rescue RuntimeError - raise Puppet::ParseError, "No data returned from search" - ensure - ldap.unbind - end - return(results) + return Puppet::Parser::Functions::ldapinfo('*', attributes) end end diff --git a/files/etc/puppet/lib/puppet/parser/functions/ldapinfo.rb b/files/etc/puppet/lib/puppet/parser/functions/ldapinfo.rb new file mode 100644 index 000000000..a2c95cc2e --- /dev/null +++ b/files/etc/puppet/lib/puppet/parser/functions/ldapinfo.rb @@ -0,0 +1,32 @@ +module Puppet::Parser::Functions + newfunction(:ldapinfo, :type => :rvalue) do |attributes| + + host = attributes.shift + + unless attributes.include?("*") or attributes.include?('hostname') + attributes << 'hostname' + end + + ldap = LDAP::SSLConn.new('db.debian.org', 636) + + results = {} + filter = '(hostname=' + host + ')' + begin + ldap.search2('ou=hosts,dc=debian,dc=org', LDAP::LDAP_SCOPE_SUBTREE, filter, attrs=attributes, false, 0, 0, s_attr="hostname").each do |x| + # If a returned value doesn't have all the attributes we're searching for, skip + # We'll skip if the array is empty, but we also seem to get back a nil object for empty attributes sometimes + unless attributes.include?("*") + next if attributes.any?{ |a| not x[a] or x[a].empty? } + end + results[x['hostname'] = x + end + rescue LDAP::ResultError + raise Puppet::ParseError, "LDAP error" + rescue RuntimeError + raise Puppet::ParseError, "No data returned from search" + ensure + ldap.unbind + end + return( host == '*' ? results : results[host] ) + end +end diff --git a/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb b/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb index d7f3daea7..fc9a25cb7 100644 --- a/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb +++ b/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb @@ -44,22 +44,7 @@ module Puppet::Parser::Functions end end - ldap = LDAP::SSLConn.new('db.debian.org', 636) - - results['ldap'] = [] - filter = '(hostname=' + host +')' - begin - ldap.search2('ou=hosts,dc=debian,dc=org', LDAP::LDAP_SCOPE_SUBTREE, filter) do |x| - results['ldap'] << x - end - rescue LDAP::ResultError - raise Puppet::ParseError, "LDAP error" - rescue RuntimeError - raise Puppet::ParseError, "No data returned from search" - ensure - ldap.unbind - end - return(results) + results['ldap'] = Puppet::Parser::Functions::ldapinfo(host, '*') end end -- 2.20.1