]> review.fuel-infra Code Review - puppet-modules/puppetlabs-firewall.git/commitdiff
Added host_to_mask method and added tests for it
authorJan Vansteenkiste <jan@vstone.eu>
Wed, 22 Aug 2012 15:11:23 +0000 (17:11 +0200)
committerPatrick Hemmer <patrick.hemmer@gmail.com>
Fri, 20 Dec 2013 20:09:50 +0000 (15:09 -0500)
lib/puppet/util/firewall.rb
spec/unit/puppet/util/firewall_spec.rb

index 13c1a89b113c2731440deee48952e6d876d44880..610b6034d74e968fb91ef2072ffdc97ee7dfd6f0 100644 (file)
@@ -116,6 +116,20 @@ module Puppet::Util::Firewall
     value.cidr
   end
 
+  # Takes an address mask and converts the host portion to CIDR notation.
+  #
+  # This takes into account you can negate a mask but follows all rules
+  # defined in host_to_ip for the host/address part.
+  #
+  def host_to_mask(value)
+    match = value.match /(!)\s?(.*)$/
+    return host_to_ip(value) unless match
+
+    cidr = host_to_ip(match[2])
+    return nil if cidr == nil
+    "#{match[1]} #{cidr}"
+  end
+
   # Validates the argument is int or hex, and returns valid hex
   # conversion of the value or nil otherwise.
   def to_hex32(value)
index be1576fa83d43b0a960fcc4386e60978717fd97b..2d1ddb0fd1b7a6c74c67a2ee9c86e7459aa2eb2a 100644 (file)
@@ -25,6 +25,24 @@ describe 'Puppet::Util::Firewall' do
     specify { subject.host_to_ip('::/0').should == nil }
   end
 
+  describe '#host_to_mask' do
+    subject { resource }
+    specify { subject.host_to_mask('puppetlabs.com').should == '96.126.112.51/32' }
+    specify { subject.host_to_mask('!puppetlabs.com').should == '! 96.126.112.51/32' }
+    specify { subject.host_to_mask('96.126.112.51').should == '96.126.112.51/32' }
+    specify { subject.host_to_mask('!96.126.112.51').should == '! 96.126.112.51/32' }
+    specify { subject.host_to_mask('96.126.112.51/32').should == '96.126.112.51/32' }
+    specify { subject.host_to_mask('! 96.126.112.51/32').should == '! 96.126.112.51/32' }
+    specify { subject.host_to_mask('2001:db8:85a3:0:0:8a2e:370:7334').should == '2001:db8:85a3::8a2e:370:7334/128' }
+    specify { subject.host_to_mask('!2001:db8:85a3:0:0:8a2e:370:7334').should == '! 2001:db8:85a3::8a2e:370:7334/128' }
+    specify { subject.host_to_mask('2001:db8:1234::/48').should == '2001:db8:1234::/48' }
+    specify { subject.host_to_mask('! 2001:db8:1234::/48').should == '! 2001:db8:1234::/48' }
+    specify { subject.host_to_mask('0.0.0.0/0').should == nil }
+    specify { subject.host_to_mask('!0.0.0.0/0').should == nil }
+    specify { subject.host_to_mask('::/0').should == nil }
+    specify { subject.host_to_mask('! ::/0').should == nil }
+  end
+
   describe '#icmp_name_to_number' do
     describe 'proto unsupported' do
       subject { resource }