Puppet Function: validate_port

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

Overview

validate_port()N

Validates whether or not the passed argument is a valid port (i.e. between 1 - 65535).

Examples:

Passing

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

Failing

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

Returns:

  • (N)

    N



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

newfunction(:validate_port, :doc => <<-EOS) do |args|

  unless args.length > 0 then
    raise(Puppet::ParseError, "validate_port(): Wrong number of args: #{args.length}; must be > 0")
  Validates whether or not the passed argument is a valid port
  (i.e. between `1` - `65535`).

  @example Passing
    $port = '10541'
    $ports = ['5555', '7777', '1', '65535']
    validate_port($port)
    validate_port($ports)
    validate_port('11', '22')

  @example Failing
    validate_port('0')
    validate_port('65536')
    validate_port(['1', '1000', '100000'])

  @return [Nil]
  EOS
  end

  args.each do |arg|
    arg = Array(arg)
    arg.each do |port|
      if not port.to_i.between?(1,65535) then
        raise Puppet::ParseError, ("'#{port}' is not a valid port.")
      end
    end
  end
end