barbeque 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1ae358429ce7e4ddebf8a84f91790148e68a6bb
4
- data.tar.gz: ab9055b050139257e02b6beb7f8848789f42d84e
3
+ metadata.gz: '0956004e27b896eda31157ad3cb964d216b1c575'
4
+ data.tar.gz: 20e711d2e9c7d68444d5cd79a73f1b83cd86eb90
5
5
  SHA512:
6
- metadata.gz: c7ec8c8160b7f41594f1d0bf6fa5a6d0531823b592a59e3b2f5f71970a39272c7fa09bb0dc3d0f109c3ccaa51356a90b52c5b829464b2c5226fb88430758a23a
7
- data.tar.gz: 3bec39419f1ffc4780104bf5c005f5844c5fe40aee74ab8c10ee02402428dfc4a6196236ae4f81559e545d96ebf670d3976b33d4b037bafed44add52181b8877
6
+ metadata.gz: 22b89b6062790e1d7e87e340ba18a95facfa7714d38ced1b4482fdfc3aab1e0a9a861a67f3ab5f99b3e156074129eec12f679ffec525a279d0e4e7a8d71682e0
7
+ data.tar.gz: 95d7fee60b607d649f444ef3e1c9ae71f30edfe7a8b59ec4e49a35e6c2efe561dab321d644df4d7a5d4864be4bef4ab002120fe720f8ab4e116fcc32145e7621
@@ -49,6 +49,9 @@ class Barbeque::JobDefinitionsController < Barbeque::ApplicationController
49
49
 
50
50
  def destroy
51
51
  @job_definition = Barbeque::JobDefinition.find(params[:id])
52
+ @job_definition.sns_subscriptions.each do |sns_subscription|
53
+ Barbeque::SNSSubscriptionService.new.unsubscribe(sns_subscription)
54
+ end
52
55
  @job_definition.destroy
53
56
  redirect_to job_definitions_url, notice: 'Job definition was successfully destroyed.'
54
57
  end
@@ -18,23 +18,10 @@ class Barbeque::SnsSubscriptionsController < Barbeque::ApplicationController
18
18
 
19
19
  def create
20
20
  @sns_subscription = Barbeque::SNSSubscription.new(params.require(:sns_subscription).permit(:topic_arn, :job_queue_id, :job_definition_id))
21
- if @sns_subscription.valid?
22
- begin
23
- subscribe_topic!
24
- rescue Aws::SNS::Errors::AuthorizationError
25
- @sns_subscription.errors[:topic_arn] << 'is not authorized'
26
- @sns_topic_arns = fetch_sns_topic_arns
27
- render :new
28
- rescue Aws::SNS::Errors::NotFound
29
- @sns_subscription.errors[:topic_arn] << 'is not found'
30
- @sns_topic_arns = fetch_sns_topic_arns
31
- render :new
32
- else
33
- @sns_subscription.save!
34
- update_sqs_policy!
35
- redirect_to @sns_subscription, notice: 'SNS subscription was successfully created.'
36
- end
21
+ if Barbeque::SNSSubscriptionService.new.subscribe(@sns_subscription)
22
+ redirect_to @sns_subscription, notice: 'SNS subscription was successfully created.'
37
23
  else
24
+ @sns_topic_arns = fetch_sns_topic_arns
38
25
  render :new
39
26
  end
40
27
  end
@@ -49,101 +36,14 @@ class Barbeque::SnsSubscriptionsController < Barbeque::ApplicationController
49
36
  end
50
37
 
51
38
  def destroy
52
- @sns_subscription = Barbeque::SNSSubscription.find(params[:id])
53
- @sns_subscription.destroy
54
- update_sqs_policy!
55
- unsubscribe_topic!
39
+ sns_subscription = Barbeque::SNSSubscription.find(params[:id])
40
+ Barbeque::SNSSubscriptionService.new.unsubscribe(sns_subscription)
56
41
  redirect_to sns_subscriptions_path, notice: 'SNS subscription was successfully destroyed.'
57
42
  end
58
43
 
59
44
  private
60
45
 
61
46
  def fetch_sns_topic_arns
