kapacitor-ruby 0.0.14 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82ce3690bdcf6c10b5f3470557f61812dacdf11b
4
- data.tar.gz: bf3d8bfb1845055a22735db2450808c63c72b6e4
3
+ metadata.gz: 102b40a3d2e642e578b98767820e73cfda264ba6
4
+ data.tar.gz: 86987d17ef079c29882d6987c39746346034bd3a
5
5
  SHA512:
6
- metadata.gz: 2cdf19fe98d1d673e0ca8a340a9f8f2934e4cc77cd2624f16f7cdda2f4abd6916e5896edc61df80b015fe0ac9f3579564ebf4775fc94b0155179da7431143662
7
- data.tar.gz: 8e8f9f3bf74a98f6e67cd456a34605626d3a42b6f4ad10d83cb01303a5b9d70a630561c759c2b67bbdbabc4c55474fa16fb8da21879dbae881ddae424923d4e1
6
+ metadata.gz: 157f9a71d7a9bcef701a72d25d602151acc7f12b3376e00fa9273c30d12f47c2fb12b133989005c9fbe8d631659017762dcd421af2cc650409900fbd50fabc97
7
+ data.tar.gz: e82e8b70df2a1bc3051e6912d46e07fde3a16f5ec0a9848ca9418402bc0e3b1131c0e2611587b7d4d885a21c024a738222359dedc00e4a65ee7ee7213806f587
data/README.md CHANGED
@@ -11,63 +11,7 @@ gem install kapacitor-ruby
11
11
  ```
12
12
 
13
13
  ## Usage
14
- ```
15
- require 'kapacitor/client'
16
- kapacitor = Kapacitor::Client.new(host: 'localhost:9092', version: 'v1')
17
- ```
18
-
19
- ### Templates
20
-
21
- #### `define_template`
22
- Create a new template definition
23
- ```
24
- define_template(id: 'name', type: 'stream', script: 'tickscript')
25
- ```
26
-
27
- #### `update_template`
28
- Update one or more templates' options
29
- ```
30
- update_template(id: 'name', type: 'batch')
31
- ```
32
-
33
- #### `delete_template`
34
- Delete a template
35
- ```
36
- delete_template(id: 'name')
37
- ```
38
-
39
- #### `templates`
40
- Fetch all templates
41
- ```
42
- templates()
43
- ```
44
-
45
- ### Tasks
46
-
47
- #### `define_task`
48
- Create a new task
49
- ```
50
- define_task(id: 'name', template_id: 'optional template', type: 'stream', dbrps: [{'db' => 'telegraf', 'rp' => 'default'}], script: 'tickscript', status: 'enabled', vars: {})
51
- ```
52
-
53
- #### `update_task`
54
- Update one or more task's options
55
-
56
- ```
57
- update_task(id: 'name', template_id: 'optional template', type: 'stream', dbrps: [{'db' => 'telegraf', 'rp' => 'default'}], script: 'tickscript', status: 'enabled', vars: {})
58
- ```
59
-
60
- #### `delete_task`
61
- Delete a task
62
- ```
63
- delete_task(id: 'name')
64
- ```
65
-
66
- #### `tasks`
67
- Fetch all tasks
68
- ```
69
- tasks()
70
- ```
14
+ Online documentation is available at [rubydoc](http://www.rubydoc.info/gems/kapacitor-ruby/).
71
15
 
72
16
  ## Contact
73
17
  Matteo Cerutti - matteo.cerutti@hotmail.co.uk
@@ -1,136 +1,172 @@
1
- #
2
- # client.rb
3
- #
4
-
5
1
  require 'net/http'
6
2
  require 'json'
7
3
 
8
4
  module Kapacitor
9
5
  class Client
10
- attr_reader :uri, :http
11
-
12
- def initialize(opts = {})
13
- host = opts['host'] || 'localhost:9092'
14
- version = opts['version'] || 'v1'
15
-
16
- @uri = URI.parse("http://#{host}/kapacitor/#{version}")
6
+ # @return [URI] Kapacitor REST API URL
7
+ attr_reader :uri
8
+ # @return [Net::HTTP] HTTP client instance
9
+ attr_reader :http
10
+
11
+ # Create a new client
12
+ #
13
+ # @param url [String] Kapacitor REST API's URL (defaults to `http://localhost:9092`)
14
+ # @param version [Integer] API version (defaults to `v1preview`)
15
+ #
16
+ def initialize(host: 'http://localhost:9092', version: 'v1preview')
17
+ @uri = URI.parse("#{host}/kapacitor/#{version}")
17
18
  @http = Net::HTTP.new(@uri.host, @uri.port)
