From f34a1639250e08cf49a0384eeacedb85b441ece4 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 26 May 2020 10:07:08 +1000 Subject: [PATCH 1/3] add ability to configure queue by ARN --- lib/shoryuken/queue.rb | 16 ++++++++++++++++ spec/shoryuken/queue_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/shoryuken/queue.rb b/lib/shoryuken/queue.rb index 9c0ea711..b93226ab 100644 --- a/lib/shoryuken/queue.rb +++ b/lib/shoryuken/queue.rb @@ -65,6 +65,16 @@ def set_by_url(url) self.url = url end + def arn_to_url(arn_str) + _,_,_,region,account_id,resource = arn_str.split(":") + + required = [region, account_id, resource] + + raise "please pass a shoryuken queue ARN containing, account_id, and resource values (#{arn_str})" if required.any?(&:empty?) + + result = "https://sqs.#{region}.amazonaws.com/#{account_id}/#{resource}" + end + def set_name_and_url(name_or_url) if name_or_url.include?('://') set_by_url(name_or_url) @@ -73,6 +83,12 @@ def set_name_and_url(name_or_url) return fifo? end + if name_or_url.include?('arn:') + url = arn_to_url(name_or_url) + set_by_url(url) + return + end + set_by_name(name_or_url) rescue Aws::Errors::NoSuchEndpointError, Aws::SQS::Errors::NonExistentQueue => ex raise ex, "The specified queue #{name_or_url} does not exist." diff --git a/spec/shoryuken/queue_spec.rb b/spec/shoryuken/queue_spec.rb index e514d34a..099046c0 100644 --- a/spec/shoryuken/queue_spec.rb +++ b/spec/shoryuken/queue_spec.rb @@ -39,6 +39,27 @@ end end + context 'when queue ARN supplied' do + let(:queue_arn) { "arn:aws:sqs:ap-southeast-2:000000000000:queue-name" } + + it 'instantiates by URL and validate the URL' do + subject = described_class.new(sqs, queue_arn) + + expect(subject.name).to eq("queue-name") + expect(subject.url).to eq("https://sqs.ap-southeast-2.amazonaws.com/000000000000/queue-name") + end + end + + context 'when inadequate queue ARN supplied' do + let(:queue_arn) { "arn:aws:sqs::000000000000:queue-name" } + + it 'raises an error' do + expect do + described_class.new(sqs, queue_arn) + end.to raise_error("please pass a shoryuken queue ARN containing, account_id, and resource values (arn:aws:sqs::000000000000:queue-name)") + end + end + context 'when queue name supplied' do subject { described_class.new(sqs, queue_name) } From 2609dc8aabfe709cd8ebca3e2b6d8be9b49e4397 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 26 May 2020 10:42:12 +1000 Subject: [PATCH 2/3] fixup! add ability to configure queue by ARN --- lib/shoryuken/queue.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/shoryuken/queue.rb b/lib/shoryuken/queue.rb index b93226ab..d4494d15 100644 --- a/lib/shoryuken/queue.rb +++ b/lib/shoryuken/queue.rb @@ -8,9 +8,9 @@ class Queue attr_accessor :name, :client, :url - def initialize(client, name_or_url) + def initialize(client, name_or_url_or_arn) self.client = client - set_name_and_url(name_or_url) + set_name_and_url(name_or_url_or_arn) end def visibility_timeout @@ -75,23 +75,23 @@ def arn_to_url(arn_str) result = "https://sqs.#{region}.amazonaws.com/#{account_id}/#{resource}" end - def set_name_and_url(name_or_url) - if name_or_url.include?('://') - set_by_url(name_or_url) + def set_name_and_url(name_or_url_or_arn) + if name_or_url_or_arn.include?('://') + set_by_url(name_or_url_or_arn) # anticipate the fifo? checker for validating the queue URL return fifo? end - if name_or_url.include?('arn:') - url = arn_to_url(name_or_url) + if name_or_url_or_arn.include?('arn:') + url = arn_to_url(name_or_url_or_arn) set_by_url(url) return end - set_by_name(name_or_url) + set_by_name(name_or_url_or_arn) rescue Aws::Errors::NoSuchEndpointError, Aws::SQS::Errors::NonExistentQueue => ex - raise ex, "The specified queue #{name_or_url} does not exist." + raise ex, "The specified queue #{name_or_url_or_arn} does not exist." end def queue_attributes From 9ddeb86990bc6f558527fb6999aae95fe0e5fbba Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Tue, 26 May 2020 10:51:52 +1000 Subject: [PATCH 3/3] fixup! add ability to configure queue by ARN --- lib/shoryuken/queue.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/shoryuken/queue.rb b/lib/shoryuken/queue.rb index d4494d15..04bc3531 100644 --- a/lib/shoryuken/queue.rb +++ b/lib/shoryuken/queue.rb @@ -66,13 +66,14 @@ def set_by_url(url) end def arn_to_url(arn_str) - _,_,_,region,account_id,resource = arn_str.split(":") + _, _, _, region, account_id, resource = arn_str.split(':') required = [region, account_id, resource] + valid = required.none?(&:empty?) - raise "please pass a shoryuken queue ARN containing, account_id, and resource values (#{arn_str})" if required.any?(&:empty?) + raise "please pass a shoryuken queue ARN containing, account_id, and resource values (#{arn_str})" unless valid - result = "https://sqs.#{region}.amazonaws.com/#{account_id}/#{resource}" + "https://sqs.#{region}.amazonaws.com/#{account_id}/#{resource}" end def set_name_and_url(name_or_url_or_arn)