logstash-output-slack 2.0.3 → 2.1.0

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
- SHA1:
3
- metadata.gz: 0897c3faaa18a89fce7646142a13c0188280a934
4
- data.tar.gz: d301820d07c3157c2da4420f36df84395c903e8b
2
+ SHA256:
3
+ metadata.gz: 7e2dae06476ae2f501462eb76832aa5d2b24e6686bfe0bacc64a422a6b3676c7
4
+ data.tar.gz: 8115f85427fe509cfc28bf6812a77af90054530514ca511c84572c0ec8046df1
5
5
  SHA512:
6
- metadata.gz: 86c8fe1525a38d591e5d2131f0e70b98a51cc9376d23534f8a86f7d1947ed1bab528c524bb7dd8d88914b46c54d907c67e99f446a20b814604e3095dda681b79
7
- data.tar.gz: 6437a6cc6a846de24ffd713429c3ffcd6d4e8b076428165e077257e1a567ea5e7976928601d739f550035d877f41629c55bc97fb10446c9ce9c7b3db29fa0247
6
+ metadata.gz: f0c28f6a9714e3b1f39ef046c3583b5601c359ca72cd2f13c760472e4ce33c83a1584add02538d87153acb184f5410cd083da0820ae68e1ebd051a7221a3a515
7
+ data.tar.gz: def643b508f11e9859b4257da6ff193bd8e92f3c5717e9d5f5f0224283bd20c6606a0277cff16d96e17c3b8923a0d47f749452c81347cc9e6d63fc18ed676f6f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 2.1.0
2
+ - allow interpolation of event fields in url [#11](https://github.com/logstash-plugins/logstash-output-slack/pull/11)
3
+
1
4
  ## 2.0.2
2
5
  - Enable event based data in attachments
3
6
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://travis-ci.org/cyli/logstash-output-slack.svg?branch=master)](https://travis-ci.org/cyli/logstash-output-slack)
1
+ [![Build Status](https://travis-ci.org/logstash-plugins/logstash-output-slack.svg?branch=master)](https://travis-ci.org/logstash-plugins/logstash-output-slack)
2
2
 
3
3
  Reviews of the code/contributions are very welcome (particularly with testing!), since I don't really know Ruby.
4
4
 
data/docs/index.asciidoc CHANGED
@@ -12,7 +12,7 @@ START - GENERATED VARIABLES, DO NOT EDIT!
12
12
  END - GENERATED VARIABLES, DO NOT EDIT!
13
13
  ///////////////////////////////////////////
14
14
 
15
- [id="plugins-{type}-{plugin}"]
15
+ [id="plugins-{type}s-{plugin}"]
16
16
 
17
17
  === Slack output plugin
18
18
 
@@ -42,6 +42,7 @@ class LogStash::Outputs::Slack < LogStash::Outputs::Base
42
42
 
43
43
  payload_json = Hash.new
44
44
  payload_json['text'] = event.sprintf(@format)
45
+ url = event.sprintf(@url)
45
46
 
46
47
  if not @channel.nil?
47
48
  payload_json['channel'] = event.sprintf(@channel)
@@ -75,7 +76,7 @@ class LogStash::Outputs::Slack < LogStash::Outputs::Base
75
76
 
76
77
  begin
77
78
  RestClient.post(
78
- @url,
79
+ url,
79
80
  "payload=#{CGI.escape(JSON.dump(payload_json))}",
80
81
  :accept => "application/json",
81
82
  :'User-Agent' => "logstash-output-slack",
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-output-slack'
3
- s.version = '2.0.3'
3
+ s.version = '2.1.0'
4
4
  s.licenses = ['MIT','Apache License (2.0)']
5
5
  s.summary = "Write events to Slack"
6
6
  s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
@@ -2,17 +2,22 @@ require_relative "../spec_helper"
2
2
 
3
3
  describe LogStash::Outputs::Slack do
4
4
 
5
+ subject { described_class.new(config) }
6
+ let(:config) { { } }
7
+ let(:event) { LogStash::Event.new(data) }
8
+ let(:data) { {} }
9
+
5
10
  # Actually do most of the boiler plate by stubbing out the request, running
6
11
  # the logstash pipeline, and asserting that a request was made with the
7
12
  # expected JSON.
8
- def test_one_event(logstash_config, expected_json)
13
+ def test_one_event(event, expected_json, expected_url = "http://requestb.in/r9lkbzr9")
9
14
  stub_request(:post, "requestb.in").
10
15
  to_return(:body => "", :status => 200,
11
16
  :headers => { 'Content-Length' => 0 })
12
17
 
13
- LogStash::Pipeline.new(logstash_config).run
18
+ subject.receive(event)
14
19
 
15
- expect(a_request(:post, "http://requestb.in/r9lkbzr9").
20
+ expect(a_request(:post, expected_url).
16
21
  with(:body => "payload=#{CGI.escape(JSON.dump(expected_json))}",
17
22
  :headers => {
18
23
  'Content-Type' => 'application/x-www-form-urlencoded',
@@ -22,275 +27,231 @@ describe LogStash::Outputs::Slack do
22
27
  to have_been_made.once
23
28
  end
24
29
 
25
- before do
30
+ before(:each) do
31
+ subject.register
26
32
  WebMock.disable_net_connect!
27
33
  end
28
34
 
29
- after do
35
+ after(:each) do
30
36
  WebMock.reset!
31
37
  WebMock.allow_net_connect!
32
38
  end
33
39
 
34
- context "passes the right payload to slack and" do
35
- it "uses all default values" do
36
- expected_json = {
37
- :text => "This message should show in slack"
38
- }
39
- logstash_config = <<-CONFIG
40
- input {
41
- generator {
42
- message => "This message should show in slack"
43
- count => 1
44
- }
45
- }
46
- output {
47
- slack {
48
- url => "http://requestb.in/r9lkbzr9"
49
- }
50
- }
51
- CONFIG
40
+ context "passes the right payload to slack" do
41
+ context "simple" do
42
+ let(:data) { { "message" => "This message should show in slack" } }
43
+ let(:config) { { "url" => "http://requestb.in/r9lkbzr9" } }
52
44
 
53
- test_one_event(logstash_config, expected_json)
45
+ it "uses all default values" do
46
+ expected_json = { :text => "This message should show in slack" }
47
+ test_one_event(event, expected_json)
48
+ end
54
49
  end
55
50
 
56
- it "uses and formats all provided values" do
57
- expected_json = {
58
- :text => "This message should show in slack 3",
59
- :channel => "mychannel",
60
- :username => "slackbot",
61
- :icon_emoji => ":chart_with_upwards_trend:",
62
- :icon_url => "http://lorempixel.com/48/48"
63
- }
64
-
65
- logstash_config = <<-CONFIG
66
- input {
67
- generator {
68
- message => "This message should show in slack"
69
- add_field => {"x" => "3"
70
- "channelname" => "mychannel"
71
- "username" => "slackbot"}
72
- count => 1
73
- }
74
- }
75
- output {
76
- slack {
77
- url => "http://requestb.in/r9lkbzr9"
78
- format => "%{message} %{x}"
79
- channel => "%{channelname}"
80
- username => "%{username}"
81
- icon_emoji => ":chart_with_upwards_trend:"
82
- icon_url => "http://lorempixel.com/48/48"
83
- }
84
- }
85
- CONFIG
86
-
87
- test_one_event(logstash_config, expected_json)
51
+ context "with multiple interpolations" do
52
+ let(:data) { {
53
+ "message" => "This message should show in slack",
54
+ "x" => "3",
55
+ "channelname" => "mychannel",
56
+ "username" => "slackbot"
57
+ } }
58
+
59
+ let(:config) { {
60
+ "url" => "http://requestb.in/r9lkbzr9",
61
+ "format" => "%{message} %{x}",
62
+ "channel" => "%{channelname}",
63
+ "username" => "%{username}",
64
+ "icon_emoji" => ":chart_with_upwards_trend:",
65
+ "icon_url" => "http://lorempixel.com/48/48",
66
+ } }
67
+
68
+ it "uses and formats all provided values" do
69
+ expected_json = {
70
+ :text => "This message should show in slack 3",
71
+ :channel => "mychannel",
72
+ :username => "slackbot",
73
+ :icon_emoji => ":chart_with_upwards_trend:",
74
+ :icon_url => "http://lorempixel.com/48/48"
75
+ }
76
+ test_one_event(event, expected_json)
77
+ end
88
78
  end
89
79
 
90
- it "uses and formats all provided values" do
91
- expected_json = {
92
- :text => "Unformatted message",
93
- :channel => "mychannel",
94
- :username => "slackbot",
95
- :icon_emoji => ":chart_with_upwards_trend:",
96
- :icon_url => "http://lorempixel.com/48/48"
97
- }
98
-
99
- logstash_config = <<-CONFIG
100
- input {
101
- generator {
102
- message => "This message should show in slack"
103
- count => 1
104
- }
105
- }
106
- output {
107
- slack {
108
- url => "http://requestb.in/r9lkbzr9"
109
- format => "Unformatted message"
110
- channel => "mychannel"
111
- username => "slackbot"
112
- icon_emoji => ":chart_with_upwards_trend:"
113
- icon_url => "http://lorempixel.com/48/48"
114
- }
115
- }
116
- CONFIG
117
-
118
- test_one_event(logstash_config, expected_json)
80
+ context "if the message contains no interpolations" do
81
+
82
+ let(:data) { {
83
+ "message" => "This message should show in slack"
84
+ } }
85
+
86
+ let(:config) { {
87
+ "url" => "http://requestb.in/r9lkbzr9",
88
+ "format" => "Unformatted message",
89
+ "channel" => "mychannel",
90
+ "username" => "slackbot",
91
+ "icon_emoji" => ":chart_with_upwards_trend:",
92
+ "icon_url" => "http://lorempixel.com/48/48"
93
+ } }
94
+
95
+ it "uses and formats all provided values" do
96
+ expected_json = {
97
+ :text => "Unformatted message",
98
+ :channel => "mychannel",
99
+ :username => "slackbot",
100
+ :icon_emoji => ":chart_with_upwards_trend:",
101
+ :icon_url => "http://lorempixel.com/48/48"
102
+ }
103
+ test_one_event(event, expected_json)
104
+ end
119
105
  end
120
106
 
121
- it "uses the default attachments if none are in the event" do
122
- expected_json = {
123
- :text => "This message should show in slack",
124
- :attachments => [{:image_url => "http://example.com/image.png"}]
125
- }
126
-
127
- logstash_config = <<-CONFIG
128
- input {
129
- generator {
130
- message => "This message should show in slack"
131
- count => 1
132
- }
107
+ describe "default attachements" do
108
+ let(:config) { {
109
+ "url" => "http://requestb.in/r9lkbzr9",
110
+ "attachments" => [{ "image_url" => "http://example.com/image.png" }]
111
+ } }
112
+
113
+ context "when none are in the event" do
114
+ let(:data) { { "message" => "This message should show in slack" } }
115
+ it "uses the default" do
116
+ expected_json = {
117
+ :text => "This message should show in slack",
118
+ :attachments => [{:image_url => "http://example.com/image.png"}]
133
119
  }
134
- output {
135
- slack {
136
- url => "http://requestb.in/r9lkbzr9"
137
- attachments => [
138
- {image_url => "http://example.com/image.png"}
139
- ]
140
- }
120
+ test_one_event(event, expected_json)
121
+ end
122
+ end
123
+
124
+ context "when using multiple attachments" do
125
+ let(:data) { { "message" => "This message should show in slack" } }
126
+ let(:config) { {
127
+ "url" => "http://requestb.in/r9lkbzr9",
128
+ "attachments" => [
129
+ {"image_url" => "http://example.com/image1.png"},
130
+ {"image_url" => "http://example.com/image2.png"}
131
+ ]
132
+ } }
133
+ it "sends them" do
134
+ expected_json = {
135
+ :text => "This message should show in slack",
136
+ :attachments => [{:image_url => "http://example.com/image1.png"},
137
+ {:image_url => "http://example.com/image2.png"}]
141
138
  }
142
- CONFIG
143
-
144
- test_one_event(logstash_config, expected_json)
139
+ test_one_event(event, expected_json)
140
+ end
141
+ end
145
142
  end
146
143
 
147
- it "supports multiple default attachments" do
148
- expected_json = {
149
- :text => "This message should show in slack",
150
- :attachments => [{:image_url => "http://example.com/image1.png"},
151
- {:image_url => "http://example.com/image2.png"}]
152
- }
153
-
154
- logstash_config = <<-CONFIG
155
- input {
156
- generator {
157
- message => "This message should show in slack"
158
- count => 1
159
- }
160
- }
161
- output {
162
- slack {
163
- url => "http://requestb.in/r9lkbzr9"
164
- attachments => [
165
- {image_url => "http://example.com/image1.png"},
166
- {image_url => "http://example.com/image2.png"}
167
- ]
168
- }
169
- }
170
- CONFIG
171
-
172
- test_one_event(logstash_config, expected_json)
144
+ context "empty default attachments" do
145
+ let(:data) { {
146
+ "message" => "This message should show in slack"
147
+ }}
148
+
149
+ let(:config) { {
150
+ "url" => "http://requestb.in/r9lkbzr9",
151
+ "attachments" => []
152
+ }}
153
+
154
+ it "are ignored" do
155
+ expected_json = {
156
+ :text => "This message should show in slack"
157
+ }
158
+ test_one_event(event, expected_json)
159
+ end
173
160
  end
174
161
 
175
- it "ignores empty default attachments" do
176
- expected_json = {
177
- :text => "This message should show in slack"
178
- }
179
-
180
- logstash_config = <<-CONFIG
181
- input {
182
- generator {
183
- message => "This message should show in slack"
184
- count => 1
185
- }
186
- }
187
- output {
188
- slack {
189
- url => "http://requestb.in/r9lkbzr9"
190
- attachments => []
191
- }
192
- }
193
- CONFIG
194
-
195
- test_one_event(logstash_config, expected_json)
162
+ context "when both event attachments and default attachments are set" do
163
+
164
+ let(:data) { {
165
+ "message" => "This message should show in slack",
166
+ "attachments" => [{"thumb_url" => "http://other.com/thumb.png"}]
167
+ } }
168
+
169
+ let(:config) { {
170
+ "url" => "http://requestb.in/r9lkbzr9",
171
+ "attachments" => [
172
+ {"image_url" => "http://example.com/image1.png"},
173
+ {"image_url" => "http://example.com/image2.png"}
174
+ ]
175
+ } }
176
+
177
+ it "event attachements prevail" do
178
+ expected_json = {
179
+ :text => "This message should show in slack",
180
+ :attachments => [{:thumb_url => "http://other.com/thumb.png"}]
181
+ }
182
+ test_one_event(event, expected_json)
183
+ end
196
184
  end
197
185
 
198
- it "uses event attachments over default attachments" do
199
- expected_json = {
200
- :text => "This message should show in slack",
201
- :attachments => [{:thumb_url => "http://other.com/thumb.png"}]
202
- }
203
-
204
- # add_field only takes string values, so we'll have to mutate to JSON
205
- logstash_config = <<-CONFIG
206
- input {
207
- generator {
208
- message => "This message should show in slack"
209
- count => 1
210
- add_field => {
211
- attachments => '[{"thumb_url": "http://other.com/thumb.png"}]'
212
- }
213
- }
214
- }
215
- filter {
216
- json {
217
- source => "attachments"
218
- target => "attachments"
219
- }
220
- }
221
- output {
222
- slack {
223
- url => "http://requestb.in/r9lkbzr9"
224
- attachments => [
225
- {image_url => "http://example.com/image1.png"},
226
- {image_url => "http://example.com/image2.png"}
227
- ]
228
- }
229
- }
230
- CONFIG
186
+ context "if event attachments empty" do
187
+
188
+ let(:data) { {
189
+ "message" => "This message should show in slack",
190
+ "attachments" => []
191
+ } }
192
+
193
+ let(:config) { {
194
+ "url" => "http://requestb.in/r9lkbzr9",
195
+ "attachments" => [
196
+ {"image_url" => "http://example.com/image1.png"},
197
+ {"image_url" => "http://example.com/image2.png"}
198
+ ]
199
+ } }
200
+
201
+ it "erases default attachments" do
202
+ expected_json = {
203
+ :text => "This message should show in slack"
204
+ }
205
+ test_one_event(event, expected_json)
206
+ end
207
+ end
231
208
 
232
- test_one_event(logstash_config, expected_json)
209
+ context "when event attachements field isn't an array" do
210
+ let(:data) { {
211
+ "message" => "This message should show in slack",
212
+ "attachments" => "baddata"
213
+ } }
214
+
215
+ let(:config) { {
216
+ "url" => "http://requestb.in/r9lkbzr9",
217
+ "attachments" => [
218
+ {"image_url" => "http://example.com/image.png"}
219
+ ]
220
+ } }
221
+
222
+
223
+ it "is ignored" do
224
+ expected_json = {
225
+ :text => "This message should show in slack",
226
+ :attachments => [{:image_url => "http://example.com/image.png"}]
227
+ }
228
+ test_one_event(event, expected_json)
229
+ end
233
230
  end
231
+ end
234
232
 
235
- it "erases default attachments if event attachments empty" do
236
- expected_json = {
237
- :text => "This message should show in slack"
238
- }
233
+ describe "interpolation in url field" do
234
+ let(:expected_url) { "http://incoming-webhook.example.com" }
239
235
 
240
- # add_field only takes string values, so we'll have to mutate to JSON
241
- logstash_config = <<-CONFIG
242
- input {
243
- generator {
244
- message => "This message should show in slack"
245
- count => 1
246
- add_field => {attachments => '[]'}
247
- }
248
- }
249
- filter {
250
- json {
251
- source => "attachments"
252
- target => "attachments"
253
- }
254
- }
255
- output {
256
- slack {
257
- url => "http://requestb.in/r9lkbzr9"
258
- attachments => [
259
- {image_url => "http://example.com/image1.png"},
260
- {image_url => "http://example.com/image2.png"}
261
- ]
262
- }
263
- }
264
- CONFIG
236
+ let(:event) {
237
+ event = LogStash::Event.new("message" => "This message should show in slack")
238
+ event.set("[@metadata][slack_url]", "#{expected_url}")
239
+ event
240
+ }
265
241
 
266
- test_one_event(logstash_config, expected_json)
267
- end
242
+ let(:config) { {
243
+ "url" => "%{[@metadata][slack_url]}",
244
+ "attachments" => [
245
+ {"image_url" => "http://example.com/image.png"}
246
+ ]
247
+ } }
268
248
 
269
- it "ignores event attachment if not array" do
249
+ it "allows interpolation in url field" do
270
250
  expected_json = {
271
251
  :text => "This message should show in slack",
272
252
  :attachments => [{:image_url => "http://example.com/image.png"}]
273
253
  }
274
-
275
- logstash_config = <<-CONFIG
276
- input {
277
- generator {
278
- message => "This message should show in slack"
279
- count => 1
280
- add_field => {attachments => "baddata"}
281
- }
282
- }
283
- output {
284
- slack {
285
- url => "http://requestb.in/r9lkbzr9"
286
- attachments => [
287
- {image_url => "http://example.com/image.png"}
288
- ]
289
- }
290
- }
291
- CONFIG
292
-
293
- test_one_event(logstash_config, expected_json)
254
+ test_one_event(event, expected_json, expected_url)
294
255
  end
295
256
  end
296
257
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-output-slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ying Li
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-23 00:00:00.000000000 Z
11
+ date: 2017-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -140,7 +140,9 @@ dependencies:
140
140
  - - ">="
141
141
  - !ruby/object:Gem::Version
142
142
  version: 1.21.0
143
- description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
143
+ description: This gem is a logstash plugin required to be installed on top of the
144
+ Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not
145
+ a stand-alone program
144
146
  email: cyli@ying.com
145
147
  executables: []
146
148
  extensions: []
@@ -178,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
180
  version: '0'
179
181
  requirements: []
180
182
  rubyforge_project:
181
- rubygems_version: 2.4.8
183
+ rubygems_version: 2.6.13
182
184
  signing_key:
183
185
  specification_version: 4
184
186
  summary: Write events to Slack