mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #2322 from chrisroberts/feature/s3-tags
Add s3 bucket tagging support
This commit is contained in:
commit
37d5081ff9
6 changed files with 221 additions and 4 deletions
37
lib/fog/aws/parsers/storage/get_bucket_tagging.rb
Normal file
37
lib/fog/aws/parsers/storage/get_bucket_tagging.rb
Normal file
|
@ -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
|
46
lib/fog/aws/requests/storage/delete_bucket_tagging.rb
Normal file
46
lib/fog/aws/requests/storage/delete_bucket_tagging.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
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
|
||||
|
||||
class Mock # :nodoc:all
|
||||
|
||||
def delete_bucket_tagging(bucket_name)
|
||||
response = Excon::Response.new
|
||||
if self.data[:buckets][bucket_name]
|
||||
self.data[:bucket_tagging].delete(bucket_name)
|
||||
response.status = 204
|
||||
else
|
||||
response.status = 404
|
||||
raise(Excon::Errors.status_error({:expects => 204}, response))
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
53
lib/fog/aws/requests/storage/get_bucket_tagging.rb
Normal file
53
lib/fog/aws/requests/storage/get_bucket_tagging.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
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
|
||||
|
||||
class Mock # :nodoc:all
|
||||
|
||||
def get_bucket_tagging(bucket_name)
|
||||
response = Excon::Response.new
|
||||
if self.data[:buckets][bucket_name] && self.data[:bucket_tagging][bucket_name]
|
||||
response.status = 200
|
||||
response.body = {'BucketTagging' => self.data[:bucket_tagging][bucket_name]}
|
||||
else
|
||||
response.status = 404
|
||||
raise(Excon::Errors.status_error({:expects => 200}, response))
|
||||
end
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
60
lib/fog/aws/requests/storage/put_bucket_tagging.rb
Normal file
60
lib/fog/aws/requests/storage/put_bucket_tagging.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
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|
|
||||
"<Tag><Key>#{k}</Key><Value>#{v}</Value></Tag>"
|
||||
end.join("\n")
|
||||
data =
|
||||
<<-DATA
|
||||
<Tagging xmlns="http://doc.s3.amazonaws.com/2006-03-01" >
|
||||
<TagSet>
|
||||
#{tagging}
|
||||
</TagSet>
|
||||
</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
|
||||
|
||||
class Mock # :nodoc:all
|
||||
|
||||
def put_bucket_tagging(bucket_name, tags)
|
||||
response = Excon::Response.new
|
||||
|
||||
if self.data[:buckets][bucket_name]
|
||||
self.data[:bucket_tagging][bucket_name] = tags
|
||||
response.status = 204
|
||||
else
|
||||
response.status = 404
|
||||
raise(Excon::Errors.status_error({:expects => 204}, response))
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
@ -332,7 +336,8 @@ module Fog
|
|||
:buckets => {},
|
||||
:cors => {
|
||||
:bucket => {}
|
||||
}
|
||||
},
|
||||
:bucket_tagging => {}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -24,14 +24,14 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do
|
|||
}
|
||||
@bucket_lifecycle_format = {
|
||||
'Rules' => [{
|
||||
'ID' => String,
|
||||
'ID' => String,
|
||||
'Prefix' => Fog::Nullable::String,
|
||||
'Enabled' => Fog::Boolean,
|
||||
'Enabled' => Fog::Boolean,
|
||||
'Expiration' => Fog::Nullable::Hash,
|
||||
'Transition' => Fog::Nullable::Hash
|
||||
}]
|
||||
}
|
||||
|
||||
|
||||
@service_format = {
|
||||
'Buckets' => [{
|
||||
'CreationDate' => Time,
|
||||
|
@ -275,6 +275,22 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do
|
|||
Fog::Storage[:aws].put_bucket_cors(@aws_bucket_name, cors)
|
||||
end
|
||||
|
||||
tests("bucket tagging") do
|
||||
|
||||
tests("#put_bucket_tagging('#{@aws_bucket_name}')").succeeds do
|
||||
Fog::Storage[:aws].put_bucket_tagging(@aws_bucket_name, {'Key1' => 'Value1', 'Key2' => 'Value2'})
|
||||
end
|
||||
|
||||
tests("#get_bucket_tagging('#{@aws_bucket_name}')").
|
||||
returns('BucketTagging' => {'Key1' => 'Value1', 'Key2' => 'Value2'}) do
|
||||
Fog::Storage[:aws].get_bucket_tagging(@aws_bucket_name).body
|
||||
end
|
||||
|
||||
tests("#delete_bucket_tagging('#{@aws_bucket_name}')").succeeds do
|
||||
Fog::Storage[:aws].delete_bucket_tagging(@aws_bucket_name)
|
||||
end
|
||||
end
|
||||
|
||||
tests("#delete_bucket('#{@aws_bucket_name}')").succeeds do
|
||||
Fog::Storage[:aws].delete_bucket(@aws_bucket_name)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue