Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_ipv4_address.rb
1 #
2 # validate_ipv4_address.rb
3 #
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.
8
9     The following values will pass:
10
11     $my_ip = "1.2.3.4"
12     validate_ipv4_address($my_ip)
13     validate_ipv4_address("8.8.8.8", "172.16.0.1", $my_ip)
14
15     The following values will fail, causing compilation to abort:
16
17     $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
18     validate_ipv4_address($some_array)
19
20     DOC
21              ) do |args|
22
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.'])
25
26     require 'ipaddr'
27     rescuable_exceptions = [ArgumentError]
28
29     if defined?(IPAddr::InvalidAddressError)
30       rescuable_exceptions << IPAddr::InvalidAddressError
31     end
32
33     if args.empty?
34       raise Puppet::ParseError, "validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)"
35     end
36
37     args.each do |arg|
38       unless arg.is_a?(String)
39         raise Puppet::ParseError, "#{arg.inspect} is not a string."
40       end
41
42       begin
43         unless IPAddr.new(arg).ipv4?
44           raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
45         end
46       rescue *rescuable_exceptions
47         raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
48       end
49     end
50   end
51 end