normalize_ip_for_uri: allow to give an array of IP addresses
Allow to give an array of IP addresses to normalize_ip_for_url function. Each IP in the list will be normalized like it would be for a string. Change-Id: I8d361ce9cfcfe6a3f8592b2b7991971a3c748c75 Co-Authored-By: Athlan-Guyot sofer <sathlang@redhat.com>
This commit is contained in:

committed by
Sofer Athlan-Guyot

parent
7c889eccbe
commit
435c50fe88
@@ -3,7 +3,6 @@ require 'ipaddr'
|
|||||||
module Puppet::Parser::Functions
|
module Puppet::Parser::Functions
|
||||||
newfunction(:normalize_ip_for_uri,
|
newfunction(:normalize_ip_for_uri,
|
||||||
:type => :rvalue,
|
:type => :rvalue,
|
||||||
:arity => 1,
|
|
||||||
:doc => <<-EOD
|
:doc => <<-EOD
|
||||||
Add brackets if the argument is an IPv6 address.
|
Add brackets if the argument is an IPv6 address.
|
||||||
Returns the argument untouched otherwise.
|
Returns the argument untouched otherwise.
|
||||||
@@ -13,19 +12,26 @@ module Puppet::Parser::Functions
|
|||||||
and port 8080. This code will change it to
|
and port 8080. This code will change it to
|
||||||
[2001::1:8080] as it's a valid ip address. This
|
[2001::1:8080] as it's a valid ip address. This
|
||||||
shouldn't be an issue in most cases.
|
shouldn't be an issue in most cases.
|
||||||
|
If an array is given, each member will be normalized to
|
||||||
|
a valid IPv6 address with brackets when needed.
|
||||||
EOD
|
EOD
|
||||||
) do |args|
|
) do |args|
|
||||||
ip = args[0]
|
result = []
|
||||||
begin
|
args = args[0] if args[0].kind_of?(Array)
|
||||||
if IPAddr.new(ip).ipv6?
|
args.each do |ip|
|
||||||
unless ip.match(/\[.+\]/)
|
begin
|
||||||
Puppet.debug("IP #{ip} is changed to [#{ip}]")
|
if IPAddr.new(ip).ipv6?
|
||||||
ip = "[#{ip}]"
|
unless ip.match(/\[.+\]/)
|
||||||
|
Puppet.debug("IP #{ip} is changed to [#{ip}]")
|
||||||
|
ip = "[#{ip}]"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
rescue ArgumentError
|
||||||
|
# ignore it
|
||||||
end
|
end
|
||||||
rescue ArgumentError => e
|
result << ip
|
||||||
# ignore it
|
|
||||||
end
|
end
|
||||||
return ip
|
return result[0] if args.size == 1
|
||||||
|
result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- Allow to give an array of IP addresses to normalize_ip_for_uri and normalize
|
||||||
|
each IP in the list.
|
@@ -6,8 +6,10 @@ describe 'normalize_ip_for_uri' do
|
|||||||
it { should run.with_params('127.0.0.1').and_return('127.0.0.1')}
|
it { should run.with_params('127.0.0.1').and_return('127.0.0.1')}
|
||||||
it { should run.with_params('::1').and_return('[::1]')}
|
it { should run.with_params('::1').and_return('[::1]')}
|
||||||
it { should run.with_params('[2001::01]').and_return('[2001::01]')}
|
it { should run.with_params('[2001::01]').and_return('[2001::01]')}
|
||||||
it do
|
# You're not forced to pass an array, a list of argument will do.
|
||||||
is_expected.to run.with_params('one', 'two')
|
it { should run.with_params('::1','::2').and_return(['[::1]','[::2]'])}
|
||||||
.and_raise_error(ArgumentError, /Wrong number of arguments/)
|
it { should run.with_params(['::1','::2']).and_return(['[::1]','[::2]'])}
|
||||||
end
|
it { should run.with_params(['::1','[::2]','::3']).and_return(['[::1]','[::2]','[::3]'])}
|
||||||
|
it { should run.with_params(['192.168.0.1','[::2]']).and_return(['192.168.0.1','[::2]'])}
|
||||||
|
it { should run.with_params(['192.168.0.1','::2']).and_return(['192.168.0.1','[::2]'])}
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user