Puppet Function: validate_ip_address_array

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

Overview

validate_ip_address_array()Any

Validates an array of IP addresses, raising a ParseError should one or more addresses fail. Validates both v4 and v6 IP addresses.

The following values will pass:

validate_ip_address_array([‘127.0.0.1’, ‘::1’])

The following values will raise an error:

validate_ip_address_array(‘127.0.0.1’) validate_ip_address_array()

Returns:

  • (Any)


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
# File 'lib/puppet/parser/functions/validate_ip_address_array.rb', line 38

newfunction(:validate_ip_address_array, :doc => <<-ENDHEREDOC
  Validates an array of IP addresses, raising a ParseError should one or
  more addresses fail. Validates both v4 and v6 IP addresses.

  The following values will pass:

  validate_ip_address_array(['127.0.0.1', '::1'])

  The following values will raise an error:

  validate_ip_address_array('127.0.0.1')
  validate_ip_address_array(['not-an-address'])

ENDHEREDOC
) do |args|

  require 'ipaddr'

  # Make sure that we've got something to validate!
  unless args.length > 0
    error_msg = 'validate_ip_address_array: wrong number of arguments ' +
        "(#{args.length}; must be > 0)"
    raise Puppet::ParseError, (error_msg)
  end

  args.each do |arg|
    # Raise an error if we don't have an array.
    unless arg.is_a?(Array)
      error_msg = "#{arg.inspect} is not an Array.  It looks to be a " +
          "#{arg.class}"
      raise Puppet::ParseError, (error_msg)
    end

    arg.each do |addr|
      # Make sure that we were given a string.
      unless addr.is_a?(String)
        raise Puppet::ParseError, "#{addr.inspect} is not a string."
      end

      error_msg = "#{addr.inspect} is not a valid IP address."

      # Raise an error if we don't get a valid IP address (v4 or v6).
      begin
        ip = IPAddr.new(addr)
        raise Puppet::ParseError, error_msg unless ip.ipv4? or ip.ipv6?
      rescue ArgumentError
        raise Puppet::ParseError, error_msg
      end
    end
  end

end