Skip to content
Open
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
7 changes: 7 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# @param option_static_route
# When enabled it sets the options rfc3442-classless-static-routes and ms-classless-static-routes
# @param shared_network
# When used it configure subnet in shared-network statement, it is used to inform the DHCP server that it share the same physical network
class dhcp (
Array[String] $dnsdomain = $dhcp::params::dnsdomain,
Array[String] $nameservers = [],
Expand Down Expand Up @@ -46,6 +48,7 @@
Hash[String, Hash] $hosts = {},
Variant[Array[String], Optional[String]] $includes = undef,
String $config_comment = 'dhcpd.conf',
Optional[Hash[String, Hash]] $shared_networks = undef,
) inherits dhcp::params {
# In case people set interface instead of interfaces work around
# that. If they set both, use interfaces and the user is a unwise
Expand Down Expand Up @@ -157,6 +160,10 @@
order => '01',
}

if $shared_networks {
create_resources('dhcp::sharednet', $shared_networks)
}

create_resources('dhcp::subnet', $subnets)
create_resources('dhcp::pool', $pools)
create_resources('dhcp::host', $hosts)
Expand Down
22 changes: 22 additions & 0 deletions manifests/sharednet.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @summary
# Define a DHCP shared-network
# @param subnets
# subnets to include in the shared-network statement
# @param order
# Fragment order in the dhcpd.conf
define dhcp::sharednet (
Hash[String,Hash] $subnets = {},
Integer[1] $order = 75,
) {
concat::fragment { "dhcp.conf+${order}-start_${name}.dhcp":
target => "${dhcp::dhcp_dir}/dhcpd.conf",
content => "shared-network ${name} {\n",
order => "${order}-0start",
}
concat::fragment { "dhcp.conf+${order}-end_${name}.dhcp":
target => "${dhcp::dhcp_dir}/dhcpd.conf",
content => "}\n",
order => "${order}-9end",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably I need to change 9end in Zend to ensure it is at the end os the of the dhcp::subnet resource

}
create_resources('dhcp::subnet', $subnets, { order => $order })
}
7 changes: 5 additions & 2 deletions manifests/subnet.pp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
# Partial that is appended to the dhcpd.conf (before the final `}`)
# @param raw_prepend
# Partial that is prepended to the dhcpd.conf (after the first `{`)
# @param order
# Fragment order in the dhcpd.conf
define dhcp::subnet (
Stdlib::IP::Address::Nosubnet $network,
Stdlib::IP::Address::Nosubnet $mask,
Expand All @@ -61,10 +63,11 @@
Variant[Array[String], Optional[String]] $search_domains = undef,
Optional[String] $raw_append = undef,
Optional[String] $raw_prepend = undef,
Integer[1] $order = 70,
) {
concat::fragment { "dhcp.conf+70_${name}.dhcp":
concat::fragment { "dhcp.conf+${order}_${name}.dhcp":
target => "${dhcp::dhcp_dir}/dhcpd.conf",
content => template('dhcp/dhcpd.subnet.erb'),
order => "70-${name}",
order => "${order}-${name}",
}
}
41 changes: 41 additions & 0 deletions spec/acceptance/shared_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper_acceptance'

describe 'Simple installation' do
interface = 'eth0'
config_file = fact('os.family') == 'Archlinux' ? '/etc/dhcpd.conf' : '/etc/dhcp/dhcpd.conf'

it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-EOS
$interface = $facts['networking']['interfaces']['#{interface}']

class { 'dhcp':
interfaces => ['#{interface}'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable interpolation looks a bit complicated. can't we just do:

Suggested change
interfaces => ['#{interface}'],
interfaces => [$facts['networking']['interfaces']['eth0']],

Then you can drop line 10

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've copied the test from the repo, it use the variable interface during the test
I can replace it in all the manifest with the long facts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the current code is correct.

shared_networks => {
'shared' => {
subnets => {
$interface['network'] => {
network => $interface['network'],
mask => $interface['netmask'],
pools => [],
},
'172.20.0.0' => {
network => '172.20.0.0',
mask => '255.255.255.0',
pools => [],
}
}
}
},
}
EOS
end
end

it_behaves_like 'a DHCP server'

describe file(config_file) do
it { is_expected.to be_file }
its(:content) { should match(/shared-network shared/) }
end
end
14 changes: 14 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,20 @@
it { is_expected.to compile.and_raise_error(%r{dnsupdateserver or nameservers parameter is required to enable ddns}) }
end
end

describe "with shared_network" do
let(:params) { super().merge(shared_network: "shared") }

let(:expected_content) do
<<~CONTENT
shared-network shared {
CONTENT
end

it do
is_expected.to contain_concat__fragment('dhcp.conf+75-start_shared.dhcp').with_content(expected_content)
end
end
end
end
end