18
19
  end
19
20
 
20
- def define_template(id, opts = {})
21
- raise ArgumentError, "Kapacitor template type is required" unless opts['type']
22
- raise ArgumentError, "Kapacitor template tickscript required" unless opts['script']
23
-
24
- if opts['type']
25
- raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
26
- end
21
+ # Define a Kapacitor template
22
+ #
23
+ # @param id [String] Template ID
24
+ # @param type [String] Template type. Valid values: `batch`, `stream`.
25
+ # @param script [String] Tick script
26
+ #
27
+ def define_template(id:, type:, script:)
28
+ raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless type == 'batch' || type == 'stream'
27
29
 
28
30
  req = {
29
31
  'id' => id,
30
- 'type' => opts['type'],
31
- 'script' => opts['script']
32
+ 'type' => type,
33
+ 'script' => script
32
34
  }
33
35
 
34
- api_post('/templates', req)
36
+ api_post(endpoint: '/templates', data: req)
35
37
  end
36
38
 
37
- def update_template(id, opts = {})
39
+ # Update a Kapacitor template
40
+ #
41
+ # @param id [String] Template ID
42
+ # @param **opts [Hash] Any number of template parameters to push into the Hash
43
+ #
44
+ def update_template(id:, **opts)
38
45
  req = {}
39
- req['type'] = opts['type'] if opts['type']
40
- req['script'] = opts['script'] if opts['script']
46
+ req['type'] = opts[:type] if opts[:type]
47
+ req['script'] = opts[:script] if opts[:script]
41
48
 
42
- if opts['type']
43
- raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
49
+ if opts[:type]
50
+ raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless opts[:type] == 'batch' or opts[:type] == 'stream'
44
51
  end
45
52
 
46
- api_patch("/templates/#{id}", req) unless req.empty?
53
+ api_patch(endpoint: "/templates/#{id}", data: req) unless req.empty?
47
54
  end
48
55
 
49
- def delete_template(id)
50
- api_delete("/templates/#{id}")
56
+ # Delete a Kapacitor template
57
+ #
58
+ # @param id [String] Template ID
59
+ #
60
+ def delete_template(id:)
61
+ api_delete(endpoint: "/templates/#{id}")
51
62
  end
52
63
 
64
+ # Retrieve Kapacitor templates
65
+ #
66
+ # @param offset [Integer] Offset count for paginating through templates
67
+ # @param limit [Integer] Maximum number of templates to return
68
+ # @return [Array[Hash]] List of templates
69
+ #
53
70
  def templates(offset: 0, limit: 100)
54
- templates = []
71
+ ret = []
55
72
 
56
73
  loop do
57
- res = api_get("/templates?offset=#{offset}&limit=#{limit}")['templates']
74
+ res = api_get(endpoint: "/templates?offset=#{offset}&limit=#{limit}")['templates']
58
75
  break unless res.size > 0
59
- templates += res
76
+ ret += res
60
77
  offset += limit
61
78
  end
62
79
 
63
- templates
80
+ ret
64
81
  end
65
82
 
66
- def define_task(id, opts = {})
67
- raise ArgumentError, "Kapacitor task dbrps is required" unless opts['dbrps']
68
-
69
- if (opts['template_id'].nil? and opts['type'].nil? and opts['script'].nil?) or (opts['template_id'] and (opts['type'] or opts['script']))
83
+ # Define a Kapacitor task
84
+ #
85
+ # @param id [String] Task ID
86
+ # @param dbrps [String] List of database retention policy pairs the task is allowed to access.
87
+ # @param **opts [Hash] Any number of task parameters to push into the Hash
88
+ #
89
+ def define_task(id:, dbrps:, **opts)
90
+ if (opts[:template_id].nil? && opts[:type].nil? && opts[:script].nil?) || (opts[:template_id] && (opts[:type] || opts[:script]))
70
91
  raise ArgumentError, "Must specify either a Template ID or a script and type"
71
- elsif opts['template_id'].nil? and (opts['type'].nil? or opts['script'].nil?)
92
+ elsif opts['template_id'].nil? && (opts['type'].nil? || opts['script'].nil?)
72
93
  raise ArgumentError, "Must specify both task type and script when not using a Template ID"
73
94
  end
74
95
 
75
- if opts['status']
76
- raise ArgumentError, "Kapacitor task status can be either 'enabled' or 'disabled'" unless (opts['status'] == 'enabled' or opts['status'] == 'disabled')
96
+ if opts[:status]
97
+ raise ArgumentError, "Kapacitor task status can be either 'enabled' or 'disabled'" unless opts[:status] == 'enabled' || opts[:status] == 'disabled'
77
98
  end
