summaryrefslogtreecommitdiff
path: root/lib/chef/search_index.rb
blob: 6af408b68cf49edbddd34a7522f26910d247db4d (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
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
# 
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU 
# General Public License as published by the Free Software 
# Foundation; either version 2 of the License, or any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

require File.join(File.dirname(__FILE__), "mixin", "params_validate")
require 'ferret'

class Chef
  class SearchIndex
    
    def initialize
      @index = Ferret::Index::Index.new(
        :path => Chef::Config[:search_index_path],
        :key => [ :id ]
      )
    end
    
    def add(to_index)
      type = check_type(to_index)
      result = self.send("_prepare_#{type}", to_index)
      Chef::Log.debug("Indexing #{type} with #{result.inspect}")
      @index.add_document(result)
    end
    
    def delete(index_obj)
      type = check_type(index_obj)
      to_index = self.send("_prepare_#{type}", index_obj)
      Chef::Log.debug("Removing #{type} with #{to_index.inspect}")
      @index.delete(:id => "#{to_index[:id]}")
    end
    
    private
    
      def check_type(to_check)
        type = nil
        case to_check
        when Chef::Node
          type = "node"
        end
      end
    
      def _prepare_node(node)
        result = Hash.new
        result[:id] = "node-#{node.name}"
        result[:type] = "node"
        result[:name] = node.name
        node.each_attribute do |k,v|
          result[k.to_sym] = v
        end
        result[:recipe] = node.recipes
        result
      end
  end
end