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

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

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


41
42
43
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 41

def chef_class
  Chef::DataBagItem
end

#normalize(data_bag_item, entry) ⇒ Object


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

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


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

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


33
34
35
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 33

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

#preserve_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

37
38
39
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 37

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.

Parameters:

Yields:

  • (s)

    callback to handle errors

Yield Parameters:

  • error (s<string>)

    message


51
52
53
54
55
56
# File 'lib/chef/chef_fs/data_handler/data_bag_item_data_handler.rb', line 51

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']}')")
  end
end