Skip to content

Commit

Permalink
Following @h3poteto and @phstc comments on the PR ruby-shoryuken#252
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
agustin committed Oct 24, 2016
1 parent 9478610 commit d5cdfb6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
33 changes: 27 additions & 6 deletions lib/shoryuken/aws_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 2 additions & 12 deletions lib/shoryuken/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def queues(name)
end

def sns
@sns ||= Aws::SNS::Client.new(aws_client_options(:sns_endpoint))
@sns ||= Shoryuken::AwsConfig.sns
end

def sns_arn
@sns_arn ||= SnsArn
end

def sqs
@sqs ||= Aws::SQS::Client.new(aws_client_options(:sqs_endpoint))
@sqs ||= Shoryuken::AwsConfig.sqs
end

def topics(name)
Expand All @@ -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

0 comments on commit d5cdfb6

Please sign in to comment.