Skip to content

Commit 8ae8a64

Browse files
author
chris
committed
Light-weight resources (well, any resource that calls the provider setter method) can now specify the provider by the light-weight provider identifier via string or symbol.
1 parent bd29634 commit 8ae8a64

6 files changed

Lines changed: 36 additions & 3 deletions

File tree

chef/lib/chef/resource.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require 'chef/mixin/params_validate'
2121
require 'chef/mixin/check_helper'
2222
require 'chef/mixin/language'
23+
require 'chef/mixin/convert_to_class_name'
2324
require 'chef/resource_collection'
2425
require 'chef/node'
2526

@@ -29,6 +30,7 @@ class Resource
2930
include Chef::Mixin::CheckHelper
3031
include Chef::Mixin::ParamsValidate
3132
include Chef::Mixin::Language
33+
include Chef::Mixin::ConvertToClassName
3234

3335
attr_accessor :actions, :params, :provider, :updated, :allowed_actions, :collection, :cookbook_name, :recipe_name
3436
attr_reader :resource_name, :source_line, :node
@@ -84,9 +86,18 @@ def supports(args={})
8486
end
8587

8688
def provider(arg=nil)
89+
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+
raise ArgumentError, "Undefined provider for #{arg}"
94+
end
95+
else
96+
arg
97+
end
8798
set_or_return(
8899
:provider,
89-
arg,
100+
klass,
90101
:kind_of => [ Class ]
91102
)
92103
end

features/cookbooks/lightweight_resources_and_providers.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Feature: Light-weight resources and providers
1515
| non_default_resource | Non-default resource |
1616
| overridden_resource_initialize | Overridden initialize |
1717
| overridden_provider_load_current_resource | Overridden load_current_resource |
18+
| provider_is_a_string | Provider is a string |
19+
| provider_is_a_symbol | Provider is a symbol |
20+
| provider_is_a_class | Provider is a class |
1821

1922
@client @api
2023
Scenario Outline: Chef client handles light-weight resources and providers
@@ -31,4 +34,6 @@ Feature: Light-weight resources and providers
3134
| non_default_resource | Non-default resource |
3235
| overridden_resource_initialize | Overridden initialize |
3336
| overridden_provider_load_current_resource | Overridden load_current_resource |
34-
37+
| provider_is_a_string | Provider is a string |
38+
| provider_is_a_symbol | Provider is a symbol |
39+
| provider_is_a_class | Provider is a class |

features/data/cookbooks/lwrp/recipes/default_everything.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
message "Default everything"
33
action :print_message
44

5-
# TODO: should we provide an implementation of the provider method for lwps that converts the provided string or symbol to the appropriate class, as well as allow for a class arg?
65
provider Chef::Provider::Lwrp
76
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
lwrp :provider_is_a_class do
2+
message "Provider is a class"
3+
action :print_message
4+
5+
provider Chef::Provider::Lwrp
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
lwrp :provider_is_a_string do
2+
message "Provider is a string"
3+
action :print_message
4+
5+
provider "lwrp"
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
lwrp :provider_is_a_symbol do
2+
message "Provider is a symbol"
3+
action :print_message
4+
5+
provider :lwrp
6+
end

0 commit comments

Comments
 (0)