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

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

Instance Attribute Summary collapse

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods inherited from BaseFSDir

#dir?

Methods inherited from BaseFSObject

#chef_object, #compare_to, #delete, #dir?, #exists?, #path_for_printing, #read, #root, #write

Constructor Details

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

Returns a new instance of RestListDir.



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

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

Instance Attribute Details

#api_pathObject (readonly)

Returns the value of attribute api_path.



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

def api_path
  @api_path
end

#data_handlerObject (readonly)

Returns the value of attribute data_handler.



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

def data_handler
  @data_handler
end

Instance Method Details

#_make_child_entry(name, exists = nil) ⇒ Object



109
110
111
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 109

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

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 42

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

#child(name) ⇒ Object



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

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



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

def children
  begin
    @children ||= root.get_json(api_path).keys.sort.map do |key|
      _make_child_entry("#{key}.json", true)
    end
  rescue Timeout::Error => e
    raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e), "Timeout retrieving children: #{e}"
  rescue Net::HTTPServerException => e
    if $!.response.code == "404"
      raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
    else
      raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e), "HTTP error retrieving children: #{e}"
    end
  end
end

#create_child(name, file_contents) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 62

def create_child(name, file_contents)
  begin
    object = Chef::JSONCompat.parse(file_contents)
  rescue Chef::Exceptions::JSON::ParseError => e
    raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self, e), "Parse error reading JSON creating child '#{name}': #{e}"
  end

  result = _make_child_entry(name, true)

  if data_handler
    object = data_handler.normalize_for_post(object, result)
    data_handler.verify_integrity(object, result) do |error|
      raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self), "Error creating '#{name}': #{error}"
    end
  end

  begin
    rest.post(api_path, object)
  rescue Timeout::Error => e
    raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self, e), "Timeout creating '#{name}': #{e}"
  rescue Net::HTTPServerException => e
    if e.response.code == "404"
      raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e)
    elsif $!.response.code == "409"
      raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, self, e), "Failure creating '#{name}': #{path}/#{name} already exists"
    else
      raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self, e), "Failure creating '#{name}': #{e.message}"
    end
  end

  @children = nil

  result
end

#environmentObject



101
102
103
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 101

def environment
  parent.environment
end

#orgObject



97
98
99
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 97

def org
  parent.org
end

#restObject



105
106
107
# File 'lib/chef/chef_fs/file_system/rest_list_dir.rb', line 105

def rest
  parent.rest
end