Skip to content

Commit 7573dce

Browse files
committed
Add shared-network configuration
1 parent 997a3b4 commit 7573dce

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

manifests/init.pp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# @param option_static_route
44
# When enabled it sets the options rfc3442-classless-static-routes and ms-classless-static-routes
5+
# @param shared_network
6+
# When used it configure subnet in shared-network statement, it is used to inform the DHCP server that it share the same physical network
57
class dhcp (
68
Array[String] $dnsdomain = $dhcp::params::dnsdomain,
79
Array[String] $nameservers = [],
@@ -46,6 +48,7 @@
4648
Hash[String, Hash] $hosts = {},
4749
Variant[Array[String], Optional[String]] $includes = undef,
4850
String $config_comment = 'dhcpd.conf',
51+
Optional[Hash[String, Hash]] $shared_networks = undef,
4952
) inherits dhcp::params {
5053
# In case people set interface instead of interfaces work around
5154
# that. If they set both, use interfaces and the user is a unwise
@@ -157,6 +160,10 @@
157160
order => '01',
158161
}
159162

163+
if $shared_networks {
164+
create_resources('dhcp::sharednet', $shared_networks)
165+
}
166+
160167
create_resources('dhcp::subnet', $subnets)
161168
create_resources('dhcp::pool', $pools)
162169
create_resources('dhcp::host', $hosts)

manifests/sharednet.pp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# @summary
2+
# Define a DHCP shared-network
3+
# @param subnets
4+
# subnets to include in the shared-network statement
5+
# @param order
6+
# Fragment order in the dhcpd.conf
7+
define dhcp::sharednet (
8+
Hash[String,Hash] $subnets = {},
9+
Integer[1] $order = 75,
10+
) {
11+
concat::fragment { "dhcp.conf+${order}-start_${name}.dhcp":
12+
target => "${dhcp::dhcp_dir}/dhcpd.conf",
13+
content => "shared-network ${name} {\n",
14+
order => "${order}-0start",
15+
}
16+
concat::fragment { "dhcp.conf+${order}-end_${name}.dhcp":
17+
target => "${dhcp::dhcp_dir}/dhcpd.conf",
18+
content => "}\n",
19+
order => "${order}-9end",
20+
}
21+
create_resources('dhcp::subnet', $subnets, { order => $order })
22+
}

manifests/subnet.pp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
# Partial that is appended to the dhcpd.conf (before the final `}`)
4646
# @param raw_prepend
4747
# Partial that is prepended to the dhcpd.conf (after the first `{`)
48+
# @param order
49+
# Fragment order in the dhcpd.conf
4850
define dhcp::subnet (
4951
Stdlib::IP::Address::Nosubnet $network,
5052
Stdlib::IP::Address::Nosubnet $mask,
@@ -61,10 +63,11 @@
6163
Variant[Array[String], Optional[String]] $search_domains = undef,
6264
Optional[String] $raw_append = undef,
6365
Optional[String] $raw_prepend = undef,
66+
Integer[1] $order = 70,
6467
) {
65-
concat::fragment { "dhcp.conf+70_${name}.dhcp":
68+
concat::fragment { "dhcp.conf+${order}_${name}.dhcp":
6669
target => "${dhcp::dhcp_dir}/dhcpd.conf",
6770
content => template('dhcp/dhcpd.subnet.erb'),
68-
order => "70-${name}",
71+
order => "${order}-${name}",
6972
}
7073
}

spec/acceptance/shared_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'spec_helper_acceptance'
2+
3+
describe 'Simple installation' do
4+
interface = 'eth0'
5+
config_file = fact('os.family') == 'Archlinux' ? '/etc/dhcpd.conf' : '/etc/dhcp/dhcpd.conf'
6+
7+
it_behaves_like 'an idempotent resource' do
8+
let(:manifest) do
9+
<<-EOS
10+
$interface = $facts['networking']['interfaces']['#{interface}']
11+
12+
class { 'dhcp':
13+
interfaces => ['#{interface}'],
14+
shared_networks => {
15+
'shared' => {
16+
subnets => {
17+
$interface['network'] => {
18+
network => $interface['network'],
19+
mask => $interface['netmask'],
20+
pools => [],
21+
},
22+
'172.20.0.0' => {
23+
network => '172.20.0.0',
24+
mask => '255.255.255.0',
25+
pools => [],
26+
}
27+
}
28+
}
29+
},
30+
}
31+
EOS
32+
end
33+
end
34+
35+
it_behaves_like 'a DHCP server'
36+
37+
describe file(config_file) do
38+
it { is_expected.to be_file }
39+
its(:content) { should match(/shared-network shared/) }
40+
end
41+
end

spec/classes/init_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,20 @@
534534
it { is_expected.to compile.and_raise_error(%r{dnsupdateserver or nameservers parameter is required to enable ddns}) }
535535
end
536536
end
537+
538+
describe "with shared_network" do
539+
let(:params) { super().merge(shared_network: "shared") }
540+
541+
let(:expected_content) do
542+
<<~CONTENT
543+
shared-network shared {
544+
CONTENT
545+
end
546+
547+
it do
548+
is_expected.to contain_concat__fragment('dhcp.conf+75-start_shared.dhcp').with_content(expected_content)
549+
end
550+
end
537551
end
538552
end
539553
end

0 commit comments

Comments
 (0)