Merge pull request #570 from gfidente/master
Add is_ipv4_address and is_ipv6_address functions
This commit is contained in:
commit
69ca8d09c6
5 changed files with 182 additions and 0 deletions
|
@ -583,6 +583,14 @@ Returns 'true' if the variable returned to this string is an integer. *Type*: rv
|
|||
|
||||
Returns 'true' if the string passed to this function is a valid IP address. *Type*: rvalue.
|
||||
|
||||
#### `is_ipv6_address`
|
||||
|
||||
Returns 'true' if the string passed to this function is a valid IPv6 address. *Type*: rvalue.
|
||||
|
||||
#### `is_ipv4_address`
|
||||
|
||||
Returns 'true' if the string passed to this function is a valid IPv4 address. *Type*: rvalue.
|
||||
|
||||
#### `is_mac_address`
|
||||
|
||||
Returns 'true' if the string passed to this function is a valid MAC address. *Type*: rvalue.
|
||||
|
|
28
lib/puppet/parser/functions/is_ipv4_address.rb
Normal file
28
lib/puppet/parser/functions/is_ipv4_address.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
#
|
||||
# is_ipv4_address.rb
|
||||
#
|
||||
|
||||
module Puppet::Parser::Functions
|
||||
newfunction(:is_ipv4_address, :type => :rvalue, :doc => <<-EOS
|
||||
Returns true if the string passed to this function is a valid IPv4 address.
|
||||
EOS
|
||||
) do |arguments|
|
||||
|
||||
require 'ipaddr'
|
||||
|
||||
if (arguments.size != 1) then
|
||||
raise(Puppet::ParseError, "is_ipv4_address(): Wrong number of arguments "+
|
||||
"given #{arguments.size} for 1")
|
||||
end
|
||||
|
||||
begin
|
||||
ip = IPAddr.new(arguments[0])
|
||||
rescue ArgumentError
|
||||
return false
|
||||
end
|
||||
|
||||
return ip.ipv4?
|
||||
end
|
||||
end
|
||||
|
||||
# vim: set ts=2 sw=2 et :
|
28
lib/puppet/parser/functions/is_ipv6_address.rb
Normal file
28
lib/puppet/parser/functions/is_ipv6_address.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
#
|
||||
# is_ipv6_address.rb
|
||||
#
|
||||
|
||||
module Puppet::Parser::Functions
|
||||
newfunction(:is_ipv6_address, :type => :rvalue, :doc => <<-EOS
|
||||
Returns true if the string passed to this function is a valid IPv6 address.
|
||||
EOS
|
||||
) do |arguments|
|
||||
|
||||
require 'ipaddr'
|
||||
|
||||
if (arguments.size != 1) then
|
||||
raise(Puppet::ParseError, "is_ipv6_address(): Wrong number of arguments "+
|
||||
"given #{arguments.size} for 1")
|
||||
end
|
||||
|
||||
begin
|
||||
ip = IPAddr.new(arguments[0])
|
||||
rescue ArgumentError
|
||||
return false
|
||||
end
|
||||
|
||||
return ip.ipv6?
|
||||
end
|
||||
end
|
||||
|
||||
# vim: set ts=2 sw=2 et :
|
52
spec/acceptance/is_ipv4_address_spec.rb
Executable file
52
spec/acceptance/is_ipv4_address_spec.rb
Executable file
|
@ -0,0 +1,52 @@
|
|||
#! /usr/bin/env ruby -S rspec
|
||||
require 'spec_helper_acceptance'
|
||||
|
||||
describe 'is_ipv4_address function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
|
||||
describe 'success' do
|
||||
it 'is_ipv4_addresss' do
|
||||
pp = <<-EOS
|
||||
$a = '1.2.3.4'
|
||||
$b = true
|
||||
$o = is_ipv4_address($a)
|
||||
if $o == $b {
|
||||
notify { 'output correct': }
|
||||
}
|
||||
EOS
|
||||
|
||||
apply_manifest(pp, :catch_failures => true) do |r|
|
||||
expect(r.stdout).to match(/Notice: output correct/)
|
||||
end
|
||||
end
|
||||
it 'is_ipv4_addresss strings' do
|
||||
pp = <<-EOS
|
||||
$a = "aoeu"
|
||||
$b = false
|
||||
$o = is_ipv4_address($a)
|
||||
if $o == $b {
|
||||
notify { 'output correct': }
|
||||
}
|
||||
EOS
|
||||
|
||||
apply_manifest(pp, :catch_failures => true) do |r|
|
||||
expect(r.stdout).to match(/Notice: output correct/)
|
||||
end
|
||||
end
|
||||
it 'is_ipv4_addresss ipv4 out of range' do
|
||||
pp = <<-EOS
|
||||
$a = '1.2.3.400'
|
||||
$b = false
|
||||
$o = is_ipv4_address($a)
|
||||
if $o == $b {
|
||||
notify { 'output correct': }
|
||||
}
|
||||
EOS
|
||||
|
||||
apply_manifest(pp, :catch_failures => true) do |r|
|
||||
expect(r.stdout).to match(/Notice: output correct/)
|
||||
end
|
||||
end
|
||||
end
|
||||
describe 'failure' do
|
||||
it 'handles improper argument counts'
|
||||
end
|
||||
end
|
66
spec/acceptance/is_ipv6_address_spec.rb
Executable file
66
spec/acceptance/is_ipv6_address_spec.rb
Executable file
|
@ -0,0 +1,66 @@
|
|||
#! /usr/bin/env ruby -S rspec
|
||||
require 'spec_helper_acceptance'
|
||||
|
||||
describe 'is_ipv6_address function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
|
||||
describe 'success' do
|
||||
it 'is_ipv6_addresss' do
|
||||
pp = <<-EOS
|
||||
$a = "fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"
|
||||
$b = true
|
||||
$o = is_ipv6_address($a)
|
||||
if $o == $b {
|
||||
notify { 'output correct': }
|
||||
}
|
||||
EOS
|
||||
|
||||
apply_manifest(pp, :catch_failures => true) do |r|
|
||||
expect(r.stdout).to match(/Notice: output correct/)
|
||||
end
|
||||
end
|
||||
it 'is_ipv6_addresss ipv6 compressed' do
|
||||
pp = <<-EOS
|
||||
$a = "fe00::1"
|
||||
$b = true
|
||||
$o = is_ipv6_address($a)
|
||||
if $o == $b {
|
||||
notify { 'output correct': }
|
||||
}
|
||||
EOS
|
||||
|
||||
apply_manifest(pp, :catch_failures => true) do |r|
|
||||
expect(r.stdout).to match(/Notice: output correct/)
|
||||
end
|
||||
end
|
||||
it 'is_ipv6_addresss strings' do
|
||||
pp = <<-EOS
|
||||
$a = "aoeu"
|
||||
$b = false
|
||||
$o = is_ipv6_address($a)
|
||||
if $o == $b {
|
||||
notify { 'output correct': }
|
||||
}
|
||||
EOS
|
||||
|
||||
apply_manifest(pp, :catch_failures => true) do |r|
|
||||
expect(r.stdout).to match(/Notice: output correct/)
|
||||
end
|
||||
end
|
||||
it 'is_ipv6_addresss ip out of range' do
|
||||
pp = <<-EOS
|
||||
$a = 'fe80:0000:cd12:d123:e2f8:47ff:fe09:gggg'
|
||||
$b = false
|
||||
$o = is_ipv6_address($a)
|
||||
if $o == $b {
|
||||
notify { 'output correct': }
|
||||
}
|
||||
EOS
|
||||
|
||||
apply_manifest(pp, :catch_failures => true) do |r|
|
||||
expect(r.stdout).to match(/Notice: output correct/)
|
||||
end
|
||||
end
|
||||
end
|
||||
describe 'failure' do
|
||||
it 'handles improper argument counts'
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue