Class: ChefFS::FileSystem::RestListDir

Inherits:
BaseFSDir show all
Defined in:
lib/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.



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

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.



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

def api_path
  @api_path
end

#data_handlerObject (readonly)

Returns the value of attribute data_handler.



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

def data_handler
  @data_handler
end

Instance Method Details

#_make_child_entry(name, exists = nil) ⇒ Object



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

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

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#child(name) ⇒ Object



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

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



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

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

#create_child(name, file_contents) ⇒ Object



61
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
# File 'lib/chef_fs/file_system/rest_list_dir.rb', line 61

def create_child(name, file_contents)
  begin
    object = JSON.parse(file_contents, :create_additions => false)
  rescue JSON::ParserError => e
    raise 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 ChefFS::FileSystem::OperationFailedError.new(:create_child, self), "Error creating '#{name}': #{error}"
    end
  end

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

  @children = nil

  result
end

#environmentObject



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

def environment
  parent.environment
end

#orgObject



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

def org
  parent.org
end

#restObject



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

def rest
  parent.rest
end