kapacitor-ruby 1.0.0 → 1.0.5

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: 102b40a3d2e642e578b98767820e73cfda264ba6
4
- data.tar.gz: 86987d17ef079c29882d6987c39746346034bd3a
2
+ SHA256:
3
+ metadata.gz: b44ea41ae78d506eb56c16be57c70948724e91f5f07f6d25f1a54ccd84cfc5f7
4
+ data.tar.gz: 36200bc005660d681749a7106219efeb69447429692df1c202f422eb48ff2e48
5
5
  SHA512:
6
- metadata.gz: 157f9a71d7a9bcef701a72d25d602151acc7f12b3376e00fa9273c30d12f47c2fb12b133989005c9fbe8d631659017762dcd421af2cc650409900fbd50fabc97
7
- data.tar.gz: e82e8b70df2a1bc3051e6912d46e07fde3a16f5ec0a9848ca9418402bc0e3b1131c0e2611587b7d4d885a21c024a738222359dedc00e4a65ee7ee7213806f587
6
+ metadata.gz: 5d84c1cb3ae92d8f59335dcbbd056df4a5fef42454b495e92226b8d762629a2fa88e1c82976f60dd39b275195fd0675680c80a978f85c9353838b879c56bc0f1
7
+ data.tar.gz: 6eb9cd633c323513e310a059e470ee81e7426ae362fddfaab6ae5930baab2302ca7847709ac3dda15125f90895258191ca8c8805e98339405a4bdfd3080cdb6f
@@ -1,10 +1,10 @@
1
- require 'net/http'
1
+ require 'httpclient'
2
2
  require 'json'
3
3
 
4
4
  module Kapacitor
5
5
  class Client
6
6
  # @return [URI] Kapacitor REST API URL
7
- attr_reader :uri
7
+ attr_reader :url
8
8
  # @return [Net::HTTP] HTTP client instance
9
9
  attr_reader :http
10
10
 
@@ -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(host: 'http://localhost:9092', version: 'v1preview')
17
- @uri = URI.parse("#{host}/kapacitor/#{version}")
18
- @http = Net::HTTP.new(@uri.host, @uri.port)
16
+ def initialize(url: 'http://localhost:9092/kapacitor', version: 'v1')
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: '/templates', data: req)
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: "/templates/#{id}", data: req) unless req.empty?
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: "/templates/#{id}")
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")['topics']
70
+ return res['topics'].map { |v| v['id'] }
62
71
  end
63
72
 
64
73
  # Retrieve Kapacitor templates
@@ -71,7 +80,7 @@ module Kapacitor
71
80
  ret = []
72
81
 
73
82
  loop do
74
- res = api_get(endpoint: "/templates?offset=#{offset}&limit=#{limit}")['templates']
83
+ res = api_get(endpoint: "templates?offset=#{offset}&limit=#{limit}")['templates']
75
84
  break unless res.size > 0
76
85
  ret += res
77
86
  offset += limit
@@ -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
 
@@ -116,7 +125,7 @@ module Kapacitor
116
125
 
117
126
  req['vars'] = opts[:vars] if opts[:vars]
118
127
 
119
- api_post(endpoint: '/tasks', data: req)
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: "/tasks/#{id}", data: req) unless req.empty?
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: "/tasks/#{id}")
161
+ api_delete(endpoint: "tasks/#{id}")
153
162
  end
154
163
 
155
164
  # Retrieve Kapacitor tasks
@@ -162,11 +171,11 @@ module Kapacitor
162
171
  tasks = []
163
172
 
164
173
  loop do