78
99
 
79
- if opts['type']
80
- raise ArgumentError, "Kapacitor task type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
100
+ if opts[:type]
101
+ raise ArgumentError, "Kapacitor task type can be either 'batch' or 'stream'" unless opts[:type] == 'batch' || opts[:type] == 'stream'
81
102
  end
82
103
 
83
104
  req = {
84
105
  'id' => id,
85
- 'dbrps' => opts['dbrps'],
86
- 'status' => opts['status'] || 'enabled'
106
+ 'dbrps' => dbrps,
107
+ 'status' => opts[:status] || 'enabled'
87
108
  }
88
109
 
89
- if opts['template_id']
90
- req['template-id'] = opts['template_id']
110
+ if opts[:template_id]
111
+ req['template-id'] = opts[:template_id]
91
112
  else
92
- req['type'] = opts['type']
93
- req['script'] = opts['script']
113
+ req['type'] = opts[:type]
114
+ req['script'] = opts[:script]
94
115
  end
95
116
 
96
- req['vars'] = opts['vars'] if opts['vars']
117
+ req['vars'] = opts[:vars] if opts[:vars]
97
118
 
98
- api_post('/tasks', req)
119
+ api_post(endpoint: '/tasks', data: req)
99
120
  end
100
121
 
101
- def update_task(id, opts = {})
122
+ # Update a Kapacitor task
123
+ #
124
+ # @param id [String] Task ID
125
+ # @param **opts [Hash] Any number of task parameters to push into the Hash
126
+ #
127
+ def update_task(id:, **opts)
102
128
  req = {}
103
- req['template-id'] = opts['template_id'] if opts['template_id']
104
- req['type'] = opts['type'] if opts['type']
105
- req['dbrps'] = opts['dbrps'] if opts['dbrps']
106
- req['script'] = opts['script'] if opts['script']
107
- req['status'] = opts['status'] if opts['status']
108
- req['vars'] = opts['vars'] if opts['vars']
109
-
110
- if opts['type']
111
- raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless (opts['type'] == 'batch' or opts['type'] == 'stream')
129
+ req['template-id'] = opts[:template_id] if opts[:template_id]
130
+ req['type'] = opts[:type] if opts[:type]
131
+ req['dbrps'] = opts[:dbrps] if opts[:dbrps]
132
+ req['script'] = opts[:script] if opts[:script]
133
+ req['status'] = opts[:status] if opts[:status]
134
+ req['vars'] = opts[:vars] if opts[:vars]
135
+
136
+ if opts[:type]
137
+ raise ArgumentError, "Kapacitor template type can be either 'batch' or 'stream'" unless opts[:type] == 'batch' || opts[:type] == 'stream'
112
138
  end
113
139
 
114
140
  if opts['status']
115
- raise ArgumentError, "Kapacitor task status can be either 'enabled' or 'disabled'" unless (opts['status'] == 'enabled' or opts['status'] == 'disabled')
141
+ raise ArgumentError, "Kapacitor task status can be either 'enabled' or 'disabled'" unless opts[:status] == 'enabled' || opts[:status] == 'disabled'
116
142
  end
117
143
 
118
- api_patch("/tasks/#{id}", req) unless req.empty?
144
+ api_patch(endpoint: "/tasks/#{id}", data: req) unless req.empty?
119
145
  end
120
146
 
121
- def delete_task(id)
122
- api_delete("/tasks/#{id}")
147
+ # Delete a Kapacitor task
148
+ #
149
+ # @param id [String] Task ID
150
+ #
151
+ def delete_task(id:)
152
+ api_delete(endpoint: "/tasks/#{id}")
123
153
  end
124
154
 
155
+ # Retrieve Kapacitor tasks
156
+ #
157
+ # @param offset [Integer] Offset count for paginating through tasks
158
+ # @param limit [Integer] Maximum number of tasks to return
159
+ # @return [Array[Hash]] List of tasks
160
+ #
125
161
  def tasks(offset: 0, limit: 100)
126
162
  tasks = []
127
163
 
128
164
  loop do
