Skip to content

Commit 69ca8d0

Browse files
committed
Merge pull request #570 from gfidente/master
Add is_ipv4_address and is_ipv6_address functions
2 parents 2c3beac + d85aec4 commit 69ca8d0

5 files changed

Lines changed: 182 additions & 0 deletions

File tree

README.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,14 @@ Returns 'true' if the variable returned to this string is an integer. *Type*: rv
583583

584584
Returns 'true' if the string passed to this function is a valid IP address. *Type*: rvalue.
585585

586+
#### `is_ipv6_address`
587+
588+
Returns 'true' if the string passed to this function is a valid IPv6 address. *Type*: rvalue.
589+
590+
#### `is_ipv4_address`
591+
592+
Returns 'true' if the string passed to this function is a valid IPv4 address. *Type*: rvalue.
593+
586594
#### `is_mac_address`
587595

588596
Returns 'true' if the string passed to this function is a valid MAC address. *Type*: rvalue.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# is_ipv4_address.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:is_ipv4_address, :type => :rvalue, :doc => <<-EOS
7+
Returns true if the string passed to this function is a valid IPv4 address.
8+
EOS
9+
) do |arguments|
10+
11+
require 'ipaddr'
12+
13+
if (arguments.size != 1) then
14+
raise(Puppet::ParseError, "is_ipv4_address(): Wrong number of arguments "+
15+
"given #{arguments.size} for 1")
16+
end
17+
18+
begin
19+
ip = IPAddr.new(arguments[0])
20+
rescue ArgumentError
21+
return false
22+
end
23+
24+
return ip.ipv4?
25+
end
26+
end
27+
28+
# vim: set ts=2 sw=2 et :
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# is_ipv6_address.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:is_ipv6_address, :type => :rvalue, :doc => <<-EOS
7+
Returns true if the string passed to this function is a valid IPv6 address.
8+
EOS
9+
) do |arguments|
10+
11+
require 'ipaddr'
12+
13+
if (arguments.size != 1) then
14+
raise(Puppet::ParseError, "is_ipv6_address(): Wrong number of arguments "+
15+
"given #{arguments.size} for 1")
16+
end
17+
18+
begin
19+
ip = IPAddr.new(arguments[0])
20+
rescue ArgumentError
21+
return false
22+
end
23+
24+
return ip.ipv6?
25+
end
26+
end
27+
28+
# vim: set ts=2 sw=2 et :
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper_acceptance'
3+
4+
describe 'is_ipv4_address function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
5+
describe 'success' do
6+
it 'is_ipv4_addresss' do
7+
pp = <<-EOS
8+
$a = '1.2.3.4'
9+
$b = true
10+
$o = is_ipv4_address($a)
11+
if $o == $b {
12+
notify { 'output correct': }
13+
}
14+
EOS
15+
16+
apply_manifest(pp, :catch_failures => true) do |r|
17+
expect(r.stdout).to match(/Notice: output correct/)
18+
end
19+
end
20+
it 'is_ipv4_addresss strings' do
21+
pp = <<-EOS
22+
$a = "aoeu"
23+
$b = false
24+
$o = is_ipv4_address($a)
25+
if $o == $b {
26+
notify { 'output correct': }
27+
}
28+
EOS
29+
30+
apply_manifest(pp, :catch_failures => true) do |r|
31+
expect(r.stdout).to match(/Notice: output correct/)
32+
end
33+
end
34+
it 'is_ipv4_addresss ipv4 out of range' do
35+
pp = <<-EOS
36+
$a = '1.2.3.400'
37+
$b = false
38+
$o = is_ipv4_address($a)
39+
if $o == $b {
40+
notify { 'output correct': }
41+
}
42+
EOS
43+
44+
apply_manifest(pp, :catch_failures => true) do |r|
45+
expect(r.stdout).to match(/Notice: output correct/)
46+
end
47+
end
48+
end
49+
describe 'failure' do
50+
it 'handles improper argument counts'
51+
end
52+
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper_acceptance'
3+
4+
describe 'is_ipv6_address function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
5+
describe 'success' do
6+
it 'is_ipv6_addresss' do
7+
pp = <<-EOS
8+
$a = "fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"
9+
$b = true
10+
$o = is_ipv6_address($a)
11+
if $o == $b {
12+
notify { 'output correct': }
13+
}
14+
EOS
15+
16+
apply_manifest(pp, :catch_failures => true) do |r|
17+
expect(r.stdout).to match(/Notice: output correct/)
18+
end
19+
end
20+
it 'is_ipv6_addresss ipv6 compressed' do
21+
pp = <<-EOS
22+
$a = "fe00::1"
23+
$b = true
24+
$o = is_ipv6_address($a)
25+
if $o == $b {
26+
notify { 'output correct': }
27+
}
28+
EOS
29+
30+
apply_manifest(pp, :catch_failures => true) do |r|
31+
expect(r.stdout).to match(/Notice: output correct/)
32+
end
33+
end
34+
it 'is_ipv6_addresss strings' do
35+
pp = <<-EOS
36+
$a = "aoeu"
37+
$b = false
38+
$o = is_ipv6_address($a)
39+
if $o == $b {
40+
notify { 'output correct': }
41+
}
42+
EOS
43+
44+
apply_manifest(pp, :catch_failures => true) do |r|
45+
expect(r.stdout).to match(/Notice: output correct/)
46+
end
47+
end
48+
it 'is_ipv6_addresss ip out of range' do
49+
pp = <<-EOS
50+
$a = 'fe80:0000:cd12:d123:e2f8:47ff:fe09:gggg'
51+
$b = false
52+
$o = is_ipv6_address($a)
53+
if $o == $b {
54+
notify { 'output correct': }
55+
}
56+
EOS
57+
58+
apply_manifest(pp, :catch_failures => true) do |r|
59+
expect(r.stdout).to match(/Notice: output correct/)
60+
end
61+
end
62+
end
63+
describe 'failure' do
64+
it 'handles improper argument counts'
65+
end
66+
end

0 commit comments

Comments
 (0)