Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_ipv6_address.rb
1 module Puppet::Parser::Functions
2
3   newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC
4     Validate that all values passed are valid IPv6 addresses.
5     Fail compilation if any value fails this check.
6
7     The following values will pass:
8
9     $my_ip = "3ffe:505:2"
10     validate_ipv6_address(1)
11     validate_ipv6_address($my_ip)
12     validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
13
14     The following values will fail, causing compilation to abort:
15
16     $some_array = [ true, false, "garbage string", "1.2.3.4" ]
17     validate_ipv6_address($some_array)
18
19     ENDHEREDOC
20   ) do |args|
21
22     function_deprecation([:validate_ipv6_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ipv6. There is further documentation for validate_legacy function in the README.'])
23
24     require "ipaddr"
25     rescuable_exceptions = [ ArgumentError ]
26
27     if defined?(IPAddr::InvalidAddressError)
28       rescuable_exceptions << IPAddr::InvalidAddressError
29     end
30
31     unless args.length > 0 then
32       raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)")
33     end
34
35     args.each do |arg|
36       unless arg.is_a?(String)
37         raise Puppet::ParseError, "#{arg.inspect} is not a string."
38       end
39
40       begin
41         unless IPAddr.new(arg).ipv6?
42           raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
43         end
44       rescue *rescuable_exceptions
45         raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
46       end
47     end
48
49   end
50
51 end