diff --git a/manifests/init.pp b/manifests/init.pp index 50c4b0692..446c3ea8c 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -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 diff --git a/manifests/pool.pp b/manifests/pool.pp index 48b0f666d..a739d2743 100644 --- a/manifests/pool.pp +++ b/manifests/pool.pp @@ -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 diff --git a/manifests/pool6.pp b/manifests/pool6.pp index b8889b1f3..60cf7d702 100644 --- a/manifests/pool6.pp +++ b/manifests/pool6.pp @@ -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 diff --git a/spec/classes/dhcp_spec.rb b/spec/classes/dhcp_spec.rb index 6c8127df8..1eb0099ce 100644 --- a/spec/classes/dhcp_spec.rb +++ b/spec/classes/dhcp_spec.rb @@ -673,5 +673,63 @@ expect(content.split("\n").reject { |l| l =~ %r{^#|^$} }).to match_array(expected_lines) end end + + context 'when event clauses 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 diff --git a/spec/defines/pool6_spec.rb b/spec/defines/pool6_spec.rb index 23f1d7091..aa1c8d6df 100644 --- a/spec/defines/pool6_spec.rb +++ b/spec/defines/pool6_spec.rb @@ -13,7 +13,7 @@ } } end - let :params do + let :default_params do { 'network' => '2001:db8::', 'prefix' => 64, @@ -21,5 +21,58 @@ } 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 diff --git a/spec/defines/pool_spec.rb b/spec/defines/pool_spec.rb index 373f104c8..5c4d1b775 100644 --- a/spec/defines/pool_spec.rb +++ b/spec/defines/pool_spec.rb @@ -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 diff --git a/templates/dhcpd.conf-header.erb b/templates/dhcpd.conf-header.erb index 77422a4ea..4b2cf0bff 100644 --- a/templates/dhcpd.conf-header.erb +++ b/templates/dhcpd.conf-header.erb @@ -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 diff --git a/templates/dhcpd.pool.erb b/templates/dhcpd.pool.erb index 0114019c4..a8d1c6847 100644 --- a/templates/dhcpd.pool.erb +++ b/templates/dhcpd.pool.erb @@ -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 -%> } diff --git a/templates/dhcpd.pool6.erb b/templates/dhcpd.pool6.erb index 105933697..ae24e57b8 100644 --- a/templates/dhcpd.pool6.erb +++ b/templates/dhcpd.pool6.erb @@ -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 -%> }