129
- res = api_get("/tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
165
+ res = api_get(endpoint: "/tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
130
166
  break unless res.size > 0
131
167
 
132
168
  res.each do |task|
133
- tasks << api_get("/tasks/#{task['id']}")
169
+ tasks << api_get(endpoint: "/tasks/#{task['id']}")
134
170
  end
135
171
 
136
172
  offset += limit
@@ -139,10 +175,70 @@ module Kapacitor
139
175
  tasks
140
176
  end
141
177
 
178
+ # Define a topic handler
179
+ #
180
+ # @param id [String] Handler ID
181
+ # @param topic [String] Topic name
182
+ # @param actions [Array[Hash]] Handler actions
183
+ #
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
196
+ api_post(endpoint: "/alerts/topics/#{topic}/handlers", data: req)
197
+ end
198
+
199
+ # Update a topic handler
200
+ #
201
+ # @param id [String] Handler ID
202
+ # @param topic [String] Topic name
203
+ # @param actions [Array[Hash]] Handler actions
204
+ #
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
212
+ api_put(endpoint: "/alerts/topics/#{topic}/handlers/#{id}", data: req) unless req.empty?
213
+ end
214
+
215
+ # Delete a topic handler
216
+ #
217
+ # @param id [String] Handler ID
218
+ # @param topic [String] Topic name
219
+ #
220
+ def delete_topic_handler(id:, topic:)
221
+ api_delete(endpoint: "/alerts/topics/#{topic}/handlers/#{id}")
222
+ end
223
+
224
+ # Retrieve topic's handlers
225
+ #
226
+ # @param topic [String] Topic name
227
+ # @return [Array[Hash]] List of handlers
228
+ #
229
+ def topic_handlers(topic:)
230
+ return api_get(endpoint: "/alerts/topics/#{topic}/handlers")['handlers']
231
+ end
232
+
142
233
  private
143
- def api_get(q)
234
+ # Perform a HTTP GET request
235
+ #
236
+ # @param endpoint [String] HTTP API endpoint
237
+ # @return [Array[Hash], Hash] API response
238
+ #
239
+ def api_get(endpoint:)
144
240
  begin
145
- req = Net::HTTP::Get.new(self.uri.path + q, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
241
+ req = Net::HTTP::Get.new(self.uri.path + endpoint, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
146
242
  resp = self.http.request(req)
147
243
 
148
244
  if resp.code == '200'
@@ -161,9 +257,14 @@ private
161
257
  data
162
258
  end
163
259
 
164
- def api_post(q, data)
260
+ # Perform a HTTP POST request
261
+ #
262
+ # @param endpoint [String] HTTP API endpoint
263
+ # @param data [Hash] Request data
264
+ #
265
+ def api_post(endpoint:, data:)
165
266
  begin
166
- req = Net::HTTP::Post.new(self.uri.path + q, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
267
+ req = Net::HTTP::Post.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
167
268
  req.body = data.to_json
168
269
  resp = self.http.request(req)
169
270
 
@@ -183,9 +284,13 @@ private
183
284
  data
184
285
  end
185
286
 
186
- def api_delete(q)
287
+ # Perform a HTTP DELETE request
288
+ #
289
+ # @param endpoint [String] HTTP API endpoint
290
+ #
291
+ def api_delete(endpoint:)
187
292
  begin
188
- req = Net::HTTP::Delete.new(self.uri.path + q, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
293
+ req = Net::HTTP::Delete.new(self.uri.path + endpoint, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
189
294
  resp = self.http.request(req)
190
295
 
191
296
  if resp.code == '204'
@@ -206,9 +311,14 @@ private
206
311
  data
207
312
  end
208
313
 
209
- def api_patch(q, data)
314
+ # Perform a HTTP PATCH request
315
+ #
316
+ # @param endpoint [String] HTTP API endpoint
317
+ # @param data [Hash] Request data
318
+ #
319
+ def api_patch(endpoint:, data:)
210
320
  begin
211
- req = Net::HTTP::Patch.new(self.uri.path + q, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
321
+ req = Net::HTTP::Patch.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
212
322
  req.body = data.to_json
213
323
  resp = self.http.request(req)
214
324
 
@@ -227,5 +337,32 @@ private
227
337
 
228
338
  data
229
339
  end
340
+
341
+ # Perform a HTTP PUT request
342
+ #
343
+ # @param endpoint [String] HTTP API endpoint
344
+ # @param data [Hash] Request data
345
+ #
346
+ def api_put(endpoint:, data:)
347
+ 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})"
360
+ end
361
+ rescue
362
+ raise Exception, "Failed to execute PUT request to Kapacitor REST API (#{$!})"
363
+ end
364
+
365
+ data
366
+ end
230
367
  end
231
368
  end
@@ -3,7 +3,7 @@
3
3
  #
4
4
 
5
5
  module Kapacitor
6
- VERSION = "0.0.14"
6
+ VERSION = "1.0.0"
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: 0.0.14
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Cerutti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-24 00:00:00.000000000 Z
11
+ date: 2017-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json