summaryrefslogtreecommitdiffstats
path: root/lib/puppet
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 /lib/puppet
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.
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/util/resource_template.rb38
1 files changed, 38 insertions, 0 deletions
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
+