Class: HealthInspector::Checklists::DataBagItems

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

Instance Method Summary collapse

Methods inherited from Base

#all_item_names, #banner, #indent, #initialize, #load_ruby_or_json_from_local, #load_validate, option, #print_failures, #print_failures_from_hash, #print_key, #print_success, #print_value_diff, run, #run, title, #ui, underscored_class, #validate_item

Methods included from HealthInspector::Color

#color

Constructor Details

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

Instance Method Details

#load_item(name) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 12

def load_item(name)
  DataBagItem.new(@context,
                  name: name,
                  server: load_item_from_server(name),
                  local: load_item_from_local(name)
  )
end

#load_item_from_local(name) ⇒ Object

We support data bags that are inside a folder or git submodule, for example:

data_bags/corp/apps/some_app.json is in the repo, but apps/some_app on the server



47
48
49
50
51
52
53
54
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 47

def load_item_from_local(name)
  local_data_bag_item = Dir["#{@context.repo_path}/data_bags/**/#{name}.json"].first
  return nil if local_data_bag_item.nil?

  FFI_Yajl::Parser.parse(File.read(local_data_bag_item))
rescue FFI_Yajl::ParseError
  nil
end

#load_item_from_server(name) ⇒ Object



35
36
37
38
39
40
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 35

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

#local_itemsObject

JSON files are data bag items, their parent folder is the data bag



29
30
31
32
33
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 29

def local_items
  Dir["#{@context.repo_path}/data_bags/**/*.json"].map do |e|
    e.split('/')[-2..-1].join('/').gsub('.json', '')
  end
end

#server_itemsObject



20
21
22
23
24
25
26
# File 'lib/health_inspector/checklists/data_bag_items.rb', line 20

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