62
- sns_client.list_topics.topics.map(&:topic_arn)
63
- end
64
-
65
- def update_sqs_policy!
66
- attrs = sqs_client.get_queue_attributes(
67
- queue_url: @sns_subscription.job_queue.queue_url,
68
- attribute_names: ['QueueArn'],
69
- )
70
- queue_arn = attrs.attributes['QueueArn']
71
- topic_arns = @sns_subscription.job_queue.sns_subscriptions.map(&:topic_arn)
72
-
73
- if topic_arns.present?
74
- policy = generate_policy(queue_arn: queue_arn, topic_arns: topic_arns)
75
- else
76
- policy = '' # Be blank when there're no subscriptions.
77
- end
78
-
79
- sqs_client.set_queue_attributes(
80
- queue_url: @sns_subscription.job_queue.queue_url,
81
- attributes: { 'Policy' => policy },
82
- )
83
- end
84
-
85
- # @paaram [String] queue_arn
86
- # @param [Array<String>] topic_arns
87
- # @return [String] JSON formatted policy
88
- def generate_policy(queue_arn:, topic_arns:)
89
- {
90
- 'Version' => '2012-10-17',
91
- 'Statement' => [
92
- 'Effect' => 'Allow',
93
- 'Principal' => '*',
94
- 'Action' => 'sqs:SendMessage',
95
- 'Resource' => queue_arn,
96
- 'Condition' => {
97
- 'ArnEquals' => {
98
- 'aws:SourceArn' => topic_arns,
99
- }
100
- }
101
- ]
102
- }.to_json
103
- end
104
-
105
- def subscribe_topic!
106
- sns_client.subscribe(
107
- topic_arn: @sns_subscription.topic_arn,
108
- protocol: 'sqs',
109
- endpoint: queue_arn
110
- )
111
- end
112
-
113
- def unsubscribe_topic!
114
- sqs_attrs = sqs_client.get_queue_attributes(
115
- queue_url: @sns_subscription.job_queue.queue_url,
116
- attribute_names: ['QueueArn'],
117
- )
118
- queue_arn = sqs_attrs.attributes['QueueArn']
119
-
120
- subscriptions = sns_client.list_subscriptions_by_topic(
121
- topic_arn: @sns_subscription.topic_arn,
122
- )
123
- subscription_arn = subscriptions.subscriptions.find {|subscription| subscription.endpoint == queue_arn }.try!(:subscription_arn)
124
-
125
- if subscription_arn
126
- sns_client.unsubscribe(
127
- subscription_arn: subscription_arn,
128
- )
129
- end
130
- end
131
-
132
- def sqs_client
133
- @sqs_client ||= Aws::SQS::Client.new
134
- end
135
-
136
- def sns_client
137
- @sns_client ||= Aws::SNS::Client.new
138
- end
139
-
140
- def queue_arn
141
- return @queue_arn if @queue_arn
142
-
143
- sqs_attrs = sqs_client.get_queue_attributes(
144
- queue_url: @sns_subscription.job_queue.queue_url,
145
- attribute_names: ['QueueArn'],
146
- )
147
- @queue_arn = sqs_attrs.attributes['QueueArn']
47
+ Barbeque::SNSSubscriptionService.sns_client.list_topics.topics.map(&:topic_arn)
148
48
  end
149
49
  end
@@ -1,6 +1,7 @@
1
1
  class Barbeque::JobDefinition < Barbeque::ApplicationRecord
2
2
  belongs_to :app
3
3
  has_many :job_executions, dependent: :destroy
4
+ has_many :sns_subscriptions, class_name: 'SNSSubscription'
4
5
  has_one :slack_notification, dependent: :destroy
5
6
 
6
7
  validates :job, uniqueness: { scope: :app_id }
