]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/puppetmaster/lib/puppet/parser/functions/ldapinfo.rb
Merge branch 'master' of git+ssh://puppet.debian.org/srv/puppet.debian.org/git/dsa...
[dsa-puppet.git] / modules / puppetmaster / 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     require 'ldap'
11     ldap = LDAP::SSLConn.new('db.debian.org', 636)
12
13     results = {}
14     filter = '(hostname=' + host + ')'
15     begin
16       ldap.search2('ou=hosts,dc=debian,dc=org', LDAP::LDAP_SCOPE_SUBTREE, filter, attrs=attributes, false, 0, 0, s_attr="hostname").each do |x|
17         # If a returned value doesn't have all the attributes we're searching for, skip
18         # We'll skip if the array is empty, but we also seem to get back a nil object for empty attributes sometimes
19         unless attributes.include?("*")
20           next if attributes.any?{ |a|  not x[a] or x[a].empty? }
21         end
22         results[x['hostname'][0]] = x
23       end
24     rescue LDAP::ResultError
25       raise Puppet::ParseError, "LDAP error"
26     rescue RuntimeError
27       raise Puppet::ParseError, "No data returned from search"
28     ensure
29       ldap.unbind
30     end
31     if host == '*'
32       return(results)
33     else
34       return(results[host])
35     end
36   end
37 end