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