Puppet Function: simplib::nets2ddq

Defined in:
lib/puppet/functions/simplib/nets2ddq.rb
Function type:
Ruby 4.x API

Overview

Tranforms a list of networks into an equivalent array in dotted quad notation.

  • IPv4 CIDR networks are converted to dotted quad notation networks. All other IP addresses and hostnames are left untouched.

  • Terminates catalog compilation if any input item is not a valid network or hostname.

Signatures:

  • simplib::nets2ddq(Array $networks)Array[String]

    Returns Converted input.

    Examples:

    Convert Array input

    
    $foo = [ '10.0.1.0/24',
             '10.0.2.0/255.255.255.0',
             '10.0.3.25',
             'myhost',
             '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
             '2001:0db8:85a3:0000:0000:8a2e:0370:7334/64' ]
    
    $bar = simplib::nets2ddq($foo)
    
    $bar contains:[ '10.0.1.0/255.255.255.0',
                    '10.0.2.0/255.255.255.0',
                    '10.0.3.25',
                    'myhost',
                    '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
                    '2001:0db8:85a3:0000:0000:8a2e:0370:7334/64' ]

    Parameters:

    • networks (Array)

      The networks to convert

    Returns:

    • (Array[String])

      Converted input

    Raises:

    • (RuntimeError)

      if any input item is not a valid network or hostname

  • simplib::nets2ddq(String $networks_string)Array[String]

    Returns Converted input.

    Examples:

    Convert String input

    
    $foo = '10.0.1.0/24 10.0.2.0/255.255.255.0 10.0.3.25 myhost 2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3:0000:0000:8a2e:0370:7334/64'
    
    $bar = simplib::nets2ddq($foo)
    
    $bar contains:[ '10.0.1.0/255.255.255.0',
                    '10.0.2.0/255.255.255.0',
                    '10.0.3.25',
                    'myhost',
                    '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
                    '2001:0db8:85a3:0000:0000:8a2e:0370:7334/64' ]

    Parameters:

    • networks_string (String)

      String containing the list of networks to convert; list elements are separated by spaces, commas or semicolons.

    Returns:

    • (Array[String])

      Converted input

    Raises:

    • (RuntimeError)

      if any input item is not a valid network or hostname



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet/functions/simplib/nets2ddq.rb', line 9

Puppet::Functions.create_function(:'simplib::nets2ddq') do

  # @param networks The networks to convert
  # @return [Array[String]] Converted input
  # @raise [RuntimeError] if any input item is not a valid network
  #   or hostname
  #
  # @example Convert Array input
  #
  #   $foo = [ '10.0.1.0/24',
  #            '10.0.2.0/255.255.255.0',
  #            '10.0.3.25',
  #            'myhost',
  #            '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  #            '2001:0db8:85a3:0000:0000:8a2e:0370:7334/64' ]
  #
  #   $bar = simplib::nets2ddq($foo)
  #
  #   $bar contains:[ '10.0.1.0/255.255.255.0',
  #                   '10.0.2.0/255.255.255.0',
  #                   '10.0.3.25',
  #                   'myhost',
  #                   '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  #                   '2001:0db8:85a3:0000:0000:8a2e:0370:7334/64' ]
  #
  dispatch :nets2ddq do
    required_param 'Array', :networks
  end

  # @param networks_string String containing the list of networks to
  #   convert; list elements are separated by spaces, commas or semicolons.
  # @return [Array[String]] Converted input
  # @raise [RuntimeError] if any input item is not a valid network
  #   or hostname
  #
  # @example Convert String input
  #
  #   $foo = '10.0.1.0/24 10.0.2.0/255.255.255.0 10.0.3.25 myhost 2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3:0000:0000:8a2e:0370:7334/64'
  #
  #   $bar = simplib::nets2ddq($foo)
  #
  #   $bar contains:[ '10.0.1.0/255.255.255.0',
  #                   '10.0.2.0/255.255.255.0',
  #                   '10.0.3.25',
  #                   'myhost',
  #                   '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  #                   '2001:0db8:85a3:0000:0000:8a2e:0370:7334/64' ]
  #
  dispatch :nets2ddq_string_input do
    required_param 'String', :networks_string
  end

  def nets2ddq_string_input(networks_string)
    networks = networks_string.split(/\s|,|;/).delete_if{ |y| y.empty? }
    nets2ddq(networks)
  end

  def nets2ddq(networks)
    require 'ipaddr'
    require File.expand_path(File.dirname(__FILE__) + '/../../../puppetx/simp/simplib.rb')

    retval = Array.new
    networks.each do |lnet|
      begin
        ipaddr = IPAddr.new(lnet)
      rescue
        if PuppetX::SIMP::Simplib.hostname?(lnet) then
          retval << lnet
          next
        end
        fail("simplib::nets2ddq(): '#{lnet}' is not a valid network.")
      end

      # Just add it without touching if it is an ipv6 addr
      if ipaddr.ipv6?()
        retval << lnet
      # Just add it if it doesn't have a specified netmask.
      elsif lnet =~ /\//
        retval << "#{ipaddr.to_s}/#{ipaddr.inspect.split('/').last.chop}"
      else
        retval << lnet
      end
    end
    retval
  end
end