summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-05-26 00:24:57 -0500
committerLuke Kanies <luke@madstop.com>2008-06-09 16:40:54 -0500
commit29c840ae8f2d4e81df1154b1099ce3ca92e348e3 (patch)
treeeaf1c36e2bb0af54f166e5cb7681318e074b92e9
parentdbd9b40c6537c261f01976238ef9ccfd6a6d6d08 (diff)
downloadpuppet-29c840ae8f2d4e81df1154b1099ce3ca92e348e3.tar.gz
puppet-29c840ae8f2d4e81df1154b1099ce3ca92e348e3.tar.xz
puppet-29c840ae8f2d4e81df1154b1099ce3ca92e348e3.zip
Adding a class for using templates directly within resources
(i.e., client-side templates). This would really only be used for composite resources that pass the results of the template on to generated resources.
-rw-r--r--CHANGELOG5
-rw-r--r--lib/puppet/util/resource_template.rb38
-rwxr-xr-xspec/unit/util/resource_template.rb58
3 files changed, 101 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 2bc14fc8e..d51e3f0b6 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,8 @@
+ Adding a ResourceTemplate class for using templates directly
+ within resources (i.e., client-side templates). This would really
+ only be used for composite resources that pass the results of the
+ template on to generated resources.
+
Exporting or collecting resources no longer raises an exception
when no storeconfigs is enabled, it just produces a warning.
diff --git a/lib/puppet/util/resource_template.rb b/lib/puppet/util/resource_template.rb
new file mode 100644
index 000000000..f85078005
--- /dev/null
+++ b/lib/puppet/util/resource_template.rb
@@ -0,0 +1,38 @@
+require 'puppet/util'
+require 'puppet/util/logging'
+require 'erb'
+
+# A template wrapper that evaluates a template in the
+# context of a resource, allowing the resource attributes
+# to be looked up from within the template.
+# This provides functionality essentially equivalent to
+# the language's template() function. You pass your file
+# path and the resource you want to use into the initialization
+# method, then call result() on the instance, and you get back
+# a chunk of text.
+# The resource's parameters are available as instance variables
+# (as opposed to the language, where we use a method_missing trick).
+class Puppet::Util::ResourceTemplate
+ include Puppet::Util::Logging
+
+ def evaluate
+ set_resource_variables
+ ERB.new(File.read(@file), 0, "-").result(binding)
+ end
+
+ def initialize(file, resource)
+ raise ArgumentError, "Template %s does not exist" % file unless FileTest.exist?(file)
+ @file = file
+ @resource = resource
+ end
+
+ private
+
+ def set_resource_variables
+ @resource.to_hash.each do |param, value|
+ var = "@#{param.to_s}"
+ instance_variable_set(var, value)
+ end
+ end
+end
+
diff --git a/spec/unit/util/resource_template.rb b/spec/unit/util/resource_template.rb
new file mode 100755
index 000000000..b4d529e5d
--- /dev/null
+++ b/spec/unit/util/resource_template.rb
@@ -0,0 +1,58 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/util/resource_template'
+
+describe Puppet::Util::ResourceTemplate do
+ describe "when initializing" do
+ it "should fail if the template does not exist" do
+ FileTest.expects(:exist?).with("/my/template").returns false
+ lambda { Puppet::Util::ResourceTemplate.new("/my/template", mock('resource')) }.should raise_error(ArgumentError)
+ end
+
+ it "should not create the ERB template" do
+ ERB.expects(:new).never
+ FileTest.expects(:exist?).with("/my/template").returns true
+ Puppet::Util::ResourceTemplate.new("/my/template", mock('resource'))
+ end
+ end
+
+ describe "when evaluating" do
+ before do
+ FileTest.stubs(:exist?).returns true
+ File.stubs(:read).returns "eh"
+
+ @template = stub 'template', :result => nil
+ ERB.stubs(:new).returns @template
+
+ @resource = mock 'resource'
+ @wrapper = Puppet::Util::ResourceTemplate.new("/my/template", @resource)
+ end
+
+ it "should set all of the resource's parameters as instance variables" do
+ @resource.expects(:to_hash).returns(:one => "uno", :two => "dos")
+ @template.expects(:result).with do |bind|
+ eval("@one", bind) == "uno" and eval("@two", bind) == "dos"
+ end
+ @wrapper.evaluate
+ end
+
+ it "should create a template instance with the contents of the file" do
+ File.expects(:read).with("/my/template").returns "yay"
+ ERB.expects(:new).with("yay", 0, "-").returns(@template)
+
+ @wrapper.stubs :set_resource_variables
+
+ @wrapper.evaluate
+ end
+
+ it "should return the result of the template" do
+ @wrapper.stubs :set_resource_variables
+
+ @wrapper.expects(:binding).returns "mybinding"
+ @template.expects(:result).with("mybinding").returns "myresult"
+ @wrapper.evaluate.should == "myresult"
+ end
+ end
+end