Skip to content

Commit

Permalink
(network-voxpupuli#54) Allow ip address netmasks for network_route
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienthebo committed Aug 7, 2013
1 parent dea5d0b commit 6f547bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/puppet/type/network_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

ensurable

IPV4_ADDRESS_REGEX = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/

newparam(:name) do
isnamevar
desc "The name of the network route"
Expand All @@ -27,13 +29,18 @@
desc "The subnet mask to apply to the route"

validate do |value|
unless (value.length <= 2 or value =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/) # yikes
unless (value.length <= 2 or value =~ IPV4_ADDRESS_REGEX)
fail("Invalid value for argument netmask: #{value}")
end
end

munge do |value|
r = IPAddr.new('255.255.255.255').mask(value.strip.to_i).to_s
case value
when IPV4_ADDRESS_REGEX
value
when /^\d+$/
IPAddr.new('255.255.255.255').mask(value.strip.to_i).to_s
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/unit/type/network_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
r = described_class.new(:name => '192.168.1.0/24', :network => '192.168.1.0', :netmask => '24', :gateway => '23.23.23.42', :interface => 'eth0')
r[:netmask].should == '255.255.255.0'
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'
end
end

describe "gateway" do
Expand Down

0 comments on commit 6f547bf

Please sign in to comment.