Puppet Function: simplib::validate_port

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

Overview

simplib::validate_port(Variant[String[1],Integer,StringOrIntegerArray] *$port_args)Nil

Validates whether each passed argument contains valid port(s).

  • Each element of each argument must, numerically, be in the range [1, 65535].

  • Terminates catalog compilation if validation fails.

Examples:

Passing

$port = '10541'
$ports = [5555, '7777', '1', '65535']
simplib::validate_port($port)
simplib::validate_port($ports)
simplib::validate_port('11', 22)
simplib::validate_port('11', $ports)

Failing

simplib::validate_port('0')
simplib::validate_port(65536)
simplib::validate_port('1', '1000', '100000')
simplib::validate_port(['1', '1000', '100000'])
simplib::validate_port('1', ['1000', '100000'])

Parameters:

  • *port_args (Variant[String[1],Integer,StringOrIntegerArray])

    Arguments each of which contain either an individual port or an array of ports.

Returns:

  • (Nil)

Raises:

  • (RuntimeError)

    if validation fails



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
# File 'lib/puppet/functions/simplib/validate_port.rb', line 7

Puppet::Functions.create_function(:'simplib::validate_port') do
  local_types do
    type 'StringOrInteger = Variant[String[1],Integer]'
    type 'StringOrIntegerArray = Array[StringOrInteger,1]'
  end

  # @param port_args Arguments each of which contain either an
  #   individual port or an array of ports.
  # @return [Nil]
  # @raise [RuntimeError] if validation fails
  #
  # @example Passing
  #   $port = '10541'
  #   $ports = [5555, '7777', '1', '65535']
  #   simplib::validate_port($port)
  #   simplib::validate_port($ports)
  #   simplib::validate_port('11', 22)
  #   simplib::validate_port('11', $ports)
  #
  # @example Failing
  #   simplib::validate_port('0')
  #   simplib::validate_port(65536)
  #   simplib::validate_port('1', '1000', '100000')
  #   simplib::validate_port(['1', '1000', '100000'])
  #   simplib::validate_port('1', ['1000', '100000'])
  dispatch :validate_ports do
    required_repeated_param 'Variant[String[1],Integer,StringOrIntegerArray]', :port_args
  end

  def validate_ports(*port_args)
    ports = Array(port_args).flatten
    ports.each do |port|
      if not port.to_i.between?(1,65535) then
        fail("simplib::validate_ports: '#{port}' is not a valid port.")
      end
    end
  end
end