mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace|storage] added support for container metadata; added directory tests
This commit is contained in:
parent
361d278347
commit
7ea22bb60b
6 changed files with 162 additions and 4 deletions
|
@ -26,8 +26,11 @@ module Fog
|
|||
directory.merge_attributes(key => value)
|
||||
end
|
||||
end
|
||||
|
||||
directory.metadata = Metadata.from_headers(data.headers)
|
||||
directory.files.merge_attributes(options)
|
||||
directory.files.instance_variable_set(:@loaded, true)
|
||||
|
||||
data.body.each do |file|
|
||||
directory.files << directory.files.new(file)
|
||||
end
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'fog/core/model'
|
||||
require 'fog/rackspace/models/storage/files'
|
||||
require 'fog/rackspace/models/storage/metadata'
|
||||
|
||||
module Fog
|
||||
module Storage
|
||||
|
@ -9,10 +10,23 @@ module Fog
|
|||
|
||||
identity :key, :aliases => 'name'
|
||||
|
||||
attribute :bytes, :aliases => 'X-Container-Bytes-Used'
|
||||
attribute :count, :aliases => 'X-Container-Object-Count'
|
||||
attribute :bytes, :aliases => 'X-Container-Bytes-Used', :type => :integer
|
||||
attribute :count, :aliases => 'X-Container-Object-Count', :type => :integer
|
||||
attribute :cdn_cname
|
||||
|
||||
def metadata=(hash)
|
||||
if hash.is_a? Fog::Storage::Rackspace::Metadata
|
||||
@metadata = hash
|
||||
else
|
||||
@metadata = Fog::Storage::Rackspace::Metadata.new(hash)
|
||||
end
|
||||
@metadata
|
||||
end
|
||||
|
||||
def metadata
|
||||
@metadata ||= Fog::Storage::Rackspace::Metadata.new
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :key
|
||||
service.delete_container(key)
|
||||
|
@ -58,7 +72,7 @@ module Fog
|
|||
|
||||
def save
|
||||
requires :key
|
||||
service.put_container(key)
|
||||
service.put_container(key, metadata.to_headers)
|
||||
|
||||
if service.cdn && public?
|
||||
# if public and CDN connection then update cdn to public
|
||||
|
|
68
lib/fog/rackspace/models/storage/metadata.rb
Normal file
68
lib/fog/rackspace/models/storage/metadata.rb
Normal file
|
@ -0,0 +1,68 @@
|
|||
module Fog
|
||||
module Storage
|
||||
class Rackspace
|
||||
|
||||
class Metadata
|
||||
|
||||
# META_PREFIX = "X-Object-Meta-"
|
||||
# REMOVE_META_PREFIX = "X-Remove-Object-Meta-"
|
||||
META_PREFIX = "X-Container-Meta-"
|
||||
REMOVE_META_PREFIX = "X-Remove-Container-Meta-"
|
||||
|
||||
KEY_REGEX = /^#{META_PREFIX}(.*)/
|
||||
|
||||
attr_reader :data
|
||||
|
||||
def initialize(hash={})
|
||||
@data = hash || {}
|
||||
end
|
||||
|
||||
def to_headers
|
||||
headers = {}
|
||||
@data.each_pair do |k,v|
|
||||
key = to_header_key(k,v)
|
||||
headers[key] = v
|
||||
end
|
||||
headers
|
||||
end
|
||||
|
||||
def self.from_headers(headers)
|
||||
metadata = Metadata.new
|
||||
headers.each_pair do |k, v|
|
||||
key = Metadata.to_key(k)
|
||||
next unless key
|
||||
metadata.data[key] = v
|
||||
end
|
||||
metadata
|
||||
end
|
||||
|
||||
def respond_to?(method_sym, include_private = false)
|
||||
super(method_sym, include_private) || @data.method_missing(method_missing, include_private)
|
||||
end
|
||||
|
||||
def method_missing(method, *args, &block)
|
||||
@data.send(method, *args, &block)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.to_key(key)
|
||||
m = key.match KEY_REGEX
|
||||
return nil unless m && m[1]
|
||||
|
||||
a = m[1].split('-')
|
||||
a.collect!(&:downcase)
|
||||
str = a.join('_')
|
||||
str.to_sym
|
||||
end
|
||||
|
||||
def to_header_key(key, value)
|
||||
prefix = value.nil? ? REMOVE_META_PREFIX : META_PREFIX
|
||||
prefix + key.to_s.split(/[-_]/).collect(&:capitalize).join('-')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,10 +8,11 @@ module Fog
|
|||
# ==== Parameters
|
||||
# * name<~String> - Name for container, should be < 256 bytes and must not contain '/'
|
||||
#
|
||||
def put_container(name)
|
||||
def put_container(name, options={})
|
||||
request(
|
||||
:expects => [201, 202],
|
||||
:method => 'PUT',
|
||||
:headers => options,
|
||||
:path => Fog::Rackspace.escape(name)
|
||||
)
|
||||
end
|
||||
|
|
29
tests/rackspace/models/storage/directories_tests.rb
Normal file
29
tests/rackspace/models/storage/directories_tests.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
Shindo.tests('Fog::Rackspace::Storage | directories', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
@service = Fog::Storage[:rackspace]
|
||||
|
||||
|
||||
begin
|
||||
@name = "fog-directories-test-#{Time.now.to_i.to_s}"
|
||||
@filename = 'lorem.txt'
|
||||
@dir = @service.directories.create :key => @name, :metadata => {:fog_test => true}
|
||||
@file = @dir.files.create :key => @filename, :body => lorem_file
|
||||
|
||||
tests('#get').succeeds do
|
||||
instance = @service.directories.get @name
|
||||
returns(false) { instance.nil? }
|
||||
returns('true') { instance.metadata[:fog_test] }
|
||||
returns(@name) { instance.key }
|
||||
returns(1) { instance.count }
|
||||
returns( Fog::Storage.get_body_size(lorem_file)) {instance.bytes }
|
||||
returns(@filename) { instance.files.first.key }
|
||||
end
|
||||
|
||||
ensure
|
||||
@file.destroy if @file
|
||||
@dir.destroy if @dir
|
||||
end
|
||||
|
||||
end
|
43
tests/rackspace/models/storage/directory_tests.rb
Normal file
43
tests/rackspace/models/storage/directory_tests.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
Shindo.tests('Fog::Rackspace::Storage | directory', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
@service = Fog::Storage[:rackspace]
|
||||
|
||||
def container_meta_attributes
|
||||
@service.head_container(@instance.key).headers.reject {|k, v| !(k =~ /X-Container-Meta-/)}
|
||||
end
|
||||
|
||||
directory_attributes = {
|
||||
# Add a random suffix to prevent collision
|
||||
:key => "fog-directory-tests-#{rand(65536)}"
|
||||
}
|
||||
|
||||
model_tests(@service.directories, directory_attributes, Fog.mocking?) do
|
||||
tests('#public_url').returns(nil) do
|
||||
@instance.public_url
|
||||
end
|
||||
end
|
||||
|
||||
directory_attributes[:metadata] = {:draft => 'true'}
|
||||
|
||||
tests('metadata') do
|
||||
pending if Fog.mocking?
|
||||
model_tests(@service.directories, directory_attributes, Fog.mocking?) do
|
||||
tests('sets metadata on create').returns({:draft => 'true'}) do
|
||||
@instance.metadata.data
|
||||
end
|
||||
tests('update metadata').returns({"X-Container-Meta-Draft"=>"true", "X-Container-Meta-Color"=>"green"}) do
|
||||
@instance.metadata[:color] = 'green'
|
||||
@instance.save
|
||||
container_meta_attributes
|
||||
end
|
||||
tests('delete metadata').returns({"X-Container-Meta-Draft"=>"true"}) do
|
||||
@instance.metadata[:color] = nil
|
||||
@instance.save
|
||||
container_meta_attributes
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue