Puppet Function: nets2ddq

Defined in:
lib/puppet/parser/functions/nets2ddq.rb
Function type:
Ruby 3.x API

Overview

nets2ddq(Variant[Array[String], String] $networks)Variant[Array[String], Strin

Take an input Array of networks and returns an equivalent Array in Dotted Quad notation.

It can also accept a String separated by spaces, commas, or semicolons.

Parameters:

  • networks (Variant[Array[String], String])

Returns:



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/parser/functions/nets2ddq.rb', line 6

newfunction(:nets2ddq, :type => :rvalue, :doc => <<-EOM) do |args|

  require File.expand_path(File.dirname(__FILE__) + '/../../../puppetx/simp/simplib.rb')
  Take an input `Array` of networks and returns an equivalent `Array` in
  Dotted Quad notation.

  It can also accept a `String` separated by spaces, commas, or semicolons.

  @param networks [Variant[Array[String], String]]

  @return [Variant[Array[String], String]]
  EOM

  networks = Array(args.dup).flatten
  retval = Array.new

  # Try to be smart about pulling the string apart.
  networks = networks.map{|x|
    if !x.is_a?(Array) then
      x = x.split(/\s|,|;/).delete_if{ |y| y.empty? }
    end
  }.flatten

  networks.each do |lnet|
    begin
      ipaddr = IPAddr.new(lnet)
    rescue
      if PuppetX::SIMP::Simplib.hostname?(lnet) then
        retval << lnet
        next
      end
      raise Puppet::ParseError,"nets2ddq: #{lnet} is not a valid IP address!"
    end

    # Just add it if it doesn't have a specified netmask.
    if lnet =~ /\// then
      retval << "#{ipaddr.to_s}/#{ipaddr.inspect.split('/').last.chop}"
    else
      retval << lnet
    end
  end
  retval
end