Skip to content

Commit

Permalink
Extend support for event clauses to global, pool and pool6
Browse files Browse the repository at this point in the history
Extend support for event clauses to global, pool and pool6

Fix rubocopy errors
  • Loading branch information
mergwyn committed Oct 30, 2021
1 parent 0dcc718 commit 5c23ba2
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 5 deletions.
3 changes: 3 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
Hash[String, Hash] $ignoredsubnets = {},
Hash[String, Hash] $pools = {},
Hash[String, Hash] $pools6 = {},
Array[String[1]] $on_commit = [],
Array[String[1]] $on_release = [],
Array[String[1]] $on_expiry = [],
Optional[Stdlib::Absolutepath] $dhcpd_binary = $dhcp::params::dhcpd_binary
) inherits dhcp::params {
# check if extra_config is a string, if so convert it to an array
Expand Down
4 changes: 4 additions & 0 deletions manifests/pool.pp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
Optional[Integer] $mtu = undef,
String $domain_name = '',
$ignore_unknown = undef,
Array[String[1]] $on_commit = [],
Array[String[1]] $on_release = [],
Array[String[1]] $on_expiry = [],

) {
include dhcp::params

Expand Down
3 changes: 3 additions & 0 deletions manifests/pool6.pp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
Optional[Integer] $mtu = undef,
String $domain_name = '',
$ignore_unknown = undef,
Array[String[1]] $on_commit = [],
Array[String[1]] $on_release = [],
Array[String[1]] $on_expiry = [],
) {
include dhcp::params

Expand Down
58 changes: 58 additions & 0 deletions spec/classes/dhcp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,5 +673,63 @@
expect(content.split("\n").reject { |l| l =~ %r{^#|^$} }).to match_array(expected_lines)
end
end

context 'when event claused defined' do
let :params do
default_params.merge(
'interface' => 'eth0',
'on_commit' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP)'
],
'on_release' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Released IP: ", ClientIP))'
],
'on_expiry' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Expired IP: ", ClientIP))'
]
)
end

it 'event clauses are present' do
content = catalogue.resource('concat::fragment', 'dhcp-conf-header').send(:parameters)[:content]
expected_lines = [
'# BEGIN DHCP Header',
'# ----------',
'# dhcpd.conf',
'# ----------',
'authoritative;',
'default-lease-time 43200;',
'max-lease-time 86400;',
'log-facility daemon;',
'',
'# ----------',
'# Options',
'# ----------',
'option domain-name "sampledomain.com";',
'option domain-name-servers 1.1.1.1;',
'option dhcp6.name-servers 1:5ee:bad::c0de;',
'option fqdn.no-client-update on; # set the "O" and "S" flag bits',
'option fqdn.rcode2 255;',
'option pxegrub code 150 = text;',
'on commit {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP);',
'}',
'on release {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Released IP: ", ClientIP));',
'}',
'on expiry {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Expired IP: ", ClientIP));',
'}',
'# END DHCP Header',
]
expect(content.split("\n")).to match_array(expected_lines)
end
end
end
end
57 changes: 55 additions & 2 deletions spec/defines/pool6_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,66 @@
}
}
end
let :params do
let :default_params do
{
'network' => '2001:db8::',
'prefix' => 64,
'range' => '2001:db8::100 2001:db8::110'
}
end

it { is_expected.to contain_concat__fragment("dhcp_pool_#{title}") }
context 'creates a pool definition' do
let(:params) { default_params }

it { is_expected.to contain_concat__fragment("dhcp_pool_#{title}") }
end

context 'when optional parameters defined' do
let(:params) do
default_params.merge(
'on_commit' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP)'
],
'on_release' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Released IP: ", ClientIP))'
],
'on_expiry' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Expired IP: ", ClientIP))'
]
)
end

