diff options
author | Paul Berry <paul@puppetlabs.com> | 2010-09-03 11:17:35 -0700 |
---|---|---|
committer | Markus Roberts <Markus@reality.com> | 2010-09-22 21:11:22 -0700 |
commit | f95006148c3a0b4d7e8ee1812b1993b674f050e4 (patch) | |
tree | 5059295466481f3a1380d29642808bdfae006397 /lib/puppet | |
parent | 8ff4b9a0b16310729a0411ad151ad0d0636069d5 (diff) | |
download | puppet-f95006148c3a0b4d7e8ee1812b1993b674f050e4.tar.gz puppet-f95006148c3a0b4d7e8ee1812b1993b674f050e4.tar.xz puppet-f95006148c3a0b4d7e8ee1812b1993b674f050e4.zip |
[#4716] ResourceTypeAPI exposes implementation details that are likely to change
Made the following modifications to ResourceTypeAPI:
(1) returned nil from “define”, “hostclass”, and “node”.
(2) renamed “mk_resource_type” and “munge_type_arguments” to
“__mk_resource_type__” and “__munge_type_arguments__” to discourage
customers from calling them.
(3) Made ResourceTypeAPI a class rather than a module, and changed the
parser to evaluate the contents of pure ruby manifests using a
instances of this class.
(4) Changed ResourceTypeAPI to insert newly instantiated types into
Thread.current[:known_resource_types] rather than the default
environment's known_resource_types.
This effectively backports the fix for issue #4657 to 2.6.x.
Also backported the new spec tests from #4657.
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/dsl.rb | 4 | ||||
-rw-r--r-- | lib/puppet/dsl/resource_type_api.rb | 26 | ||||
-rw-r--r-- | lib/puppet/parser/parser_support.rb | 4 |
3 files changed, 19 insertions, 15 deletions
diff --git a/lib/puppet/dsl.rb b/lib/puppet/dsl.rb index abdb78f67..97a310436 100644 --- a/lib/puppet/dsl.rb +++ b/lib/puppet/dsl.rb @@ -5,7 +5,3 @@ end require 'puppet/dsl/resource_type_api' require 'puppet/dsl/resource_api' - -class Object - include Puppet::DSL::ResourceTypeAPI -end diff --git a/lib/puppet/dsl/resource_type_api.rb b/lib/puppet/dsl/resource_type_api.rb index 487aab99d..ecb914189 100644 --- a/lib/puppet/dsl/resource_type_api.rb +++ b/lib/puppet/dsl/resource_type_api.rb @@ -1,33 +1,39 @@ require 'puppet/resource/type' -module Puppet::DSL::ResourceTypeAPI +class Puppet::DSL::ResourceTypeAPI def define(name, *args, &block) - result = mk_resource_type(:definition, name, Hash.new, block) - result.set_arguments(munge_type_arguments(args)) - result + result = __mk_resource_type__(:definition, name, Hash.new, block) + result.set_arguments(__munge_type_arguments__(args)) + nil end def hostclass(name, options = {}, &block) - mk_resource_type(:hostclass, name, options, block) + __mk_resource_type__(:hostclass, name, options, block) + nil end def node(name, options = {}, &block) - mk_resource_type(:node, name, options, block) + __mk_resource_type__(:node, name, options, block) + nil end - private + # Note: we don't want the user to call the following methods + # directly. However, we can't stop them by making the methods + # private because the user's .rb code gets instance_eval'ed on an + # instance of this class. So instead we name the methods using + # double underscores to discourage customers from calling them. - def mk_resource_type(type, name, options, code) + def __mk_resource_type__(type, name, options, code) klass = Puppet::Resource::Type.new(type, name, options) klass.ruby_code = code if code - Puppet::Node::Environment.new.known_resource_types.add klass + Thread.current[:known_resource_types].add klass klass end - def munge_type_arguments(args) + def __munge_type_arguments__(args) args.inject([]) do |result, item| if item.is_a?(Hash) item.each { |p, v| result << [p, v] } diff --git a/lib/puppet/parser/parser_support.rb b/lib/puppet/parser/parser_support.rb index 4f3a4ddff..c90c1978f 100644 --- a/lib/puppet/parser/parser_support.rb +++ b/lib/puppet/parser/parser_support.rb @@ -215,7 +215,9 @@ class Puppet::Parser::Parser end def parse_ruby_file - require self.file + # Execute the contents of the file inside its own "main" object so + # that it can call methods in the resource type API. + Puppet::DSL::ResourceTypeAPI.new.instance_eval(File.read(self.file)) end def string=(string) |