Class: HealthInspector::Checklists::DataBagItems

Inherits:
Base
  • Object
show all
Defined in:
lib/health_inspector/checklists/data_bag_items.rb

Defined Under Namespace

Classes: DataBagItem

Instance Method Summary collapse

Methods inherited from Base

add_check, #banner, #checks, #chef_rest, #indent, #initialize, #load_ruby_or_json_from_local, #print_failures, #print_failures_from_hash, #print_key, #print_success, #print_value_diff, #run, run, #run_check, #run_checks

Methods included from HealthInspector::Color

#color

Constructor Details

This class inherits a constructor from HealthInspector::Checklists::Base

Instance Method Details

#data_bag_items_in_repoObject



49
50
51
52
53
54
55
56
57
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 49

def data_bag_items_in_repo
  entries = nil

  Dir.chdir("#{@context.repo_path}/data_bags") do
    entries = Dir["**/*.json"].map { |entry| entry.gsub('.json', '') }
  end

  return entries
end

#data_bag_items_on_serverObject



41
42
43
44
45
46
47
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 41

def data_bag_items_on_server
  @data_bags_on_server ||= Chef::DataBag.list.keys.map do |bag_name|
    [ bag_name, Chef::DataBag.load(bag_name) ]
  end.inject([]) do |arr, (bag_name, data_bag)|
    arr += data_bag.keys.map { |item_name| "#{bag_name}/#{item_name}"}
  end
end

#each_itemObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 25

def each_item
  server_data_bag_items   = data_bag_items_on_server
  local_data_bag_items    = data_bag_items_in_repo
  all_data_bag_item_names = ( server_data_bag_items + local_data_bag_items ).uniq.sort

  all_data_bag_item_names.each do |name|
    item = DataBagItem.new.tap do |data_bag_item|
      data_bag_item.name   = name
      data_bag_item.server = load_item_from_server(name)
      data_bag_item.local  = load_item_from_local(name)
    end

    yield item
  end
end

#load_item_from_local(name) ⇒ Object



66
67
68
69
70
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 66

def load_item_from_local(name)
  Yajl::Parser.parse( File.read("#{@context.repo_path}/data_bags/#{name}.json") )
rescue IOError, Errno::ENOENT
  nil
end

#load_item_from_server(name) ⇒ Object



59
60
61
62
63
64
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 59

def load_item_from_server(name)
  bag_name, item_name = name.split("/")
  Chef::DataBagItem.load(bag_name, item_name).raw_data
rescue
  nil
end