From: Sharif Nassar Date: Tue, 27 Nov 2012 19:39:59 +0000 (-0800) Subject: (#14463) Convert port Fixnum into strings X-Git-Tag: 0.1.0~12^2~1 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=53d3ef9da087b684331601fd112f57b13f931c6a;p=puppet-modules%2Fpuppetlabs-firewall.git (#14463) Convert port Fixnum into strings Avert errors like this: Parameter dport failed: Munging failed for value 1194 in class dport: can’t convert Fixnum into String Also, pass along the protocol so Socket can make well informed decisions. --- diff --git a/lib/puppet/type/firewall.rb b/lib/puppet/type/firewall.rb index 6d6f289..06b82fc 100644 --- a/lib/puppet/type/firewall.rb +++ b/lib/puppet/type/firewall.rb @@ -131,7 +131,7 @@ Puppet::Type.newtype(:firewall) do EOS munge do |value| - @resource.string_to_port(value) + @resource.string_to_port(value, :proto) end def is_to_s(value) @@ -161,7 +161,7 @@ Puppet::Type.newtype(:firewall) do EOS munge do |value| - @resource.string_to_port(value) + @resource.string_to_port(value, :proto) end def is_to_s(value) @@ -191,7 +191,7 @@ Puppet::Type.newtype(:firewall) do EOS munge do |value| - @resource.string_to_port(value) + @resource.string_to_port(value, :proto) end def is_to_s(value) diff --git a/lib/puppet/util/firewall.rb b/lib/puppet/util/firewall.rb index e93650c..7ee8cdd 100644 --- a/lib/puppet/util/firewall.rb +++ b/lib/puppet/util/firewall.rb @@ -71,15 +71,16 @@ module Puppet::Util::Firewall # If the string already contains a port number or perhaps a range of ports # in the format 22:1000 for example, it simply returns the string and does # nothing. - def string_to_port(value) + def string_to_port(value, proto) + proto = proto.to_s if value.kind_of?(String) if value.match(/^\d+(-\d+)?$/) return value else - return Socket.getservbyname(value).to_s + return Socket.getservbyname(value, proto).to_s end else - Socket.getservbyname(value) + Socket.getservbyname(value.to_s, proto) end end diff --git a/spec/unit/puppet/type/firewall_spec.rb b/spec/unit/puppet/type/firewall_spec.rb index ead5c9a..6fa5e8e 100755 --- a/spec/unit/puppet/type/firewall_spec.rb +++ b/spec/unit/puppet/type/firewall_spec.rb @@ -140,6 +140,11 @@ describe firewall do @resource[port].should == ['22','23'] end + it "should accept a #{port} as a number" do + @resource[port] = 22 + @resource[port].should == ['22'] + end + it "should accept a #{port} as a hyphen separated range" do @resource[port] = ['22-1000'] @resource[port].should == ['22-1000'] diff --git a/spec/unit/puppet/util/firewall_spec.rb b/spec/unit/puppet/util/firewall_spec.rb index c5ab12c..097782d 100644 --- a/spec/unit/puppet/util/firewall_spec.rb +++ b/spec/unit/puppet/util/firewall_spec.rb @@ -68,8 +68,10 @@ describe 'Puppet::Util::Firewall' do describe '#string_to_port' do subject { resource } - specify { subject.string_to_port('80').should == '80' } - specify { subject.string_to_port('http').should == '80' } + specify { subject.string_to_port('80','tcp').should == '80' } + specify { subject.string_to_port(80,'tcp').should == '80' } + specify { subject.string_to_port('http','tcp').should == '80' } + specify { subject.string_to_port('domain','udp').should == '53' } end describe '#to_hex32' do