Skip to content

Commit 72f6e37

Browse files
committed
Merge pull request #577 from EmilienM/enclose_ipv6
Add enclose_ipv6 function
2 parents 69ca8d0 + 0378336 commit 72f6e37

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

README.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ Converts the case of a string or of all strings in an array to lowercase. *Type*
344344

345345
Returns true if the argument is an array or hash that contains no elements, or an empty string. Returns false when the argument is a numerical value. *Type*: rvalue.
346346

347+
#### `enclose_ipv6`
348+
349+
Takes an array of ip addresses and encloses the ipv6 addresses with square
350+
brackets. *Type*: rvalue.
351+
347352
#### `ensure_packages`
348353

349354
Takes a list of packages and only installs them if they don't already exist. It optionally takes a hash as a second parameter to be passed as the third argument to the `ensure_resource()` function. *Type*: statement.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# enclose_ipv6.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:enclose_ipv6, :type => :rvalue, :doc => <<-EOS
7+
Takes an array of ip addresses and encloses the ipv6 addresses with square brackets.
8+
EOS
9+
) do |arguments|
10+
11+
require 'ipaddr'
12+
13+
rescuable_exceptions = [ ArgumentError ]
14+
if defined?(IPAddr::InvalidAddressError)
15+
rescuable_exceptions << IPAddr::InvalidAddressError
16+
end
17+
18+
if (arguments.size != 1) then
19+
raise(Puppet::ParseError, "enclose_ipv6(): Wrong number of arguments "+
20+
"given #{arguments.size} for 1")
21+
end
22+
unless arguments[0].is_a?(String) or arguments[0].is_a?(Array) then
23+
raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type "+
24+
"given #{arguments[0].class} expected String or Array")
25+
end
26+
27+
input = [arguments[0]].flatten.compact
28+
result = []
29+
30+
input.each do |val|
31+
unless val == '*'
32+
begin
33+
ip = IPAddr.new(val)
34+
rescue *rescuable_exceptions
35+
raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument "+
36+
"given #{val} is not an ip address.")
37+
end
38+
val = "[#{ip.to_s}]" if ip.ipv6?
39+
end
40+
result << val
41+
end
42+
43+
return result.uniq
44+
end
45+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the enclose_ipv6 function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
expect(Puppet::Parser::Functions.function("enclose_ipv6")).to eq("function_enclose_ipv6")
9+
end
10+
11+
it "should raise a ParseError if there is less than 1 arguments" do
12+
expect { scope.function_enclose_ipv6([]) }.to( raise_error(Puppet::ParseError) )
13+
end
14+
15+
it "should raise a ParseError if there is more than 1 arguments" do
16+
expect { scope.function_enclose_ipv6(['argument1','argument2']) }.to( raise_error(Puppet::ParseError) )
17+
end
18+
19+
it "should raise a ParseError when given garbage" do
20+
expect { scope.function_enclose_ipv6(['garbage']) }.to( raise_error(Puppet::ParseError) )
21+
end
22+
23+
it "should raise a ParseError when given something else than a string or an array" do
24+
expect { scope.function_enclose_ipv6([['1' => '127.0.0.1']]) }.to( raise_error(Puppet::ParseError) )
25+
end
26+
27+
it "should not raise a ParseError when given a single ip string" do
28+
expect { scope.function_enclose_ipv6(['127.0.0.1']) }.to_not raise_error
29+
end
30+
31+
it "should not raise a ParseError when given * as ip string" do
32+
expect { scope.function_enclose_ipv6(['*']) }.to_not raise_error
33+
end
34+
35+
it "should not raise a ParseError when given an array of ip strings" do
36+
expect { scope.function_enclose_ipv6([['127.0.0.1','fe80::1']]) }.to_not raise_error
37+
end
38+
39+
it "should not raise a ParseError when given differently notations of ip addresses" do
40+
expect { scope.function_enclose_ipv6([['127.0.0.1','fe80::1','[fe80::1]']]) }.to_not raise_error
41+
end
42+
43+
it "should raise a ParseError when given a wrong ipv4 address" do
44+
expect { scope.function_enclose_ipv6(['127..0.0.1']) }.to( raise_error(Puppet::ParseError) )
45+
end
46+
47+
it "should raise a ParseError when given a ipv4 address with square brackets" do
48+
expect { scope.function_enclose_ipv6(['[127.0.0.1]']) }.to( raise_error(Puppet::ParseError) )
49+
end
50+
51+
it "should raise a ParseError when given a wrong ipv6 address" do
52+
expect { scope.function_enclose_ipv6(['fe80:::1']) }.to( raise_error(Puppet::ParseError) )
53+
end
54+
55+
it "should embrace ipv6 adresses within an array of ip addresses" do
56+
result = scope.function_enclose_ipv6([['127.0.0.1','fe80::1','[fe80::2]']])
57+
expect(result).to(eq(['127.0.0.1','[fe80::1]','[fe80::2]']))
58+
end
59+
60+
it "should embrace a single ipv6 adresse" do
61+
result = scope.function_enclose_ipv6(['fe80::1'])
62+
expect(result).to(eq(['[fe80::1]']))
63+
end
64+
65+
it "should not embrace a single ipv4 adresse" do
66+
result = scope.function_enclose_ipv6(['127.0.0.1'])
67+
expect(result).to(eq(['127.0.0.1']))
68+
end
69+
end

0 commit comments

Comments
 (0)