The default value for a keyattribute like protocol in port.rb does only
work when the titlepattern does not set it (even if the titlepattern
will set the parameter to nil).

Now the first titlepattern searches for tcp or udp (e.g.
telnet:udp) and if this titlepattern does not match the resourcetitle
only :name will be set in the parameter hash. If protocol is not
explicitly set puppet will finally take the default value into account
(which is :tcp)

Signed-off-by: Stefan Schulte <[email protected]>
---
Local-branch: feature/next/5660
 lib/puppet/type/port.rb     |   15 ++++++++++++---
 spec/unit/type/port_spec.rb |    9 ++++++++-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/lib/puppet/type/port.rb b/lib/puppet/type/port.rb
index e2a4afa..efaa3d8 100755
--- a/lib/puppet/type/port.rb
+++ b/lib/puppet/type/port.rb
@@ -6,13 +6,22 @@ module Puppet
 
     def self.title_patterns
       [
-        # we just have one titlepattern "name:protocol"
+        # we have two titlepatterns "name" and "name:protocol". We won't use
+        # one pattern (that will eventually set :protocol to nil) because we
+        # want to use a default value for :protocol. And that does only work
+        # if :protocol is not put in the parameter hash while initialising
         [
-          /^(.*?)(?::(tcp|udp))?$/, # Regex to parse title
+          /^(.*?):(tcp|udp)$/, # Set name and protocol
           [
             # We don't need a lot of postparsing
             [ :name, lambda{|x| x} ],
-            [ :protocol, lambda{ |x| x.intern unless x.nil? } ],
+            [ :protocol, lambda{ |x| x.intern unless x.nil? } ]
+          ]
+        ],
+        [
+          /^(.*)$/,
+          [
+            [ :name, lambda{|x| x} ]
           ]
         ]
       ]
diff --git a/spec/unit/type/port_spec.rb b/spec/unit/type/port_spec.rb
index f23ea2b..8f22231 100644
--- a/spec/unit/type/port_spec.rb
+++ b/spec/unit/type/port_spec.rb
@@ -25,7 +25,14 @@ describe port do
     regex = @class.title_patterns[0][0]
     regex.match("telnet:tcp").captures.should == ['telnet','tcp' ]
     regex.match("telnet:udp").captures.should == ['telnet','udp' ]
-    regex.match("telnet:baz").captures.should == ['telnet:baz',nil ]
+    regex.match("telnet:baz").should == nil
+  end
+
+  it "should have a second title pattern that will set only name" do
+    regex = @class.title_patterns[1][0]
+    regex.match("telnet:tcp").captures.should == ['telnet:tcp' ]
+    regex.match("telnet:udp").captures.should == ['telnet:udp' ]
+    regex.match("telnet:baz").captures.should == ['telnet:baz' ]
   end
 
   it "should have two key_attributes" do
-- 
1.7.3.4

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to