kapacitor-ruby 1.0.2 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/kapacitor/client.rb +81 -75
- data/lib/kapacitor/version.rb +1 -1
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4855f87f3ec102488bb9c853b8f7a9d1d9e161d759ec62617859a784b8b521ac
|
4
|
+
data.tar.gz: a7875fdb26ce45b801297caec2ebc17946a6b8e1932f07c11e73fced251e03cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a7c13869776805389686060b8e3a47abda670b81d1adf2c8ff2690774af85d61dda6cf83c3ad35fb21f286b37f01d62c668253403e7c45d405dc76148f196c8
|
7
|
+
data.tar.gz: 321f5d4263ae5ec53fde9bb28ea158a4881967643e0071edaee2778ac37485cdf5db72de58503c6659fe06da362786a6f5d1cc667726e21bcac6708053502780
|
data/lib/kapacitor/client.rb
CHANGED
@@ -13,9 +13,9 @@ module Kapacitor
|
|
13
13
|
# @param url [String] Kapacitor REST API's URL (defaults to `http://localhost:9092`)
|
14
14
|
# @param version [Integer] API version (defaults to `v1preview`)
|
15
15
|
#
|
16
|
-
def initialize(url: 'http://localhost:9092/kapacitor', version: '
|
17
|
-
@url = URI.join(url, version)
|
16
|
+
def initialize(url: 'http://localhost:9092/kapacitor', version: 'v1')
|
18
17
|
@http = HTTPClient.new
|
18
|
+
@url = [url, version].join('/')
|
19
19
|
end
|
20
20
|
|
21
21
|
# Define a Kapacitor template
|
@@ -33,7 +33,7 @@ module Kapacitor
|
|
33
33
|
'script' => script
|
34
34
|
}
|
35
35
|
|
36
|
-
api_post(endpoint: '
|
36
|
+
api_post(endpoint: 'templates', data: req)
|
37
37
|
end
|
38
38
|
|
39
39
|
# Update a Kapacitor template
|
@@ -50,7 +50,7 @@ module Kapacitor
|
|
50
50
|
raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless opts[:type] == 'batch' or opts[:type] == 'stream'
|
51
51
|
end
|
52
52
|
|
53
|
-
api_patch(endpoint: "
|
53
|
+
api_patch(endpoint: "templates/#{id}", data: req) unless req.empty?
|
54
54
|
end
|
55
55
|
|
56
56
|
# Delete a Kapacitor template
|
@@ -58,7 +58,16 @@ module Kapacitor
|
|
58
58
|
# @param id [String] Template ID
|
59
59
|
#
|
60
60
|
def delete_template(id:)
|
61
|
-
api_delete(endpoint: "
|
61
|
+
api_delete(endpoint: "templates/#{id}")
|
62
|
+
end
|
63
|
+
|
64
|
+
# Retrieve Kapacitor topic
|
65
|
+
#
|
66
|
+
# @return [List[String]] List of topics
|
67
|
+
#
|
68
|
+
def topics()
|
69
|
+
res = api_get(endpoint: "alerts/topics")
|
70
|
+
return res['topics'].map { |v| v['id'] }
|
62
71
|
end
|
63
72
|
|
64
73
|
# Retrieve Kapacitor templates
|
@@ -89,7 +98,7 @@ module Kapacitor
|
|
89
98
|
def define_task(id:, dbrps:, **opts)
|
90
99
|
if (opts[:template_id].nil? && opts[:type].nil? && opts[:script].nil?) || (opts[:template_id] && (opts[:type] || opts[:script]))
|
91
100
|
raise ArgumentError, "Must specify either a Template ID or a script and type"
|
92
|
-
elsif opts[
|
101
|
+
elsif opts[:template_id].nil? && (opts[:type].nil? || opts[:script].nil?)
|
93
102
|
raise ArgumentError, "Must specify both task type and script when not using a Template ID"
|
94
103
|
end
|
95
104
|
|
@@ -116,7 +125,7 @@ module Kapacitor
|
|
116
125
|
|
117
126
|
req['vars'] = opts[:vars] if opts[:vars]
|
118
127
|
|
119
|
-
api_post(endpoint: '
|
128
|
+
api_post(endpoint: 'tasks', data: req)
|
120
129
|
end
|
121
130
|
|
122
131
|
# Update a Kapacitor task
|
@@ -141,7 +150,7 @@ module Kapacitor
|
|
141
150
|
raise ArgumentError, "Kapacitor task status can be either 'enabled' or 'disabled'" unless opts[:status] == 'enabled' || opts[:status] == 'disabled'
|
142
151
|
end
|
143
152
|
|
144
|
-
api_patch(endpoint: "
|
153
|
+
api_patch(endpoint: "tasks/#{id}", data: req) unless req.empty?
|
145
154
|
end
|
146
155
|
|
147
156
|
# Delete a Kapacitor task
|
@@ -149,7 +158,7 @@ module Kapacitor
|
|
149
158
|
# @param id [String] Task ID
|
150
159
|
#
|
151
160
|
def delete_task(id:)
|
152
|
-
api_delete(endpoint: "
|
161
|
+
api_delete(endpoint: "tasks/#{id}")
|
153
162
|
end
|
154
163
|
|
155
164
|
# Retrieve Kapacitor tasks
|
@@ -179,37 +188,36 @@ module Kapacitor
|
|
179
188
|
#
|
180
189
|
# @param id [String] Handler ID
|
181
190
|
# @param topic [String] Topic name
|
182
|
-
# @param
|
191
|
+
# @param kind [String] Kind of handler
|
192
|
+
# @param match [String] Lambda expression
|
193
|
+
# @param options [Hash] Handler options
|
183
194
|
#
|
184
|
-
def define_topic_handler(id:, topic:,
|
185
|
-
req = {
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
raise ArgumentError, "Missing required kind attribute for action #{action}"
|
193
|
-
end
|
194
|
-
|
195
|
-
req['actions'] = actions
|
196
|
-
api_post(endpoint: "/alerts/topics/#{topic}/handlers", data: req)
|
195
|
+
def define_topic_handler(id:, topic:, kind:, match: nil, options: {})
|
196
|
+
req = {
|
197
|
+
'id': id,
|
198
|
+
'kind': kind
|
199
|
+
}
|
200
|
+
req['match'] = match unless match.nil?
|
201
|
+
req['options'] = options
|
202
|
+
api_post(endpoint: "alerts/topics/#{topic}/handlers", data: req)
|
197
203
|
end
|
198
204
|
|
199
205
|
# Update a topic handler
|
200
206
|
#
|
201
207
|
# @param id [String] Handler ID
|
202
208
|
# @param topic [String] Topic name
|
203
|
-
# @param
|
209
|
+
# @param kind [String] Kind of handler
|
210
|
+
# @param match [String] Lambda expression
|
211
|
+
# @param options [Hash] Handler options
|
204
212
|
#
|
205
|
-
def update_topic_handler(id:, topic:,
|
206
|
-
req = {
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
req['
|
212
|
-
api_put(endpoint: "
|
213
|
+
def update_topic_handler(id:, topic:, kind:, match: nil, options: {})
|
214
|
+
req = {
|
215
|
+
'id': id,
|
216
|
+
'kind': kind
|
217
|
+
}
|
218
|
+
req['match'] = match unless match.nil?
|
219
|
+
req['options'] = options
|
220
|
+
api_put(endpoint: "alerts/topics/#{topic}/handlers/#{id}", data: req) unless req.empty?
|
213
221
|
end
|
214
222
|
|
215
223
|
# Delete a topic handler
|
@@ -218,7 +226,7 @@ module Kapacitor
|
|
218
226
|
# @param topic [String] Topic name
|
219
227
|
#
|
220
228
|
def delete_topic_handler(id:, topic:)
|
221
|
-
api_delete(endpoint: "
|
229
|
+
api_delete(endpoint: "alerts/topics/#{topic}/handlers/#{id}")
|
222
230
|
end
|
223
231
|
|
224
232
|
# Retrieve topic's handlers
|
@@ -240,14 +248,14 @@ private
|
|
240
248
|
def api_get(endpoint:, query: nil)
|
241
249
|
begin
|
242
250
|
resp = self.http.get([self.url, endpoint].join('/'), query, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}
|
251
|
+
begin
|
252
|
+
data = JSON.parse(resp.body) unless resp.body.empty?
|
253
|
+
rescue JSON::ParserError
|
254
|
+
raise Exception, "Failed to decode response message"
|
255
|
+
end
|
256
|
+
if resp.status != 200
|
257
|
+
error = data.include?('error') ? data['error'] : data.inspect if data
|
258
|
+
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
|
251
259
|
end
|
252
260
|
rescue
|
253
261
|
raise Exception, "Failed to execute GET request to Kapacitor REST API (#{$!})"
|
@@ -264,14 +272,14 @@ private
|
|
264
272
|
def api_post(endpoint:, data:)
|
265
273
|
begin
|
266
274
|
resp = self.http.post([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}
|
275
|
+
begin
|
276
|
+
data = JSON.parse(resp.body) unless resp.body.empty?
|
277
|
+
rescue JSON::ParserError
|
278
|
+
raise Exception, "Failed to decode response message"
|
279
|
+
end
|
280
|
+
if resp.status != 200
|
281
|
+
error = data.include?('error') ? data['error'] : data.inspect if data
|
282
|
+
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
|
275
283
|
end
|
276
284
|
rescue
|
277
285
|
raise Exception, "Failed to execute POST request to Kapacitor REST API (#{$!})"
|
@@ -287,16 +295,14 @@ private
|
|
287
295
|
def api_delete(endpoint:)
|
288
296
|
begin
|
289
297
|
resp = self.http.delete([self.url, endpoint].join('/'), {'Content-type' => 'application/json', 'Accept' => 'application/json'})
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
else
|
299
|
-
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
|
298
|
+
begin
|
299
|
+
data = JSON.parse(resp.body) unless resp.body.empty?
|
300
|
+
rescue JSON::ParserError
|
301
|
+
raise Exception, "Failed to decode response message"
|
302
|
+
end
|
303
|
+
if resp.status != 204
|
304
|
+
error = data.include?('error') ? data['error'] : data.inspect if data
|
305
|
+
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
|
300
306
|
end
|
301
307
|
rescue
|
302
308
|
raise Exception, "Failed to execute DELETE request to Kapacitor REST API (#{$!})"
|
@@ -313,14 +319,14 @@ private
|
|
313
319
|
def api_patch(endpoint:, data:)
|
314
320
|
begin
|
315
321
|
resp = self.http.patch([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
raise Exception, "Query returned a non successful HTTP code (
|
322
|
+
begin
|
323
|
+
data = JSON.parse(resp.body) unless resp.body.empty?
|
324
|
+
rescue JSON::ParserError
|
325
|
+
raise Exception, "Failed to decode response message"
|
326
|
+
end
|
327
|
+
if resp.status != 200
|
328
|
+
error = data.include?('error') ? data['error'] : data.inspect if data
|
329
|
+
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
|
324
330
|
end
|
325
331
|
rescue
|
326
332
|
raise Exception, "Failed to execute PATCH request to Kapacitor REST API (#{$!})"
|
@@ -337,14 +343,14 @@ private
|
|
337
343
|
def api_put(endpoint:, data:)
|
338
344
|
begin
|
339
345
|
resp = self.http.put([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}
|
346
|
+
begin
|
347
|
+
data = JSON.parse(resp.body) unless resp.body.empty?
|
348
|
+
rescue JSON::ParserError
|
349
|
+
raise Exception, "Failed to decode response message"
|
350
|
+
end
|
351
|
+
if resp.status != 200
|
352
|
+
error = data.include?('error') ? data['error'] : data.inspect if data
|
353
|
+
raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
|
348
354
|
end
|
349
355
|
rescue
|
350
356
|
raise Exception, "Failed to execute PUT request to Kapacitor REST API (#{$!})"
|
data/lib/kapacitor/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kapacitor-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matteo Cerutti
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|
@@ -52,7 +52,7 @@ homepage: https://github.com/m4ce/kapacitor-ruby
|
|
52
52
|
licenses:
|
53
53
|
- Apache 2.0
|
54
54
|
metadata: {}
|
55
|
-
post_install_message:
|
55
|
+
post_install_message:
|
56
56
|
rdoc_options: []
|
57
57
|
require_paths:
|
58
58
|
- lib
|
@@ -67,9 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
|
-
|
71
|
-
|
72
|
-
signing_key:
|
70
|
+
rubygems_version: 3.0.1
|
71
|
+
signing_key:
|
73
72
|
specification_version: 4
|
74
73
|
summary: Ruby client library that allows to interact with the Kapacitor JSON REST
|
75
74
|
API
|