From e7a6e55143a4cc909e95f5784e9a61d735041df2 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 25 Oct 2013 11:29:24 -0700 Subject: [PATCH] Add s3 bucket tagging support --- .../aws/parsers/storage/get_bucket_tagging.rb | 37 +++++++++++++++++ .../requests/storage/delete_bucket_tagging.rb | 29 +++++++++++++ .../requests/storage/get_bucket_tagging.rb | 37 +++++++++++++++++ .../requests/storage/put_bucket_tagging.rb | 41 +++++++++++++++++++ lib/fog/aws/storage.rb | 4 ++ 5 files changed, 148 insertions(+) create mode 100644 lib/fog/aws/parsers/storage/get_bucket_tagging.rb create mode 100644 lib/fog/aws/requests/storage/delete_bucket_tagging.rb create mode 100644 lib/fog/aws/requests/storage/get_bucket_tagging.rb create mode 100644 lib/fog/aws/requests/storage/put_bucket_tagging.rb diff --git a/lib/fog/aws/parsers/storage/get_bucket_tagging.rb b/lib/fog/aws/parsers/storage/get_bucket_tagging.rb new file mode 100644 index 000000000..3f5319c10 --- /dev/null +++ b/lib/fog/aws/parsers/storage/get_bucket_tagging.rb @@ -0,0 +1,37 @@ +module Fog + module Parsers + module Storage + module AWS + + class GetBucketTagging < Fog::Parsers::Base + + def reset + @in_tag = {} + @response = {'BucketTagging' => {}} + end + + def start_element(name, *args) + super + if name == 'Tag' + @in_tag = {} + end + end + + def end_element(name) + case name + when 'Tag' + @response['BucketTagging'].merge!(@in_tag) + @in_tag = {} + when 'Key' + @in_tag[value] = nil + when 'Value' + @in_tag = {@in_tag.keys.first => value} + end + end + + end + + end + end + end +end diff --git a/lib/fog/aws/requests/storage/delete_bucket_tagging.rb b/lib/fog/aws/requests/storage/delete_bucket_tagging.rb new file mode 100644 index 000000000..1a7d8532f --- /dev/null +++ b/lib/fog/aws/requests/storage/delete_bucket_tagging.rb @@ -0,0 +1,29 @@ +module Fog + module Storage + class AWS + class Real + + # Delete tagging for a bucket + # + # @param bucket_name [String] name of bucket to delete tagging from + # + # @return [Excon::Response] response: + # * status [Integer] - 204 + # + # @see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketDELETEtagging.html + + def delete_bucket_tagging(bucket_name) + request({ + :expects => 204, + :headers => {}, + :bucket_name => bucket_name, + :method => 'DELETE', + :query => {'tagging' => nil} + }) + end + + end + + end + end +end diff --git a/lib/fog/aws/requests/storage/get_bucket_tagging.rb b/lib/fog/aws/requests/storage/get_bucket_tagging.rb new file mode 100644 index 000000000..b7c5fa0da --- /dev/null +++ b/lib/fog/aws/requests/storage/get_bucket_tagging.rb @@ -0,0 +1,37 @@ +module Fog + module Storage + class AWS + class Real + + require 'fog/aws/parsers/storage/get_bucket_tagging' + + # Get tags for an S3 bucket + # + # @param bucket_name [String] name of bucket to get tags for + # + # @return [Excon::Response] response: + # * body [Hash]: + # * BucketTagging [Hash]: + # * Key [String] - tag key + # * Value [String] - tag value + # @see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETtagging.html + + def get_bucket_tagging(bucket_name) + unless bucket_name + raise ArgumentError.new('bucket_name is required') + end + request({ + :expects => 200, + :headers => {}, + :bucket_name => bucket_name, + :idempotent => true, + :method => 'GET', + :parser => Fog::Parsers::Storage::AWS::GetBucketTagging.new, + :query => {'tagging' => nil} + }) + end + + end + end + end +end diff --git a/lib/fog/aws/requests/storage/put_bucket_tagging.rb b/lib/fog/aws/requests/storage/put_bucket_tagging.rb new file mode 100644 index 000000000..46dfd517d --- /dev/null +++ b/lib/fog/aws/requests/storage/put_bucket_tagging.rb @@ -0,0 +1,41 @@ +module Fog + module Storage + class AWS + class Real + + # Change tag set for an S3 bucket + # + # @param bucket_name [String] name of bucket to modify + # @param tags [Hash]: + # * Key [String]: tag key + # * Value [String]: tag value + # + # @see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTtagging.html + + def put_bucket_tagging(bucket_name, tags) + tagging = tags.map do |k,v| + "#{k}#{v}" + end.join("\n") + data = +<<-DATA + + + #{tagging} + + +DATA + + request({ + :body => data, + :expects => 204, + :headers => {'Content-MD5' => Base64.encode64(Digest::MD5.digest(data)).chomp!, 'Content-Type' => 'application/xml'}, + :bucket_name => bucket_name, + :method => 'PUT', + :query => {'tagging' => nil} + }) + end + + end + end + end +end diff --git a/lib/fog/aws/storage.rb b/lib/fog/aws/storage.rb index f193fb77a..d4094b399 100644 --- a/lib/fog/aws/storage.rb +++ b/lib/fog/aws/storage.rb @@ -34,6 +34,7 @@ module Fog response-content-type response-expires restore + tagging torrent uploadId uploads @@ -65,6 +66,7 @@ module Fog request :delete_bucket_website request :delete_object request :delete_multiple_objects + request :delete_bucket_tagging request :get_bucket request :get_bucket_acl request :get_bucket_cors @@ -73,6 +75,7 @@ module Fog request :get_bucket_logging request :get_bucket_object_versions request :get_bucket_policy + request :get_bucket_tagging request :get_bucket_versioning request :get_bucket_website request :get_object @@ -95,6 +98,7 @@ module Fog request :put_bucket_lifecycle request :put_bucket_logging request :put_bucket_policy + request :put_bucket_tagging request :put_bucket_versioning request :put_bucket_website request :put_object