it 'creates a pool declaration with optional parameters' do
content = catalogue.resource('concat::fragment', "dhcp_pool_#{title}").send(:parameters)[:content]
expected_lines = [
'#################################',
"# #{title} #{params['network']}/#{params['prefix']}",
'#################################',
"subnet6 #{params['network']}/#{params['prefix']} {",
' pool6',
' {',
" range6 #{params['range']};",
'',
' }',
'',
' on commit {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP);',
' }',
' on release {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Released IP: ", ClientIP));',
' }',
' on expiry {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Expired IP: ", ClientIP));',
' }',
'}',
]
expect(content.split("\n")).to match_array(expected_lines)
end
end
end
60 changes: 57 additions & 3 deletions spec/defines/pool_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,68 @@
}
}
end
let :params do
let :default_params do
{
'gateway' => '1.1.1.1',
'mask' => '255.255.255.0',
'network' => '1.1.1.0',
'range' => ['1.1.1.100', '1.1.1.110']
'range' => '1.1.1.100 1.1.1.110'
}
end

it { is_expected.to contain_concat__fragment("dhcp_pool_#{title}") }
context 'creates a pool definition' do
let(:params) { default_params }

it { is_expected.to contain_concat__fragment("dhcp_pool_#{title}") }
end

context 'when optional parameters defined' do
let(:params) do
default_params.merge(
'on_commit' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP)'
],
'on_release' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Released IP: ", ClientIP))'
],
'on_expiry' => [
'set ClientIP = binary-to-ascii(10, 8, ".", leased-address)',
'log(concat("Expired IP: ", ClientIP))'
]
)
end

it 'creates a pool declaration with optional parameters' do
content = catalogue.resource('concat::fragment', "dhcp_pool_#{title}").send(:parameters)[:content]
expected_lines = [
'#################################',
"# #{title} #{params['network']} #{params['mask']}",
'#################################',
"subnet #{params['network']} netmask #{params['mask']} {",
' pool',
' {',
" range #{params['range']};",
' }',
'',
" option subnet-mask #{params['mask']};",
" option routers #{params['gateway']};",
' on commit {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' execute("/usr/local/bin/my_dhcp_helper.sh", ClientIP);',
' }',
' on release {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Released IP: ", ClientIP));',
' }',
' on expiry {',
' set ClientIP = binary-to-ascii(10, 8, ".", leased-address);',
' log(concat("Expired IP: ", ClientIP));',
' }',
'}',
]
expect(content.split("\n")).to match_array(expected_lines)
end
end
end
21 changes: 21 additions & 0 deletions templates/dhcpd.conf-header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,25 @@ option <%= @globaloptions %>;
<% if has_variable?( 'mtu' ) && @mtu -%>
option interface-mtu <%= @mtu %>;
<% end -%>
<% if not @on_commit.empty? -%>
on commit {
<% @on_commit.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_release.empty? -%>
on release {
<% @on_release.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_expiry.empty? -%>
on expiry {
<% @on_expiry.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
# END DHCP Header
21 changes: 21 additions & 0 deletions templates/dhcpd.pool.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,26 @@ subnet <%= @network %> netmask <%= @mask %> {
<% if @mtu -%>
option interface-mtu <%= @mtu %>;
<% end -%>
<% if not @on_commit.empty? -%>
on commit {
<% @on_commit.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_release.empty? -%>
on release {
<% @on_release.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_expiry.empty? -%>
on expiry {
<% @on_expiry.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
}

21 changes: 21 additions & 0 deletions templates/dhcpd.pool6.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,26 @@ subnet6 <%= @network %>/<%= @prefix %> {
<% if @mtu -%>
option interface-mtu <%= @mtu %>;
<% end -%>
<% if not @on_commit.empty? -%>
on commit {
<% @on_commit.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_release.empty? -%>
on release {
<% @on_release.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
<% if not @on_expiry.empty? -%>
on expiry {
<% @on_expiry.each do |statement| -%>
<%= statement %>;
<% end -%>
}
<% end -%>
}

0 comments on commit 5c23ba2

Please sign in to comment.