From 3a6683ea720a53bd2ddb34b9215bdc676bdcdb2c Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 18 Oct 2006 06:01:18 +0000 Subject: Changing the realize() function to be just syntactic sugar for a collection -- it literally creates a collector object now. The benefit of this is that it is late-binding, so file order does not affect whether a resource is available. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1810 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/parser/collector.rb | 31 ++++++++++++++++++++++++++++++- lib/puppet/parser/functions.rb | 18 +++++------------- 2 files changed, 35 insertions(+), 14 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/collector.rb b/lib/puppet/parser/collector.rb index 1fa314ae2..61807717d 100644 --- a/lib/puppet/parser/collector.rb +++ b/lib/puppet/parser/collector.rb @@ -1,7 +1,7 @@ # An object that collects stored objects from the central cache and returns # them to the current host, yo. class Puppet::Parser::Collector - attr_accessor :type, :scope, :vquery, :rquery, :form + attr_accessor :type, :scope, :vquery, :rquery, :form, :resources # Collect exported objects. def collect_exported @@ -48,6 +48,31 @@ class Puppet::Parser::Collector return resources end + def collect_resources + unless @resources.is_a?(Array) + @resources = [@resources] + end + method = "collect_#{form.to_s}_resources" + send(method) + end + + def collect_exported_resources + raise Puppet::ParseError, + "realize() is not yet implemented for exported resources" + end + + def collect_virtual_resources + @resources.collect do |ref| + if res = @scope.findresource(ref.to_s) + res + else + raise Puppet::ParseError, "Could not find resource %s" % ref + end + end.each do |res| + res.virtual = false + end + end + # Collect just virtual objects, from our local configuration. def collect_virtual(exported = false) if exported @@ -63,6 +88,10 @@ class Puppet::Parser::Collector # Call the collection method, mark all of the returned objects as non-virtual, # and then delete this object from the list of collections to evaluate. def evaluate + if self.resources + return collect_resources + end + method = "collect_#{@form.to_s}" objects = send(method).each do |obj| obj.virtual = false diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb index 6f15f7f2c..7fb19475e 100644 --- a/lib/puppet/parser/functions.rb +++ b/lib/puppet/parser/functions.rb @@ -155,22 +155,14 @@ module Functions end.join("") end + # This is just syntactic sugar for a collection, although it will generally + # be a good bit faster. newfunction(:realize, :statement) do |vals| + coll = Puppet::Parser::Collector.new(self, :nomatter, nil, nil, :virtual) vals = [vals] unless vals.is_a?(Array) - vals.each do |val| - unless val.is_a?(Puppet::Parser::Resource::Reference) - raise Puppet::ParseError, - "'realize' expects a resource reference; " + - "e.g., File['/etc/passwd'], not %s" % val - end + coll.resources = vals - if resource = findresource(val.to_s) - resource.virtual = false - else - raise Puppet::ParseError, "Could not find virtual resource %s" % - val.to_s - end - end + newcollection(coll) end end end -- cgit