From f4e7bdf477586e34e47a972172c15809ac92fe3c Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 10:38:14 +0200 Subject: Improve template host.conf.erb with validations and add spec for dhcp::hosts --- templates/host.conf.erb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'templates/host.conf.erb') diff --git a/templates/host.conf.erb b/templates/host.conf.erb index e6a7073..398d798 100644 --- a/templates/host.conf.erb +++ b/templates/host.conf.erb @@ -1,16 +1,26 @@ -<%- @hash_data.sort.each do |host, datas| -%> -<%- datas.fetch('interfaces').sort.each do |if_name, if_mac| -%> +<%- @hash_data.sort.each do |host, data| + scope.function_validate_hash([data]) -%> +<%- + raise Puppet::ParseError, "Missing interfaces hash for host '#{host}'" unless data.has_key? 'interfaces' + data.fetch('interfaces').sort.each do |if_name, if_mac| + scope.function_validate_re([if_name, '^\S+$']) + scope.function_validate_string([if_mac]) + scope.function_validate_re([if_mac, '^[a-f0-9:.]+$']) + -%> host <%= host %>-<%= if_name %> { hardware ethernet <%= if_mac %>; -<% if datas.fetch('fixed_address', false) -%> - fixed-address <%= datas.fetch('fixed_address') %>; -<% else -%> - fixed-address <%= host %>; -<% end -%> -<% if datas.fetch('option', false) -%> - <%= datas.fetch('option') %> -<% elsif @global_options -%> - <%= @global_options %> +<% + fixed_address = data.fetch('fixed_address', nil) || host + scope.function_validate_string([fixed_address]) + scope.function_validate_re([fixed_address, '^\S+$']) + -%> + fixed-address <%= fixed_address %>; +<% + options = data.fetch('options', nil) || @global_options + scope.function_validate_array([options]) + unless options.empty? + -%> + <%= options.join(";\n") %>; <% end -%> } <% end -%> -- cgit v1.2.3 From 558df46dc9bec98c6c90719d9aed11ec7a338411 Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 11:05:42 +0200 Subject: Simplify template --- templates/host.conf.erb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'templates/host.conf.erb') diff --git a/templates/host.conf.erb b/templates/host.conf.erb index 398d798..1c690b4 100644 --- a/templates/host.conf.erb +++ b/templates/host.conf.erb @@ -1,6 +1,5 @@ <%- @hash_data.sort.each do |host, data| - scope.function_validate_hash([data]) -%> -<%- + scope.function_validate_hash([data]) raise Puppet::ParseError, "Missing interfaces hash for host '#{host}'" unless data.has_key? 'interfaces' data.fetch('interfaces').sort.each do |if_name, if_mac| scope.function_validate_re([if_name, '^\S+$']) -- cgit v1.2.3 From 1ad8359dc57c99c1671d7cb41b961c451fb1c22f Mon Sep 17 00:00:00 2001 From: Raphaël Pinson Date: Fri, 12 Apr 2013 14:27:05 +0200 Subject: capital letters are valid in mac addresses --- spec/defines/dhcp_hosts_spec.rb | 4 ++-- templates/host.conf.erb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'templates/host.conf.erb') diff --git a/spec/defines/dhcp_hosts_spec.rb b/spec/defines/dhcp_hosts_spec.rb index f5f44ca..8170ed5 100644 --- a/spec/defines/dhcp_hosts_spec.rb +++ b/spec/defines/dhcp_hosts_spec.rb @@ -350,7 +350,7 @@ describe 'dhcp::hosts' do }, 'host2' => { 'interfaces' => { - 'eth1' => '00:11:af:33:44:55', + 'eth1' => '00:11:AF:33:44:55', }, 'fixed_address' => 'foo.example.com', 'options' => ['opt1'], @@ -363,7 +363,7 @@ describe 'dhcp::hosts' do :target => '/etc/dhcp/hosts.d/1.2.3.4.conf').with_content( /host host1-eth0 \{\n hardware ethernet 00:11:22:33:44:55;\n fixed-address host1;\n\}/).with_content( /host host1-wlan0 \{\n hardware ethernet 00:aa:bb:44:55:ff;\n fixed-address host1;\n\}/).with_content( - /host host2-eth1 \{\n hardware ethernet 00:11:af:33:44:55;\n fixed-address foo\.example\.com;\n opt1;\n\}/) + /host host2-eth1 \{\n hardware ethernet 00:11:AF:33:44:55;\n fixed-address foo\.example\.com;\n opt1;\n\}/) } end end diff --git a/templates/host.conf.erb b/templates/host.conf.erb index 1c690b4..c483d62 100644 --- a/templates/host.conf.erb +++ b/templates/host.conf.erb @@ -4,7 +4,7 @@ data.fetch('interfaces').sort.each do |if_name, if_mac| scope.function_validate_re([if_name, '^\S+$']) scope.function_validate_string([if_mac]) - scope.function_validate_re([if_mac, '^[a-f0-9:.]+$']) + scope.function_validate_re([if_mac, '^[A-Fa-f0-9:.]+$']) -%> host <%= host %>-<%= if_name %> { hardware ethernet <%= if_mac %>; -- cgit v1.2.3