diff options
| author | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-07-28 19:37:11 +0200 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-08-01 11:15:29 +1000 |
| commit | b45ccf8d38a10d3f5226cbabe494240901e4e383 (patch) | |
| tree | 9e5e8bbbd88ce7f66dc5a42c6aa7c2c6c57e7b4e /lib/puppet/parser/ast | |
| parent | 58a73b5c68485dc5d41a46936c31e5fad5f037b5 (diff) | |
| download | puppet-b45ccf8d38a10d3f5226cbabe494240901e4e383.tar.gz puppet-b45ccf8d38a10d3f5226cbabe494240901e4e383.tar.xz puppet-b45ccf8d38a10d3f5226cbabe494240901e4e383.zip | |
Implement node matching with regexes
This patch enhance AST::HostName to support regexes, and modifies
the parser to allow regex to be used as node name.
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>]
Diffstat (limited to 'lib/puppet/parser/ast')
| -rw-r--r-- | lib/puppet/parser/ast/definition.rb | 6 | ||||
| -rw-r--r-- | lib/puppet/parser/ast/leaf.rb | 29 | ||||
| -rw-r--r-- | lib/puppet/parser/ast/node.rb | 9 |
3 files changed, 40 insertions, 4 deletions
diff --git a/lib/puppet/parser/ast/definition.rb b/lib/puppet/parser/ast/definition.rb index 092afef46..00b0416a0 100644 --- a/lib/puppet/parser/ast/definition.rb +++ b/lib/puppet/parser/ast/definition.rb @@ -24,9 +24,13 @@ class Puppet::Parser::AST::Definition < Puppet::Parser::AST::Branch false end + def get_classname(scope) + self.classname + end + # Create a resource that knows how to evaluate our actual code. def evaluate(scope) - resource = Puppet::Parser::Resource.new(:type => self.class.name, :title => self.classname, :scope => scope, :source => scope.source) + resource = Puppet::Parser::Resource.new(:type => self.class.name, :title => get_classname(scope), :scope => scope, :source => scope.source) scope.catalog.tag(*resource.tags) diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb index 59bfc9e9d..1c1eae972 100644 --- a/lib/puppet/parser/ast/leaf.rb +++ b/lib/puppet/parser/ast/leaf.rb @@ -19,6 +19,10 @@ class Puppet::Parser::AST obj == value end + def match(value) + @value == value + end + def to_s return @value.to_s unless @value.nil? end @@ -85,12 +89,12 @@ class Puppet::Parser::AST # undef values; equiv to nil class Undef < AST::Leaf; end - # Host names, either fully qualified or just the short name + # Host names, either fully qualified or just the short name, or even a regex class HostName < AST::Leaf def initialize(hash) super - @value = @value.to_s.downcase + @value = @value.to_s.downcase unless @value.is_a?(Regex) if @value =~ /[^-\w.]/ raise Puppet::DevError, "'%s' is not a valid hostname" % @value @@ -98,7 +102,9 @@ class Puppet::Parser::AST end def to_classname - return @value + classname = @value.to_s.downcase + classname.gsub!(/[^-a-zA-Z0-9:.]/,'') if regex? + classname end # implementing eql? and hash so that when an HostName is stored @@ -111,6 +117,19 @@ class Puppet::Parser::AST def hash return @value.hash end + + def match(value) + value = value.value if value.is_a?(HostName) + return @value.match(value) + end + + def regex? + @value.is_a?(Regex) + end + + def to_s + @value.to_s + end end # A simple variable. This object is only used during interpolation; @@ -153,6 +172,10 @@ class Puppet::Parser::AST matched end + def match(value) + @value.match(value) + end + def to_s return "/#{@value.source}/" end diff --git a/lib/puppet/parser/ast/node.rb b/lib/puppet/parser/ast/node.rb index b2d404487..4f75201eb 100644 --- a/lib/puppet/parser/ast/node.rb +++ b/lib/puppet/parser/ast/node.rb @@ -17,6 +17,15 @@ class Puppet::Parser::AST::Node < Puppet::Parser::AST::HostClass "" end + # in Regex mode, our classname can't be our Regex. + # so we use the currently connected client as our + # classname, mimicing exactly what would have happened + # if there was a specific node definition for this node. + def get_classname(scope) + return scope.host if name.regex? + classname + end + # Make sure node scopes are marked as such. def subscope(*args) scope = super |
