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