summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-11-28 12:57:16 -0600
committerLuke Kanies <luke@madstop.com>2007-11-28 12:57:16 -0600
commit228204600b1ba40b5c2b2404b52f0a2d42e4b3ce (patch)
tree1fbcc0b78c2af1b89923c2b10c722f271e67b0be
parentc6d1746199c043c833a34393faa10d0a960f201a (diff)
downloadpuppet-228204600b1ba40b5c2b2404b52f0a2d42e4b3ce.tar.gz
puppet-228204600b1ba40b5c2b2404b52f0a2d42e4b3ce.tar.xz
puppet-228204600b1ba40b5c2b2404b52f0a2d42e4b3ce.zip
Adding a top-level ResourceReference class that everything
else can use to canonize how we refer to resources. Finally.
-rw-r--r--lib/puppet/resource_reference.rb61
-rwxr-xr-xspec/unit/resource_reference.rb43
2 files changed, 104 insertions, 0 deletions
diff --git a/lib/puppet/resource_reference.rb b/lib/puppet/resource_reference.rb
new file mode 100644
index 000000000..5e4cc649e
--- /dev/null
+++ b/lib/puppet/resource_reference.rb
@@ -0,0 +1,61 @@
+#
+# Created by Luke Kanies on 2007-11-28.
+# Copyright (c) 2007. All rights reserved.
+
+require 'puppet'
+
+# A simple class to canonize how we refer to and retrieve
+# resources.
+class Puppet::ResourceReference
+ attr_reader :type
+ attr_accessor :title, :configuration
+
+ def initialize(type, title)
+ @title = title
+ self.type = type
+
+ @builtin_type = nil
+ end
+
+ # Find our resource.
+ def resolve
+ if configuration
+ return configuration.resource(to_s)
+ end
+ # If it's builtin, then just ask for it directly from the type.
+ if t = builtin_type
+ t[@title]
+ else # Else, look for a component with the full reference as the name.
+ Puppet::Type::Component[to_s]
+ end
+ end
+
+ # Canonize the type so we know it's always consistent.
+ def type=(value)
+ @type = value.to_s.split("::").collect { |s| s.capitalize }.join("::")
+ end
+
+ # Convert to the standard way of referring to resources.
+ def to_s
+ "%s[%s]" % [@type, @title]
+ end
+
+ private
+
+ def builtin_type?
+ builtin_type ? true : false
+ end
+
+ def builtin_type
+ if @builtin_type.nil?
+ if @type =~ /::/
+ @builtin_type = false
+ elsif klass = Puppet::Type.type(@type.to_s.downcase)
+ @builtin_type = klass
+ else
+ @builtin_type = false
+ end
+ end
+ @builtin_type
+ end
+end
diff --git a/spec/unit/resource_reference.rb b/spec/unit/resource_reference.rb
new file mode 100755
index 000000000..dad33866c
--- /dev/null
+++ b/spec/unit/resource_reference.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../spec_helper'
+
+require 'puppet/resource_reference'
+
+describe Puppet::ResourceReference do
+ it "should have a :title attribute" do
+ Puppet::ResourceReference.new(:file, "foo").title.should == "foo"
+ end
+
+ it "should canonize types to capitalized strings" do
+ Puppet::ResourceReference.new(:file, "foo").type.should == "File"
+ end
+
+ it "should canonize qualified types so all strings are capitalized" do
+ Puppet::ResourceReference.new("foo::bar", "foo").type.should == "Foo::Bar"
+ end
+end
+
+describe Puppet::ResourceReference, "when resolving resources without a configuration" do
+ it "should be able to resolve builtin resources from their types" do
+ Puppet::Type.type(:file).expects(:[]).with("myfile").returns(:myfile)
+ Puppet::ResourceReference.new(:file, "myfile").resolve.should == :myfile
+ end
+
+ it "should be able to resolve defined resources from Components" do
+ Puppet::Type.type(:component).expects(:[]).with("Foo::Bar[yay]").returns(:mything)
+ Puppet::ResourceReference.new("foo::bar", "yay").resolve.should == :mything
+ end
+end
+
+describe Puppet::ResourceReference, "when resolving resources with a configuration" do
+ it "should resolve all resources using the configuration" do
+ config = mock 'configuration'
+ ref = Puppet::ResourceReference.new("foo::bar", "yay")
+ ref.configuration = config
+
+ config.expects(:resource).with("Foo::Bar[yay]").returns(:myresource)
+
+ ref.resolve.should == :myresource
+ end
+end