2 # validate_ipv4_address.rb
4 module Puppet::Parser::Functions
5 newfunction(:validate_ipv4_address, :doc => <<-DOC
6 Validate that all values passed are valid IPv4 addresses.
7 Fail compilation if any value fails this check.
9 The following values will pass:
12 validate_ipv4_address($my_ip)
13 validate_ipv4_address("8.8.8.8", "172.16.0.1", $my_ip)
15 The following values will fail, causing compilation to abort:
17 $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
18 validate_ipv4_address($some_array)
23 function_deprecation([:validate_ipv4_address, 'This method is deprecated, please use the stdlib validate_legacy function,
24 with Stdlib::Compat::Ipv4. There is further documentation for validate_legacy function in the README.'])
27 rescuable_exceptions = [ArgumentError]
29 if defined?(IPAddr::InvalidAddressError)
30 rescuable_exceptions << IPAddr::InvalidAddressError
34 raise Puppet::ParseError, "validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)"
38 unless arg.is_a?(String)
39 raise Puppet::ParseError, "#{arg.inspect} is not a string."
43 unless IPAddr.new(arg).ipv4?
44 raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
46 rescue *rescuable_exceptions
47 raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."