summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-03-07 17:53:30 -0800
committerJay Mundrawala <jdmundrawala@gmail.com>2015-03-20 14:38:04 -0700
commit4eada072f98f877cb071918f6430de6cee3f4daa (patch)
tree4260755901eb591572c10ce380fd045d4deb266e
parent5c2ef0d3f8aaa17c9848b6df837b0aeead05149f (diff)
downloadchef-4eada072f98f877cb071918f6430de6cee3f4daa.tar.gz
Added resource store for dsc
This is a cache that will be used to lookup the module name for resources. If in the future we decide to do any validation, the data for that can is available.
-rw-r--r--lib/chef/util/dsc/resource_store.rb114
1 files changed, 114 insertions, 0 deletions
diff --git a/lib/chef/util/dsc/resource_store.rb b/lib/chef/util/dsc/resource_store.rb
new file mode 100644
index 0000000000..3adc763483
--- /dev/null
+++ b/lib/chef/util/dsc/resource_store.rb
@@ -0,0 +1,114 @@
+#
+# Author:: Jay Mundrawala (<jdm@chef.io>)
+#
+# Copyright:: Copyright (c) 2015 Chef Software, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef/util/powershell/cmdlet'
+require 'chef/util/powershell/cmdlet_result'
+
+class Chef
+class Util
+class DSC
+ class ResourceStore
+
+ def self.instance
+ @@instance ||= ResourceStore.new.tap do |store|
+ store.send(:populate_cache)
+ end
+ end
+
+ def resources
+ @resources ||= []
+ end
+
+ def resource(name, module_name=nil)
+ found = find_resources(name, module_name, resources)
+
+ # We don't have it, query for the resource...it might
+ # have been added since we last queried
+ if found.length == 0
+ rs = query_resource(name)
+ add_resources(rs)
+ found = find_resources(name, module_name, rs)
+ end
+
+ case found.length
+ when 0
+ nil
+ when 1
+ found[0]
+ else
+ raise 'Multiple matching resources'
+ end
+ end
+
+ private
+
+ def add_resource(new_r)
+ count = resources.count do |r|
+ r['ResourceType'].casecmp(new_r['ResourceType']) == 0
+ end
+ if count == 0
+ resources << new_r
+ end
+ end
+
+ def add_resources(rs)
+ rs.each do |r|
+ add_resource(r)
+ end
+ end
+
+ def populate_cache
+ @resources = query_resources
+ end
+
+ def find_resources(name, module_name, rs)
+ found = rs.find_all do |r|
+ name_matches = r['FriendlyName'].casecmp(name) == 0
+ if name_matches
+ module_name == nil || (r['Module'] and r['Module']['Name'].casecmp(module_name) == 0)
+ else
+ false
+ end
+ end
+ end
+
+
+ # Returns a list of dsc resources
+ def query_resources
+ cmdlet = Chef::Util::Powershell::Cmdlet.new(nil, 'get-dscresource',
+ :object)
+ result = cmdlet.run
+ result.return_value
+ end
+
+ # Returns a list of dsc resources matching the provided name
+ def query_resource(resource_name)
+ cmdlet = Chef::Util::Powershell::Cmdlet.new(nil, "get-dscresource #{resource_name}",
+ :object)
+ result = cmdlet.run
+ ret_val = result.return_value
+ if ret_val.is_a? Array
+ ret_val
+ else
+ [ret_val]
+ end
+ end
+ end
+end
+end
+end