summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-18 06:01:18 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-18 06:01:18 +0000
commit3a6683ea720a53bd2ddb34b9215bdc676bdcdb2c (patch)
tree3865ab57a73d5b07554cda594eb9c5fff15b027a
parent05080ff50b73597f0a3f86242b63985a1e5e6003 (diff)
downloadpuppet-3a6683ea720a53bd2ddb34b9215bdc676bdcdb2c.tar.gz
puppet-3a6683ea720a53bd2ddb34b9215bdc676bdcdb2c.tar.xz
puppet-3a6683ea720a53bd2ddb34b9215bdc676bdcdb2c.zip
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
-rw-r--r--lib/puppet/parser/collector.rb31
-rw-r--r--lib/puppet/parser/functions.rb18
-rwxr-xr-xtest/language/collector.rb36
-rwxr-xr-xtest/language/functions.rb22
4 files changed, 91 insertions, 16 deletions
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
diff --git a/test/language/collector.rb b/test/language/collector.rb
index 0204778de..ceab76ccc 100755
--- a/test/language/collector.rb
+++ b/test/language/collector.rb
@@ -22,6 +22,42 @@ class TestCollector < Test::Unit::TestCase
@interp, @scope, @source = mkclassframing
end
+ # Test just collecting a specific resource. This is used by the 'realize'
+ # function, and it's much faster than iterating over all of the resources.
+ def test_collect_resource
+ # Make a couple of virtual resources
+ one = mkresource(:type => "file", :title => "/tmp/virtual1",
+ :virtual => true, :params => {:owner => "root"})
+ two = mkresource(:type => "file", :title => "/tmp/virtual2",
+ :virtual => true, :params => {:owner => "root"})
+ @scope.setresource one
+ @scope.setresource two
+
+ # Now make a collector
+ coll = nil
+ assert_nothing_raised do
+ coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :virtual)
+ end
+
+ # Now set the resource in the collector
+ assert_nothing_raised do
+ coll.resources = one.ref
+ end
+
+ # Now run the collector
+ assert_nothing_raised do
+ coll.evaluate
+ end
+
+ # And make sure the resource is no longer virtual
+ assert(! one.virtual?,
+ "Resource is still virtual")
+
+ # But the other still is
+ assert(two.virtual?,
+ "Resource got realized")
+ end
+
def test_virtual
# Make a virtual resource
virtual = mkresource(:type => "file", :title => "/tmp/virtual",
diff --git a/test/language/functions.rb b/test/language/functions.rb
index 21af606db..bd7143db6 100755
--- a/test/language/functions.rb
+++ b/test/language/functions.rb
@@ -321,19 +321,37 @@ class TestLangFunctions < Test::Unit::TestCase
@scope.function_realize(ref)
end
+ # Make sure it created a collection
+ assert_equal(1, @scope.collections.length,
+ "Did not set collection")
+
+ assert_nothing_raised do
+ @scope.collections.each do |coll| coll.evaluate end
+ end
+ @scope.collections.clear
+
# Now make sure the virtual resource is no longer virtual
assert(! virtual.virtual?, "Did not make virtual resource real")
# Make sure we puke on any resource that doesn't exist
-
none = Puppet::Parser::Resource::Reference.new(
:type => "file", :title => "/tmp/nosuchfile",
:scope => @scope
)
- assert_raise(Puppet::ParseError) do
+ # The function works
+ assert_nothing_raised do
@scope.function_realize(none)
end
+
+ # Make sure it created a collection
+ assert_equal(1, @scope.collections.length,
+ "Did not set collection")
+
+ # But the collection fails
+ assert_raise(Puppet::ParseError) do
+ @scope.collections.each do |coll| coll.evaluate end
+ end
end
end