Catch empty data arrays at start of entropy_provider function
[mirror/dsa-puppet.git] / modules / puppetmaster / lib / puppet / parser / functions / whohosts.rb
1 require 'puppet/file_system'
2
3 module Puppet::Parser::Functions
4   newfunction(:whohosts, :type => :rvalue) do |args|
5     require 'ipaddr'
6     require 'yaml'
7
8     ipAddrs = args[0]
9
10     ans = {"name" => "unknown"}
11     yamlfile = Puppet::Parser::Files.find_file('debian_org/misc/hoster.yaml', compiler.environment)
12     yaml = YAML.load_file(yamlfile)
13
14     ipAddrs.each do |addr|
15       yaml.keys.each do |hoster|
16         next unless yaml[hoster].kind_of?(Hash) and yaml[hoster].has_key?('netrange')
17         netrange = yaml[hoster]['netrange']
18
19         netrange.each do |net|
20           begin
21             if IPAddr.new(net).include?(addr)
22               ans = yaml[hoster]
23               ans['name'] = hoster
24             end
25           rescue => e
26             raise Puppet::ParseError, "Error while trying to match addr #{addr} for net #{net}: #{e.message}\n#{e.backtrace}"
27           end
28         end
29       end
30     end
31     if not ans['longname']
32       ans['longname'] = ans['name']
33     end
34     return ans
35   end
36 end
37 # vim:set ts=2:
38 # vim:set et:
39 # vim:set shiftwidth=2: