From 86cf226916f793277e2405711993d7ccbc4e7965 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 8 Jan 2010 17:22:49 -0800 Subject: Adding virtual and exported resource support to the DSL Also changed the internals - we're no longer using Resource instances with the ruby block, instead we're using a simple new class. We had to do this because Resource has too many methods - e.g., 'file' returned the file name rather than created a new resource type. Signed-off-by: Luke Kanies --- lib/puppet/dsl/resource_api.rb | 61 ++++++++++++++++++++++++++++++++++++++++-- lib/puppet/resource/type.rb | 6 +---- 2 files changed, 60 insertions(+), 7 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/dsl/resource_api.rb b/lib/puppet/dsl/resource_api.rb index d4f623b0c..bac26d38a 100644 --- a/lib/puppet/dsl/resource_api.rb +++ b/lib/puppet/dsl/resource_api.rb @@ -3,13 +3,32 @@ # hooking into the scope system. require 'puppet/resource/type_collection_helper' -module Puppet::DSL::ResourceAPI +class Puppet::DSL::ResourceAPI include Puppet::Resource::TypeCollectionHelper FUNCTION_MAP = {:acquire => :include} + attr_reader :scope, :resource, :block + + def environment + scope.environment + end + + def evaluate + set_instance_variables + instance_eval(&block) + end + + def initialize(resource, scope, block) + @scope = scope + @resource = resource + @block = block + end + # Try to convert a missing method into a resource type or a function. def method_missing(name, *args) + raise "MethodMissing loop when searching for #{name} with #{args.inspect}" if searching_for_method? + @searching_for_method = true return create_resource(name, args[0], args[1]) if valid_type?(name) name = map_function(name) @@ -17,10 +36,12 @@ module Puppet::DSL::ResourceAPI return call_function(name, args) if Puppet::Parser::Functions.function(name) super + ensure + @searching_for_method = true end def set_instance_variables - eachparam do |param| + resource.eachparam do |param| instance_variable_set("@#{param.name}", param.value) end end @@ -37,6 +58,8 @@ module Puppet::DSL::ResourceAPI resource[param] = value end + resource.exported = true if exporting? + resource.virtual = true if virtualizing? scope.compiler.add_resource(scope, resource) resource end @@ -47,6 +70,28 @@ module Puppet::DSL::ResourceAPI scope.send(method, *args) end + def export(resources = nil, &block) + if resources + resources.each { |resource| resource.exported = true } + return resources + end + @exporting = true + instance_eval(&block) + ensure + @exporting = false + end + + def virtual(resources = nil, &block) + if resources + resources.each { |resource| resource.virtual = true } + return resources + end + @virtualizing = true + instance_eval(&block) + ensure + @virtualizing = false + end + def valid_type?(name) return true if [:class, :node].include?(name) return true if Puppet::Type.type(name) @@ -56,7 +101,19 @@ module Puppet::DSL::ResourceAPI private + def exporting? + @exporting + end + def map_function(name) return FUNCTION_MAP[name] || name end + + def searching_for_method? + @searching_for_method + end + + def virtualizing? + @virtualizing + end end diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb index 2acb990ce..1192a1a9c 100644 --- a/lib/puppet/resource/type.rb +++ b/lib/puppet/resource/type.rb @@ -220,11 +220,7 @@ class Puppet::Resource::Type end def evaluate_ruby_code(resource, scope) - resource.extend(Puppet::DSL::ResourceAPI) - - resource.set_instance_variables - - resource.instance_eval(&ruby_code) + Puppet::DSL::ResourceAPI.new(resource, scope, ruby_code).evaluate end # Split an fq name into a namespace and name -- cgit