mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
fixes/specs for s3 buckets and bucket models
This commit is contained in:
parent
37f7339df8
commit
1b14059d95
9 changed files with 205 additions and 93 deletions
|
@ -21,6 +21,8 @@ module Fog
|
|||
connection.delete_bucket(name)
|
||||
buckets.delete(name)
|
||||
true
|
||||
rescue Fog::Errors::NotFound
|
||||
false
|
||||
end
|
||||
|
||||
def location
|
||||
|
@ -49,10 +51,6 @@ module Fog
|
|||
@payer = new_payer
|
||||
end
|
||||
|
||||
def new_record?
|
||||
buckets.key?(name)
|
||||
end
|
||||
|
||||
def reload
|
||||
buckets.get(name)
|
||||
end
|
||||
|
|
|
@ -8,23 +8,18 @@ module Fog
|
|||
|
||||
class Buckets < Fog::Collection
|
||||
|
||||
def [](name)
|
||||
self[name] ||= begin
|
||||
get(name)
|
||||
end
|
||||
end
|
||||
|
||||
def all
|
||||
data = connection.get_service.body
|
||||
owner = Fog::AWS::S3::Owner.new(data.delete('Owner').merge!(:connection => connection))
|
||||
buckets = Fog::AWS::S3::Buckets.new(:connection => connection)
|
||||
data['Buckets'].each do |bucket|
|
||||
self[bucket['Name']] = Fog::AWS::S3::Bucket.new({
|
||||
buckets[bucket['Name']] = Fog::AWS::S3::Bucket.new({
|
||||
:buckets => buckets,
|
||||
:connection => connection,
|
||||
:owner => owner
|
||||
}.merge!(bucket))
|
||||
end
|
||||
self
|
||||
buckets
|
||||
end
|
||||
|
||||
def create(attributes = {})
|
||||
|
@ -34,39 +29,43 @@ module Fog
|
|||
end
|
||||
|
||||
def get(name, options = {})
|
||||
remap_attributes(options, {
|
||||
:is_truncated => 'IsTruncated',
|
||||
:marker => 'Marker',
|
||||
:max_keys => 'MaxKeys',
|
||||
:prefix => 'Prefix'
|
||||
})
|
||||
data = connection.get_bucket(name, options).body
|
||||
bucket = Fog::AWS::S3::Bucket.new({
|
||||
:buckets => self,
|
||||
:connection => connection,
|
||||
:name => data['Name']
|
||||
})
|
||||
self[bucket.name] = bucket
|
||||
objects_data = {}
|
||||
for key, value in data
|
||||
if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
|
||||
objects_data[key] = value
|
||||
end
|
||||
end
|
||||
objects = Fog::AWS::S3::Objects.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection
|
||||
}.merge!(objects_data))
|
||||
data['Contents'].each do |object|
|
||||
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
|
||||
objects[object['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
self[name] ||= begin
|
||||
remap_attributes(options, {
|
||||
:is_truncated => 'IsTruncated',
|
||||
:marker => 'Marker',
|
||||
:max_keys => 'MaxKeys',
|
||||
:prefix => 'Prefix'
|
||||
})
|
||||
data = connection.get_bucket(name, options).body
|
||||
bucket = Fog::AWS::S3::Bucket.new({
|
||||
:buckets => self,
|
||||
:connection => connection,
|
||||
:objects => self,
|
||||
:owner => owner
|
||||
}.merge!(object))
|
||||
:name => data['Name']
|
||||
})
|
||||
self[bucket.name] = bucket
|
||||
objects_data = {}
|
||||
for key, value in data
|
||||
if ['IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
|
||||
objects_data[key] = value
|
||||
end
|
||||
end
|
||||
objects = Fog::AWS::S3::Objects.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection
|
||||
}.merge!(objects_data))
|
||||
data['Contents'].each do |object|
|
||||
owner = Fog::AWS::S3::Owner.new(object.delete('Owner').merge!(:connection => connection))
|
||||
objects[object['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:objects => self,
|
||||
:owner => owner
|
||||
}.merge!(object))
|
||||
end
|
||||
bucket
|
||||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
bucket
|
||||
end
|
||||
|
||||
def new(attributes = {})
|
||||
|
|
|
@ -38,10 +38,8 @@ module Fog
|
|||
connection.delete_object(bucket, key)
|
||||
objects.delete(key)
|
||||
true
|
||||
end
|
||||
|
||||
def new_record?
|
||||
objects.key?(key)
|
||||
rescue Fog::Errors::NotFound
|
||||
false
|
||||
end
|
||||
|
||||
def reload
|
||||
|
|
|
@ -13,12 +13,6 @@ module Fog
|
|||
super
|
||||
end
|
||||
|
||||
def [](key)
|
||||
self[key] ||= begin
|
||||
get(key)
|
||||
end
|
||||
end
|
||||
|
||||
def all(options = {})
|
||||
merge_attributes(options)
|
||||
bucket.buckets.get(bucket.name, attributes).objects
|
||||
|
@ -35,33 +29,45 @@ module Fog
|
|||
end
|
||||
|
||||
def get(key, options = {})
|
||||
data = connection.get_object(bucket.name, key, options)
|
||||
object_data = { :body => data.body}
|
||||
for key, value in data.headers
|
||||
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
||||
object_data[key] = value
|
||||
if self[key] && self[key].body
|
||||
self[key]
|
||||
else
|
||||
self[key] ||= begin
|
||||
data = connection.get_object(bucket.name, key, options)
|
||||
object_data = { :body => data.body}
|
||||
for key, value in data.headers
|
||||
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
||||
object_data[key] = value
|
||||
end
|
||||
end
|
||||
self[object_data['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:objects => self
|
||||
}.merge!(object_data))
|
||||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
self[object_data['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:objects => self
|
||||
}.merge!(object_data))
|
||||
end
|
||||
|
||||
def head(key, options = {})
|
||||
data = connection.head_object(bucket.name, key, options)
|
||||
object_data = {}
|
||||
for key, value in data.headers
|
||||
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
||||
object_data[key] = value
|
||||
self[key] ||= begin
|
||||
data = connection.head_object(bucket.name, key, options)
|
||||
object_data = {}
|
||||
for key, value in data.headers
|
||||
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
||||
object_data[key] = value
|
||||
end
|
||||
end
|
||||
self[object_data['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:objects => self
|
||||
}.merge!(object_data))
|
||||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
self[object_data['key']] = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:objects => self
|
||||
}.merge!(object_data))
|
||||
end
|
||||
|
||||
def new(attributes = {})
|
||||
|
|
|
@ -20,13 +20,13 @@ module Fog
|
|||
end
|
||||
|
||||
def initialize(attributes = {})
|
||||
update_attributes(attributes)
|
||||
merge_attributes(attributes)
|
||||
end
|
||||
|
||||
def inspect
|
||||
data = "#<#{self.class.name}"
|
||||
for attribute in (self.instance_variables - ['@connection'])
|
||||
data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}"
|
||||
for attribute in self.class.attributes
|
||||
data << " #{attribute}=#{send(attribute).inspect}"
|
||||
end
|
||||
data << " ["
|
||||
for key, value in self
|
||||
|
@ -38,7 +38,7 @@ module Fog
|
|||
|
||||
def attributes
|
||||
attributes = {}
|
||||
for attribute in self.attributes
|
||||
for attribute in self.class.attributes
|
||||
attributes[attribute] = send(:"#{attribute}")
|
||||
end
|
||||
attributes
|
||||
|
@ -46,7 +46,7 @@ module Fog
|
|||
|
||||
def merge_attributes(new_attributes = {})
|
||||
for key, value in new_attributes
|
||||
if aliased_key = self.aliases[key]
|
||||
if aliased_key = self.class.aliases[key]
|
||||
send(:"#{aliased_key}=", value)
|
||||
else
|
||||
send(:"#{key}=", value)
|
||||
|
|
|
@ -25,7 +25,7 @@ module Fog
|
|||
|
||||
def attributes
|
||||
attributes = {}
|
||||
for attribute in self.attributes
|
||||
for attribute in self.class.attributes
|
||||
attributes[attribute] = send(:"#{attribute}")
|
||||
end
|
||||
attributes
|
||||
|
@ -33,15 +33,15 @@ module Fog
|
|||
|
||||
def inspect
|
||||
data = "#<#{self.class.name}"
|
||||
for attribute in (self.instance_variables - ['@connection'])
|
||||
data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}"
|
||||
for attribute in self.class.attributes
|
||||
data << " #{attribute}=#{send(attribute).inspect}"
|
||||
end
|
||||
data << ">"
|
||||
end
|
||||
|
||||
def merge_attributes(new_attributes = {})
|
||||
for key, value in new_attributes
|
||||
if aliased_key = self.aliases[key]
|
||||
if aliased_key = self.class.aliases[key]
|
||||
send(:"#{aliased_key}=", value)
|
||||
else
|
||||
send(:"#{key}=", value)
|
||||
|
|
|
@ -1,16 +1,44 @@
|
|||
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||
|
||||
describe 'S3::Bucket' do
|
||||
describe 'Fog::AWS::S3::Bucket' do
|
||||
|
||||
describe "#initialize" do
|
||||
|
||||
it "should return an S3::Bucket"
|
||||
it "should return a Fog:AWS::S3::Bucket" do
|
||||
s3.buckets.new.should be_an(Fog::AWS::S3::Bucket)
|
||||
end
|
||||
|
||||
it "should remap attributes from parser" do
|
||||
now = Time.now
|
||||
bucket = s3.buckets.new(
|
||||
'CreationDate' => now,
|
||||
'Name' => 'bucketname'
|
||||
)
|
||||
bucket.creation_date.should == now
|
||||
bucket.name.should == 'bucketname'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#delete" do
|
||||
describe "#buckets" do
|
||||
|
||||
it "should return the success value"
|
||||
it "should return a Fog::AWS::S3::Buckets" do
|
||||
s3.buckets.new.buckets.should be_a(Fog::AWS::S3::Buckets)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#destroy" do
|
||||
|
||||
it "should return true if the object is deleted" do
|
||||
bucket = s3.buckets.create(:name => 'fogmodelbucket')
|
||||
bucket.destroy.should be_true
|
||||
end
|
||||
|
||||
it "should return false if the object does not exist" do
|
||||
bucket = s3.buckets.new(:name => 'fogmodelbucket')
|
||||
bucket.destroy.should be_false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
@ -22,7 +50,10 @@ describe 'S3::Bucket' do
|
|||
|
||||
describe "#objects" do
|
||||
|
||||
it "should return an S3::Objects"
|
||||
it "should return a Fog::AWS::S3::Objects" do
|
||||
bucket = s3.buckets.new(:name => 'fogmodelbucket')
|
||||
bucket.objects.should be_an(Fog::AWS::S3::Objects)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
@ -38,9 +69,47 @@ describe 'S3::Bucket' do
|
|||
|
||||
end
|
||||
|
||||
describe "#reload" do
|
||||
|
||||
before(:each) do
|
||||
@bucket = s3.buckets.create(:name => 'fogmodelbucket')
|
||||
@reloaded = @bucket.reload
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
@bucket.destroy
|
||||
end
|
||||
|
||||
it "should return a Fog::AWS::S3::Bucket" do
|
||||
@reloaded.should be_a(Fog::AWS::S3::Bucket)
|
||||
end
|
||||
|
||||
it "should have the same attributes as the bucket that created it" do
|
||||
@bucket.attributes.should == @reloaded.attributes
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#save" do
|
||||
|
||||
it "should return the success value"
|
||||
before(:each) do
|
||||
@bucket = s3.buckets.new(:name => 'fogmodelbucket')
|
||||
end
|
||||
|
||||
it "should return true when it succeeds" do
|
||||
@bucket.save.should be_true
|
||||
@bucket.destroy
|
||||
end
|
||||
|
||||
it "should not exist in buckets before save" do
|
||||
@bucket.buckets.key?(@bucket.name).should be_false
|
||||
end
|
||||
|
||||
it "should exist in buckets after save" do
|
||||
@bucket.save
|
||||
@bucket.buckets.key?(@bucket.name).should be_true
|
||||
@bucket.destroy
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
|
@ -1,22 +1,64 @@
|
|||
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||
|
||||
describe 'S3::Buckets' do
|
||||
describe 'Fog::AWS::S3::Buckets' do
|
||||
|
||||
describe "#all" do
|
||||
|
||||
it "should return an S3::Buckets"
|
||||
it "should return a Fog::AWS::S3::Buckets" do
|
||||
s3.buckets.all.should be_a(Fog::AWS::S3::Buckets)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#create" do
|
||||
|
||||
it "should return an S3::Bucket that has been persisted to s3"
|
||||
before(:each) do
|
||||
@bucket = s3.buckets.create(:name => 'fogbucketname')
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
@bucket.destroy
|
||||
end
|
||||
|
||||
it "should return a Fog::AWS::S3::Bucket" do
|
||||
@bucket.should be_a(Fog::AWS::S3::Bucket)
|
||||
end
|
||||
|
||||
it "should exist on s3" do
|
||||
s3.buckets.get(@bucket.name).should_not be_nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#get" do
|
||||
|
||||
it "should return a Fog::AWS::S3::Bucket if a matching bucket exists" do
|
||||
bucket = s3.buckets.create(:name => 'fogbucketname')
|
||||
get = s3.buckets.get('fogbucketname')
|
||||
bucket.attributes.should == get.attributes
|
||||
bucket.destroy
|
||||
end
|
||||
|
||||
it "should return memoized bucket on subsequent gets" do
|
||||
bucket = s3.buckets.create(:name => 'fogbucketname')
|
||||
get1 = s3.buckets.get('fogbucketname')
|
||||
s3.should_not_receive(:get_bucket)
|
||||
get2 = s3.buckets.get('fogbucketname')
|
||||
get1.attributes.should == get2.attributes
|
||||
bucket.destroy
|
||||
end
|
||||
|
||||
it "should return nil if no matching bucket exists" do
|
||||
s3.buckets.get('fogbucketname').should be_nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#new" do
|
||||
|
||||
it "should return an S3::Bucket"
|
||||
it "should return a Fog::AWS::S3::Bucket" do
|
||||
s3.buckets.new.should be_a(Fog::AWS::S3::Bucket)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ describe 'S3::Object' do
|
|||
|
||||
end
|
||||
|
||||
describe "#delete" do
|
||||
describe "#destroy" do
|
||||
|
||||
it "should return the success value"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue