summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Bode <bodepd@gmail.com>2011-01-08 17:18:25 -0600
committerMatt Robinson <matt@puppetlabs.com>2011-01-24 16:44:41 -0800
commit18ca97b0d16fae149a1eab04c520421b5eb38969 (patch)
tree043de05a8f0b30838c7beafdaa9e5678c312c341
parent41090d3617d99f9eaa58df32be93f3d16467bc50 (diff)
downloadpuppet-18ca97b0d16fae149a1eab04c520421b5eb38969.tar.gz
puppet-18ca97b0d16fae149a1eab04c520421b5eb38969.tar.xz
puppet-18ca97b0d16fae149a1eab04c520421b5eb38969.zip
(#5045) Adds support to resource/type to also accept a param hash
The params are added as attributes when the resource is created. Also, even if the class already exists, if params are passed, we still try to create it. Reviewed-by: Matt Robinson
-rw-r--r--lib/puppet/resource/type.rb14
-rwxr-xr-xspec/unit/resource/type_spec.rb25
2 files changed, 36 insertions, 3 deletions
diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb
index d40adc145..a31795a1b 100644
--- a/lib/puppet/resource/type.rb
+++ b/lib/puppet/resource/type.rb
@@ -143,18 +143,26 @@ class Puppet::Resource::Type
# classes and nodes. No parameters are be supplied--if this is a
# parameterized class, then all parameters take on their default
# values.
- def ensure_in_catalog(scope)
+ def ensure_in_catalog(scope, attributes=nil)
type == :definition and raise ArgumentError, "Cannot create resources for defined resource types"
resource_type = type == :hostclass ? :class : :node
# Do nothing if the resource already exists; this makes sure we don't
# get multiple copies of the class resource, which helps provide the
# singleton nature of classes.
- if resource = scope.catalog.resource(resource_type, name)
+ # we should not do this for classes with attributes
+ # if attributes are passed, we should still try to create the resource
+ # even if it exists so that we can fail
+ # this prevents us from being able to combine param classes with include
+ if resource = scope.catalog.resource(resource_type, name) and !attributes
return resource
end
-
resource = Puppet::Parser::Resource.new(resource_type, name, :scope => scope, :source => self)
+ if attributes
+ attributes.each do |k,v|
+ resource.set_parameter(k,v)
+ end
+ end
instantiate_resource(scope, resource)
scope.compiler.add_resource(scope, resource)
resource
diff --git a/spec/unit/resource/type_spec.rb b/spec/unit/resource/type_spec.rb
index 7b240bb82..11288f100 100755
--- a/spec/unit/resource/type_spec.rb
+++ b/spec/unit/resource/type_spec.rb
@@ -601,12 +601,37 @@ describe Puppet::Resource::Type do
@compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
end
+ it "should add specified attributes to the resource" do
+ @top.ensure_in_catalog(@scope, {'one'=>'1', 'two'=>'2'})
+ @compiler.catalog.resource(:class, "top")['one'].should == '1'
+ @compiler.catalog.resource(:class, "top")['two'].should == '2'
+ end
+
+ it "should not require params for a param class" do
+ @top.ensure_in_catalog(@scope, {})
+ @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
+ end
+
it "should evaluate the parent class if one exists" do
@middle.ensure_in_catalog(@scope)
@compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
end
+ it "should evaluate the parent class if one exists" do
+ @middle.ensure_in_catalog(@scope, {})
+
+ @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource)
+ end
+
+ it "should fail if you try to create duplicate class resources" do
+ othertop = Puppet::Parser::Resource.new(:class, 'top',:source => @source, :scope => @scope )
+ # add the same class resource to the catalog
+ @compiler.catalog.add_resource(othertop)
+ @compiler.catalog.expects(:resource).with(:class, 'top').returns true
+ lambda { @top.ensure_in_catalog(@scope, {}) }.should raise_error(Puppet::Resource::Catalog::DuplicateResourceError)
+ end
+
it "should fail to evaluate if a parent class is defined but cannot be found" do
othertop = Puppet::Resource::Type.new :hostclass, "something", :parent => "yay"
@code.add othertop