@@ -0,0 +1,125 @@
1
+ class Barbeque::SNSSubscriptionService
2
+ def self.sqs_client
3
+ @sqs_client ||= Aws::SQS::Client.new
4
+ end
5
+
6
+ def self.sns_client
7
+ @sns_client ||= Aws::SNS::Client.new
8
+ end
9
+
10
+ # @param [Barbeque::SNSSubscription] sns_subscription
11
+ # @return [Boolean] `true` if succeeded to subscribe
12
+ def subscribe(sns_subscription)
13
+ if sns_subscription.valid?
14
+ begin
15
+ subscribe_topic!(sns_subscription)
16
+ sns_subscription.save!
17
+ update_sqs_policy!(sns_subscription)
18
+ true
19
+ rescue Aws::SNS::Errors::AuthorizationError
20
+ sns_subscription.errors[:topic_arn] << 'is not authorized'
21
+ false
22
+ rescue Aws::SNS::Errors::NotFound
23
+ sns_subscription.errors[:topic_arn] << 'is not found'
24
+ false
25
+ end
26
+ else
27
+ false
28
+ end
29
+ end
30
+
31
+ # @param [Barbeque::SNSSubscription] sns_subscription
32
+ def unsubscribe(sns_subscription)
33
+ sns_subscription.destroy
34
+ update_sqs_policy!(sns_subscription)
35
+ unsubscribe_topic!(sns_subscription)
36
+ nil
37
+ end
38
+
39
+ private
40
+
41
+ def sqs_client
42
+ Barbeque::SNSSubscriptionService.sqs_client
43
+ end
44
+
45
+ def sns_client
46
+ Barbeque::SNSSubscriptionService.sns_client
47
+ end
48
+
49
+ # @param [Barbeque::SNSSubscription] sns_subscription
50
+ def update_sqs_policy!(sns_subscription)
51
+ attrs = sqs_client.get_queue_attributes(
52
+ queue_url: sns_subscription.job_queue.queue_url,
53
+ attribute_names: ['QueueArn'],
54
+ )
55
+ queue_arn = attrs.attributes['QueueArn']
56
+ topic_arns = sns_subscription.job_queue.sns_subscriptions.map(&:topic_arn)
57
+
58
+ if topic_arns.present?
59
+ policy = generate_policy(queue_arn: queue_arn, topic_arns: topic_arns)
60
+ else
61
+ policy = '' # Be blank when there're no subscriptions.
62
+ end
63
+
64
+ sqs_client.set_queue_attributes(
65
+ queue_url: sns_subscription.job_queue.queue_url,
66
+ attributes: { 'Policy' => policy },
67
+ )
68
+ end
69
+
70
+ # @paaram [String] queue_arn
71
+ # @param [Array<String>] topic_arns
72
+ # @return [String] JSON formatted policy
73
+ def generate_policy(queue_arn:, topic_arns:)
74
+ {
75
+ 'Version' => '2012-10-17',
76
+ 'Statement' => [
77
+ 'Effect' => 'Allow',
78
+ 'Principal' => '*',
79
+ 'Action' => 'sqs:SendMessage',
80
+ 'Resource' => queue_arn,
81
+ 'Condition' => {
82
+ 'ArnEquals' => {
83
+ 'aws:SourceArn' => topic_arns,
84
+ }
85
+ }
86
+ ]
87
+ }.to_json
88
+ end
89
+
90
+ # @param [Barbeque::SNSSubscription] sns_subscription
91
+ def subscribe_topic!(sns_subscription)
92
+ sqs_attrs = sqs_client.get_queue_attributes(
93
+ queue_url: sns_subscription.job_queue.queue_url,
94
+ attribute_names: ['QueueArn'],
95
+ )
96
+ queue_arn = sqs_attrs.attributes['QueueArn']
97
+
98
+ sns_client.subscribe(
99
+ topic_arn: sns_subscription.topic_arn,
100
+ protocol: 'sqs',
101
+ endpoint: queue_arn
102
+ )
103
+ end
104
+
105
+ # @param [Barbeque::SNSSubscription] sns_subscription
106
+ def unsubscribe_topic!(sns_subscription)
107
+ sqs_attrs = sqs_client.get_queue_attributes(
108
+ queue_url: sns_subscription.job_queue.queue_url,
109
+ attribute_names: ['QueueArn'],
110
+ )
111
+ queue_arn = sqs_attrs.attributes['QueueArn']
112
+
113
+ subscriptions = sns_client.list_subscriptions_by_topic(
114
+ topic_arn: sns_subscription.topic_arn,
115
+ )
116
+ subscription_arn = subscriptions.subscriptions.find {|subscription| subscription.endpoint == queue_arn }.try!(:subscription_arn)
117
+
118
+ if subscription_arn
119
+ sns_client.unsubscribe(
120
+ subscription_arn: subscription_arn,
121
+ )
122
+ end
123
+ end
124
+
125
+ end
@@ -1,3 +1,3 @@
1
1
  module Barbeque
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barbeque
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-01 00:00:00.000000000 Z
11
+ date: 2017-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: adminlte2-rails
@@ -309,6 +309,7 @@ files:
309
309
  - app/models/barbeque/sns_subscription.rb
310
310
  - app/services/barbeque/message_enqueuing_service.rb
311
311
  - app/services/barbeque/message_retrying_service.rb
312
+ - app/services/barbeque/sns_subscription_service.rb
312
313
  - app/views/barbeque/apps/_form.html.haml
313
314
  - app/views/barbeque/apps/edit.html.haml
314
315
  - app/views/barbeque/apps/index.html.haml