diff --git a/README.md b/README.md index 2d1d6d07..9e7f6082 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ network_config { 'eth0': ensure => 'present', family => 'inet', method => 'dhcp', - onboot => 'true', - hotplug => 'true', + onboot => true, + hotplug => true, options => {'pre-up' => 'sleep 2'}, } @@ -29,7 +29,7 @@ network_config { 'lo': ensure => 'present', family => 'inet', method => 'loopback', - onboot => 'true', + onboot => true, } network_config { 'eth1': @@ -38,7 +38,7 @@ network_config { 'eth1': ipaddress => '169.254.0.1', method => 'static', netmask => '255.255.0.0', - onboot => 'true', + onboot => true, } ``` @@ -82,26 +82,28 @@ network_route { 'default': Create resources on the fly with the `puppet resource` command: - root@debian-6:~# puppet resource network_config eth1 ensure=present family=inet method=static ipaddress=169.254.0.1 netmask=255.255.0.0 - notice: /Network_config[eth1]/ensure: created - network_config { 'eth1': - ensure => 'present', - family => 'inet', - ipaddress => '169.254.0.1', - method => 'static', - netmask => '255.255.0.0', - onboot => 'true', - } - - # puppet resource network_route 23.23.42.0 ensure=present netmask=255.255.255.0 interface=eth0 gateway=192.168.1.1 - notice: /Network_route[23.23.42.0]/ensure: created - network_route { '23.23.42.0': - ensure => 'present', - gateway => '192.168.1.1', - interface => 'eth0', - netmask => '255.255.255.0', - options => 'table 200', - } +``` +root@debian-6:~# puppet resource network_config eth1 ensure=present family=inet method=static ipaddress=169.254.0.1 netmask=255.255.0.0 +notice: /Network_config[eth1]/ensure: created +network_config { 'eth1': + ensure => 'present', + family => 'inet', + ipaddress => '169.254.0.1', + method => 'static', + netmask => '255.255.0.0', + onboot => true, +} + +# puppet resource network_route 23.23.42.0 ensure=present netmask=255.255.255.0 interface=eth0 gateway=192.168.1.1 +notice: /Network_route[23.23.42.0]/ensure: created +network_route { '23.23.42.0': + ensure => 'present', + gateway => '192.168.1.1', + interface => 'eth0', + netmask => '255.255.255.0', + options => 'table 200', +} +``` ## Dependencies diff --git a/lib/puppet/provider/network_config/interfaces.rb b/lib/puppet/provider/network_config/interfaces.rb index 73b32865..c27e86ea 100644 --- a/lib/puppet/provider/network_config/interfaces.rb +++ b/lib/puppet/provider/network_config/interfaces.rb @@ -226,7 +226,7 @@ def self.parse_file(_filename, contents) case key # rubocop:disable Metrics/BlockNesting when 'address' then Instance[name].ipaddress = val when 'netmask' then Instance[name].netmask = val - when 'mtu' then Instance[name].mtu = val + when 'mtu' then Instance[name].mtu = val.to_i else Instance[name].options[key] << val end end diff --git a/lib/puppet/type/network_config.rb b/lib/puppet/type/network_config.rb index 1e3cadd4..6b19ce25 100644 --- a/lib/puppet/type/network_config.rb +++ b/lib/puppet/type/network_config.rb @@ -86,15 +86,8 @@ desc 'The Maximum Transmission Unit size to use for the interface' validate do |value| # reject floating point and negative integers - # XXX this lets 1500.0 pass - if value.is_a? Numeric - unless value.integer? - raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)" - end - else - unless value =~ %r{^\d+$} - raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)" - end + unless value.is_a?(Numeric) && value.integer? + raise ArgumentError, "#{value} is not a valid mtu (must be a positive integer)" end # Intel 82598 & 82599 chips support MTUs up to 16110; is there any @@ -106,7 +99,7 @@ # is 42 with a 802.1q header and 46 without. min_mtu = 42 max_mtu = 65_536 - unless (min_mtu..max_mtu).cover?(value.to_i) + unless min_mtu <= value && value <= max_mtu raise ArgumentError, "#{value} is not in the valid mtu range (#{min_mtu} .. #{max_mtu})" end end diff --git a/manifests/bond.pp b/manifests/bond.pp index 64e936b4..92bcd5a1 100644 --- a/manifests/bond.pp +++ b/manifests/bond.pp @@ -139,25 +139,25 @@ # define network::bond( $slaves, - $ensure = present, - $ipaddress = undef, - $netmask = undef, - $method = undef, - $family = undef, - $onboot = undef, - $hotplug = undef, - $lacp_rate = undef, - $mtu = undef, - $options = undef, - $slave_options = undef, + $ensure = present, + $ipaddress = undef, + $netmask = undef, + $method = undef, + $family = undef, +Optional[Boolean] $onboot = undef, +Optional[Boolean] $hotplug = undef, + $lacp_rate = undef, +Network::MTU $mtu = undef, + $options = undef, + $slave_options = undef, - $mode = 'active-backup', - $miimon = '100', - $downdelay = '200', - $updelay = '200', - $primary = $slaves[0], - $primary_reselect = 'always', - $xmit_hash_policy = 'layer2', + $mode = 'active-backup', +Network::PositiveInteger $miimon = 100, +Network::PositiveInteger $downdelay = 200, +Network::PositiveInteger $updelay = 200, + $primary = $slaves[0], + $primary_reselect = 'always', + $xmit_hash_policy = 'layer2', ) { require network::bond::setup diff --git a/spec/defines/bond/debian_spec.rb b/spec/defines/bond/debian_spec.rb index ab5dd8f5..4259a483 100644 --- a/spec/defines/bond/debian_spec.rb +++ b/spec/defines/bond/debian_spec.rb @@ -13,9 +13,9 @@ 'slaves' => %w[eth0 eth1], 'mode' => 'active-backup', - 'miimon' => '100', - 'downdelay' => '200', - 'updelay' => '200', + 'miimon' => 100, + 'downdelay' => 200, + 'updelay' => 200, 'lacp_rate' => 'slow', 'primary' => 'eth0', 'primary_reselect' => 'always', @@ -37,9 +37,9 @@ 'options' => { 'bond-slaves' => 'eth0 eth1', 'bond-mode' => 'active-backup', - 'bond-miimon' => '100', - 'bond-downdelay' => '200', - 'bond-updelay' => '200', + 'bond-miimon' => 100, + 'bond-downdelay' => 200, + 'bond-updelay' => 200, 'bond-lacp-rate' => 'slow', 'bond-primary' => 'eth0', 'bond-primary-reselect' => 'always', @@ -59,12 +59,12 @@ 'mtu' => 1550, 'options' => { 'bond-future-option' => 'yes' }, 'slave_options' => { 'slave-future-option' => 'no' }, - 'hotplug' => 'false', + 'hotplug' => false, 'mode' => 'balance-rr', - 'miimon' => '50', - 'downdelay' => '100', - 'updelay' => '100', + 'miimon' => 50, + 'downdelay' => 100, + 'updelay' => 100, 'lacp_rate' => 'fast', 'xmit_hash_policy' => 'layer3+4' } @@ -85,9 +85,9 @@ 'options' => { 'bond-slaves' => 'eth0 eth1 eth2', 'bond-mode' => 'balance-rr', - 'bond-miimon' => '50', - 'bond-downdelay' => '100', - 'bond-updelay' => '100', + 'bond-miimon' => 50, + 'bond-downdelay' => 100, + 'bond-updelay' => 100, 'bond-lacp-rate' => 'fast', 'bond-xmit-hash-policy' => 'layer3+4', 'bond-future-option' => 'yes', diff --git a/spec/defines/bond/redhat_spec.rb b/spec/defines/bond/redhat_spec.rb index e900f861..654b75b5 100644 --- a/spec/defines/bond/redhat_spec.rb +++ b/spec/defines/bond/redhat_spec.rb @@ -13,9 +13,9 @@ 'slaves' => %w[eth0 eth1], 'mode' => 'active-backup', - 'miimon' => '100', - 'downdelay' => '200', - 'updelay' => '200', + 'miimon' => 100, + 'downdelay' => 200, + 'updelay' => 200, 'lacp_rate' => 'slow', 'primary' => 'eth0', 'primary_reselect' => 'always', @@ -58,12 +58,12 @@ 'mtu' => '1550', 'options' => { 'NM_CONTROLLED' => 'yes' }, 'slave_options' => { 'NM_CONTROLLED' => 'no' }, - 'hotplug' => 'false', + 'hotplug' => false, 'mode' => 'balance-rr', - 'miimon' => '50', - 'downdelay' => '100', - 'updelay' => '100', + 'miimon' => 50, + 'downdelay' => 100, + 'updelay' => 100, 'lacp_rate' => 'fast', 'xmit_hash_policy' => 'layer3+4' } @@ -89,7 +89,7 @@ 'ipaddress' => '10.20.2.1', 'netmask' => '255.255.255.192', 'hotplug' => false, - 'mtu' => '1550', + 'mtu' => 1550, 'options' => { 'BONDING_OPTS' => 'mode=balance-rr miimon=50 downdelay=100 updelay=100 lacp_rate=fast xmit_hash_policy=layer3+4', 'NM_CONTROLLED' => 'yes' diff --git a/spec/defines/bond_spec.rb b/spec/defines/bond_spec.rb index 410bfba3..d30ba872 100644 --- a/spec/defines/bond_spec.rb +++ b/spec/defines/bond_spec.rb @@ -15,9 +15,9 @@ 'slave_options' => { 'NM_CONTROLLED' => 'no' }, 'mode' => 'active-backup', - 'miimon' => '100', - 'downdelay' => '200', - 'updelay' => '200', + 'miimon' => 100, + 'downdelay' => 200, + 'updelay' => 200, 'lacp_rate' => 'slow', 'primary' => 'eth0', 'primary_reselect' => 'always', diff --git a/spec/unit/provider/network_config/interfaces_spec.rb b/spec/unit/provider/network_config/interfaces_spec.rb index c298379a..43ac496b 100644 --- a/spec/unit/provider/network_config/interfaces_spec.rb +++ b/spec/unit/provider/network_config/interfaces_spec.rb @@ -81,7 +81,7 @@ def fixture_data(file) ipaddress: '192.168.0.2', netmask: '255.255.255.0', onboot: true, - mtu: '1500', + mtu: 1500, options: { 'broadcast' => '192.168.0.255', 'gateway' => '192.168.0.1' @@ -118,7 +118,7 @@ def fixture_data(file) ipaddress: '192.168.0.2', netmask: '255.255.255.0', onboot: true, - mtu: '1500', + mtu: 1500, options: { 'broadcast' => '192.168.0.255', 'gateway' => '192.168.0.1' @@ -129,7 +129,7 @@ def fixture_data(file) ipaddress: '172.16.0.2', netmask: '255.255.255.0', onboot: true, - mtu: '1500', + mtu: 1500, mode: :vlan, options: { 'broadcast' => '172.16.0.255', @@ -164,7 +164,7 @@ def fixture_data(file) method: 'static', ipaddress: '169.254.0.1', netmask: '255.255.0.0', - mtu: '1500', + mtu: 1500, mode: nil, options: nil) end @@ -179,7 +179,7 @@ def fixture_data(file) method: 'static', ipaddress: '169.254.0.1', netmask: '255.255.0.0', - mtu: '1500', + mtu: 1500, mode: :vlan, options: { 'vlan-raw-device' => 'eth1' @@ -226,7 +226,7 @@ def fixture_data(file) method: 'static', ipaddress: '169.254.0.1', netmask: '255.255.0.0', - mtu: '576', + mtu: 576, mode: nil, options: { 'pre-up' => '/bin/touch /tmp/eth1-up', @@ -246,7 +246,7 @@ def fixture_data(file) method: 'loopback', ipaddress: nil, netmask: nil, - mtu: '65536', + mtu: 65_536, mode: nil, options: nil) end diff --git a/spec/unit/provider/network_config/redhat_spec.rb b/spec/unit/provider/network_config/redhat_spec.rb index 51a48e8a..70907dac 100644 --- a/spec/unit/provider/network_config/redhat_spec.rb +++ b/spec/unit/provider/network_config/redhat_spec.rb @@ -419,7 +419,7 @@ def fixture_data(file) method: 'none', ipaddress: '169.254.0.1', netmask: '255.255.255.0', - mtu: '1500', + mtu: 1500, mode: :vlan, options: { 'NM_CONTROLLED' => 'no', 'USERCTL' => 'no' }) end @@ -460,7 +460,7 @@ def fixture_data(file) ipaddress: '172.20.1.9', netmask: '255.255.255.0', method: 'static', - mtu: '1500', + mtu: 1500, mode: nil, options: { 'BONDING_OPTS' => %(mode=4 miimon=100 xmit_hash_policy=layer3+4) diff --git a/spec/unit/type/network_config_spec.rb b/spec/unit/type/network_config_spec.rb index f050ca08..02e4e1f1 100644 --- a/spec/unit/type/network_config_spec.rb +++ b/spec/unit/type/network_config_spec.rb @@ -140,26 +140,14 @@ end describe 'mtu' do - it 'validates a tiny mtu size' do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '42') - end - it 'validates a tiny mtu size as a number' do Puppet::Type.type(:network_config).new(name: 'yay', mtu: 42) end - it 'validates a normal mtu size' do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500') - end - it 'validates a normal mtu size as a number' do Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500) end - it 'validates a large mtu size' do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '16384') - end - it 'validates a large mtu size as a number' do Puppet::Type.type(:network_config).new(name: 'yay', mtu: 16_384) end @@ -170,63 +158,39 @@ end.to raise_error(%r{must be a positive integer}) end - it 'fails on values < 42' do - expect do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '41') - end.to raise_error(%r{is not in the valid mtu range}) - end - it 'fails on numeric values < 42' do expect do Puppet::Type.type(:network_config).new(name: 'yay', mtu: 41) end.to raise_error(%r{is not in the valid mtu range}) end - it 'fails on zero' do - expect do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '0') - end.to raise_error(%r{is not in the valid mtu range}) - end - it 'fails on numeric zero' do expect do Puppet::Type.type(:network_config).new(name: 'yay', mtu: 0) end.to raise_error(%r{is not in the valid mtu range}) end - it 'fails on values > 65536' do - expect do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '65537') - end.to raise_error(%r{is not in the valid mtu range}) - end - it 'fails on numeric values > 65536' do expect do Puppet::Type.type(:network_config).new(name: 'yay', mtu: 65_537) end.to raise_error(%r{is not in the valid mtu range}) end - it 'fails on negative values' do - expect do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '-1500') - end.to raise_error(%r{is not a valid mtu}) - end - it 'fails on negative numbers' do expect do Puppet::Type.type(:network_config).new(name: 'yay', mtu: -1500) end.to raise_error(%r{is not in the valid mtu range}) end - it 'fails on non-integer values' do + it 'fails on numeric non-integer values' do expect do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500.1') + Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500.1) end.to raise_error(%r{must be a positive integer}) end - it 'fails on numeric non-integer values' do + it 'fails on integers passed as strings' do expect do - Puppet::Type.type(:network_config).new(name: 'yay', mtu: 1500.1) + Puppet::Type.type(:network_config).new(name: 'yay', mtu: '1500.1') end.to raise_error(%r{must be a positive integer}) end end diff --git a/templates/bond/opts-redhat.erb b/templates/bond/opts-redhat.erb index 37fd1f8a..d6460e6c 100644 --- a/templates/bond/opts-redhat.erb +++ b/templates/bond/opts-redhat.erb @@ -1,4 +1,6 @@ -<%= %w[mode miimon downdelay updelay lacp_rate primary primary_reselect xmit_hash_policy].reject { - |option| scope.lookupvar(option).nil? || scope.lookupvar(option) == :undef || scope.lookupvar(option) == :absent || scope.lookupvar(option).empty? }.map { - |key| "#{key}=#{scope.lookupvar(key)}" - }.join(' ') -%> +<%= %w[mode miimon downdelay updelay lacp_rate primary primary_reselect xmit_hash_policy].reject { |option| + v = scope.lookupvar(option) + v.nil? || v == :undef || v == :absent || ( (v === Enumerable || v.is_a?(String) ) && v.empty?) +}.map { |key| + "#{key}=#{scope.lookupvar(key)}" +}.join(' ') -%> diff --git a/types/mtu.pp b/types/mtu.pp new file mode 100644 index 00000000..73887cbe --- /dev/null +++ b/types/mtu.pp @@ -0,0 +1 @@ +type Network::MTU = Network::PositiveInteger diff --git a/types/positiveinteger.pp b/types/positiveinteger.pp new file mode 100644 index 00000000..bc5e27ca --- /dev/null +++ b/types/positiveinteger.pp @@ -0,0 +1 @@ +type Network::PositiveInteger = Integer[1,default]