Sha256: 917a23058ae9bf001f1d9778fa91e2872b953ff2db3ce7ece174cb6d50b667a6

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

require 'chef_fs/data_handler/data_handler_base'
require 'chef/data_bag_item'

module ChefFS
  module DataHandler
    class DataBagItemDataHandler < DataHandlerBase
      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 only come back from PUT and POST, but we'll
        # normalize them in in case someone is comparing with those results.
        super(data_bag_item, {
          'chef_type' => 'data_bag_item',
          'data_bag' => entry.parent.name,
          'id' => remove_dot_json(entry.name)
        })
      end

      def normalize_for_post(data_bag_item, entry)
        {
          "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

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

      def preserve_key(key)
        return key == 'id'
      end

      def chef_class
        Chef::DataBagItem
      end

      def verify_integrity(object, entry, &on_error)
        base_name = remove_dot_json(entry.name)
        if object['raw_data']['id'] != base_name
          on_error.call("ID in #{entry.path_for_printing} must be '#{base_name}' (is '#{object['raw_data']['id']}')")
        end
      end

      # Data bags do not support .rb files (or if they do, it's undocumented)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
knife-essentials-1.3 lib/chef_fs/data_handler/data_bag_item_data_handler.rb
knife-essentials-1.2.1 lib/chef_fs/data_handler/data_bag_item_data_handler.rb
knife-essentials-1.2 lib/chef_fs/data_handler/data_bag_item_data_handler.rb
knife-essentials-1.1.1 lib/chef_fs/data_handler/data_bag_item_data_handler.rb
knife-essentials-1.1 lib/chef_fs/data_handler/data_bag_item_data_handler.rb
knife-essentials-1.0.0 lib/chef_fs/data_handler/data_bag_item_data_handler.rb
knife-essentials-1.0.0.beta5 lib/chef_fs/data_handler/data_bag_item_data_handler.rb