Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / enclose_ipv6.rb
1 #
2 # enclose_ipv6.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:enclose_ipv6, :type => :rvalue, :doc => <<-EOS
7 Takes an array of ip addresses and encloses the ipv6 addresses with square brackets.
8     EOS
9   ) do |arguments|
10
11     require 'ipaddr'
12
13     rescuable_exceptions = [ ArgumentError ]
14     if defined?(IPAddr::InvalidAddressError)
15             rescuable_exceptions << IPAddr::InvalidAddressError
16     end
17
18     if (arguments.size != 1) then
19       raise(Puppet::ParseError, "enclose_ipv6(): Wrong number of arguments given #{arguments.size} for 1")
20     end
21     unless arguments[0].is_a?(String) or arguments[0].is_a?(Array) then
22       raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type given #{arguments[0].class} expected String or Array")
23     end
24
25     input = [arguments[0]].flatten.compact
26     result = []
27
28     input.each do |val|
29       unless val == '*'
30         begin
31           ip = IPAddr.new(val)
32         rescue *rescuable_exceptions
33           raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument given #{val} is not an ip address.")
34         end
35         val = "[#{ip.to_s}]" if ip.ipv6?
36       end
37       result << val
38     end
39
40     return result.uniq
41   end
42 end