]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/validate_ipv4_address.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_ipv4_address.rb
1 module Puppet::Parser::Functions
2
3   newfunction(:validate_ipv4_address, :doc => <<-ENDHEREDOC
4     Validate that all values passed are valid IPv4 addresses.
5     Fail compilation if any value fails this check.
6
7     The following values will pass:
8
9     $my_ip = "1.2.3.4"
10     validate_ipv4_address($my_ip)
11     validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
12
13     The following values will fail, causing compilation to abort:
14
15     $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
16     validate_ipv4_address($some_array)
17
18     ENDHEREDOC
19   ) do |args|
20
21     require "ipaddr"
22     rescuable_exceptions = [ ArgumentError ]
23
24     if defined?(IPAddr::InvalidAddressError)
25       rescuable_exceptions << IPAddr::InvalidAddressError
26     end
27
28     unless args.length > 0 then
29       raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)")
30     end
31
32     args.each do |arg|
33       unless arg.is_a?(String)
34         raise Puppet::ParseError, "#{arg.inspect} is not a string."
35       end
36
37       begin
38         unless IPAddr.new(arg).ipv4?
39           raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
40         end
41       rescue *rescuable_exceptions
42         raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
43       end
44     end
45
46   end
47
48 end