summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-12-16 17:56:53 -0600
committerLuke Kanies <luke@madstop.com>2008-12-18 11:10:24 -0600
commit1c7f8f685d3beec267f7e45e7d1217d7db770082 (patch)
tree2e5bb7f538aa90318cfb94a9b65e27796f2137dc
parente601babb9266258f55580fcf2a91ea5ca4c5d368 (diff)
downloadpuppet-1c7f8f685d3beec267f7e45e7d1217d7db770082.tar.gz
puppet-1c7f8f685d3beec267f7e45e7d1217d7db770082.tar.xz
puppet-1c7f8f685d3beec267f7e45e7d1217d7db770082.zip
Adding name/namevar abstraction to Puppet::Resource.
This hopefully provides a single place to manage this complexity, and I'll be using it to simplify Puppet::Type. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/resource.rb40
-rwxr-xr-xspec/unit/resource.rb50
2 files changed, 82 insertions, 8 deletions
diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb
index db06667e7..d82a7f996 100644
--- a/lib/puppet/resource.rb
+++ b/lib/puppet/resource.rb
@@ -7,7 +7,7 @@ require 'puppet/resource/reference'
class Puppet::Resource
include Puppet::Util::Tagging
include Enumerable
- attr_accessor :type, :title, :file, :line, :catalog
+ attr_accessor :type, :title, :file, :line, :catalog, :implicit
# Proxy these methods to the parameters hash. It's likely they'll
# be overridden at some point, but this works for now.
@@ -64,7 +64,14 @@ class Puppet::Resource
# Produce a simple hash of our parameters.
def to_hash
- @parameters.dup
+ result = @parameters.dup
+ unless result.include?(namevar)
+ result[namevar] = title
+ end
+ if result.has_key?(nil)
+ raise "wtf? %s" % namevar.inspect
+ end
+ result
end
def to_s
@@ -146,6 +153,30 @@ class Puppet::Resource
private
+ # Produce a canonical method name.
+ def parameter_name(param)
+ param = param.to_s.downcase.to_sym
+ if param == :name and n = namevar()
+ param = namevar
+ end
+ param
+ end
+
+ # The namevar for our resource type. If the type doesn't exist,
+ # always use :name.
+ def namevar
+ if t = resource_type
+ t.namevar
+ else
+ :name
+ end
+ end
+
+ # Retrieve the resource type.
+ def resource_type
+ Puppet::Type.type(type)
+ end
+
# Create an old-style TransBucket instance, for non-builtin resource types.
def to_transbucket
bucket = Puppet::TransBucket.new([])
@@ -156,9 +187,4 @@ class Puppet::Resource
# TransBuckets don't support parameters, which is why they're being deprecated.
return bucket
end
-
- # Produce a canonical method name.
- def parameter_name(param)
- param.to_s.downcase.to_sym
- end
end
diff --git a/spec/unit/resource.rb b/spec/unit/resource.rb
index 6a6ece79b..a743c03ca 100755
--- a/spec/unit/resource.rb
+++ b/spec/unit/resource.rb
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
require 'puppet/resource'
describe Puppet::Resource do
- [:catalog, :file, :line].each do |attr|
+ [:catalog, :file, :line, :implicit].each do |attr|
it "should have an #{attr} attribute" do
resource = Puppet::Resource.new("file", "/my/file")
resource.should respond_to(attr)
@@ -88,6 +88,29 @@ describe Puppet::Resource do
@resource[:foo].should == "bar"
end
+ it "should set the namevar when asked to set the name" do
+ Puppet::Type.type(:file).stubs(:namevar).returns :myvar
+ @resource[:name] = "/foo"
+ @resource[:myvar].should == "/foo"
+ end
+
+ it "should return the namevar when asked to return the name" do
+ Puppet::Type.type(:file).stubs(:namevar).returns :myvar
+ @resource[:myvar] = "/foo"
+ @resource[:name].should == "/foo"
+ end
+
+ it "should be able to set the name for non-builtin types" do
+ resource = Puppet::Resource.new(:foo, "bar")
+ lambda { resource[:name] = "eh" }.should_not raise_error
+ end
+
+ it "should be able to return the name for non-builtin types" do
+ resource = Puppet::Resource.new(:foo, "bar")
+ resource[:name] = "eh"
+ resource[:name].should == "eh"
+ end
+
it "should be able to iterate over parameters" do
@resource[:foo] = "bar"
@resource[:fee] = "bare"
@@ -132,6 +155,31 @@ describe Puppet::Resource do
@resource[:foo] = "bar"
@resource.should_not be_empty
end
+
+ it "should be able to produce a hash of all existing parameters" do
+ @resource[:foo] = "bar"
+ @resource[:fee] = "yay"
+
+ hash = @resource.to_hash
+ hash[:foo].should == "bar"
+ hash[:fee].should == "yay"
+ end
+
+ it "should not provide direct access to the internal parameters hash when producing a hash" do
+ hash = @resource.to_hash
+ hash[:foo] = "bar"
+ @resource[:foo].should be_nil
+ end
+
+ it "should use the title as the namevar to the hash if no namevar is present" do
+ Puppet::Type.type(:file).stubs(:namevar).returns :myvar
+ @resource.to_hash[:myvar].should == "/my/file"
+ end
+
+ it "should set :name to the title if :name is not present for non-builtin types" do
+ resource = Puppet::Resource.new :foo, "bar"
+ resource.to_hash[:name].should == "bar"
+ end
end
describe "when serializing" do