Update stdlib
[mirror/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_ipv4_address("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     function_deprecation([:validate_ipv4_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ipv4. There is further documentation for validate_legacy function in the README.'])
22
23     require "ipaddr"
24     rescuable_exceptions = [ ArgumentError ]
25
26     if defined?(IPAddr::InvalidAddressError)
27       rescuable_exceptions << IPAddr::InvalidAddressError
28     end
29
30     unless args.length > 0 then
31       raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)")
32     end
33
34     args.each do |arg|
35       unless arg.is_a?(String)
36         raise Puppet::ParseError, "#{arg.inspect} is not a string."
37       end
38
39       begin
40         unless IPAddr.new(arg).ipv4?
41           raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
42         end
43       rescue *rescuable_exceptions
44         raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
45       end
46     end
47
48   end
49
50 end