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

Merge pull request #1476 from estonfer/bucket_lifecycle

adds support for bucket transitioning/fixes bucket lifecycle management
This commit is contained in:
Eric Stonfer 2013-01-16 08:35:32 -08:00
commit 263e65f5a5
4 changed files with 104 additions and 20 deletions

View file

@ -6,26 +6,60 @@ module Fog
class GetBucketLifecycle < Fog::Parsers::Base
def reset
@expiration = {}
@transition = {}
@rule = {}
@response = { 'Rules' => [] }
end
def start_element(name, attrs=[])
super
case name
when 'Expiration'
@in_expiration = true
when 'Transition'
@in_transition = true
end
end
def end_element(name)
if @in_expiration
case name
when 'Days'
@expiration[name] = value.to_i
when 'Date'
@expiration[name] = value
when 'Expiration'
@rule['Expiration'] = @expiration
@in_expiration = false
@expiration = {}
end
elsif @in_transition
case name
when 'StorageClass',
@transition['StorageClass'] = value
when 'Date'
@transition[name] = value
when 'Days'
@transition[name] = value.to_i
when 'Transition'
@rule['Transition'] = @transition
@in_transition = false
@transition = {}
end
else
case name
when 'ID', 'Prefix'
@rule[name] = value
when 'Status'
@rule['Enabled'] = value == 'Enabled'
when 'Days'
@rule[name] = value.to_i
when 'Rule'
@response['Rules'] << @rule
@rule = {}
end
end
end
end
end
end
end

View file

@ -11,7 +11,18 @@ module Fog
# * ID [String] Unique identifier for the rule
# * Prefix [String] Prefix identifying one or more objects to which the rule applies
# * Enabled [Boolean] if rule is currently being applied
# * Expiration [Hash] Container for the object expiration rule.
# * Days [Integer] lifetime, in days, of the objects that are subject to the rule
# * Date [Date] Indicates when the specific rule take effect.
# The date value must conform to the ISO 8601 format. The time is always midnight UTC.
# * Transition [Hash] Container for the transition rule that describes when objects transition
# to the Glacier storage class
# * Days [Integer] lifetime, in days, of the objects that are subject to the rule
# * Date [Date] Indicates when the specific rule take effect.
# The date value must conform to the ISO 8601 format. The time is always midnight UTC.
# * StorageClass [String] Indicates the Amazon S3 storage class to which you want the object
# to transition to.
#
#
# @see http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html
#
@ -23,14 +34,33 @@ module Fog
ID rule['ID']
Prefix rule['Prefix']
Status rule['Enabled'] ? 'Enabled' : 'Disabled'
unless (rule['Expiration'] or rule['Transition'])
Expiration { Days rule['Days'] }
else
if rule['Expiration']
if rule['Expiration']['Days']
Expiration { Days rule['Expiration']['Days'] }
elsif rule['Expiration']['Date']
Expiration { Date rule['Expiration']['Date'].is_a?(Time) ? rule['Expiration']['Date'].utc.iso8601 : Time.parse(rule['Expiration']['Date']).utc.iso8601 }
end
end
if rule['Transition']
Transition {
if rule['Transition']['Days']
Days rule['Transition']['Days']
elsif rule['Transition']['Date']
Date rule['Transition']['Date'].is_a?(Time) ? time.utc.iso8601 : Time.parse(time).utc.iso8601
end
StorageClass rule['Transition']['StorageClass'].nil? ? 'GLACIER' : rule['Transition']['StorageClass']
}
end
end
}
end
}
end
body = builder.to_xml
body.gsub! /<([^<>]+)\/>/, '<\1></\1>'
request({
:body => body,
:expects => 200,

View file

@ -22,6 +22,15 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do
'StorageClass' => String
}]
}
@bucket_lifecycle_format = {
'Rules' => [{
'ID' => String,
'Prefix' => Fog::Nullable::String,
'Enabled' => Fog::Boolean,
'Expiration' => Fog::Nullable::Hash,
'Transition' => Fog::Nullable::Hash
}]
}
@service_format = {
'Buckets' => [{
@ -221,11 +230,22 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do
tests('create').succeeds do
Fog::Storage[:aws].put_bucket_lifecycle(@aws_bucket_name, lifecycle)
end
tests('read').returns(lifecycle) do
tests('read').formats(@bucket_lifecycle_format) do
Fog::Storage[:aws].get_bucket_lifecycle(@aws_bucket_name).body
end
lifecycle = { 'Rules' => 5.upto(6).map { |i| {'ID' => "rule\##{i}", 'Prefix' => i.to_s, 'Enabled' => true, 'Days' => i} } }
tests('update').returns(lifecycle) do
lifecycle_return = { 'Rules' => 5.upto(6).map { |i| {'ID' => "rule\##{i}", 'Prefix' => i.to_s, 'Enabled' => true, 'Expiration' => {'Days' => i}} } }
tests('update').returns(lifecycle_return) do
Fog::Storage[:aws].put_bucket_lifecycle(@aws_bucket_name, lifecycle)
Fog::Storage[:aws].get_bucket_lifecycle(@aws_bucket_name).body
end
lifecycle = {'Rules' => [{'ID' => 'test rule', 'Prefix' => '/prefix', 'Enabled' => true, 'Expiration' => {'Days' => 42}, 'Transition' => {'Days' => 6, 'StorageClass'=>'GLACIER'}}]}
tests('transition').returns(lifecycle) do
Fog::Storage[:aws].put_bucket_lifecycle(@aws_bucket_name, lifecycle)
Fog::Storage[:aws].get_bucket_lifecycle(@aws_bucket_name).body
end
lifecycle = {'Rules' => [{'ID' => 'test rule', 'Prefix' => '/prefix', 'Enabled' => true, 'Expiration' => {'Date' => '2012-12-31T00:00:00.000Z'}}]}
tests('date').returns(lifecycle) do
Fog::Storage[:aws].put_bucket_lifecycle(@aws_bucket_name, lifecycle)
Fog::Storage[:aws].get_bucket_lifecycle(@aws_bucket_name).body
end