Method: ChefAPI::Resource::DataBag.from_file

Defined in:
lib/chef-api/resources/data_bag.rb

.from_file(path, name = File.basename(path)) ⇒ Array<DataBagItem>

Load the data bag from a collection of JSON files on disk. Just like knife, the basename of the folder is assumed to be the name of the data bag and all containing items a proper JSON data bag.

This will load all items in the data bag, returning an array of those items. To load an individual data bag item, see ChefAPI::Resource::DataBagItem.from_file.

**This method does NOT return an instance of a ChefAPI::Resource::DataBag!**

Parameters:

  • path (String)

    the path to the data bag folder on disk

  • name (String) (defaults to: File.basename(path))

    the name of the data bag

Returns:

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef-api/resources/data_bag.rb', line 28

def from_file(path, name = File.basename(path))
  path  = File.expand_path(path)

  raise Error::FileNotFound.new(path: path)  unless File.exists?(path)
  raise Error::NotADirectory.new(path: path) unless File.directory?(path)

  raise ArgumentError unless File.directory?(path)

  bag = new(name: name)

  Util.fast_collect(Dir["#{path}/*.json"]) do |item|
    DataBagItem.from_file(item, bag)
  end
end