From 49820add1d1e5f63343615ead9b551b8679f466d Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Mon, 16 Oct 2017 15:06:37 -0500 Subject: [PATCH 1/1] ipv6 static route support --- .../lib/puppet/provider/network_route/redhat.rb | 3 ++- .../network/lib/puppet/type/network_route.rb | 26 ++++++++++++++-------- .../network/spec/unit/type/network_route_spec.rb | 5 +++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb b/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb index 5073519..c289f5f 100644 --- a/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb +++ b/packstack/puppet/modules/network/lib/puppet/provider/network_route/redhat.rb @@ -93,7 +93,8 @@ Puppet::Type.type(:network_route).provide(:redhat) do if provider.network == "default" contents << "#{provider.network} via #{provider.gateway} dev #{provider.interface}" else - contents << "#{provider.network}/#{provider.netmask} via #{provider.gateway} dev #{provider.interface}" + # provider.name will have cidr notation + contents << "#{provider.name} via #{provider.gateway} dev #{provider.interface}" end contents << (provider.options == :absent ? "\n" : " #{provider.options}\n") end diff --git a/packstack/puppet/modules/network/lib/puppet/type/network_route.rb b/packstack/puppet/modules/network/lib/puppet/type/network_route.rb index 7ab67dd..fd52c58 100644 --- a/packstack/puppet/modules/network/lib/puppet/type/network_route.rb +++ b/packstack/puppet/modules/network/lib/puppet/type/network_route.rb @@ -5,8 +5,6 @@ Puppet::Type.newtype(:network_route) do ensurable - IPV4_ADDRESS_REGEX = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ - newparam(:name) do isnamevar desc "The name of the network route" @@ -18,7 +16,7 @@ Puppet::Type.newtype(:network_route) do validate do |value| begin t = IPAddr.new(value) unless value == "default" - rescue ArgumentError + rescue fail("Invalid value for network: #{value}") end end @@ -29,17 +27,27 @@ Puppet::Type.newtype(:network_route) do desc "The subnet mask to apply to the route" validate do |value| - unless (value.length <= 2 or value =~ IPV4_ADDRESS_REGEX) + unless value.length <= 3 || (IPAddr.new(value) rescue false) fail("Invalid value for argument netmask: #{value}") end end munge do |value| - case value - when IPV4_ADDRESS_REGEX - value - when /^\d+$/ - IPAddr.new('255.255.255.255').mask(value.strip.to_i).to_s + # '255.255.255.255'.to_i will return 255, so we try to convert it back: + if value.to_i.to_s == value + if value.to_i <= 32 + IPAddr.new('255.255.255.255').mask(value.strip.to_i).to_s + else + IPAddr.new('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff').mask(value.strip.to_i).to_s + end + else + if (IPAddr.new(value).ipv6? rescue false) + IPAddr.new('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff').mask(value).to_s + elsif (IPAddr.new(value).ipv4? rescue false) + IPAddr.new('255.255.255.255').mask(value).to_s + else + raise("Invalid value for argument netmask: #{value}") + end end end end diff --git a/packstack/puppet/modules/network/spec/unit/type/network_route_spec.rb b/packstack/puppet/modules/network/spec/unit/type/network_route_spec.rb index 24e9da3..6e6f3e4 100644 --- a/packstack/puppet/modules/network/spec/unit/type/network_route_spec.rb +++ b/packstack/puppet/modules/network/spec/unit/type/network_route_spec.rb @@ -55,6 +55,11 @@ describe Puppet::Type.type(:network_route) do r[:netmask].should == '255.255.255.0' end + it 'should convert IPv6 netmasks of the CIDR form' do + r = Puppet::Type.type(:network_route).new(name: 'lxd bridge', network: 'fd58:281b:6eef:eb3d::', netmask: '64', gateway: 'fd58:281b:6eef:eb3d::1', interface: 'lxdbr0') + expect(r[:netmask]).to eq('ffff:ffff:ffff:ffff::') + end + it "should convert netmasks of the expanded netmask form" do r = described_class.new(:name => '192.168.1.0/24', :network => '192.168.1.0', :netmask => '255.255.128.0', :gateway => '23.23.23.42', :interface => 'eth0') r[:netmask].should == '255.255.128.0' -- 1.8.3.1