Module: Search::Overrides

Defined in:
lib/knife-solo/resources/patch_cookbooks/chef-solo-search/libraries/search/overrides.rb

Instance Method Summary collapse

Instance Method Details

#search(obj, query = nil, sort = nil, start = 0, rows = 1000, &block) ⇒ Object

Overwrite the search method of recipes to operate locally by using data found in data_bags. Only very basic lucene syntax is supported and also sorting the result is not implemented, if this search method does not support a given query an exception is raised. This search() method returns a block iterator or an Array, depending on how this method is called.



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
# File 'lib/knife-solo/resources/patch_cookbooks/chef-solo-search/libraries/search/overrides.rb', line 30

def search(obj, query=nil, sort=nil, start=0, rows=1000, &block)
  if !sort.nil?
    raise "Sorting search results is not supported"
  end
  _query = Query.parse(query)
  if _query.nil?
    raise "Query #{query} is not supported"
  end
  _result = []

  case obj
  when :node
    nodes = search_nodes(_query, start, rows, &block)
    _result += nodes
  when :role
    roles = search_roles(_query, start, rows, &block)
     _result += roles
  else
    bags = search_data_bag(_query, obj, start, rows, &block)
    _result += bags
  end


  if block_given?
    pos = 0
    while (pos >= start and pos < (start + rows) and pos < _result.size)
      yield _result[pos]
      pos += 1
    end
  else
    return _result.slice(start, rows)
  end
end

#search_data_bag(_query, bag_name, start, rows, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/knife-solo/resources/patch_cookbooks/chef-solo-search/libraries/search/overrides.rb', line 88

def search_data_bag(_query, bag_name, start, rows, &block)
  _result = []
  data_bag(bag_name.to_s).each do |bag_item_id|
    bag_item = data_bag_item(bag_name.to_s, bag_item_id)
    if _query.match(bag_item)
      _result << bag_item
    end
  end
  return _result
end

#search_nodes(_query, start, rows, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/knife-solo/resources/patch_cookbooks/chef-solo-search/libraries/search/overrides.rb', line 64

def search_nodes(_query, start, rows, &block)
  _result = []
  Dir.glob(File.join(Chef::Config[:data_bag_path], "node", "*.json")).map do |f|
    # parse and hashify the node
    node = Chef::JSONCompat.from_json(IO.read(f))
    if _query.match(node.to_hash)
      _result << node
    end
  end
  return _result
end

#search_roles(_query, start, rows, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/knife-solo/resources/patch_cookbooks/chef-solo-search/libraries/search/overrides.rb', line 76

def search_roles(_query, start, rows, &block)
  _result = []
  Dir.glob(File.join(Chef::Config[:role_path], "*.json")).map do |f|
    # parse and hashify the role
    role = Chef::JSONCompat.from_json(IO.read(f))
    if _query.match(role.to_hash)
      _result << role
    end
  end
  return _result
end