Skip to content

Commit 46748b8

Browse files
committed
build a simple structured networking fact for older facter versions, refs saz#293
1 parent 29a9ae7 commit 46748b8

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/puppet/functions/ssh/ipaddresses.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,24 @@ def ipaddresses(excluded_interfaces = [])
1717
# always exclude loopback interface
1818
excluded_interfaces += ['lo']
1919

20+
if !facts['networking'].nil? && !facts['networking'].empty?
21+
interfaces = facts['networking']['interfaces']
22+
else
23+
interfaces = {}
24+
facts['interfaces'].split(',').each do |iface|
25+
next if facts["ipaddress_#{iface}"].nil? && facts["ipaddress6_#{iface}"].nil?
26+
interfaces[iface] = {}
27+
if !facts["ipaddress_#{iface}"].nil? && !facts["ipaddress_#{iface}"].empty?
28+
interfaces[iface]['bindings'] = [{ 'address' => facts["ipaddress_#{iface}"] }]
29+
end
30+
if !facts["ipaddress6_#{iface}"].nil? && !facts["ipaddress6_#{iface}"].empty?
31+
interfaces[iface]['bindings6'] = [{ 'address' => facts["ipaddress6_#{iface}"] }]
32+
end
33+
end
34+
end
35+
2036
result = []
21-
facts['networking']['interfaces'].each do |iface, data|
37+
interfaces.each do |iface, data|
2238
# skip excluded interfaces
2339
next if excluded_interfaces.include?(iface)
2440

spec/functions/ssh/ipaddresses_spec.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
is_expected.not_to be_nil
66
end
77

8-
context 'with dummy fact data' do
8+
context 'with dummy structured fact data' do
99
let(:facts) do
1010
JSON.parse File.read(File.join(File.dirname(__FILE__), '../../fixtures/mock-interface-fact.json'))
1111
end
@@ -27,4 +27,33 @@
2727
end
2828
end
2929
end
30+
31+
context 'with dummy legacy fact data' do
32+
let(:facts) do
33+
{
34+
networking: {},
35+
interfaces: 'lo,docker0,eno1',
36+
ipaddress_lo: '127.0.0.1',
37+
ipaddress_eno1: '10.13.42.61',
38+
ipaddress_docker0: '172.17.0.1'
39+
}
40+
end
41+
42+
describe 'without parameters' do
43+
it 'returns all IPs other than localhost' do
44+
is_expected.to run.and_return(['172.17.0.1', '10.13.42.61'])
45+
end
46+
end
47+
48+
describe 'with excluded interface' do
49+
it 'doesn\'t return the IPs of excluded interface' do
50+
is_expected.to run.with_params(['docker0']).and_return(['10.13.42.61'])
51+
end
52+
end
53+
describe 'with excluded interfaces' do
54+
it 'doesn\'t return the IPs of those interfaces' do
55+
is_expected.to run.with_params(%w[docker0 eno1]).and_return([])
56+
end
57+
end
58+
end
3059
end

0 commit comments

Comments
 (0)