summaryrefslogtreecommitdiff
path: root/chef/lib/chef/index_queue/indexable.rb
blob: c01bb0f7485ac2569d17ecbffe2b47ae3d0a027d (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
#
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Copyright:: Copyright (c) 2009 Daniel DeLeo
# License:: Apache License, Version 2.0
#
# 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.
#

class Chef
  module IndexQueue
    module Indexable
      
      module ClassMethods
        
        def index_object_type(explicit_type_name=nil)
          @index_object_type = explicit_type_name.to_s if explicit_type_name
          @index_object_type
        end
        
        # Resets all metadata used for indexing to nil. Used for testing
        def reset_index_metadata!
          @index_object_type = nil
        end
        
      end

      def self.included(including_class)
        including_class.send(:extend, ClassMethods)
      end
      
      attr_accessor :index_id
      
      def index_object_type
        self.class.index_object_type || Mixin::ConvertToClassName.snake_case_basename(self.class.name)
      end
      
      def with_indexer_metadata(indexer_metadata={})
        # changing input param symbol keys to strings, as the keys in hash that goes to solr are expected to be strings [cb]
        # Ruby 1.9 hates you, cb [dan]
        with_metadata = {}
        indexer_metadata.each_key do |key|
          with_metadata[key.to_s] = indexer_metadata[key]
        end

        with_metadata["type"]     ||= self.index_object_type
        with_metadata["id"]       ||= self.index_id
        with_metadata["database"] ||= Chef::Config[:couchdb_database]
        with_metadata["item"]     ||= self.to_hash
        with_metadata["enqueued_at"] ||= Time.now.utc.to_i

        raise ArgumentError, "Type, Id, or Database missing in index operation: #{with_metadata.inspect}" if (with_metadata["id"].nil? or with_metadata["type"].nil?)
        with_metadata        
      end
      
      def add_to_index(metadata={})
        Chef::Log.debug("pushing item to index queue for addition: #{self.with_indexer_metadata(metadata)}")
        object_with_metadata = with_indexer_metadata(metadata)
        obj_id = object_with_metadata["id"]
        AmqpClient.instance.queue_for_object(obj_id) do |queue|
          queue.publish({:action => :add, :payload => self.with_indexer_metadata(metadata)}.to_json)
        end
      end

      def delete_from_index(metadata={})
        Chef::Log.debug("pushing item to index queue for deletion: #{self.with_indexer_metadata(metadata)}")
        object_with_metadata = with_indexer_metadata(metadata)
        obj_id = object_with_metadata["id"]
        AmqpClient.instance.queue_for_object(obj_id) do |queue|
          queue.publish({:action => :delete, :payload => self.with_indexer_metadata(metadata)}.to_json)
        end
      end
      
    end
  end
end