Skip to content

Commit

Permalink
Add Support for S3 Accelerate with Dualstack
Browse files Browse the repository at this point in the history
  • Loading branch information
awood45 committed Oct 12, 2016
1 parent 5c309a2 commit 2a730f2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
19 changes: 18 additions & 1 deletion aws-sdk-core/lib/aws-sdk-core/plugins/s3_accelerate.rb
Expand Up @@ -15,6 +15,7 @@ module Plugins
class S3Accelerate < Seahorse::Client::Plugin

option(:use_accelerate_endpoint, false)
option(:use_dualstack_endpoint, false)

def add_handlers(handlers, config)
operations = config.api.operation_names - [
Expand All @@ -38,7 +39,13 @@ def call(context)
class AccelerateHandler < Seahorse::Client::Handler

def call(context)
use_accelerate_endpoint(context) if context[:use_accelerate_endpoint]
if context[:use_accelerate_endpoint]
if context[:use_dualstack_endpoint]
use_combined_accelerate_dualstack_endpoint(context)
else
use_accelerate_endpoint(context)
end
end
@handler.call(context)
end

Expand All @@ -54,6 +61,16 @@ def use_accelerate_endpoint(context)
context.http_request.endpoint = endpoint.to_s
end

def use_combined_accelerate_dualstack_endpoint(context)
bucket_name = context.params[:bucket]
validate_bucket_name!(bucket_name)
endpoint = URI.parse(context.http_request.endpoint.to_s)
endpoint.scheme = 'https'
endpoint.port = 443
endpoint.host = "#{bucket_name}.s3-accelerate.dualstack.amazonaws.com"
context.http_request.endpoint = endpoint.to_s
end

def validate_bucket_name!(bucket_name)
unless S3BucketDns.dns_compatible?(bucket_name, ssl = true)
msg = "unable to use `accelerate: true` on buckets with "
Expand Down
15 changes: 6 additions & 9 deletions aws-sdk-core/lib/aws-sdk-core/plugins/s3_dualstack.rb
Expand Up @@ -8,6 +8,7 @@ module Plugins
class S3Dualstack < Seahorse::Client::Plugin

option(:use_dualstack_endpoint, false)
option(:use_accelerate_endpoint, false)

def add_handlers(handlers, config)
handlers.add(OptionHandler, step: :initialize)
Expand All @@ -27,12 +28,12 @@ def call(context)
# @api private
class DualstackHandler < Seahorse::Client::Handler
def call(context)
use_dualstack_endpoint(context) if context[:use_dualstack_endpoint]
apply_dualstack_endpoint(context) if use_dualstack_endpoint?(context)
@handler.call(context)
end

private
def use_dualstack_endpoint(context)
def apply_dualstack_endpoint(context)
bucket_name = context.params[:bucket]
region = context.config.region
force_path_style = context.config.force_path_style
Expand All @@ -55,16 +56,12 @@ def use_bucket_dns?(bucket_name, context)
bucket_name && S3BucketDns.dns_compatible?(bucket_name, ssl) &&
!context.config.force_path_style
end
end

def after_initialize(client)
cfg = client.config
if cfg.use_accelerate_endpoint && cfg.use_dualstack_endpoint
msg = "Use of the :use_accelerate_endpoint and :use_dualstack_endpoint"\
" options together is not currently supported."
raise ArgumentError, msg
def use_dualstack_endpoint?(context)
context[:use_dualstack_endpoint] && !context[:use_accelerate_endpoint]
end
end

end
end
end
34 changes: 34 additions & 0 deletions aws-sdk-core/spec/aws/s3/client/accelerate_spec.rb
Expand Up @@ -11,6 +11,14 @@ module S3

let(:accelerated_client) { Client.new(options.merge(use_accelerate_endpoint: true)) }

let(:accel_dualstack_client) {
Client.new(
options.merge(
use_accelerate_endpoint: true, use_dualstack_endpoint: true
)
)
}

describe ':use_accelerate_endpoint' do

it 'defaults to false' do
Expand Down Expand Up @@ -58,6 +66,32 @@ module S3
end

end

describe ':use_accelerate_endpoint with :use_dualstack_endpoint' do

it 'properly uses the combined endpoint' do
resp = accel_dualstack_client.put_object(bucket:'bucket-name', key:'key', use_accelerate_endpoint: true)
expect(resp.context.http_request.endpoint.to_s).to eq(
'https://bucket-name.s3-accelerate.dualstack.amazonaws.com/key')
end

it 'does not apply to #create_bucket' do
resp = accel_dualstack_client.create_bucket(bucket:'bucket-name')
expect(resp.context.http_request.endpoint.to_s).to eq('https://bucket-name.s3.dualstack.us-east-1.amazonaws.com/')
end

it 'does not apply to #list_buckets' do
resp = accel_dualstack_client.list_buckets
expect(resp.context.http_request.endpoint.to_s).to eq('https://s3.dualstack.us-east-1.amazonaws.com/')
end

it 'does not apply to #delete_bucket' do
resp = accel_dualstack_client.delete_bucket(bucket:'bucket-name')
expect(resp.context.http_request.endpoint.to_s).to eq('https://bucket-name.s3.dualstack.us-east-1.amazonaws.com/')
end

end

end
end
end
8 changes: 0 additions & 8 deletions aws-sdk-core/spec/aws/s3/client/dualstack_spec.rb
Expand Up @@ -79,14 +79,6 @@ module S3
)
end

it 'throws an exception if both :use_dualstack_endpoint and :use_accelerate_endpoint are set' do
expect {
Client.new(options.merge(
use_dualstack_endpoint: true, use_accelerate_endpoint: true
))
}.to raise_error(ArgumentError)
end

it 'uses the correct endpoint pattern for us-east-1' do
client = Client.new(stub_responses: true, region: "us-east-1", use_dualstack_endpoint: true)
resp = client.put_object(bucket: 'bucket-name', key: 'key')
Expand Down

0 comments on commit 2a730f2

Please sign in to comment.