Class: Chef::ChefFS::FileSystem::RestListDir

Inherits:
BaseFSDir show all
Defined in:
lib/chef/chef_fs/file_system/rest_list_dir.rb

Direct Known Subclasses

CookbooksDir, DataBagDir, DataBagsDir, NodesDir

Instance Attribute Summary collapse

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods inherited from BaseFSDir

#dir?

Methods inherited from BaseFSObject

#compare_to, #dir?, #exists?, #path_for_printing, #root

Constructor Details

#initialize(name, parent, api_path = nil) ⇒ RestListDir

Returns a new instance of RestListDir.



27
28
29
30
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 27

def initialize(name, parent, api_path = nil)
  super(name, parent)
  @api_path = api_path || (parent.api_path == "" ? name : "#{parent.api_path}/#{name}")
end

Instance Attribute Details

#api_pathObject (readonly)

Returns the value of attribute api_path.



32
33
34
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 32

def api_path
  @api_path
end

Instance Method Details

#_make_child_entry(name, exists = nil) ⇒ Object



78
79
80
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 78

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

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 40

def can_have_child?(name, is_dir)
  name =~ /\.json$/ && !is_dir
end

#child(name) ⇒ Object



34
35
36
37
38
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 34

def child(name)
  result = @children.select { |child| child.name == name }.first if @children
  result ||= can_have_child?(name, false) ?
             _make_child_entry(name) : NonexistentFSObject.new(name, self)
end

#childrenObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 44

def children
  begin
    @children ||= rest.get_rest(api_path).keys.map do |key|
      _make_child_entry("#{key}.json", true)
    end
  rescue Net::HTTPServerException
    if $!.response.code == "404"
      raise Chef::ChefFS::FileSystem::NotFoundError.new($!), "#{path_for_printing} not found"
    else
      raise
    end
  end
end

#create_child(name, file_contents) ⇒ Object

NOTE if you change this significantly, you will likely need to change DataBagDir.create_child as well.



60
61
62
63
64
65
66
67
68
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 60

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

#environmentObject



70
71
72
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 70

def environment
  parent.environment
end

#restObject



74
75
76
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 74

def rest
  parent.rest
end