Files
puppet-tripleo/lib/puppet/parser/functions/is_ip_addresses.rb
Michele Baldessari 4c7ca4cbc3 Fail more gracefully when passed an empty ip
Introduce a new function called is_ip_addresses which will verify
if a string or an array of strings are composed of correct ip addresses.

We do this in order to fail a bit more clearly if we are passed an empty
or broken ip address. Without this the failure will be in pacemaker
failing to start a VIP called 'ip-'.

Also convert the only use of legacy is_ip_address stdlib function in
mysql::client to this new function (for consistency reasons).

Suggested-by: Rhys Oxenham <roxenham@redhat.com>

Change-Id: Ie15c585a9a902b577f35a75de191bfa91c132668
2018-03-13 17:08:34 +01:00

26 lines
775 B
Ruby

require 'ipaddr'
# Custom function to verify if the parameter is a string representing an ip address
# or an array of strings representing an ip address
# Returns true if all elements are proper ip addresses and false otherwise
module Puppet::Parser::Functions
newfunction(:is_ip_addresses, :type => :rvalue, :doc => "Verify if a string or an array of strings are all IP addresses.") do |arg|
if arg[0].class != String and arg[0].class != Array
raise Puppet::ParseError, "Syntax error: #{arg[0]} must be a String or an Array"
end
if arg[0].class == String
ips = [arg[0]]
else
ips = arg[0]
end
ips.each do |ip|
begin
tmpip = IPAddr.new ip
rescue
return false
end
end
return true
end
end