]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/validate_ipv6_address.rb
upgrade to stdlib 4.6.1
[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     require "ipaddr"
23     rescuable_exceptions = [ ArgumentError ]
24
25     if defined?(IPAddr::InvalidAddressError)
26       rescuable_exceptions << IPAddr::InvalidAddressError
27     end
28
29     unless args.length > 0 then
30       raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)")
31     end
32
33     args.each do |arg|
34       unless arg.is_a?(String)
35         raise Puppet::ParseError, "#{arg.inspect} is not a string."
36       end
37
38       begin
39         unless IPAddr.new(arg).ipv6?
40           raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
41         end
42       rescue *rescuable_exceptions
43         raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
44       end
45     end
46
47   end
48
49 end