Class: Chef::ChefFS::FileSystem::DataBagDir

Inherits:
RestListDir show all
Defined in:
lib/chef/chef_fs/file_system/data_bag_dir.rb

Instance Attribute Summary

Attributes inherited from RestListDir

#api_path

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods inherited from RestListDir

#can_have_child?, #child, #children, #environment, #rest

Methods inherited from BaseFSDir

#can_have_child?, #child

Methods inherited from BaseFSObject

#can_have_child?, #child, #compare_to, #path_for_printing, #root

Constructor Details

#initialize(name, parent, exists = nil) ⇒ DataBagDir

Returns a new instance of DataBagDir.



28
29
30
31
# File 'lib/chef/chef_fs/file_system/data_bag_dir.rb', line 28

def initialize(name, parent, exists = nil)
  super(name, parent)
  @exists = nil
end

Instance Method Details

#_make_child_entry(name, exists = nil) ⇒ Object



59
60
61
# File 'lib/chef/chef_fs/file_system/data_bag_dir.rb', line 59

def _make_child_entry(name, exists = nil)
  DataBagItem.new(name, self, exists)
end

#create_child(name, file_contents) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/chef/chef_fs/file_system/data_bag_dir.rb', line 49

def create_child(name, file_contents)
  json = Chef::JSONCompat.from_json(file_contents).to_hash
  id = name[0,name.length-5]
  if json.include?('id') && json['id'] != id
    raise "ID in #{path_for_printing}/#{name} must be '#{id}' (is '#{json['id']}')"
  end
  rest.post_rest(api_path, json)
  _make_child_entry(name, true)
end

#delete(recurse) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chef/chef_fs/file_system/data_bag_dir.rb', line 63

def delete(recurse)
  if !recurse
    raise Chef::ChefFS::FileSystem::MustDeleteRecursivelyError.new, "#{path_for_printing} must be deleted recursively"
  end
  begin
    rest.delete_rest(api_path)
  rescue Net::HTTPServerException
    if $!.response.code == "404"
      raise Chef::ChefFS::FileSystem::NotFoundError.new($!), "#{path_for_printing} not found"
    end
  end
end

#dir?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/chef/chef_fs/file_system/data_bag_dir.rb', line 33

def dir?
  exists?
end

#exists?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/chef/chef_fs/file_system/data_bag_dir.rb', line 42

def exists?
  if @exists.nil?
    @exists = parent.children.any? { |child| child.name == name }
  end
  @exists
end

#readObject



37
38
39
40
# File 'lib/chef/chef_fs/file_system/data_bag_dir.rb', line 37

def read
  # This will only be called if dir? is false, which means exists? is false.
  raise Chef::ChefFS::FileSystem::NotFoundError, "#{path_for_printing} not found"
end