2011-04-30 04:16:12 +02:00
|
|
|
#
|
2011-08-05 09:25:03 +02:00
|
|
|
# is_ip_address.rb
|
2011-04-30 04:16:12 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
2011-08-05 09:25:03 +02:00
|
|
|
newfunction(:is_ip_address, :type => :rvalue, :doc => <<-EOS
|
2011-07-30 00:09:30 +02:00
|
|
|
Returns true if the string passed to this function is a valid IP address.
|
2011-04-30 04:16:12 +02:00
|
|
|
EOS
|
|
|
|
) do |arguments|
|
2011-06-29 22:21:55 +02:00
|
|
|
|
2011-07-28 19:10:16 +02:00
|
|
|
require 'ipaddr'
|
|
|
|
|
2011-06-29 22:21:55 +02:00
|
|
|
if (arguments.size != 1) then
|
2011-08-05 09:25:03 +02:00
|
|
|
raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments "+
|
2011-06-29 22:21:55 +02:00
|
|
|
"given #{arguments.size} for 1")
|
|
|
|
end
|
|
|
|
|
2012-11-09 18:08:24 +01:00
|
|
|
begin
|
2011-07-28 19:10:16 +02:00
|
|
|
ip = IPAddr.new(arguments[0])
|
|
|
|
rescue ArgumentError
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if ip.ipv4? or ip.ipv6? then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2011-04-30 04:16:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|