Try to generalize the ldap lookups into a single module
[mirror/dsa-puppet.git] / files / etc / puppet / lib / puppet / parser / functions / ldapinfo.rb
1 module Puppet::Parser::Functions
2   newfunction(:ldapinfo, :type => :rvalue) do |attributes|
3
4     host = attributes.shift
5
6     unless attributes.include?("*") or attributes.include?('hostname')
7       attributes << 'hostname'
8     end
9
10     ldap = LDAP::SSLConn.new('db.debian.org', 636)
11
12     results = {}
13     filter = '(hostname=' + host + ')'
14     begin
15       ldap.search2('ou=hosts,dc=debian,dc=org', LDAP::LDAP_SCOPE_SUBTREE, filter, attrs=attributes, false, 0, 0, s_attr="hostname").each do |x|
16         # If a returned value doesn't have all the attributes we're searching for, skip
17         # We'll skip if the array is empty, but we also seem to get back a nil object for empty attributes sometimes
18         unless attributes.include?("*")
19           next if attributes.any?{ |a|  not x[a] or x[a].empty? }
20         end
21         results[x['hostname'] = x
22       end
23     rescue LDAP::ResultError
24       raise Puppet::ParseError, "LDAP error"
25     rescue RuntimeError
26       raise Puppet::ParseError, "No data returned from search"
27     ensure
28       ldap.unbind
29     end
30     return( host == '*' ? results : results[host] )
31   end
32 end