From a969ad2b84d0f7bfb215c4425895f237732f6813 Mon Sep 17 00:00:00 2001 From: Eric Stonfer Date: Tue, 15 Jan 2013 19:19:44 -0500 Subject: [PATCH] adds support for bucket transitioning/fixes bucket lifecycle management --- .../parsers/storage/get_bucket_lifecycle.rb | 56 +++++++++++++++---- .../requests/storage/put_bucket_lifecycle.rb | 40 +++++++++++-- tests/aws/requests/glacier/vault_tests.rb | 2 +- tests/aws/requests/storage/bucket_tests.rb | 26 ++++++++- 4 files changed, 104 insertions(+), 20 deletions(-) diff --git a/lib/fog/aws/parsers/storage/get_bucket_lifecycle.rb b/lib/fog/aws/parsers/storage/get_bucket_lifecycle.rb index 81014f229..350ac4a21 100644 --- a/lib/fog/aws/parsers/storage/get_bucket_lifecycle.rb +++ b/lib/fog/aws/parsers/storage/get_bucket_lifecycle.rb @@ -6,26 +6,60 @@ module Fog class GetBucketLifecycle < Fog::Parsers::Base def reset + @expiration = {} + @transition = {} @rule = {} @response = { 'Rules' => [] } end - def end_element(name) + def start_element(name, attrs=[]) + super 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 = {} + 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 'Rule' + @response['Rules'] << @rule + @rule = {} + end + end + end end - end end end diff --git a/lib/fog/aws/requests/storage/put_bucket_lifecycle.rb b/lib/fog/aws/requests/storage/put_bucket_lifecycle.rb index 2ab7bd643..9310739e2 100644 --- a/lib/fog/aws/requests/storage/put_bucket_lifecycle.rb +++ b/lib/fog/aws/requests/storage/put_bucket_lifecycle.rb @@ -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 - # * Days [Integer] lifetime, in days, of the objects that are subject to the rule + # * 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 # @@ -21,16 +32,35 @@ module Fog lifecycle['Rules'].each do |rule| Rule { ID rule['ID'] - Prefix rule['Prefix'] + Prefix rule['Prefix'] Status rule['Enabled'] ? 'Enabled' : 'Disabled' - Expiration { Days rule['Days'] } + 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>' request({ :body => body, :expects => 200, diff --git a/tests/aws/requests/glacier/vault_tests.rb b/tests/aws/requests/glacier/vault_tests.rb index 97d09545e..91f9d5a7b 100644 --- a/tests/aws/requests/glacier/vault_tests.rb +++ b/tests/aws/requests/glacier/vault_tests.rb @@ -32,4 +32,4 @@ Shindo.tests('AWS::Glacier | glacier vault requests', ['aws']) do Fog::AWS[:sns].delete_topic topic_arn -end \ No newline at end of file +end diff --git a/tests/aws/requests/storage/bucket_tests.rb b/tests/aws/requests/storage/bucket_tests.rb index 9cbfe7e39..353366d07 100644 --- a/tests/aws/requests/storage/bucket_tests.rb +++ b/tests/aws/requests/storage/bucket_tests.rb @@ -22,7 +22,16 @@ 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' => [{ 'CreationDate' => Time, @@ -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