diff --git a/lib/puppet/resource_reference.rb b/lib/puppet/resource_reference.rb index d5fc90d1f..771a91be7 100644 --- a/lib/puppet/resource_reference.rb +++ b/lib/puppet/resource_reference.rb @@ -1,79 +1,79 @@ # # 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, :catalog def initialize(type, title) # This will set @type if it looks like a resource reference. self.title = title # Don't override whatever was done by setting the title. self.type = type if self.type.nil? @builtin_type = nil end # Find our resource. def resolve if catalog return catalog.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 # If the title has square brackets, treat it like a reference and # set things appropriately; else, just set it. def title=(value) - if value =~ /^(.+)\[(.+)\]$/ - @type = $1 + if value =~ /^([^\[\]]+)\[(.+)\]$/ + self.type = $1 @title = $2 else @title = value end end # Canonize the type so we know it's always consistent. def type=(value) if value.nil? or value.to_s.downcase == "component" @type = "Class" else @type = value.to_s.split("::").collect { |s| s.capitalize }.join("::") end 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 index ef172d80a..cbbd6ef51 100755 --- a/spec/unit/resource_reference.rb +++ b/spec/unit/resource_reference.rb @@ -1,67 +1,73 @@ #!/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 it "should set its type to 'Class' and its title to the passed title if the passed type is :component and the title has no square brackets in it" do ref = Puppet::ResourceReference.new(:component, "foo") ref.type.should == "Class" ref.title.should == "foo" end it "should interpret the title as a reference and assign appropriately if the type is :component and the title contains square brackets" do ref = Puppet::ResourceReference.new(:component, "foo::bar[yay]") ref.type.should == "Foo::Bar" ref.title.should == "yay" end it "should set the type to 'Class' if it is nil and the title contains no square brackets" do ref = Puppet::ResourceReference.new(nil, "yay") ref.type.should == "Class" ref.title.should == "yay" end it "should interpret the title as a reference and assign appropriately if the type is nil and the title contains square brackets" do ref = Puppet::ResourceReference.new(nil, "foo::bar[yay]") ref.type.should == "Foo::Bar" ref.title.should == "yay" end + + it "should interpret the title as a reference and assign appropriately if the type is nil and the title contains nested square brackets" do + ref = Puppet::ResourceReference.new(nil, "foo::bar[baz[yay]]") + ref.type.should == "Foo::Bar" + ref.title.should =="baz[yay]" + end end describe Puppet::ResourceReference, "when resolving resources without a catalog" 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 catalog" do it "should resolve all resources using the catalog" do config = mock 'catalog' ref = Puppet::ResourceReference.new("foo::bar", "yay") ref.catalog = config config.expects(:resource).with("Foo::Bar[yay]").returns(:myresource) ref.resolve.should == :myresource end end