mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[ninefold|storage] Nested directories
This commit is contained in:
parent
c69e231c10
commit
083f7b9038
3 changed files with 65 additions and 9 deletions
|
@ -7,13 +7,18 @@ module Fog
|
||||||
|
|
||||||
class Directories < Fog::Collection
|
class Directories < Fog::Collection
|
||||||
|
|
||||||
|
attribute :directory
|
||||||
|
|
||||||
model Fog::Storage::Ninefold::Directory
|
model Fog::Storage::Ninefold::Directory
|
||||||
|
|
||||||
def all
|
def all
|
||||||
data = connection.get_namespace.body[:DirectoryList]
|
directory ? ns = directory.key : ns = ''
|
||||||
|
data = connection.get_namespace(ns).body[:DirectoryList]
|
||||||
data = {:DirectoryEntry => []} if data.kind_of? String
|
data = {:DirectoryEntry => []} if data.kind_of? String
|
||||||
data[:DirectoryEntry] = [data[:DirectoryEntry]] if data[:DirectoryEntry].kind_of? Hash
|
data[:DirectoryEntry] = [data[:DirectoryEntry]] if data[:DirectoryEntry].kind_of? Hash
|
||||||
load(data[:DirectoryEntry])
|
dirs = data[:DirectoryEntry].select {|de| de[:FileType] == 'directory'}
|
||||||
|
dirs.each {|d| d[:Filename] = ns + '/' + d[:Filename] if directory}
|
||||||
|
load(data[:DirectoryEntry].select {|de| de[:FileType] == 'directory'})
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(key, options = {})
|
def get(key, options = {})
|
||||||
|
@ -21,11 +26,16 @@ module Fog
|
||||||
res = connection.get_namespace key
|
res = connection.get_namespace key
|
||||||
emc_meta = res.headers['x-emc-meta']
|
emc_meta = res.headers['x-emc-meta']
|
||||||
obj_id = emc_meta.scan(/objectid=(\w+),/).flatten[0]
|
obj_id = emc_meta.scan(/objectid=(\w+),/).flatten[0]
|
||||||
new(:id => obj_id, :filename => key, :type => 'directory')
|
new(:objectid => obj_id, :key => key)
|
||||||
rescue Fog::Storage::Ninefold::NotFound
|
rescue Fog::Storage::Ninefold::NotFound
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def new(attributes ={})
|
||||||
|
attributes = {:directory => directory}.merge(attributes) if directory
|
||||||
|
super(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,17 +6,38 @@ module Fog
|
||||||
|
|
||||||
class Directory < Fog::Model
|
class Directory < Fog::Model
|
||||||
|
|
||||||
identity :filename, :aliases => [:'Filename', :'key']
|
identity :key, :aliases => :Filename
|
||||||
attribute :id, :aliases => :'ObjectID'
|
attribute :objectid, :aliases => :ObjectID
|
||||||
attribute :type, :aliases => :'FileType'
|
|
||||||
|
def files
|
||||||
|
@files ||= begin
|
||||||
|
Fog::Storage::Ninefold::Files.new(
|
||||||
|
:directory => self,
|
||||||
|
:connection => connection
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def directories
|
||||||
|
@directories ||= begin
|
||||||
|
Fog::Storage::Ninefold::Directories.new(
|
||||||
|
:directory => self,
|
||||||
|
:connection => connection
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def save
|
def save
|
||||||
res = connection.post_namespace filename
|
self.key = attributes[:directory].key + '/' + key if attributes[:directory]
|
||||||
|
res = connection.post_namespace key
|
||||||
reload
|
reload
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy(opts={})
|
||||||
connection.delete_namespace filename
|
if opts[:recursive]
|
||||||
|
directories.each { |d| d.destroy(opts) }
|
||||||
|
end
|
||||||
|
connection.delete_namespace key
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
25
tests/storage/models/ninefold/nested_directories_tests.rb
Normal file
25
tests/storage/models/ninefold/nested_directories_tests.rb
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
if storage_providers.keys.include? :ninefold
|
||||||
|
for provider, config in storage_providers
|
||||||
|
|
||||||
|
Shindo.tests("Storage[:ninefold] | nested directories", [provider]) do
|
||||||
|
ninefold = Fog::Storage[:ninefold]
|
||||||
|
tests("create a directory with a / character").succeeds do
|
||||||
|
ninefold.directories.create(:key => 'sub/path')
|
||||||
|
end
|
||||||
|
|
||||||
|
tests("List of top directory returns sub dir").returns(1) do
|
||||||
|
ninefold.directories.get('sub').directories.count
|
||||||
|
end
|
||||||
|
|
||||||
|
tests("create a directory in a sub dir").returns('sub/path/newdir') do
|
||||||
|
ninefold.directories.get('sub/path').directories.create(:key => 'newdir').identity
|
||||||
|
end
|
||||||
|
|
||||||
|
tests("Recursively destroy parent dir").succeeds do
|
||||||
|
ninefold.directories.get('sub').destroy(:recursive => true)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue