Class: Chef::ChefFS::DataHandler::DataBagItemDataHandler

Inherits:
DataHandlerBase show all
Defined in:
lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb

Constant Summary collapse

RESERVED_NAMES =
/^(node|role|environment|client)$/.freeze

Instance Method Summary collapse

Methods inherited from DataHandlerBase

#chef_object, #default, #from_ruby, #minimize, #normalize_hash, #normalize_run_list, #remove_file_extension, #to_ruby, #to_ruby_keys

Instance Method Details

#chef_classObject



43
44
45
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 43

def chef_class
  Chef::DataBagItem
end

#normalize(data_bag_item, entry) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 10

def normalize(data_bag_item, entry)
  # If it's wrapped with raw_data, unwrap it.
  if data_bag_item["json_class"] == "Chef::DataBagItem" && data_bag_item["raw_data"]
    data_bag_item = data_bag_item["raw_data"]
  end
  # chef_type and data_bag come back in PUT and POST results, but we don't
  # use those in knife-essentials.
  normalize_hash(data_bag_item, {
    "id" => remove_dot_json(entry.name),
  })
end

#normalize_for_post(data_bag_item, entry) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 22

def normalize_for_post(data_bag_item, entry)
  if data_bag_item["json_class"] == "Chef::DataBagItem" && data_bag_item["raw_data"]
    data_bag_item = data_bag_item["raw_data"]
  end
  {
    "name" => "data_bag_item_#{entry.parent.name}_#{remove_dot_json(entry.name)}",
    "json_class" => "Chef::DataBagItem",
    "chef_type" => "data_bag_item",
    "data_bag" => entry.parent.name,
    "raw_data" => normalize(data_bag_item, entry),
  }
end

#normalize_for_put(data_bag_item, entry) ⇒ Object



35
36
37
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 35

def normalize_for_put(data_bag_item, entry)
  normalize_for_post(data_bag_item, entry)
end

#preserve_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 39

def preserve_key?(key)
  key == "id"
end

#verify_integrity(object, entry) {|s| ... } ⇒ Object

Verify that the JSON hash for this type has a key that matches its name. Also check that the data bag name is not a reserved search index name.

Parameters:

Yields:

  • (s)

    callback to handle errors

Yield Parameters:

  • error (s<string>)

    message



54
55
56
57
58
59
60
61
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 54

def verify_integrity(object, entry)
  base_name = remove_dot_json(entry.name)
  if object["raw_data"]["id"] != base_name
    yield("ID in #{entry.path_for_printing} must be '#{base_name}' (is '#{object["raw_data"]["id"]}')")
  elsif RESERVED_NAMES.match?(entry.parent.name)
    yield("Data bag name ('#{entry.parent.name}') must not match #{RESERVED_NAMES.inspect}")
  end
end