Update stdlib and concat to 6.1.0 both
[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     @summary
7       Validate that all values passed are valid IPv6 addresses.
8       Fail compilation if any value fails this check.
9
10     @return
11       passes when the given values are valid IPv6 addresses or raise an error when they are not and fails compilation
12
13     @example **Usage**
14       The following values will pass:
15
16         $my_ip = "3ffe:505:2"
17         validate_ipv6_address(1)
18         validate_ipv6_address($my_ip)
19         validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
20
21       The following values will fail, causing compilation to abort:
22
23         $some_array = [ true, false, "garbage string", "1.2.3.4" ]
24         validate_ipv6_address($some_array)
25
26     DOC
27              ) do |args|
28
29     function_deprecation([:validate_ipv6_address, 'This method is deprecated, please use the stdlib validate_legacy function,
30                             with Stdlib::Compat::Ipv6. There is further documentation for validate_legacy function in the README.'])
31
32     require 'ipaddr'
33     rescuable_exceptions = [ArgumentError]
34
35     if defined?(IPAddr::InvalidAddressError)
36       rescuable_exceptions << IPAddr::InvalidAddressError
37     end
38
39     if args.empty?
40       raise Puppet::ParseError, "validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)"
41     end
42
43     args.each do |arg|
44       unless arg.is_a?(String)
45         raise Puppet::ParseError, "#{arg.inspect} is not a string."
46       end
47
48       begin
49         unless IPAddr.new(arg).ipv6?
50           raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
51         end
52       rescue *rescuable_exceptions
53         raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
54       end
55     end
56   end
57 end