From 7fc0303b029721a5387e151e343b48bd09ca116c Mon Sep 17 00:00:00 2001 From: Edwin Wiles Date: Sun, 22 Oct 2017 19:23:10 -0400 Subject: [PATCH 1/2] Fix both #134 and #135 --- manifests/route.pp | 37 ++++++++++++++++++++++++++----------- templates/route-eth-ip.erb | 6 ++++++ 2 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 templates/route-eth-ip.erb diff --git a/manifests/route.pp b/manifests/route.pp index 27c7534a..06579448 100644 --- a/manifests/route.pp +++ b/manifests/route.pp @@ -32,6 +32,13 @@ # gateway => [ '192.168.1.1', '10.0.0.1', ], # } # +# network::route { 'ens192': +# ipaddress => [ +# '192.168.2.0/24 via 192.168.1.1', +# '10.0.0.0/8 via 10.0.0.1' +# ] +# } +# # === Authors: # # Mike Arnold @@ -41,29 +48,37 @@ # Copyright (C) 2011 Mike Arnold, unless otherwise noted. # define network::route ( - $ipaddress, - $netmask, - $gateway, - $restart = true, + Array[String] $ipaddress, + Optional[Array[String]] $netmask = undef, + Optional[Array[String]] $gateway = undef, + Boolean $restart = true, ) { - # Validate our arrays - validate_array($ipaddress) - validate_array($netmask) - validate_array($gateway) - # Validate our booleans - validate_bool($restart) include '::network' $interface = $name + if $ipaddress != undef and $netmask != undef and $gateway != undef { + if length($ipaddress) == length($netmask) and length($netmask) == length($gateway) { + $template = 'network/route-eth.erb'; + else { + fail { 'All arrays must be the same length': } + } + } + elsif $netmask == undef and $gateway == undef { + $template = 'network/route-eth-ip.erb'; + } + else { + fail { 'Either use just ipaddress, or use all three array parameters': } + } + file { "route-${interface}": ensure => 'present', mode => '0644', owner => 'root', group => 'root', path => "/etc/sysconfig/network-scripts/route-${interface}", - content => template('network/route-eth.erb'), + content => template($template), before => File["ifcfg-${interface}"], } diff --git a/templates/route-eth-ip.erb b/templates/route-eth-ip.erb new file mode 100644 index 00000000..71a0484f --- /dev/null +++ b/templates/route-eth-ip.erb @@ -0,0 +1,6 @@ +### +### File managed by Puppet +### +<% @ipaddress.each do |addr| -%> +<%= addr %> +<% end -%> From 6f157e278be3a99d07f8d7d5c398399fe97a661c Mon Sep 17 00:00:00 2001 From: Edwin Wiles Date: Sun, 22 Oct 2017 19:33:59 -0400 Subject: [PATCH 2/2] And add at least one spec test. --- spec/defines/network_route_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/defines/network_route_spec.rb b/spec/defines/network_route_spec.rb index dd4d0541..99094b69 100644 --- a/spec/defines/network_route_spec.rb +++ b/spec/defines/network_route_spec.rb @@ -4,6 +4,34 @@ describe 'network::route', :type => 'define' do + context 'array parameters, ip command format' do + let(:title) { 'ens192' } + let :params do + { + :ipaddress => [ + '192.168.2.0/24 via 192.168.1.2', + '10.0.0.0/8 via 10.1.1.1', + ], + } + end + let(:facts) {{ :osfamily => 'RedHat' }} + it { should contain_file('route-ens192').with( + :ensure => 'present', + :mode => '0644', + :owner => 'root', + :group => 'root', + :path => '/etc/sysconfig/network-scripts/route-ens192' + )} + it 'should contain File[route-ens192 with contents "192.168.2.0/24 via 192.168.1.2\n10.0.0.0/8 via 10.1.1.1"' do + verify_contents(catalogue, 'route-ens192', [ + '192.168.2.0/24 via 192.168.1.2', + '10.0.0.0/8 via 10.1.1.1', + ]) + end + it { should contain_service('network') } + it { is_expected.to contain_file('route-ens192').that_notifies('Service[network]') } + end + context 'singular parameters' do let(:title) { 'eth1' } let :params do {