Puppet Function: iptables::slice_ports

Defined in:
lib/puppet/functions/iptables/slice_ports.rb
Function type:
Ruby 4.x API

Overview

iptables::slice_ports(Variant[String,Array[String]] $input, Integer[1] $max_length)Array[Array[String]]

Split a stringified Iptables::DestPort into an Array that contain groupings of ‘max_length` size.

Parameters:

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

    One or more ports or port ranges, all represented as strings.

  • max_length (Integer[1])

    The maximum length of each group.

Returns:

  • (Array[Array[String]])

    ]



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
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/functions/iptables/slice_ports.rb', line 3

Puppet::Functions.create_function(:'iptables::slice_ports') do

  # @param input One or more ports or port ranges, all represented
  #   as strings.
  #
  # @param max_length The maximum length of each group.
  #
  # @return [Array[Array[String]]]]
  dispatch :slice_ports do
    required_param 'Variant[String,Array[String]]', :input
    required_param 'Integer[1]',                    :max_length
  end

  def slice_ports(input, max_length)
    to_slice = Array(input).flatten
    split_char = ':'

    if max_length == 1 && to_slice.any? { |entry| entry.include?(split_char) }
      err_msg = 'iptables::slice_port: max_length must be >=2 when input has a port range'
      fail(err_msg)
    end

    retval = []
    count = 0
    group = []
    to_slice.each do |entry|
      num_values = entry.include?(split_char) ? 2 : 1
      if count + num_values <= max_length
        count += num_values
        group << entry
      else
        retval << group
        count = num_values
        group = [ entry ]
      end
    end
    retval << group unless group.empty?

    retval
  end
end