]> git.donarmstrong.com Git - dsa-puppet.git/commitdiff
Try to generalize the ldap lookups into a single module
authorStephen Gran <steve@lobefin.net>
Sat, 14 Nov 2009 17:57:20 +0000 (17:57 +0000)
committerStephen Gran <steve@lobefin.net>
Sat, 14 Nov 2009 17:57:20 +0000 (17:57 +0000)
Signed-off-by: Stephen Gran <steve@lobefin.net>
files/etc/puppet/lib/puppet/parser/functions/allnodeinfo.rb
files/etc/puppet/lib/puppet/parser/functions/ldapinfo.rb [new file with mode: 0644]
files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb

index 922f604225b71d89b3f568f10ff853c2e8a10016..0b072de773e4c44390c56b57e8d3fa2816dc3280 100644 (file)
@@ -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 (file)
index 0000000..a2c95cc
--- /dev/null
@@ -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
index d7f3daea7c41e872c23d81188f4ecbef22a88195..fc9a25cb7d088118426839137636870653213934 100644 (file)
@@ -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