summaryrefslogtreecommitdiff
path: root/lib/chef/util/dsc/resource_store.rb
blob: 49ca46832a62aa1aa719426e31dbccd068a03b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# Author:: Jay Mundrawala (<jdm@chef.io>)
#
# Copyright:: Copyright (c) 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_relative "../../mixin/powershell_exec"
require_relative "../../exceptions"

class Chef
  class Util
    class DSC
      class ResourceStore
        include Chef::Mixin::PowershellExec

        def self.instance
          @@instance ||= ResourceStore.new.tap do |store|
            store.send(:populate_cache)
          end
        end

        def resources
          @resources ||= []
        end

        def find(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

          found
        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["Name"].casecmp(name) == 0
            if name_matches
              module_name.nil? || (r["Module"] && r["Module"]["Name"].casecmp(module_name) == 0)
            else
              false
            end
          end
        end

        # Returns a list of dsc resources
        def query_resources
          powershell_exec("get-dscresource").result
        end

        # Returns a list of dsc resources matching the provided name
        def query_resource(resource_name)
          ret_val = powershell_exec("get-dscresource #{resource_name}").result
          if ret_val.empty?
            []
          elsif ret_val.is_a? Array
            ret_val
          else
            [ret_val]
          end
        end
      end
    end
  end
end