165
- res = api_get(endpoint: "/tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
174
+ res = api_get(endpoint: "tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
166
175
  break unless res.size > 0
167
176
 
168
177
  res.each do |task|
169
- tasks << api_get(endpoint: "/tasks/#{task['id']}")
178
+ tasks << api_get(endpoint: "tasks/#{task['id']}")
170
179
  end
171
180
 
172
181
  offset += limit
@@ -193,7 +202,7 @@ module Kapacitor
193
202
  end
194
203
 
195
204
  req['actions'] = actions
196
- api_post(endpoint: "/alerts/topics/#{topic}/handlers", data: req)
205
+ api_post(endpoint: "alerts/topics/#{topic}/handlers", data: req)
197
206
  end
198
207
 
199
208
  # Update a topic handler
@@ -209,7 +218,7 @@ module Kapacitor
209
218
  raise ArgumentError, "Kapacitor topic handler requires one or more actions" unless actions.size > 0
210
219
 
211
220
  req['actions'] = actions
212
- api_put(endpoint: "/alerts/topics/#{topic}/handlers/#{id}", data: req) unless req.empty?
221
+ api_put(endpoint: "alerts/topics/#{topic}/handlers/#{id}", data: req) unless req.empty?
213
222
  end
214
223
 
215
224
  # Delete a topic handler
@@ -218,7 +227,7 @@ module Kapacitor
218
227
  # @param topic [String] Topic name
219
228
  #
220
229
  def delete_topic_handler(id:, topic:)
221
- api_delete(endpoint: "/alerts/topics/#{topic}/handlers/#{id}")
230
+ api_delete(endpoint: "alerts/topics/#{topic}/handlers/#{id}")
222
231
  end
223
232
 
224
233
  # Retrieve topic's handlers
@@ -227,28 +236,27 @@ module Kapacitor
227
236
  # @return [Array[Hash]] List of handlers
228
237
  #
229
238
  def topic_handlers(topic:)
230
- return api_get(endpoint: "/alerts/topics/#{topic}/handlers")['handlers']
239
+ api_get(endpoint: "alerts/topics/#{topic}/handlers")['handlers']
231
240
  end
232
241
 
233
242
  private
234
243
  # Perform a HTTP GET request
235
244
  #
236
245
  # @param endpoint [String] HTTP API endpoint
246
+ # @param query [String] HTTP query
237
247
  # @return [Array[Hash], Hash] API response
238
248
  #
239
- def api_get(endpoint:)
249
+ def api_get(endpoint:, query: nil)
240
250
  begin
241
- req = Net::HTTP::Get.new(self.uri.path + endpoint, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
242
- resp = self.http.request(req)
243
-
244
- if resp.code == '200'
245
- begin
246
- data = JSON.parse(resp.body)
247
- rescue JSON::ParserError
248
- raise Exception, "Failed to decode response message"
249
- end
250
- else
251
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
251
+ resp = self.http.get([self.url, endpoint].join('/'), query, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
252
+ begin
253
+ data = JSON.parse(resp.body) unless resp.body.blank?
254
+ rescue JSON::ParserError
255
+ raise Exception, "Failed to decode response message"
256
+ end
257
+ if resp.status != 200
258
+ error = data.include?('error') ? data['error'] : data.inspect if data
259
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
252
260
  end
253
261
  rescue
254
262
  raise Exception, "Failed to execute GET request to Kapacitor REST API (#{$!})"
@@ -264,18 +272,15 @@ private
264
272
  #
265
273
  def api_post(endpoint:, data:)
266
274
  begin
267
- req = Net::HTTP::Post.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
268
- req.body = data.to_json
269
- resp = self.http.request(req)
270
-
271
- if resp.code == '200'
272
- begin
273
- data = JSON.parse(resp.body)
274
- rescue JSON::ParserError
275
- raise Exception, "Failed to decode response message"
276
- end
277
- else
278
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
275
+ resp = self.http.post([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
276
+ begin
277
+ data = JSON.parse(resp.body) unless resp.body.blank?
278
+ rescue JSON::ParserError
279
+ raise Exception, "Failed to decode response message"
280
+ end
281
+ if resp.status != 200
282
+ error = data.include?('error') ? data['error'] : data.inspect if data
283
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
279
284
  end
280
285
  rescue
281
286
  raise Exception, "Failed to execute POST request to Kapacitor REST API (#{$!})"
@@ -290,19 +295,15 @@ private
290
295
  #
291
296
  def api_delete(endpoint:)
292
297
  begin
293
- req = Net::HTTP::Delete.new(self.uri.path + endpoint, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
294
- resp = self.http.request(req)
295
-
296
- if resp.code == '204'
297
- if resp.body
298
- begin
299
- data = JSON.parse(resp.body)
300
- rescue JSON::ParserError
301
- raise Exception, "Failed to decode response message"
302
- end
303
- end
304
- else
305
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
298
+ resp = self.http.delete([self.url, endpoint].join('/'), {'Content-type' => 'application/json', 'Accept' => 'application/json'})
299
+ begin
300
+ data = JSON.parse(resp.body) unless resp.body.blank?
301
+ rescue JSON::ParserError
302
+ raise Exception, "Failed to decode response message"
303
+ end
304
+ if resp.status != 204
305
+ error = data.include?('error') ? data['error'] : data.inspect if data
306
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
306
307
  end
307
308
  rescue
308
309
  raise Exception, "Failed to execute DELETE request to Kapacitor REST API (#{$!})"
@@ -318,18 +319,15 @@ private
318
319
  #
319
320
  def api_patch(endpoint:, data:)
320
321
  begin
321
- req = Net::HTTP::Patch.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
322
- req.body = data.to_json
323
- resp = self.http.request(req)
324
-
325
- if resp.code == '200'
326
- begin
327
- data = JSON.parse(resp.body)
328
- rescue JSON::ParserError
329
- raise Exception, "Failed to decode response message"
330
- end
331
- else
332
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
322
+ resp = self.http.patch([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
323
+ begin
324
+ data = JSON.parse(resp.body) unless resp.body.blank?
325
+ rescue JSON::ParserError
326
+ raise Exception, "Failed to decode response message"
327
+ end
328
+ if resp.status != 200
329
+ error = data.include?('error') ? data['error'] : data.inspect if data
330
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
333
331
  end
334
332
  rescue
335
333
  raise Exception, "Failed to execute PATCH request to Kapacitor REST API (#{$!})"
@@ -345,18 +343,15 @@ private
345
343
  #
346
344
  def api_put(endpoint:, data:)
347
345
  begin
348
- req = Net::HTTP::Put.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
349
- req.body = data.to_json
350
- resp = self.http.request(req)
351
-
352
- if resp.code == '200'
353
- begin
354
- data = JSON.parse(resp.body)
355
- rescue JSON::ParserError
356
- raise Exception, "Failed to decode response message"
357
- end
358
- else
359
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
346
+ resp = self.http.put([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
347
+ begin
348
+ data = JSON.parse(resp.body) unless resp.body.blank?
349
+ rescue JSON::ParserError
350
+ raise Exception, "Failed to decode response message"
351
+ end
352
+ if resp.status != 200
353
+ error = data.include?('error') ? data['error'] : data.inspect if data
354
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason}#{", Error: #{error}" if error}"
360
355
  end
361
356
  rescue
362
357
  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.0"
6
+ VERSION = "1.0.5"
7
7
 
8
8
  def self.version
9
9
  VERSION
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kapacitor-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.5
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-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: json
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,7 +52,7 @@ homepage: https://github.com/m4ce/kapacitor-ruby
38
52
  licenses:
39
53
  - Apache 2.0
40
54
  metadata: {}
41
- post_install_message:
55
+ post_install_message:
42
56
  rdoc_options: []
43
57
  require_paths:
44
58
  - lib
@@ -53,9 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  requirements: []
56
- rubyforge_project:
57
- rubygems_version: 2.6.8
58
- signing_key:
70
+ rubygems_version: 3.0.1
71
+ signing_key:
59
72
  specification_version: 4
60
73
  summary: Ruby client library that allows to interact with the Kapacitor JSON REST
61
74
  API