source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/enclose_ipv6.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.2 KB
Line 
1#
2# enclose_ipv6.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:enclose_ipv6, :type => :rvalue, :doc => <<-EOS
7Takes 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 "+
20 "given #{arguments.size} for 1")
21 end
22 unless arguments[0].is_a?(String) or arguments[0].is_a?(Array) then
23 raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type "+
24 "given #{arguments[0].class} expected String or Array")
25 end
26
27 input = [arguments[0]].flatten.compact
28 result = []
29
30 input.each do |val|
31 unless val == '*'
32 begin
33 ip = IPAddr.new(val)
34 rescue *rescuable_exceptions
35 raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument "+
36 "given #{val} is not an ip address.")
37 end
38 val = "[#{ip.to_s}]" if ip.ipv6?
39 end
40 result << val
41 end
42
43 return result.uniq
44 end
45end
Note: See TracBrowser for help on using the repository browser.