kapacitor-ruby 1.0.3 → 1.0.8

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: 30d665b31a22923dcfe3f323870cd2dbca608f4d
4
- data.tar.gz: 2ade7584998c461366148de74ed744e29f111396
2
+ SHA256:
3
+ metadata.gz: b992f75fe340aaeb410f88845f8b0cf22f404a68f2407920450aeb8f81a749cc
4
+ data.tar.gz: 0f86926ff9b4354aa26f34cb82dbdcc48678dd9cf89f7b3a7b4161573d0b1265
5
5
  SHA512:
6
- metadata.gz: 067d28ae1d7d640bbce58ee6304859065057d1d554a7685f08ae2703edd17504d771aac95c8cdf144554fad8060c0e74511591dea6344c67b25191a973110f2a
7
- data.tar.gz: ae4153783f39f02933cf12f19e24c390a88bf173a6c363962aea1369ca6b9045441e377cdc84ba977053760b47c564ae1f687852b07a457152868672bbfb07f6
6
+ metadata.gz: 5766810cf37cf0f73c05085e5dc0e2698f2439a48b065a8c78b0dd1516ea08d5597cfd5ddc8b266f56ee175f0627ac7667f3fa9ac2b515406efb7343862cbc6d
7
+ data.tar.gz: e2e22bb48e84baf6803fa099364991221fb5f0c1d4746a38811d9fc88e208c98b6ba312eb226f27b5a4f4011580da787bb9885ddca86f43202affd92f77de6b5
@@ -13,7 +13,7 @@ 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: 'v1preview')
16
+ def initialize(url: 'http://localhost:9092/kapacitor', version: 'v1')
17
17
  @http = HTTPClient.new
18
18
  @url = [url, version].join('/')
19
19
  end
@@ -61,6 +61,15 @@ module Kapacitor
61
61
  api_delete(endpoint: "templates/#{id}")
62
62
  end
63
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'] }
71
+ end
72
+
64
73
  # Retrieve Kapacitor templates
65
74
  #
66
75
  # @param offset [Integer] Offset count for paginating through 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['template_id'].nil? && (opts['type'].nil? || opts['script'].nil?)
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
 
@@ -130,7 +139,7 @@ module Kapacitor
130
139
  req['type'] = opts[:type] if opts[:type]
131
140
  req['dbrps'] = opts[:dbrps] if opts[:dbrps]
132
141
  req['script'] = opts[:script] if opts[:script]
133
- req['status'] = opts[:status] if opts[:status]
142
+ req['status'] = 'disabled'
134
143
  req['vars'] = opts[:vars] if opts[:vars]
135
144
 
136
145
  if opts[:type]
@@ -142,6 +151,12 @@ module Kapacitor
142
151
  end
143
152
 
144
153
  api_patch(endpoint: "tasks/#{id}", data: req) unless req.empty?
154
+
155
+ if opts[:status] == 'enabled'
156
+ req['status'] = opts[:status] if opts[:status]
157
+ api_patch(endpoint: "tasks/#{id}", data: req) unless req.empty?
158
+ end
159
+
145
160
  end
146
161
 
147
162
  # Delete a Kapacitor task
@@ -179,20 +194,17 @@ module Kapacitor
179
194
  #
180
195
  # @param id [String] Handler ID
181
196
  # @param topic [String] Topic name
182
- # @param actions [Array[Hash]] Handler actions
197
+ # @param kind [String] Kind of handler
198
+ # @param match [String] Lambda expression
199
+ # @param options [Hash] Handler options
183
200
  #
184
- def define_topic_handler(id:, topic:, actions:)
185
- req = {}
186
- req['id'] = id
187
-
188
- actions = [actions] unless actions.is_a?(Array)
189
- raise ArgumentError, "Kapacitor topic handler requires one or more actions" unless actions.size > 0
190
-
191
- actions.each do |action|
192
- raise ArgumentError, "Missing required kind attribute for action #{action}"
193
- end
194
-
195
- req['actions'] = actions
201
+ def define_topic_handler(id:, topic:, kind:, match: nil, options: {})
202
+ req = {
203
+ 'id': id,
204
+ 'kind': kind
205
+ }
206
+ req['match'] = match unless match.nil?
207
+ req['options'] = options
196
208
  api_post(endpoint: "alerts/topics/#{topic}/handlers", data: req)
197
209
  end
198
210
 
@@ -200,15 +212,17 @@ module Kapacitor
200
212
  #
201
213
  # @param id [String] Handler ID
202
214
  # @param topic [String] Topic name
203
- # @param actions [Array[Hash]] Handler actions
215
+ # @param kind [String] Kind of handler
216
+ # @param match [String] Lambda expression
217
+ # @param options [Hash] Handler options
204
218
  #
205
- def update_topic_handler(id:, topic:, actions:)
206
- req = {}
207
-
208
- actions = [actions] unless actions.is_a?(Array)
209
- raise ArgumentError, "Kapacitor topic handler requires one or more actions" unless actions.size > 0
210
-
211
- req['actions'] = actions
219
+ def update_topic_handler(id:, topic:, kind:, match: nil, options: nil)
220
+ req = {
221
+ 'id': id,
222
+ 'kind': kind
223
+ }
224
+ req['match'] = match unless match.nil?
225
+ req['options'] = options unless options.nil?
212
226
  api_put(endpoint: "alerts/topics/#{topic}/handlers/#{id}", data: req) unless req.empty?
213
227
  end
214
228
 
@@ -240,14 +254,14 @@ private
240
254
  def api_get(endpoint:, query: nil)
241
255
  begin
242
256
  resp = self.http.get([self.url, endpoint].join('/'), query, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
243
- if resp.status == 200
244
- begin
245
- data = JSON.parse(resp.body)
246
- rescue JSON::ParserError
247
- raise Exception, "Failed to decode response message"
248
- end
249
- else
250
- raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
257
+ begin
258
+ data = JSON.parse(resp.body) unless resp.body.empty?
259
+ rescue JSON::ParserError
260
+ raise Exception, "Failed to decode response message"
261
+ end
262
+ if resp.status != 200
263
+ error = data.include?('error') ? data['error'] : data.inspect if data
264
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
251
265
  end
252
266
  rescue
253
267
  raise Exception, "Failed to execute GET request to Kapacitor REST API (#{$!})"
@@ -264,14 +278,14 @@ private
264
278
  def api_post(endpoint:, data:)
265
279
  begin
266
280
  resp = self.http.post([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
267
- if resp.status == 200
268
- begin
269
- data = JSON.parse(resp.body)
270
- rescue JSON::ParserError
271
- raise Exception, "Failed to decode response message"
272
- end
273
- else
274
- raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
281
+ begin
282
+ data = JSON.parse(resp.body) unless resp.body.empty?
283
+ rescue JSON::ParserError
284
+ raise Exception, "Failed to decode response message"
285
+ end
286
+ if resp.status != 200
287
+ error = data.include?('error') ? data['error'] : data.inspect if data
288
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
275
289
  end
276
290
  rescue
277
291
  raise Exception, "Failed to execute POST request to Kapacitor REST API (#{$!})"
@@ -287,16 +301,14 @@ private
287
301
  def api_delete(endpoint:)
288
302
  begin
289
303
  resp = self.http.delete([self.url, endpoint].join('/'), {'Content-type' => 'application/json', 'Accept' => 'application/json'})
290
- if resp.status == 204
291
- if resp.body
292
- begin
293
- data = JSON.parse(resp.body)
294
- rescue JSON::ParserError
295
- raise Exception, "Failed to decode response message"
296
- end
297
- end
298
- else
299
- raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
304
+ begin
305
+ data = JSON.parse(resp.body) unless resp.body.empty?
306
+ rescue JSON::ParserError
307
+ raise Exception, "Failed to decode response message"
308
+ end
309
+ if resp.status != 204
310
+ error = data.include?('error') ? data['error'] : data.inspect if data
311
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
300
312
  end
301
313
  rescue
302
314
  raise Exception, "Failed to execute DELETE request to Kapacitor REST API (#{$!})"
@@ -313,14 +325,14 @@ private
313
325
  def api_patch(endpoint:, data:)
314
326
  begin
315
327
  resp = self.http.patch([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
316
- if resp.status == 200
317
- begin
318
- data = JSON.parse(resp.body)
319
- rescue JSON::ParserError
320
- raise Exception, "Failed to decode response message"
321
- end
322
- else
323
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.status}, Reason: #{resp.reason})"
328
+ begin
329
+ data = JSON.parse(resp.body) unless resp.body.empty?
330
+ rescue JSON::ParserError
331
+ raise Exception, "Failed to decode response message"
332
+ end
333
+ if resp.status != 200
334
+ error = data.include?('error') ? data['error'] : data.inspect if data
335
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
324
336
  end
325
337
  rescue
326
338
  raise Exception, "Failed to execute PATCH request to Kapacitor REST API (#{$!})"
@@ -337,14 +349,14 @@ private
337
349
  def api_put(endpoint:, data:)
338
350
  begin
339
351
  resp = self.http.put([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
340
- if resp.status == 200
341
- begin
342
- data = JSON.parse(resp.body)
343
- rescue JSON::ParserError
344
- raise Exception, "Failed to decode response message"
345
- end
346
- else
347
- raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
352
+ begin
353
+ data = JSON.parse(resp.body) unless resp.body.empty?
354
+ rescue JSON::ParserError
355
+ raise Exception, "Failed to decode response message"
356
+ end
357
+ if resp.status != 200
358
+ error = data.include?('error') ? data['error'] : data.inspect if data
359
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
348
360
  end
349
361
  rescue
350
362
  raise Exception, "Failed to execute PUT request to Kapacitor REST API (#{$!})"
@@ -3,7 +3,7 @@
3
3
  #
4
4
 
5
5
  module Kapacitor
6
- VERSION = "1.0.3"
6
+ VERSION = "1.0.8"
7
7
 
8
8
  def self.version
9
9
  VERSION
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.3
4
+ version: 1.0.8
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: 2017-09-04 00:00:00.000000000 Z
11
+ date: 2020-12-11 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
- rubyforge_project:
71
- rubygems_version: 2.6.8
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