Skip to content

Commit 74e7eae

Browse files
committed
let resources set a base namespace for providers
1 parent 178102f commit 74e7eae

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

chef/lib/chef/resource.rb

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,7 @@ def supports(args={})
8787

8888
def provider(arg=nil)
8989
klass = if arg.kind_of?(String) || arg.kind_of?(Symbol)
90-
begin
91-
Chef::Provider.const_get(convert_to_class_name(arg.to_s))
92-
rescue NameError => e
93-
if e.to_s =~ /Chef::Provider/
94-
raise ArgumentError, "No provider found to match '#{arg}'"
95-
else
96-
raise e
97-
end
98-
end
90+
lookup_provider_constant(arg)
9991
else
10092
arg
10193
end
@@ -241,6 +233,7 @@ def run_action(action)
241233
end
242234

243235
class << self
236+
244237
def json_create(o)
245238
resource = self.new(o["instance_vars"]["@name"])
246239
o["instance_vars"].each do |k,v|
@@ -306,9 +299,34 @@ def actions_to_create
306299

307300
new_resource_class
308301
end
302+
303+
# Resources that want providers namespaced somewhere other than
304+
# Chef::Provider can set the namespace with +provider_base+
305+
# Ex:
306+
# class MyResource < Chef::Resource
307+
# provider_base Chef::Provider::Deploy
308+
# # ...other stuff
309+
# end
310+
def provider_base(arg=nil)
311+
@provider_base ||= arg
312+
@provider_base ||= Chef::Provider
313+
end
314+
309315
end
310316

311317
private
318+
319+
def lookup_provider_constant(name)
320+
begin
321+
self.class.provider_base.const_get(convert_to_class_name(name.to_s))
322+
rescue NameError => e
323+
if e.to_s =~ /#{self.class.provider_base.to_s}/
324+
raise ArgumentError, "No provider found to match '#{name}'"
325+
else
326+
raise e
327+
end
328+
end
329+
end
312330

313331
def check_timing(timing)
314332
unless timing == :delayed || timing == :immediate || timing == :immediately

chef/spec/unit/resource_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2020

21+
class ResourceTestHarness < Chef::Resource
22+
provider_base Chef::Provider::Package
23+
end
24+
2125
describe Chef::Resource do
2226
before(:each) do
2327
@resource = Chef::Resource.new("funk")
@@ -211,4 +215,16 @@
211215
end
212216
end
213217

218+
describe "setting the base provider class for the resource" do
219+
220+
it "defaults to Chef::Provider for the base class" do
221+
Chef::Resource.provider_base.should == Chef::Provider
222+
end
223+
224+
it "allows the base provider to be overriden by a " do
225+
ResourceTestHarness.provider_base.should == Chef::Provider::Package
226+
end
227+
228+
end
229+
214230
end

0 commit comments

Comments
 (0)