File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1818
1919require 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+
2125describe Chef ::Resource do
2226 before ( :each ) do
2327 @resource = Chef ::Resource . new ( "funk" )
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+
214230end
You can’t perform that action at this time.
0 commit comments