1 module Puppet::Parser::Functions
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.
7 The following values will pass:
10 validate_ipv4_address($my_ip)
11 validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
13 The following values will fail, causing compilation to abort:
15 $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
16 validate_ipv4_address($some_array)
22 rescuable_exceptions = [ ArgumentError ]
24 if defined?(IPAddr::InvalidAddressError)
25 rescuable_exceptions << IPAddr::InvalidAddressError
28 unless args.length > 0 then
29 raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)")
33 unless arg.is_a?(String)
34 raise Puppet::ParseError, "#{arg.inspect} is not a string."
38 unless IPAddr.new(arg).ipv4?
39 raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
41 rescue *rescuable_exceptions
42 raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."