Read misc files from environment
[mirror/dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / whohosts.rb
index 2835cef..3192bb7 100644 (file)
@@ -1,30 +1,36 @@
+require 'puppet/file_system'
+
 module Puppet::Parser::Functions
   newfunction(:whohosts, :type => :rvalue) do |args|
     require 'ipaddr'
     require 'yaml'
 
-    nodeinfo = args[0]
-    yamlfile = args[1]
-    parser.watch_file(yamlfile)
-
-    $KCODE = 'utf-8'
+    ipAddrs = args[0]
 
-    ans = "unknown"
+    ans = {"name" => "unknown"}
+    yamlfile = Puppet::Parser::Files.find_file('debian_org/misc/hoster.yaml', compiler.environment)
     yaml = YAML.load_file(yamlfile)
 
-    if (nodeinfo['ldap'].has_key?('ipHostNumber'))
-      nodeinfo['ldap']['ipHostNumber'].each do |addr|
-        yaml.keys.each do |hoster|
-          if yaml[hoster].has_key?'netrange'
-            yaml[hoster]['netrange'].each do |net|
-              if IPAddr.new(net).include?(addr)
-                return hoster
-              end
+    ipAddrs.each do |addr|
+      yaml.keys.each do |hoster|
+        next unless yaml[hoster].kind_of?(Hash) and yaml[hoster].has_key?('netrange')
+        netrange = yaml[hoster]['netrange']
+
+        netrange.each do |net|
+          begin
+            if IPAddr.new(net).include?(addr)
+              ans = yaml[hoster]
+              ans['name'] = hoster
             end
+          rescue => e
+            raise Puppet::ParseError, "Error while trying to match addr #{addr} for net #{net}: #{e.message}\n#{e.backtrace}"
           end
         end
       end
     end
+    if not ans['longname']
+      ans['longname'] = ans['name']
+    end
     return ans
   end
 end