Skip to content
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
8 changes: 0 additions & 8 deletions app/controllers/barbeque/sns_subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def show
end

def new
@sns_topic_arns = fetch_sns_topic_arns
@sns_subscription = Barbeque::SnsSubscription.new
end

Expand All @@ -21,7 +20,6 @@ def create
if Barbeque::SnsSubscriptionService.new.subscribe(@sns_subscription)
redirect_to @sns_subscription, notice: 'SNS subscription was successfully created.'
else
@sns_topic_arns = fetch_sns_topic_arns
render :new
end
end
Expand All @@ -40,10 +38,4 @@ def destroy
Barbeque::SnsSubscriptionService.new.unsubscribe(sns_subscription)
redirect_to sns_subscriptions_path, notice: 'SNS subscription was successfully destroyed.'
end

private

def fetch_sns_topic_arns
Barbeque::SnsSubscriptionService.sns_client.list_topics.flat_map(&:topics).map(&:topic_arn)
end
end
13 changes: 13 additions & 0 deletions app/models/barbeque/sns_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@ class SnsSubscription < ApplicationRecord
validates :topic_arn,
uniqueness: { scope: :job_queue, message: 'should be set with only one queue' },
presence: true
validate :topic_arn_is_formatted

def topic_region
Aws::ARNParser.parse(topic_arn).region
end

private

def topic_arn_is_formatted
unless Aws::ARNParser.arn?(topic_arn)
errors.add(:topic_arn, 'is not a valid ARN')
end
end
end
end
17 changes: 10 additions & 7 deletions app/services/barbeque/sns_subscription_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ def self.sqs_client
@sqs_client ||= Aws::SQS::Client.new
end

def self.sns_client
@sns_client ||= Aws::SNS::Client.new
def self.sns_client(region:)
return @sns_client[region] if @sns_client
@sns_client = Hash.new { |hash, region| hash[region] = Aws::SNS::Client.new(region: region) }
@sns_client[region]
end

# @param [Barbeque::SnsSubscription] sns_subscription
Expand Down Expand Up @@ -45,8 +47,8 @@ def sqs_client
Barbeque::SnsSubscriptionService.sqs_client
end

def sns_client
Barbeque::SnsSubscriptionService.sns_client
def sns_client(region:)
Barbeque::SnsSubscriptionService.sns_client(region: region)
end

# @param [Barbeque::SnsSubscription] sns_subscription
Expand Down Expand Up @@ -98,7 +100,7 @@ def subscribe_topic!(sns_subscription)
)
queue_arn = sqs_attrs.attributes['QueueArn']

sns_client.subscribe(
sns_client(region: sns_subscription.topic_region).subscribe(
topic_arn: sns_subscription.topic_arn,
protocol: 'sqs',
endpoint: queue_arn
Expand All @@ -112,14 +114,15 @@ def unsubscribe_topic!(sns_subscription)
attribute_names: ['QueueArn'],
)
queue_arn = sqs_attrs.attributes['QueueArn']
region = sns_subscription.topic_region

subscriptions = sns_client.list_subscriptions_by_topic(
subscriptions = sns_client(region: region).list_subscriptions_by_topic(
topic_arn: sns_subscription.topic_arn,
)
subscription_arn = subscriptions.subscriptions.find {|subscription| subscription.endpoint == queue_arn }.try!(:subscription_arn)

if subscription_arn
sns_client.unsubscribe(
sns_client(region: region).unsubscribe(
subscription_arn: subscription_arn,
)
end
Expand Down
3 changes: 1 addition & 2 deletions app/views/barbeque/sns_subscriptions/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
- if @sns_subscription.persisted?
.sns_subscription_topic_arn= @sns_subscription.topic_arn
- else
= f.collection_select :topic_arn, @sns_topic_arns.map {|t| [t] }, :first, :first,
{ prompt: true }, class: 'form-control'
= f.text_field :topic_arn, class: 'form-control'

.row.form-group
.col-md-4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
let(:subscription_arn) { 'arn:aws:sns:ap-northeast-1:012345678912:barbeque-spec:01234567-89ab-cdef-0123-456789abcdef' }

before do
allow(Barbeque::SnsSubscriptionService).to receive(:sns_client).and_return(sns_client)
allow(Barbeque::SnsSubscriptionService).to receive(:sns_client).with(region: 'ap-northest-1').and_return(sns_client)
allow(Barbeque::SnsSubscriptionService).to receive(:sqs_client).and_return(sqs_client)

allow(sqs_client).to receive(:get_queue_attributes).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
it 'does not create a record and shows error message' do
expect(sqs_client).to receive(:get_queue_attributes).with(queue_url: job_queue.queue_url, attribute_names: ['QueueArn'])
expect(sns_client).to receive(:subscribe).and_raise(Aws::SNS::Errors::NotFound.new(self, 'not found'))
allow(controller).to receive(:fetch_sns_topic_arns).and_return([])
post :create , params: { sns_subscription: attributes }
expect(response).to render_template(:new)
expect(assigns(:sns_subscription).errors[:topic_arn]).to eq(['is not found'])
Expand All @@ -73,7 +72,6 @@
it 'does not create a record and shows error message' do
expect(sqs_client).to receive(:get_queue_attributes).with(queue_url: job_queue.queue_url, attribute_names: ['QueueArn'])
expect(sns_client).to receive(:subscribe).and_raise(Aws::SNS::Errors::AuthorizationError.new(self, 'not found'))
allow(controller).to receive(:fetch_sns_topic_arns).and_return([])
post :create , params: { sns_subscription: attributes }
expect(response).to render_template(:new)
expect(assigns(:sns_subscription).errors[:topic_arn]).to eq(['is not authorized'])
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/sns_subscription.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :sns_subscription, class: Barbeque::SnsSubscription do
sequence(:topic_arn) { |n| "arn:aws:sns:ap-northest-1:123456789012/Topic-#{n}" }
sequence(:topic_arn) { |n| "arn:aws:sns:ap-northest-1:123456789012:Topic-#{n}" }
job_queue
job_definition
end
Expand Down