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)
|
connection.delete_bucket(name)
|
||||||
buckets.delete(name)
|
buckets.delete(name)
|
||||||
true
|
true
|
||||||
|
rescue Fog::Errors::NotFound
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def location
|
def location
|
||||||
|
@ -49,10 +51,6 @@ module Fog
|
||||||
@payer = new_payer
|
@payer = new_payer
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_record?
|
|
||||||
buckets.key?(name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def reload
|
def reload
|
||||||
buckets.get(name)
|
buckets.get(name)
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,23 +8,18 @@ module Fog
|
||||||
|
|
||||||
class Buckets < Fog::Collection
|
class Buckets < Fog::Collection
|
||||||
|
|
||||||
def [](name)
|
|
||||||
self[name] ||= begin
|
|
||||||
get(name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def all
|
def all
|
||||||
data = connection.get_service.body
|
data = connection.get_service.body
|
||||||
owner = Fog::AWS::S3::Owner.new(data.delete('Owner').merge!(:connection => connection))
|
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|
|
data['Buckets'].each do |bucket|
|
||||||
self[bucket['Name']] = Fog::AWS::S3::Bucket.new({
|
buckets[bucket['Name']] = Fog::AWS::S3::Bucket.new({
|
||||||
:buckets => buckets,
|
:buckets => buckets,
|
||||||
:connection => connection,
|
:connection => connection,
|
||||||
:owner => owner
|
:owner => owner
|
||||||
}.merge!(bucket))
|
}.merge!(bucket))
|
||||||
end
|
end
|
||||||
self
|
buckets
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(attributes = {})
|
def create(attributes = {})
|
||||||
|
@ -34,39 +29,43 @@ module Fog
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(name, options = {})
|
def get(name, options = {})
|
||||||
remap_attributes(options, {
|
self[name] ||= begin
|
||||||
:is_truncated => 'IsTruncated',
|
remap_attributes(options, {
|
||||||
:marker => 'Marker',
|
:is_truncated => 'IsTruncated',
|
||||||
:max_keys => 'MaxKeys',
|
:marker => 'Marker',
|
||||||
:prefix => 'Prefix'
|
:max_keys => 'MaxKeys',
|
||||||
})
|
:prefix => 'Prefix'
|
||||||
data = connection.get_bucket(name, options).body
|
})
|
||||||
bucket = Fog::AWS::S3::Bucket.new({
|
data = connection.get_bucket(name, options).body
|
||||||
:buckets => self,
|
bucket = Fog::AWS::S3::Bucket.new({
|
||||||
:connection => connection,
|
:buckets => self,
|
||||||
: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,
|
:connection => connection,
|
||||||
:objects => self,
|
:name => data['Name']
|
||||||
:owner => owner
|
})
|
||||||
}.merge!(object))
|
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
|
end
|
||||||
bucket
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def new(attributes = {})
|
def new(attributes = {})
|
||||||
|
|
|
@ -38,10 +38,8 @@ module Fog
|
||||||
connection.delete_object(bucket, key)
|
connection.delete_object(bucket, key)
|
||||||
objects.delete(key)
|
objects.delete(key)
|
||||||
true
|
true
|
||||||
end
|
rescue Fog::Errors::NotFound
|
||||||
|
false
|
||||||
def new_record?
|
|
||||||
objects.key?(key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload
|
def reload
|
||||||
|
|
|
@ -13,12 +13,6 @@ module Fog
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](key)
|
|
||||||
self[key] ||= begin
|
|
||||||
get(key)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def all(options = {})
|
def all(options = {})
|
||||||
merge_attributes(options)
|
merge_attributes(options)
|
||||||
bucket.buckets.get(bucket.name, attributes).objects
|
bucket.buckets.get(bucket.name, attributes).objects
|
||||||
|
@ -35,33 +29,45 @@ module Fog
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(key, options = {})
|
def get(key, options = {})
|
||||||
data = connection.get_object(bucket.name, key, options)
|
if self[key] && self[key].body
|
||||||
object_data = { :body => data.body}
|
self[key]
|
||||||
for key, value in data.headers
|
else
|
||||||
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
self[key] ||= begin
|
||||||
object_data[key] = value
|
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
|
||||||
end
|
end
|
||||||
self[object_data['key']] = Fog::AWS::S3::Object.new({
|
|
||||||
:bucket => bucket,
|
|
||||||
:connection => connection,
|
|
||||||
:objects => self
|
|
||||||
}.merge!(object_data))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def head(key, options = {})
|
def head(key, options = {})
|
||||||
data = connection.head_object(bucket.name, key, options)
|
self[key] ||= begin
|
||||||
object_data = {}
|
data = connection.head_object(bucket.name, key, options)
|
||||||
for key, value in data.headers
|
object_data = {}
|
||||||
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
for key, value in data.headers
|
||||||
object_data[key] = value
|
if ['Content-Length', 'Content-Type', 'ETag', 'Last-Modified'].include?(key)
|
||||||
|
object_data[key] = value
|
||||||
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
def new(attributes = {})
|
def new(attributes = {})
|
||||||
|
|
|
@ -20,13 +20,13 @@ module Fog
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(attributes = {})
|
def initialize(attributes = {})
|
||||||
update_attributes(attributes)
|
merge_attributes(attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
data = "#<#{self.class.name}"
|
data = "#<#{self.class.name}"
|
||||||
for attribute in (self.instance_variables - ['@connection'])
|
for attribute in self.class.attributes
|
||||||
data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}"
|
data << " #{attribute}=#{send(attribute).inspect}"
|
||||||
end
|
end
|
||||||
data << " ["
|
data << " ["
|
||||||
for key, value in self
|
for key, value in self
|
||||||
|
@ -38,7 +38,7 @@ module Fog
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
attributes = {}
|
attributes = {}
|
||||||
for attribute in self.attributes
|
for attribute in self.class.attributes
|
||||||
attributes[attribute] = send(:"#{attribute}")
|
attributes[attribute] = send(:"#{attribute}")
|
||||||
end
|
end
|
||||||
attributes
|
attributes
|
||||||
|
@ -46,7 +46,7 @@ module Fog
|
||||||
|
|
||||||
def merge_attributes(new_attributes = {})
|
def merge_attributes(new_attributes = {})
|
||||||
for key, value in 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)
|
send(:"#{aliased_key}=", value)
|
||||||
else
|
else
|
||||||
send(:"#{key}=", value)
|
send(:"#{key}=", value)
|
||||||
|
|
|
@ -25,7 +25,7 @@ module Fog
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
attributes = {}
|
attributes = {}
|
||||||
for attribute in self.attributes
|
for attribute in self.class.attributes
|
||||||
attributes[attribute] = send(:"#{attribute}")
|
attributes[attribute] = send(:"#{attribute}")
|
||||||
end
|
end
|
||||||
attributes
|
attributes
|
||||||
|
@ -33,15 +33,15 @@ module Fog
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
data = "#<#{self.class.name}"
|
data = "#<#{self.class.name}"
|
||||||
for attribute in (self.instance_variables - ['@connection'])
|
for attribute in self.class.attributes
|
||||||
data << " #{attribute}=#{send(attribute[1..-1].to_sym).inspect}"
|
data << " #{attribute}=#{send(attribute).inspect}"
|
||||||
end
|
end
|
||||||
data << ">"
|
data << ">"
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge_attributes(new_attributes = {})
|
def merge_attributes(new_attributes = {})
|
||||||
for key, value in 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)
|
send(:"#{aliased_key}=", value)
|
||||||
else
|
else
|
||||||
send(:"#{key}=", value)
|
send(:"#{key}=", value)
|
||||||
|
|
|
@ -1,16 +1,44 @@
|
||||||
require File.dirname(__FILE__) + '/../../../spec_helper'
|
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||||
|
|
||||||
describe 'S3::Bucket' do
|
describe 'Fog::AWS::S3::Bucket' do
|
||||||
|
|
||||||
describe "#initialize" 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
|
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
|
end
|
||||||
|
|
||||||
|
@ -22,7 +50,10 @@ describe 'S3::Bucket' do
|
||||||
|
|
||||||
describe "#objects" 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
|
end
|
||||||
|
|
||||||
|
@ -38,9 +69,47 @@ describe 'S3::Bucket' do
|
||||||
|
|
||||||
end
|
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
|
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,64 @@
|
||||||
require File.dirname(__FILE__) + '/../../../spec_helper'
|
require File.dirname(__FILE__) + '/../../../spec_helper'
|
||||||
|
|
||||||
describe 'S3::Buckets' do
|
describe 'Fog::AWS::S3::Buckets' do
|
||||||
|
|
||||||
describe "#all" 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
|
end
|
||||||
|
|
||||||
describe "#create" do
|
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
|
end
|
||||||
|
|
||||||
describe "#new" do
|
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ describe 'S3::Object' do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#delete" do
|
describe "#destroy" do
|
||||||
|
|
||||||
it "should return the success value"
|
it "should return the success value"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue