From d5cdfb64405c8ce5433b762ef50f7e25e1774afc Mon Sep 17 00:00:00 2001 From: olgalover Date: Mon, 24 Oct 2016 13:40:57 -0300 Subject: [PATCH] Following @h3poteto and @phstc comments on the PR #252 I added to the AwsConfig Class the sqs and sns methods to properly instanciate a client with the credentials and avoid the override of Aws.config ones --- lib/shoryuken/aws_config.rb | 33 +++++++++++++++++++++++++++------ lib/shoryuken/client.rb | 14 ++------------ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/shoryuken/aws_config.rb b/lib/shoryuken/aws_config.rb index 31958243..deccee6f 100644 --- a/lib/shoryuken/aws_config.rb +++ b/lib/shoryuken/aws_config.rb @@ -22,25 +22,46 @@ def setup(hash) receive_message ).map(&:to_sym) - aws_options = hash.reject do |k, _| + @aws_options = hash.reject do |k, _| shoryuken_keys.include?(k) end # assume credentials based authentication credentials = Aws::Credentials.new( - aws_options.delete(:access_key_id), - aws_options.delete(:secret_access_key) + @aws_options.delete(:access_key_id), + @aws_options.delete(:secret_access_key) ) # but only if the configuration options have valid values - aws_options = aws_options.merge(credentials: credentials) if credentials.set? + @aws_options.merge!(credentials: credentials) if credentials.set? if (callback = Shoryuken.aws_initialization_callback) Shoryuken.logger.info { 'Calling Shoryuken.on_aws_initialization block' } - callback.call(aws_options) + callback.call(@aws_options) end + end + + def sns + Aws::SNS::Client.new(aws_client_options(:sns_endpoint)) + end + + def sqs + Aws::SQS::Client.new(aws_client_options(:sqs_endpoint)) + end - Aws.config = aws_options + private + + def aws_options + @aws_options ||= {} + end + + def aws_client_options(service_endpoint_key) + environment_endpoint = ENV["AWS_#{service_endpoint_key.to_s.upcase}"] + explicit_endpoint = options[service_endpoint_key] || environment_endpoint + endpoint = {}.tap do |hash| + hash[:endpoint] = explicit_endpoint unless explicit_endpoint.to_s.empty? + end + aws_options.merge(endpoint) end end end diff --git a/lib/shoryuken/client.rb b/lib/shoryuken/client.rb index 364e4217..fbd3ccf2 100644 --- a/lib/shoryuken/client.rb +++ b/lib/shoryuken/client.rb @@ -9,7 +9,7 @@ def queues(name) end def sns - @sns ||= Aws::SNS::Client.new(aws_client_options(:sns_endpoint)) + @sns ||= Shoryuken::AwsConfig.sns end def sns_arn @@ -17,7 +17,7 @@ def sns_arn end def sqs - @sqs ||= Aws::SQS::Client.new(aws_client_options(:sqs_endpoint)) + @sqs ||= Shoryuken::AwsConfig.sqs end def topics(name) @@ -26,16 +26,6 @@ def topics(name) attr_accessor :account_id attr_writer :sns, :sqs, :sqs_resource, :sns_arn - - private - - def aws_client_options(service_endpoint_key) - environment_endpoint = ENV["AWS_#{service_endpoint_key.to_s.upcase}"] - explicit_endpoint = Shoryuken::AwsConfig.options[service_endpoint_key] || environment_endpoint - options = {} - options[:endpoint] = explicit_endpoint unless explicit_endpoint.to_s.empty? - options - end end end end