Update stdlib and concat to 6.1.0 both
[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     @summary
7       Takes an array of ip addresses and encloses the ipv6 addresses with square brackets.
8
9     @return
10       encloses the ipv6 addresses with square brackets.
11
12   DOC
13              ) do |arguments|
14
15     require 'ipaddr'
16
17     rescuable_exceptions = [ArgumentError]
18     if defined?(IPAddr::InvalidAddressError)
19       rescuable_exceptions << IPAddr::InvalidAddressError
20     end
21
22     if arguments.size != 1
23       raise(Puppet::ParseError, "enclose_ipv6(): Wrong number of arguments given #{arguments.size} for 1")
24     end
25     unless arguments[0].is_a?(String) || arguments[0].is_a?(Array)
26       raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type given #{arguments[0].class} expected String or Array")
27     end
28
29     input = [arguments[0]].flatten.compact
30     result = []
31
32     input.each do |val|
33       unless val == '*'
34         begin
35           ip = IPAddr.new(val)
36         rescue *rescuable_exceptions
37           raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument given #{val} is not an ip address.")
38         end
39         val = "[#{ip}]" if ip.ipv6?
40       end
41       result << val
42     end
43
44     return result.uniq
45   end
46 end