1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Add request method for put_shared_object. Add model and collection for shared_directory and shared_file.

This commit is contained in:
Rupak Ganguly 2012-10-19 01:33:20 -04:00
parent c87f00392a
commit 16b31facd6
6 changed files with 291 additions and 1 deletions

View file

@ -0,0 +1,49 @@
require 'fog/core/collection'
require 'fog/hp/models/storage/shared_directory'
module Fog
module Storage
class HP
class SharedDirectories < Fog::Collection
model Fog::Storage::HP::SharedDirectory
def head(url)
data = connection.head_shared_container(url)
shared_directory = new(:url => url)
for key, value in data.headers
if ['X-Container-Bytes-Used', 'X-Container-Object-Count'].include?(key)
shared_directory.merge_attributes(key => value)
end
end
shared_directory
rescue Fog::Storage::HP::NotFound, Fog::HP::Errors::Forbidden
nil
end
def get(url)
data = connection.get_shared_container(url)
shared_directory = new(:url => url)
for key, value in data.headers
if ['X-Container-Bytes-Used', 'X-Container-Object-Count'].include?(key)
shared_directory.merge_attributes(key => value)
end
end
# set the files for the directory
shared_directory.files.instance_variable_set(:@loaded, true)
data.body.each do |file|
shared_directory.files << shared_directory.files.new(file)
end
shared_directory
rescue Fog::Storage::HP::NotFound, Fog::HP::Errors::Forbidden
nil
end
end
end
end
end

View file

@ -0,0 +1,28 @@
require 'fog/core/model'
require 'fog/hp/models/storage/shared_files'
module Fog
module Storage
class HP
class SharedDirectory < Fog::Model
identity :url
attribute :bytes, :aliases => 'X-Container-Bytes-Used'
attribute :count, :aliases => 'X-Container-Object-Count'
def files
@files ||= begin
Fog::Storage::HP::SharedFiles.new(
:shared_directory => self,
:connection => connection
)
end
end
end
end
end
end

View file

@ -0,0 +1,57 @@
require 'fog/core/model'
module Fog
module Storage
class HP
class SharedFile < Fog::Model
identity :key, :aliases => 'name'
attribute :url
attribute :content_length, :aliases => ['bytes', 'Content-Length'], :type => :integer
attribute :content_type, :aliases => ['content_type', 'Content-Type']
attribute :etag, :aliases => ['hash', 'Etag']
attribute :last_modified, :aliases => ['last_modified', 'Last-Modified'], :type => :time
def url
"#{self.collection.shared_directory.url}/#{key}"
end
def body
attributes[:body] ||= if last_modified
collection.get(identity).body
else
''
end
end
def body=(new_body)
attributes[:body] = new_body
end
def shared_directory
@shared_directory
end
def save(options = {})
requires :shared_directory, :key
options['Content-Type'] = content_type if content_type
data = connection.put_shared_object(shared_directory.url, key, body, options)
merge_attributes(data.headers)
self.content_length = Fog::Storage.get_body_size(body)
true
rescue Fog::Storage::HP::NotFound, Fog::HP::Errors::Forbidden
false
end
private
def shared_directory=(new_shared_directory)
@shared_directory = new_shared_directory
end
end
end
end
end

View file

@ -0,0 +1,60 @@
require 'fog/core/collection'
require 'fog/hp/models/storage/shared_file'
module Fog
module Storage
class HP
class SharedFiles < Fog::Collection
attribute :shared_directory
model Fog::Storage::HP::SharedFile
def all
requires :shared_directory
parent = shared_directory.collection.get(shared_directory.url)
if parent
load(parent.files.map {|file| file.attributes})
else
nil
end
rescue Fog::Storage::HP::NotFound, Fog::HP::Errors::Forbidden
nil
end
def get(key, &block)
requires :shared_directory
shared_object_url = "#{shared_directory.url}/#{key}"
data = connection.get_shared_object(shared_object_url, &block)
file_data = data.headers.merge({
:body => data.body,
:key => key
})
new(file_data)
rescue Fog::Storage::HP::NotFound, Fog::HP::Errors::Forbidden
nil
end
def head(key)
requires :shared_directory
shared_object_url = "#{shared_directory.url}/#{key}"
data = connection.head_shared_object(shared_object_url)
file_data = data.headers.merge({
:key => key
})
new(file_data)
rescue Fog::Storage::HP::NotFound, Fog::HP::Errors::Forbidden
nil
end
def new(attributes = {})
requires :shared_directory
super({ :shared_directory => shared_directory }.merge!(attributes))
end
end
end
end
end

View file

@ -0,0 +1,85 @@
module Fog
module Storage
class HP
class Real
# Create a new object in a shared container
#
# ==== Parameters
# * shared_container_url<~String> - Shared url for the container
# * object<~String> - Name of the object
# * options<~Hash> - header options
#
def put_shared_object(shared_container_url, object_name, data, options = {}, &block)
# split up the shared object url
uri = URI.parse(shared_container_url)
path = uri.path
data = Fog::Storage.parse_data(data)
headers = data[:headers].merge!(options)
if block_given?
headers['Transfer-Encoding'] = 'chunked'
headers.delete('Content-Length')
return shared_request(
:request_block => block,
:expects => 201,
:headers => headers,
:method => 'PUT',
:path => "#{path}/#{Fog::HP.escape(object_name)}"
)
end
if headers.has_key?('Transfer-Encoding')
headers.delete('Content-Length')
end
response = shared_request(
:body => data[:body],
:expects => 201,
:headers => headers,
:method => 'PUT',
:path => "#{path}/#{Fog::HP.escape(object_name)}"
)
response
end
end
class Mock # :nodoc:all
def put_shared_object(shared_container_url, object_name, data, options = {}, &block)
response = Excon::Response.new
data = Fog::Storage.parse_data(data)
unless data[:body].is_a?(String)
data[:body] = data[:body].read
end
response.status = 201
object = {
:body => data[:body],
'Content-Type' => options['Content-Type'] || data[:headers]['Content-Type'],
'ETag' => Fog::HP::Mock.etag,
'Key' => object_name,
'Date' => Fog::Time.now.to_date_header,
'Content-Length' => options['Content-Length'] || data[:headers]['Content-Length'],
}
for key, value in options
case key
when 'Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-MD5', 'Expires', /^X-Object-Meta-/
object[key] = value
end
end
response.headers = {
'Content-Length' => object['Content-Length'],
'Content-Type' => object['Content-Type'],
'ETag' => object['ETag'],
'Date' => object['Date']
}
response
end
end
end
end
end

View file

@ -11,8 +11,12 @@ module Fog
model_path 'fog/hp/models/storage'
model :directory
collection :directories
model :shared_directory
collection :shared_directories
model :file
collection :files
model :shared_file
collection :shared_files
request_path 'fog/hp/requests/storage'
request :delete_container
@ -30,6 +34,7 @@ module Fog
request :head_shared_object
request :put_container
request :put_object
request :put_shared_object
module Utils
@ -295,12 +300,18 @@ module Fog
raise case error
when Excon::Errors::NotFound
Fog::Storage::HP::NotFound.slurp(error)
when Excon::Errors::Forbidden
Fog::HP::Errors::Forbidden.slurp(error)
else
error
end
end
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
response.body = MultiJson.decode(response.body)
begin
response.body = MultiJson.decode(response.body)
rescue MultiJson::DecodeError => error
response.body #### the body is not in JSON format so just return it as it is
end
end
response
end