File: inet6_prefix.rb

package info (click to toggle)
puppet-module-openstacklib 27.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 956 kB
  • sloc: ruby: 4,549; python: 33; sh: 22; makefile: 10
file content (31 lines) | stat: -rw-r--r-- 917 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Add inet6 prefix if the argument is an IPv6 address.
#
# This is useful for services relying on python-memcached which require
# the inet6:[<ip_address]:<port> format.
#
# Returns the argument untouched otherwise.
Puppet::Functions.create_function(:inet6_prefix) do
  def inet6_prefix(*args)
    require 'ipaddr'

    result = []
    args = args[0] if args[0].kind_of?(Array)
    args = [args] unless args.kind_of?(Array)
    args.each do |ip|
      begin
        unless ip.match(/^inet6:.+/)
          ip_parts = ip.split(/\s|\[|\]/).reject { |c| c.empty? }
          if IPAddr.new(ip_parts[0]).ipv6?
            Puppet.debug("#{ip} is changed to inet6:[#{ip}]")
            ip = "inet6:[#{ip_parts.shift}]#{ip_parts.join}"
          end
        end
      rescue ArgumentError, NoMethodError => e
        # ignore it
      end
      result << ip
    end
    return result[0] if args.size == 1
    result
  end
end