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