Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2046,13 +2046,21 @@ Type: Ruby 3.x API

Generates MAC addresses for all interfaces in the array which do not yet have an
address specified. The MAC addresses are based on the domain name, network and
portgroup. The function libvirt_generate_mac is used to generate a single mac address.
portgroup or the whole interface configuration (excluding the 'filter' key, to avoid
mac changes when the firewall rules change).#{' '}
The function libvirt_generate_mac is used to generate a single mac address.
Additional remark: If the same network config is attached to a node twice
the resulting mac address is the same !

#### `libvirt_generate_mac_addresses(Any $hash)`

Generates MAC addresses for all interfaces in the array which do not yet have an
address specified. The MAC addresses are based on the domain name, network and
portgroup. The function libvirt_generate_mac is used to generate a single mac address.
portgroup or the whole interface configuration (excluding the 'filter' key, to avoid
mac changes when the firewall rules change).#{' '}
The function libvirt_generate_mac is used to generate a single mac address.
Additional remark: If the same network config is attached to a node twice
the resulting mac address is the same !

Returns: `Any` the interfacess with mac address

Expand Down
11 changes: 9 additions & 2 deletions lib/puppet/parser/functions/libvirt_generate_mac_addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ module Puppet::Parser::Functions
newfunction(:libvirt_generate_mac_addresses, type: :rvalue, doc: <<-ENDHEREDOC) do |args|
Generates MAC addresses for all interfaces in the array which do not yet have an
address specified. The MAC addresses are based on the domain name, network and
portgroup. The function libvirt_generate_mac is used to generate a single mac address.
portgroup or the whole interface configuration (excluding the 'filter' key, to avoid
mac changes when the firewall rules change).#{' '}
The function libvirt_generate_mac is used to generate a single mac address.
Additional remark: If the same network config is attached to a node twice
the resulting mac address is the same !
@param
hash of interfaces
@return the interfacess with mac address
Expand All @@ -21,8 +25,11 @@ module Puppet::Parser::Functions

iface['mac'] = if iface['network']
function_libvirt_generate_mac([domain_name, iface['network'], iface['portgroup']])
elsif iface['source'] && iface['source']['network']
function_libvirt_generate_mac([domain_name, iface['source']['network'], iface['source']['portgroup']])
else
function_libvirt_generate_mac(['blah'])
function_libvirt_generate_mac([domain_name, iface.reject { |k, _v| k == 'filter' }.to_s])
# iface.reject{|k, v| k == 'filter'}
end
end
end
Expand Down
44 changes: 44 additions & 0 deletions spec/functions/libvirt_generate_mac_addresses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,54 @@
{ 'network' => 'test', 'portgroup' => 'he' },
{ 'network' => 'test', 'portgroup' => 'll', 'mac' => '42:42:42:42:42:42' },
{ 'network' => 'prod', 'portgroup' => 'oo' },
{ 'network' => 'nopo' },
]).and_return([
{ 'network' => 'test', 'portgroup' => 'he', 'mac' => '52:54:00:d8:54:bc' },
{ 'network' => 'test', 'portgroup' => 'll', 'mac' => '42:42:42:42:42:42' },
{ 'network' => 'prod', 'portgroup' => 'oo', 'mac' => '52:54:00:b8:cf:03' },
{ 'network' => 'nopo', 'mac' => '52:54:00:14:ec:f8' },
])
}
end

context 'with interface source and predefined mac' do
it {
is_expected.to run.with_params([
{ 'source' => { 'network' => 'test' }, 'mac' => '42:42::42:42:42:42' },
]).and_return([
{ 'source' => { 'network' => 'test' }, 'mac' => '42:42::42:42:42:42' },
])
}
end

context 'with multiple mixed source interfaces' do
it {
is_expected.to run.with_params([
{ 'source' => { 'network' => 'test', 'portgroup' => 'he' } },
{ 'source' => { 'network' => 'test', 'portgroup' => 'll' }, 'mac' => '42:42:42:42:42:42' },
{ 'source' => { 'network' => 'prod', 'portgroup' => 'oo' } },
{ 'source' => { 'network' => 'nopo' } },
]).and_return([
{ 'source' => { 'network' => 'test', 'portgroup' => 'he' }, 'mac' => '52:54:00:d8:54:bc' },
{ 'source' => { 'network' => 'test', 'portgroup' => 'll' }, 'mac' => '42:42:42:42:42:42' },
{ 'source' => { 'network' => 'prod', 'portgroup' => 'oo' }, 'mac' => '52:54:00:b8:cf:03' },
{ 'source' => { 'network' => 'nopo' }, 'mac' => '52:54:00:14:ec:f8' },
])
}
end

context 'with ignoring filter rules' do
it {
is_expected.to run.with_params([
{ 'interface_type' => 'bridge', 'source' => { 'bridge' => 'virbr0' } },
{ 'interface_type' => 'bridge', 'source' => { 'bridge' => 'virbr0' }, 'filter' => 'clean-traffic' },
{ 'interface_type' => 'user' },
{ 'interface_type' => 'user', 'filter' => 'clean-traffic' },
]).and_return([
{ 'interface_type' => 'bridge', 'source' => { 'bridge' => 'virbr0' }, 'mac' => '52:54:00:79:e5:40' },
{ 'interface_type' => 'bridge', 'source' => { 'bridge' => 'virbr0' }, 'filter' => 'clean-traffic', 'mac' => '52:54:00:79:e5:40' },
{ 'interface_type' => 'user', 'mac' => '52:54:00:12:36:36' },
{ 'interface_type' => 'user', 'filter' => 'clean-traffic', 'mac' => '52:54:00:12:36:36' },
])
}
end
Expand Down