Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to configure queue by ARN #603

Merged
merged 3 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions lib/shoryuken/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,17 +65,34 @@ def set_by_url(url)
self.url = url
end

def set_name_and_url(name_or_url)
if name_or_url.include?('://')
set_by_url(name_or_url)
def arn_to_url(arn_str)
_, _, _, 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})" unless valid

"https://sqs.#{region}.amazonaws.com/#{account_id}/#{resource}"
end

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

set_by_name(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_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
Expand Down
21 changes: 21 additions & 0 deletions spec/shoryuken/queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down