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