From 34638bf82798bdcf01c28fbde6ac68dd1949c301 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Wed, 13 May 2015 13:38:41 -0700 Subject: Add Chef::ResourceResolver.resolve --- DOC_CHANGES.md | 2 +- external_tests/foodcritic.gemfile | 1 + lib/chef/resource_resolver.rb | 10 ++++++++++ spec/unit/lwrp_spec.rb | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/DOC_CHANGES.md b/DOC_CHANGES.md index 31ea79f3d9..b0a8e0aaaf 100644 --- a/DOC_CHANGES.md +++ b/DOC_CHANGES.md @@ -27,7 +27,7 @@ Users are encouraged to declare resources in their own namespaces instead of put Starting with Chef 12.4.0, accessing an LWRP class by name from the `Chef::Resource` namespace will trigger a deprecation warning message. This means that if your cookbook includes the LWRP `mycookbook/resources/myresource.rb`, you will no longer be able to extend or reference `Chef::Resource::MycookbookMyresource` in Ruby code. LWRP recipe DSL does not change: the LWRP will still be available to recipes as `mycookbook_myresource`. -You can still get the LWRP class by calling `Chef::Resource.resource_matching_short_name(:mycookbook_myresource)`. +You can still get the LWRP class by calling `Chef::ResourceResolver.resolve(:mycookbook_myresource)`. The primary aim here is clearing out the `Chef::Resource` namespace. diff --git a/external_tests/foodcritic.gemfile b/external_tests/foodcritic.gemfile index 7d2261eff3..a2b71a0d8c 100644 --- a/external_tests/foodcritic.gemfile +++ b/external_tests/foodcritic.gemfile @@ -6,3 +6,4 @@ gem 'foodcritic', github: 'acrmp/foodcritic' gem 'cucumber' gem 'rubocop' gem 'simplecov' +gem 'minitest' diff --git a/lib/chef/resource_resolver.rb b/lib/chef/resource_resolver.rb index d4151e7125..a987b236c2 100644 --- a/lib/chef/resource_resolver.rb +++ b/lib/chef/resource_resolver.rb @@ -51,6 +51,16 @@ class Chef end.sort {|a,b| a.to_s <=> b.to_s } end + # + # Resolve a resource by name. + # + # @param resource_name [Symbol] The resource DSL name (e.g. `:file`) + # @param node [Chef::Node] The node on which the resource will run. + # + def self.resolve(resource_name, node: Chef.node) + new(node, resource_name).resolve + end + private # try dynamically finding a resource based on querying the resources to see what they support diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index a1cccb3071..ff7f8e7d7b 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -164,6 +164,10 @@ describe "LWRP" do expect { Chef::Resource::LwrpFoo }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) end + it "should be resolvable with Chef::ResourceResolver.resolve(:lwrp_foo)" do + expect(Chef::ResourceResolver.resolve(:lwrp_foo, node: Chef::Node.new)).to eq(get_lwrp(:lwrp_foo)) + end + it "should set resource_name" do expect(get_lwrp(:lwrp_foo).new("blah").resource_name).to eql(:lwrp_foo) end -- cgit v1.2.1