From 90607662c36aa4b5a055c3e68cd52c030ff08f98 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 8 Jan 2010 16:04:23 -0800 Subject: s/DSL::ResourceHelper/DSL::ResourceAPI/g Signed-off-by: Luke Kanies --- lib/puppet/dsl.rb | 2 +- lib/puppet/dsl/resource_api.rb | 62 ++++++++++++++++ lib/puppet/dsl/resource_helper.rb | 62 ---------------- lib/puppet/resource/type.rb | 2 +- spec/unit/dsl/resource_api.rb | 151 ++++++++++++++++++++++++++++++++++++++ spec/unit/dsl/resource_helper.rb | 151 -------------------------------------- spec/unit/resource/type.rb | 2 +- 7 files changed, 216 insertions(+), 216 deletions(-) create mode 100644 lib/puppet/dsl/resource_api.rb delete mode 100644 lib/puppet/dsl/resource_helper.rb create mode 100755 spec/unit/dsl/resource_api.rb delete mode 100755 spec/unit/dsl/resource_helper.rb diff --git a/lib/puppet/dsl.rb b/lib/puppet/dsl.rb index 93cf0fbcf..28f4a7e0c 100644 --- a/lib/puppet/dsl.rb +++ b/lib/puppet/dsl.rb @@ -4,7 +4,7 @@ module Puppet::DSL end require 'puppet/dsl/resource_type_api' -require 'puppet/dsl/resource_helper' +require 'puppet/dsl/resource_api' class Object include Puppet::DSL::ResourceTypeAPI diff --git a/lib/puppet/dsl/resource_api.rb b/lib/puppet/dsl/resource_api.rb new file mode 100644 index 000000000..d4f623b0c --- /dev/null +++ b/lib/puppet/dsl/resource_api.rb @@ -0,0 +1,62 @@ +# This module adds functionality to a resource to make it +# capable of evaluating the DSL resource type block and also +# hooking into the scope system. +require 'puppet/resource/type_collection_helper' + +module Puppet::DSL::ResourceAPI + include Puppet::Resource::TypeCollectionHelper + + FUNCTION_MAP = {:acquire => :include} + + # Try to convert a missing method into a resource type or a function. + def method_missing(name, *args) + return create_resource(name, args[0], args[1]) if valid_type?(name) + + name = map_function(name) + + return call_function(name, args) if Puppet::Parser::Functions.function(name) + + super + end + + def set_instance_variables + eachparam do |param| + instance_variable_set("@#{param.name}", param.value) + end + end + + def create_resource(type, names, arguments = nil) + names = [names] unless names.is_a?(Array) + + arguments ||= {} + raise ArgumentError, "Resource arguments must be provided as a hash" unless arguments.is_a?(Hash) + + names.collect do |name| + resource = Puppet::Parser::Resource.new(:type => type, :title => name, :scope => scope) + arguments.each do |param, value| + resource[param] = value + end + + scope.compiler.add_resource(scope, resource) + resource + end + end + + def call_function(name, args) + return false unless method = Puppet::Parser::Functions.function(name) + scope.send(method, *args) + end + + def valid_type?(name) + return true if [:class, :node].include?(name) + return true if Puppet::Type.type(name) + return true if known_resource_types.definition(name) + return false + end + + private + + def map_function(name) + return FUNCTION_MAP[name] || name + end +end diff --git a/lib/puppet/dsl/resource_helper.rb b/lib/puppet/dsl/resource_helper.rb deleted file mode 100644 index aaf8ab5f7..000000000 --- a/lib/puppet/dsl/resource_helper.rb +++ /dev/null @@ -1,62 +0,0 @@ -# This module adds functionality to a resource to make it -# capable of evaluating the DSL resource type block and also -# hooking into the scope system. -require 'puppet/resource/type_collection_helper' - -module Puppet::DSL::ResourceHelper - include Puppet::Resource::TypeCollectionHelper - - FUNCTION_MAP = {:acquire => :include} - - # Try to convert a missing method into a resource type or a function. - def method_missing(name, *args) - return create_resource(name, args[0], args[1]) if valid_type?(name) - - name = map_function(name) - - return call_function(name, args) if Puppet::Parser::Functions.function(name) - - super - end - - def set_instance_variables - eachparam do |param| - instance_variable_set("@#{param.name}", param.value) - end - end - - def create_resource(type, names, arguments = nil) - names = [names] unless names.is_a?(Array) - - arguments ||= {} - raise ArgumentError, "Resource arguments must be provided as a hash" unless arguments.is_a?(Hash) - - names.collect do |name| - resource = Puppet::Parser::Resource.new(:type => type, :title => name, :scope => scope) - arguments.each do |param, value| - resource[param] = value - end - - scope.compiler.add_resource(scope, resource) - resource - end - end - - def call_function(name, args) - return false unless method = Puppet::Parser::Functions.function(name) - scope.send(method, *args) - end - - def valid_type?(name) - return true if [:class, :node].include?(name) - return true if Puppet::Type.type(name) - return true if known_resource_types.definition(name) - return false - end - - private - - def map_function(name) - return FUNCTION_MAP[name] || name - end -end diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb index fa5fd5c88..2acb990ce 100644 --- a/lib/puppet/resource/type.rb +++ b/lib/puppet/resource/type.rb @@ -220,7 +220,7 @@ class Puppet::Resource::Type end def evaluate_ruby_code(resource, scope) - resource.extend(Puppet::DSL::ResourceHelper) + resource.extend(Puppet::DSL::ResourceAPI) resource.set_instance_variables diff --git a/spec/unit/dsl/resource_api.rb b/spec/unit/dsl/resource_api.rb new file mode 100755 index 000000000..11ea50052 --- /dev/null +++ b/spec/unit/dsl/resource_api.rb @@ -0,0 +1,151 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/dsl/resource_api' + +class DSLResourceAPITester + include Puppet::DSL::ResourceAPI +end + +describe Puppet::DSL::ResourceAPI do + before do + @resource = Puppet::Parser::Resource.new(:type => :mytype, :title => "myresource", :scope => mock("scope"), :source => mock("source")) + class << @resource + include Puppet::DSL::ResourceAPI + end + end + + it "should include the resource type collection helper" do + Puppet::DSL::ResourceAPI.ancestors.should be_include(Puppet::Resource::TypeCollectionHelper) + end + + it "should be able to set all of its parameters as instance variables" do + @resource["foo"] = "myval" + @resource.set_instance_variables + @resource.instance_variable_get("@foo").should == "myval" + end + + describe "when calling a function" do + it "should return false if the function does not exist" do + Puppet::Parser::Functions.expects(:function).with("myfunc").returns nil + @resource.call_function("myfunc", "foo").should be_false + end + + it "should use the scope the call the provided function with the provided arguments and return the results" do + scope = stub 'scope' + @resource.stubs(:scope).returns scope + Puppet::Parser::Functions.expects(:function).with("myfunc").returns "myfunc_method" + + scope.expects(:myfunc_method).with("one", "two") + @resource.call_function("myfunc", ["one", "two"]) + end + end + + describe "when determining if a provided name is a valid type" do + it "should be valid if it's :class" do + @resource.should be_valid_type(:class) + end + + it "should be valid if it's :node" do + @resource.should be_valid_type(:node) + end + + it "should be valid if it's a builtin type" do + Puppet::Type.expects(:type).with(:mytype).returns "whatever" + @resource.should be_valid_type(:mytype) + end + + it "should be valid if it's a defined resource type in the environment's known resource types" do + collection = stub 'collection' + @resource.stubs(:known_resource_types).returns collection + collection.expects(:definition).with(:mytype).returns "whatever" + @resource.should be_valid_type(:mytype) + end + + it "should not be valid unless it's a node, class, builtin type, or defined resource" do + collection = stub 'collection' + @resource.stubs(:known_resource_types).returns collection + collection.expects(:definition).returns nil + Puppet::Type.expects(:type).returns nil + @resource.should_not be_valid_type(:mytype) + end + end + + describe "when creating a resource" do + before do + @resource.scope.stubs(:source).returns stub("source") + @resource.scope.stubs(:compiler).returns stub("compiler", :add_resource => nil) + @created_resource = Puppet::Parser::Resource.new(:title => "eh", :type => 'yay', :scope => @resource.scope) + end + + it "should create and return a resource of the type specified" do + Puppet::Parser::Resource.expects(:new).with { |args| args[:type] == "mytype" }.returns @created_resource + @resource.create_resource("mytype", "myname", {:foo => "bar"}).should == [@created_resource] + end + + it "should use the name from the first element of the provided argument array" do + Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "myname" }.returns @created_resource + @resource.create_resource("mytype", "myname", {:foo => "bar"}) + end + + it "should create multiple resources if the first element of the argument array is an array" do + second_resource = Puppet::Parser::Resource.new(:title => "eh", :type => 'yay', :scope => @resource.scope) + Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "first" }.returns @created_resource + Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "second" }.returns @created_resource + @resource.create_resource("mytype", ["first", "second"], {:foo => "bar"}) + end + + it "should provide its scope as the scope" do + Puppet::Parser::Resource.expects(:new).with { |args| args[:scope] == @resource.scope }.returns @created_resource + @resource.create_resource("mytype", "myname", {:foo => "bar"}) + end + + it "should set each provided argument as a parameter on the created resource" do + result = @resource.create_resource("mytype", "myname", {"foo" => "bar", "biz" => "baz"}).shift + result["foo"].should == "bar" + result["biz"].should == "baz" + end + + it "should add the resource to the scope's copmiler" do + Puppet::Parser::Resource.expects(:new).returns @created_resource + @resource.scope.compiler.expects(:add_resource).with(@resource.scope, @created_resource) + @resource.create_resource("mytype", "myname", {:foo => "bar"}) + end + + it "should fail if the resource parameters are not a hash" do + lambda { @resource.create_resource("mytype", "myname", %w{foo bar}) }.should raise_error(ArgumentError) + end + end + + describe "when an unknown method is called" do + it "should create a resource if the method name is a valid type" do + @resource.expects(:valid_type?).with(:mytype).returns true + @resource.expects(:create_resource).with(:mytype, "myname", {:foo => "bar"}).returns true + + @resource.mytype("myname", :foo => "bar") + end + + it "should call any function whose name matches the undefined method if the name is not a valid type" do + @resource.expects(:valid_type?).with(:myfunc).returns false + @resource.expects(:create_resource).never + + Puppet::Parser::Functions.expects(:function).with(:myfunc).returns true + + @resource.expects(:call_function).with(:myfunc, %w{foo bar}) + + @resource.myfunc("foo", "bar") + end + + it "should raise a method missing error if the method is neither a type nor a function" do + @resource.expects(:valid_type?).with(:myfunc).returns false + @resource.expects(:create_resource).never + + Puppet::Parser::Functions.expects(:function).with(:myfunc).returns false + + @resource.expects(:call_function).never + + lambda { @resource.myfunc("foo", "bar") }.should raise_error(NoMethodError) + end + end +end diff --git a/spec/unit/dsl/resource_helper.rb b/spec/unit/dsl/resource_helper.rb deleted file mode 100755 index 11dad40cf..000000000 --- a/spec/unit/dsl/resource_helper.rb +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../spec_helper' - -require 'puppet/dsl/resource_helper' - -class DSLResourceHelperTester - include Puppet::DSL::ResourceHelper -end - -describe Puppet::DSL::ResourceHelper do - before do - @resource = Puppet::Parser::Resource.new(:type => :mytype, :title => "myresource", :scope => mock("scope"), :source => mock("source")) - class << @resource - include Puppet::DSL::ResourceHelper - end - end - - it "should include the resource type collection helper" do - Puppet::DSL::ResourceHelper.ancestors.should be_include(Puppet::Resource::TypeCollectionHelper) - end - - it "should be able to set all of its parameters as instance variables" do - @resource["foo"] = "myval" - @resource.set_instance_variables - @resource.instance_variable_get("@foo").should == "myval" - end - - describe "when calling a function" do - it "should return false if the function does not exist" do - Puppet::Parser::Functions.expects(:function).with("myfunc").returns nil - @resource.call_function("myfunc", "foo").should be_false - end - - it "should use the scope the call the provided function with the provided arguments and return the results" do - scope = stub 'scope' - @resource.stubs(:scope).returns scope - Puppet::Parser::Functions.expects(:function).with("myfunc").returns "myfunc_method" - - scope.expects(:myfunc_method).with("one", "two") - @resource.call_function("myfunc", ["one", "two"]) - end - end - - describe "when determining if a provided name is a valid type" do - it "should be valid if it's :class" do - @resource.should be_valid_type(:class) - end - - it "should be valid if it's :node" do - @resource.should be_valid_type(:node) - end - - it "should be valid if it's a builtin type" do - Puppet::Type.expects(:type).with(:mytype).returns "whatever" - @resource.should be_valid_type(:mytype) - end - - it "should be valid if it's a defined resource type in the environment's known resource types" do - collection = stub 'collection' - @resource.stubs(:known_resource_types).returns collection - collection.expects(:definition).with(:mytype).returns "whatever" - @resource.should be_valid_type(:mytype) - end - - it "should not be valid unless it's a node, class, builtin type, or defined resource" do - collection = stub 'collection' - @resource.stubs(:known_resource_types).returns collection - collection.expects(:definition).returns nil - Puppet::Type.expects(:type).returns nil - @resource.should_not be_valid_type(:mytype) - end - end - - describe "when creating a resource" do - before do - @resource.scope.stubs(:source).returns stub("source") - @resource.scope.stubs(:compiler).returns stub("compiler", :add_resource => nil) - @created_resource = Puppet::Parser::Resource.new(:title => "eh", :type => 'yay', :scope => @resource.scope) - end - - it "should create and return a resource of the type specified" do - Puppet::Parser::Resource.expects(:new).with { |args| args[:type] == "mytype" }.returns @created_resource - @resource.create_resource("mytype", "myname", {:foo => "bar"}).should == [@created_resource] - end - - it "should use the name from the first element of the provided argument array" do - Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "myname" }.returns @created_resource - @resource.create_resource("mytype", "myname", {:foo => "bar"}) - end - - it "should create multiple resources if the first element of the argument array is an array" do - second_resource = Puppet::Parser::Resource.new(:title => "eh", :type => 'yay', :scope => @resource.scope) - Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "first" }.returns @created_resource - Puppet::Parser::Resource.expects(:new).with { |args| args[:title] == "second" }.returns @created_resource - @resource.create_resource("mytype", ["first", "second"], {:foo => "bar"}) - end - - it "should provide its scope as the scope" do - Puppet::Parser::Resource.expects(:new).with { |args| args[:scope] == @resource.scope }.returns @created_resource - @resource.create_resource("mytype", "myname", {:foo => "bar"}) - end - - it "should set each provided argument as a parameter on the created resource" do - result = @resource.create_resource("mytype", "myname", {"foo" => "bar", "biz" => "baz"}).shift - result["foo"].should == "bar" - result["biz"].should == "baz" - end - - it "should add the resource to the scope's copmiler" do - Puppet::Parser::Resource.expects(:new).returns @created_resource - @resource.scope.compiler.expects(:add_resource).with(@resource.scope, @created_resource) - @resource.create_resource("mytype", "myname", {:foo => "bar"}) - end - - it "should fail if the resource parameters are not a hash" do - lambda { @resource.create_resource("mytype", "myname", %w{foo bar}) }.should raise_error(ArgumentError) - end - end - - describe "when an unknown method is called" do - it "should create a resource if the method name is a valid type" do - @resource.expects(:valid_type?).with(:mytype).returns true - @resource.expects(:create_resource).with(:mytype, "myname", {:foo => "bar"}).returns true - - @resource.mytype("myname", :foo => "bar") - end - - it "should call any function whose name matches the undefined method if the name is not a valid type" do - @resource.expects(:valid_type?).with(:myfunc).returns false - @resource.expects(:create_resource).never - - Puppet::Parser::Functions.expects(:function).with(:myfunc).returns true - - @resource.expects(:call_function).with(:myfunc, %w{foo bar}) - - @resource.myfunc("foo", "bar") - end - - it "should raise a method missing error if the method is neither a type nor a function" do - @resource.expects(:valid_type?).with(:myfunc).returns false - @resource.expects(:create_resource).never - - Puppet::Parser::Functions.expects(:function).with(:myfunc).returns false - - @resource.expects(:call_function).never - - lambda { @resource.myfunc("foo", "bar") }.should raise_error(NoMethodError) - end - end -end diff --git a/spec/unit/resource/type.rb b/spec/unit/resource/type.rb index bb372177d..d623ecad2 100755 --- a/spec/unit/resource/type.rb +++ b/spec/unit/resource/type.rb @@ -387,7 +387,7 @@ describe Puppet::Resource::Type do it "should include the DSL Resource Helper module in the provided resource" do @type.evaluate_code(@resource) - @resource.metaclass.ancestors.should be_include(Puppet::DSL::ResourceHelper) + @resource.metaclass.ancestors.should be_include(Puppet::DSL::ResourceAPI) end it "should convert the resource's parameters to instance variables" do -- cgit