2 # validate_ip_address.rb
4 module Puppet::Parser::Functions
5 newfunction(:validate_ip_address, :doc => <<-DOC
7 Validate that all values passed are valid IP addresses,
8 regardless they are IPv4 or IPv6
9 Fail compilation if any value fails this check.
12 passes when the given values are valid IP addresses or raise an error when they are not and fails compilation
15 The following values will pass:
18 validate_ip_address($my_ip)
19 validate_ip_address("8.8.8.8", "172.16.0.1", $my_ip)
22 validate_ip_address(1)
23 validate_ip_address($my_ip)
24 validate_ip_address("fe80::baf6:b1ff:fe19:7507", $my_ip)
26 The following values will fail, causing compilation to abort:
28 $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
29 validate_ip_address($some_array)
34 rescuable_exceptions = [ArgumentError]
36 function_deprecation([:validate_ip_address, 'This method is deprecated, please use the stdlib validate_legacy function,
37 with Stdlib::Compat::Ip_address. There is further documentation for validate_legacy function in the README.'])
39 if defined?(IPAddr::InvalidAddressError)
40 rescuable_exceptions << IPAddr::InvalidAddressError
44 raise Puppet::ParseError, "validate_ip_address(): wrong number of arguments (#{args.length}; must be > 0)"
48 unless arg.is_a?(String)
49 raise Puppet::ParseError, "#{arg.inspect} is not a string."
53 unless IPAddr.new(arg).ipv4? || IPAddr.new(arg).ipv6?
54 raise Puppet::ParseError, "#{arg.inspect} is not a valid IP address."
56 rescue *rescuable_exceptions
57 raise Puppet::ParseError, "#{arg.inspect} is not a valid IP address."