mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
first pass at models for Rackspace::Files
This commit is contained in:
parent
00dd1a9671
commit
6905b0d28c
12 changed files with 371 additions and 7 deletions
|
@ -35,13 +35,13 @@ unless Fog.mocking?
|
||||||
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
|
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
|
||||||
headers.merge!(options)
|
headers.merge!(options)
|
||||||
request({
|
request({
|
||||||
|
:block => block,
|
||||||
:expects => 200,
|
:expects => 200,
|
||||||
:headers => headers,
|
:headers => headers,
|
||||||
:host => "#{bucket_name}.#{@host}",
|
:host => "#{bucket_name}.#{@host}",
|
||||||
:idempotent => true,
|
:idempotent => true,
|
||||||
:method => 'GET',
|
:method => 'GET',
|
||||||
:path => CGI.escape(object_name),
|
:path => CGI.escape(object_name)
|
||||||
:block => block
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,10 @@ module Rackspace
|
||||||
@@connections[service]
|
@@connections[service]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def directories
|
||||||
|
self[:files].directories
|
||||||
|
end
|
||||||
|
|
||||||
def flavors
|
def flavors
|
||||||
self[:servers].flavors
|
self[:servers].flavors
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,12 +4,18 @@ module Fog
|
||||||
|
|
||||||
def self.dependencies
|
def self.dependencies
|
||||||
[
|
[
|
||||||
|
"fog/rackspace/models/files/directory.rb",
|
||||||
|
"fog/rackspace/models/files/directories.rb",
|
||||||
|
"fog/rackspace/models/files/file.rb",
|
||||||
|
"fog/rackspace/models/files/files.rb",
|
||||||
"fog/rackspace/requests/files/delete_container.rb",
|
"fog/rackspace/requests/files/delete_container.rb",
|
||||||
"fog/rackspace/requests/files/delete_object.rb",
|
"fog/rackspace/requests/files/delete_object.rb",
|
||||||
"fog/rackspace/requests/files/get_container.rb",
|
"fog/rackspace/requests/files/get_container.rb",
|
||||||
"fog/rackspace/requests/files/get_containers.rb",
|
"fog/rackspace/requests/files/get_containers.rb",
|
||||||
|
"fog/rackspace/requests/files/get_object.rb",
|
||||||
"fog/rackspace/requests/files/head_container.rb",
|
"fog/rackspace/requests/files/head_container.rb",
|
||||||
"fog/rackspace/requests/files/head_containers.rb",
|
"fog/rackspace/requests/files/head_containers.rb",
|
||||||
|
"fog/rackspace/requests/files/head_object.rb",
|
||||||
"fog/rackspace/requests/files/put_container.rb",
|
"fog/rackspace/requests/files/put_container.rb",
|
||||||
"fog/rackspace/requests/files/put_object.rb"
|
"fog/rackspace/requests/files/put_object.rb"
|
||||||
]
|
]
|
||||||
|
@ -44,12 +50,12 @@ module Fog
|
||||||
metadata[:body] = data
|
metadata[:body] = data
|
||||||
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
|
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
|
||||||
else
|
else
|
||||||
filename = File.basename(data.path)
|
filename = ::File.basename(data.path)
|
||||||
unless (mime_types = MIME::Types.of(filename)).empty?
|
unless (mime_types = MIME::Types.of(filename)).empty?
|
||||||
metadata[:headers]['Content-Type'] = mime_types.first.content_type
|
metadata[:headers]['Content-Type'] = mime_types.first.content_type
|
||||||
end
|
end
|
||||||
metadata[:body] = data.read
|
metadata[:body] = data.read
|
||||||
metadata[:headers]['Content-Length'] = File.size(data.path).to_s
|
metadata[:headers]['Content-Length'] = ::File.size(data.path).to_s
|
||||||
end
|
end
|
||||||
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
||||||
metadata
|
metadata
|
||||||
|
@ -75,7 +81,7 @@ module Fog
|
||||||
response
|
response
|
||||||
end
|
end
|
||||||
|
|
||||||
def storage_request(params)
|
def storage_request(params, parse_json = true)
|
||||||
@storage_connection = Fog::Connection.new("#{@storage_scheme}://#{@storage_host}:#{@storage_port}")
|
@storage_connection = Fog::Connection.new("#{@storage_scheme}://#{@storage_host}:#{@storage_port}")
|
||||||
response = @storage_connection.request({
|
response = @storage_connection.request({
|
||||||
:body => params[:body],
|
:body => params[:body],
|
||||||
|
@ -89,7 +95,7 @@ module Fog
|
||||||
:path => "#{@storage_path}/#{params[:path]}",
|
:path => "#{@storage_path}/#{params[:path]}",
|
||||||
:query => params[:query]
|
:query => params[:query]
|
||||||
})
|
})
|
||||||
unless response.body.empty?
|
if !response.body.empty? && parse_json
|
||||||
response.body = JSON.parse(response.body)
|
response.body = JSON.parse(response.body)
|
||||||
end
|
end
|
||||||
response
|
response
|
||||||
|
|
42
lib/fog/rackspace/models/files/directories.rb
Normal file
42
lib/fog/rackspace/models/files/directories.rb
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Files
|
||||||
|
|
||||||
|
def directories
|
||||||
|
Fog::Rackspace::Files::Directories.new(:connection => self)
|
||||||
|
end
|
||||||
|
|
||||||
|
class Directories < Fog::Collection
|
||||||
|
|
||||||
|
model Fog::Rackspace::Files::Directory
|
||||||
|
|
||||||
|
def all
|
||||||
|
if @loaded
|
||||||
|
clear
|
||||||
|
end
|
||||||
|
@loaded = true
|
||||||
|
data = connection.get_containers.body
|
||||||
|
data.each do |directory|
|
||||||
|
self << new(directory)
|
||||||
|
end
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(name, options = {})
|
||||||
|
data = connection.get_container(name, options).body
|
||||||
|
directory = new(:name => name)
|
||||||
|
directory.files.merge_attributes(options)
|
||||||
|
directory.files.instance_variable_set(:@loaded, true)
|
||||||
|
data.each do |file|
|
||||||
|
directory.files << directory.files.new(file)
|
||||||
|
end
|
||||||
|
directory
|
||||||
|
rescue Excon::Errors::NotFound
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
39
lib/fog/rackspace/models/files/directory.rb
Normal file
39
lib/fog/rackspace/models/files/directory.rb
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Files
|
||||||
|
|
||||||
|
class Directory < Fog::Model
|
||||||
|
|
||||||
|
identity :name
|
||||||
|
|
||||||
|
attribute :bytes
|
||||||
|
attribute :count
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
requires :name
|
||||||
|
connection.delete_container(@name)
|
||||||
|
true
|
||||||
|
rescue Excon::Errors::NotFound
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def files
|
||||||
|
@files ||= begin
|
||||||
|
Fog::Rackspace::Files::Files.new(
|
||||||
|
:directory => self,
|
||||||
|
:connection => connection
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def save
|
||||||
|
requires :name
|
||||||
|
connection.put_container(@name)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
59
lib/fog/rackspace/models/files/file.rb
Normal file
59
lib/fog/rackspace/models/files/file.rb
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Files
|
||||||
|
|
||||||
|
class File < Fog::Model
|
||||||
|
|
||||||
|
identity :key, 'Key'
|
||||||
|
|
||||||
|
attribute :body
|
||||||
|
attribute :content_length, 'Content-Length'
|
||||||
|
attribute :content_type, 'Content-Type'
|
||||||
|
attribute :etag, 'Etag'
|
||||||
|
attribute :last_modified, 'Last-Modified'
|
||||||
|
|
||||||
|
def body
|
||||||
|
@body ||= if last_modified
|
||||||
|
collection.get(identity).body
|
||||||
|
else
|
||||||
|
''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def directory
|
||||||
|
@directory
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
requires :directory, :key
|
||||||
|
connection.delete_object(directory.name, @key)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def owner=(new_owner)
|
||||||
|
if new_owner
|
||||||
|
@owner = {
|
||||||
|
:display_name => new_owner['DisplayName'],
|
||||||
|
:id => new_owner['ID']
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def save(options = {})
|
||||||
|
requires :body, :directory, :key
|
||||||
|
data = connection.put_object(directory.name, @key, @body, options)
|
||||||
|
@etag = data.headers['ETag']
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def directory=(new_directory)
|
||||||
|
@directory = new_directory
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
88
lib/fog/rackspace/models/files/files.rb
Normal file
88
lib/fog/rackspace/models/files/files.rb
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Files
|
||||||
|
|
||||||
|
class Files < Fog::Collection
|
||||||
|
|
||||||
|
attribute :limit
|
||||||
|
attribute :marker
|
||||||
|
attribute :path
|
||||||
|
attribute :prefix
|
||||||
|
|
||||||
|
model Fog::Rackspace::Files::File
|
||||||
|
|
||||||
|
def all(options = {})
|
||||||
|
merge_attributes(options)
|
||||||
|
if @loaded
|
||||||
|
clear
|
||||||
|
end
|
||||||
|
@loaded = true
|
||||||
|
collection = directory.collection.get(
|
||||||
|
directory.name,
|
||||||
|
options
|
||||||
|
)
|
||||||
|
if collection
|
||||||
|
self.replace(collection.files)
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def directory
|
||||||
|
@directory
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(key, options = {}, &block)
|
||||||
|
options = {
|
||||||
|
'limit' => @limit,
|
||||||
|
'marker' => @marker,
|
||||||
|
'path' => @path,
|
||||||
|
'prefix' => @prefix
|
||||||
|
}.merge!(options)
|
||||||
|
data = connection.get_object(directory.name, key, options, &block)
|
||||||
|
file_data = {
|
||||||
|
:body => data.body,
|
||||||
|
:key => key
|
||||||
|
}
|
||||||
|
for key, value in data.headers
|
||||||
|
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
||||||
|
file_data[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
new(file_data)
|
||||||
|
rescue Excon::Errors::NotFound
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_url(key, expires)
|
||||||
|
connection.get_object_url(directory.name, key, expires)
|
||||||
|
end
|
||||||
|
|
||||||
|
def head(key, options = {})
|
||||||
|
data = connection.head_object(directory.name, key, options)
|
||||||
|
file_data = { :key => key }
|
||||||
|
for key, value in data.headers
|
||||||
|
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
||||||
|
file_data[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
new(file_data)
|
||||||
|
rescue Excon::Errors::NotFound
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def new(attributes = {})
|
||||||
|
super({ :directory => directory }.merge!(attributes))
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def directory=(new_directory)
|
||||||
|
@directory = new_directory
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,7 +4,7 @@ unless Fog.mocking?
|
||||||
module Rackspace
|
module Rackspace
|
||||||
class Files
|
class Files
|
||||||
|
|
||||||
# List number of containers and total bytes stored
|
# Get details for container and total bytes stored
|
||||||
#
|
#
|
||||||
# ==== Parameters
|
# ==== Parameters
|
||||||
# * container<~String> - Name of container to retrieve info for
|
# * container<~String> - Name of container to retrieve info for
|
||||||
|
@ -20,6 +20,9 @@ unless Fog.mocking?
|
||||||
# * 'X-Account-Container-Count'<~String> - Count of containers
|
# * 'X-Account-Container-Count'<~String> - Count of containers
|
||||||
# * 'X-Account-Bytes-Used'<~String> - Bytes used
|
# * 'X-Account-Bytes-Used'<~String> - Bytes used
|
||||||
# * body<~Array>:
|
# * body<~Array>:
|
||||||
|
# * 'bytes'<~Integer> - Number of bytes used by container
|
||||||
|
# * 'count'<~Integer> - Number of items in container
|
||||||
|
# * 'name'<~String> - Name of container
|
||||||
# * item<~Hash>:
|
# * item<~Hash>:
|
||||||
# * 'bytes'<~String> - Size of object
|
# * 'bytes'<~String> - Size of object
|
||||||
# * 'content_type'<~String> Content-Type of object
|
# * 'content_type'<~String> Content-Type of object
|
||||||
|
|
41
lib/fog/rackspace/requests/files/get_object.rb
Normal file
41
lib/fog/rackspace/requests/files/get_object.rb
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
unless Fog.mocking?
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Files
|
||||||
|
|
||||||
|
# Get details for object
|
||||||
|
#
|
||||||
|
# ==== Parameters
|
||||||
|
# * container<~String> - Name of container to look in
|
||||||
|
# * object<~String> - Name of object to look for
|
||||||
|
#
|
||||||
|
def get_object(container, object, &block)
|
||||||
|
response = storage_request({
|
||||||
|
:block => block,
|
||||||
|
:expects => 200,
|
||||||
|
:method => 'GET',
|
||||||
|
:path => "#{CGI.escape(container)}/#{CGI.escape(object)}"
|
||||||
|
}, false)
|
||||||
|
response
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Servers
|
||||||
|
|
||||||
|
def get_object(container, object)
|
||||||
|
raise MockNotImplemented.new("Contributions welcome!")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
40
lib/fog/rackspace/requests/files/head_object.rb
Normal file
40
lib/fog/rackspace/requests/files/head_object.rb
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
unless Fog.mocking?
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Files
|
||||||
|
|
||||||
|
# Get headers for object
|
||||||
|
#
|
||||||
|
# ==== Parameters
|
||||||
|
# * container<~String> - Name of container to look in
|
||||||
|
# * object<~String> - Name of object to look for
|
||||||
|
#
|
||||||
|
def head_object(container, object)
|
||||||
|
response = storage_request({
|
||||||
|
:expects => 200,
|
||||||
|
:method => 'GET',
|
||||||
|
:path => "#{CGI.escape(container)}/#{CGI.escape(object)}"
|
||||||
|
}, false)
|
||||||
|
response
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
module Rackspace
|
||||||
|
class Servers
|
||||||
|
|
||||||
|
def head_object(container, object)
|
||||||
|
raise MockNotImplemented.new("Contributions welcome!")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
21
spec/rackspace/requests/files/get_object_spec.rb
Normal file
21
spec/rackspace/requests/files/get_object_spec.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||||
|
|
||||||
|
describe 'Rackspace::Files.get_object' do
|
||||||
|
describe 'success' do
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
Rackspace[:files].put_container('container_name')
|
||||||
|
Rackspace[:files].put_object('container_name', 'object_name', lorem_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:each) do
|
||||||
|
Rackspace[:files].delete_object('container_name', 'object_name')
|
||||||
|
Rackspace[:files].delete_container('container_name')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return proper attributes" do
|
||||||
|
Rackspace[:files].get_object('container_name', 'object_name')
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
21
spec/rackspace/requests/files/head_object_spec.rb
Normal file
21
spec/rackspace/requests/files/head_object_spec.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||||
|
|
||||||
|
describe 'Rackspace::Files.head_object' do
|
||||||
|
describe 'success' do
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
Rackspace[:files].put_container('container_name')
|
||||||
|
Rackspace[:files].put_object('container_name', 'object_name', lorem_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:each) do
|
||||||
|
Rackspace[:files].delete_object('container_name', 'object_name')
|
||||||
|
Rackspace[:files].delete_container('container_name')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return proper attributes" do
|
||||||
|
Rackspace[:files].head_object('container_name', 'object_name')
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue