diff options
| author | Luke Kanies <luke@madstop.com> | 2008-06-14 13:53:56 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2008-06-14 13:53:56 -0500 |
| commit | 6a61198f9293674a4bf0aa75bfbca10e20f64d20 (patch) | |
| tree | 0b1b6c4ffe6e69c3c9d3e9650620e3afbd486f18 /lib/puppet/util | |
| parent | eaa6eabc680cb6264594e30fd6a56e3e36765269 (diff) | |
| parent | 7b2c310e18b214424ae082e6ed2354a07b708c6f (diff) | |
Merge branch '0.24.x'
Also added the fixes to make the certhandler tests pass
even when certs exist; I'll deal with the conflict later.
Conflicts:
CHANGELOG
bin/puppetd
lib/puppet/network/http/handler.rb
lib/puppet/network/http/mongrel/rest.rb
spec/integration/indirector/rest.rb
spec/integration/network/server/mongrel.rb
spec/integration/network/server/webrick.rb
spec/unit/network/http/webrick.rb
Diffstat (limited to 'lib/puppet/util')
| -rw-r--r-- | lib/puppet/util/resource_template.rb | 61 |
1 files changed, 61 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..53066a192 --- /dev/null +++ b/lib/puppet/util/resource_template.rb @@ -0,0 +1,61 @@ +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). +# For example, say you have a resource that generates a file. You would +# need to implement the following style of `generate` method: +# +# def generate +# template = Puppet::Util::ResourceTemplate.new("/path/to/template", self) +# +# return Puppet::Type.type(:file).create :path => "/my/file", +# :content => template.evaluate +# end +# +# This generated file gets added to the catalog (which is what `generate` does), +# and its content is the result of the template. You need to use instance +# variables in your template, so if your template just needs to have the name +# of the generating resource, it would just have: +# +# <%= @name %> +# +# Since the ResourceTemplate class sets as instance variables all of the resource's +# parameters. +# +# Note that this example uses the generating resource as its source of +# parameters, which is generally most useful, since it allows you to configure +# the generated resource via the generating resource. +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 + |
