summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-03-14 15:44:08 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-03-20 14:38:05 -0700
commit31c218ce09ead9036c033b868571a4d4dd75b110 (patch)
tree129c4705a5d7b037071e90ed543fe8ecf3725f3b
parent34b0725c7328aad2d0542fc95bb12e352101ae94 (diff)
downloadchef-31c218ce09ead9036c033b868571a4d4dd75b110.tar.gz
ResourceStore does not deal with exceptions
Also, added a more specific exception for when we cannot find the resource
-rw-r--r--lib/chef/exceptions.rb15
-rw-r--r--lib/chef/provider/dsc_resource.rb20
-rw-r--r--lib/chef/util/dsc/resource_store.rb12
3 files changed, 32 insertions, 15 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb
index 22f090789f..eea6a2f239 100644
--- a/lib/chef/exceptions.rb
+++ b/lib/chef/exceptions.rb
@@ -442,5 +442,20 @@ class Chef
super "PID file and lockfile are not permitted to match. Specify a different location with --pid or --lockfile"
end
end
+
+ class MultipleDscResourcesFound < RuntimeError
+ attr_reader :resources_found
+ def initialize(resources_found)
+ @resources_found = resources_found
+ matches_info = @resources_found.each do |r|
+ if r['Module'].nil?
+ "Resource #{r['Name']} was found in #{r['Module']['Name']}"
+ else
+ "Resource #{r['Name']} is a binary resource"
+ end
+ end
+ super "Found multiple matching resources. #{matches_info.join("\n")}"
+ end
+ end
end
end
diff --git a/lib/chef/provider/dsc_resource.rb b/lib/chef/provider/dsc_resource.rb
index 1e20030229..bd72131e67 100644
--- a/lib/chef/provider/dsc_resource.rb
+++ b/lib/chef/provider/dsc_resource.rb
@@ -94,12 +94,20 @@ class Chef
def module_name
@module_name ||= begin
- r = resource_store.resource(@new_resource.resource.to_s)
- if r['Module']
- r['Module']['Name']
- else
- :none
- end
+ found = resource_store.find(new_resource.resource.to_s)
+
+ r = case found.length
+ when 0
+ nil
+ when 1
+ if found[0]['Module'].nil?
+ :none
+ else
+ found[0]['Module']
+ end
+ else
+ raise Chef::Exceptions::MultipleDscResourcesFound, found
+ end
end
end
diff --git a/lib/chef/util/dsc/resource_store.rb b/lib/chef/util/dsc/resource_store.rb
index e28a146afd..643f5e8d2f 100644
--- a/lib/chef/util/dsc/resource_store.rb
+++ b/lib/chef/util/dsc/resource_store.rb
@@ -18,6 +18,7 @@
require 'chef/util/powershell/cmdlet'
require 'chef/util/powershell/cmdlet_result'
+require 'chef/exceptions'
class Chef
class Util
@@ -34,7 +35,7 @@ class DSC
@resources ||= []
end
- def resource(name, module_name=nil)
+ def find(name, module_name=nil)
found = find_resources(name, module_name, resources)
# We don't have it, query for the resource...it might
@@ -45,14 +46,7 @@ class DSC
found = find_resources(name, module_name, rs)
end
- case found.length
- when 0
- nil
- when 1
- found[0]
- else
- raise 'Multiple matching resources'
- end
+ found
end
private