kapacitor-ruby 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bc6d76c1f16e0763148ff55590de5e4cc811a5e2
4
+ data.tar.gz: 2d9af022a39f11297e7269771f1d4486b6578c57
5
+ SHA512:
6
+ metadata.gz: 949bd723d24fa51e0698d67d7f76ce793d6964c2128da96556c4d220c13aa6c532b236119b119894060d4c9bd94e503fbc56e1948cf68e0627a13e5b358a13ed
7
+ data.tar.gz: 46e0e1edebce59617611767286fdae36f8d032c511b89f212fff9c059275724f81d285be67d00186de7e7d2c36c63946e98a76f4a23049edd8604901c2105cba
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright 2016 Matteo Cerutti
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+
8
+ Unless required by applicable law or agreed to in writing, software
9
+ distributed under the License is distributed on an "AS IS" BASIS,
10
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ See the License for the specific language governing permissions and
12
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Ruby client library for Kapacitor JSON REST API
2
+ This is a simple Ruby client library that allows to interact with the Kapacitor JSON REST API.
3
+
4
+ Pull requests to add additional API features are very welcome. I only implemented what I needed.
5
+
6
+ ## Install
7
+ To install it simply issue the following command:
8
+
9
+ ```
10
+ gem install kapacitor-ruby
11
+ ```
12
+
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
+ ```
71
+
72
+ ## Contact
73
+ Matteo Cerutti - matteo.cerutti@hotmail.co.uk
@@ -0,0 +1,179 @@
1
+ #
2
+ # client.rb
3
+ #
4
+ # Author: Matteo Cerutti <matteo.cerutti@hotmail.co.uk>
5
+ #
6
+
7
+ require 'net/http'
8
+ require 'json'
9
+
10
+ module Kapacitor
11
+ class Client
12
+ attr_reader :uri, :http
13
+
14
+ def initialize(host:, version: 'v1')
15
+ @uri = URI.parse("http://#{host}/kapacitor/#{version}")
16
+ @http = Net::HTTP.new(@uri.host, @uri.port)
17
+ end
18
+
19
+ def define_template(id:, type:, script:)
20
+ req = {
21
+ 'id' => id,
22
+ 'type' => type,
23
+ 'script' => script
24
+ }
25
+
26
+ api_post('/templates', req)
27
+ end
28
+
29
+ def update_template(id:, type: nil, script: nil)
30
+ req = {}
31
+ req['type'] = type if type
32
+ req['script'] = script if script
33
+
34
+ api_patch("/templates/#{id}", req) unless req.empty?
35
+ end
36
+
37
+ def delete_template(id:)
38
+ api_delete("/templates/#{id}")
39
+ end
40
+
41
+ def templates
42
+ api_get('/templates')['templates']
43
+ end
44
+
45
+ def define_task(id:, template_id: nil, type: nil, dbrps: , script: nil, status: 'enabled', vars: nil)
46
+ if (template_id.nil? and type.nil? and script.nil?) or (template_id and (type or script))
47
+ raise ArgumentError, "Must specify either a Template ID or a script and type"
48
+ elsif template_id.nil? and (type.nil? or script.nil?)
49
+ raise ArgumentError, "Must specify both task type and script when not using a Template ID"
50
+ end
51
+
52
+ req = {
53
+ 'id' => id,
54
+ 'dbrps' => dbrps,
55
+ 'status' => status
56
+ }
57
+
58
+ if template_id
59
+ req['template_id'] = template_id
60
+ else
61
+ req['type'] = type
62
+ req['script'] = script
63
+ end
64
+
65
+ req['vars'] = vars if vars
66
+
67
+ api_post('/tasks', req)
68
+ end
69
+
70
+ def update_task(id:, template_id: nil, type: nil, dbrps: nil, script: nil, status: nil, vars: nil)
71
+ req = {}
72
+ req['template_id'] = template_id if template_id
73
+ req['type'] = type if type
74
+ req['dbrps'] = dbrps if dbrps
75
+ req['script'] = script if script
76
+ req['status'] = status if status
77
+ req['vars'] = vars if vars
78
+
79
+ api_patch("/tasks/#{id}", req) unless req.empty?
80
+ end
81
+
82
+ def delete_task(id:)
83
+ api_delete("/tasks/#{id}")
84
+ end
85
+
86
+ def tasks
87
+ api_get('/tasks')['tasks']
88
+ end
89
+
90
+ private
91
+ def api_get(q)
92
+ begin
93
+ req = Net::HTTP::Get.new(self.uri.path + q, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
94
+ resp = self.http.request(req)
95
+
96
+ if resp.code == '200'
97
+ begin
98
+ data = JSON.parse(resp.body)
99
+ rescue JSON::ParserError
100
+ raise Exception, "Failed to decode response message"
101
+ end
102
+ else
103
+ raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
104
+ end
105
+ rescue
106
+ raise Exception, "Failed to execute GET request to Kapacitor REST API (#{$!})"
107
+ end
108
+
109
+ data
110
+ end
111
+
112
+ def api_post(q, data)
113
+ begin
114
+ req = Net::HTTP::Post.new(self.uri.path + q, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
115
+ req.body = data.to_json
116
+ resp = self.http.request(req)
117
+
118
+ if resp.code == '200'
119
+ begin
120
+ data = JSON.parse(resp.body)
121
+ rescue JSON::ParserError
122
+ raise Exception, "Failed to decode response message"
123
+ end
124
+ else
125
+ raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
126
+ end
127
+ rescue
128
+ raise Exception, "Failed to execute POST request to Kapacitor REST API (#{$!})"
129
+ end
130
+
131
+ data
132
+ end
133
+
134
+ def api_delete(q)
135
+ begin
136
+ req = Net::HTTP::Delete.new(self.uri.path + q, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
137
+ resp = self.http.request(req)
138
+
139
+ if resp.code == '204'
140
+ if resp.body
141
+ begin
142
+ data = JSON.parse(resp.body)
143
+ rescue JSON::ParserError
144
+ raise Exception, "Failed to decode response message"
145
+ end
146
+ end
147
+ else
148
+ raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
149
+ end
150
+ rescue
151
+ raise Exception, "Failed to execute DELETE request to Kapacitor REST API (#{$!})"
152
+ end
153
+
154
+ data
155
+ end
156
+
157
+ def api_patch(q, data)
158
+ begin
159
+ req = Net::HTTP::Patch.new(self.uri.path + q, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
160
+ req.body = data.to_json
161
+ resp = self.http.request(req)
162
+
163
+ if resp.code == '200'
164
+ begin
165
+ data = JSON.parse(resp.body)
166
+ rescue JSON::ParserError
167
+ raise Exception, "Failed to decode response message"
168
+ end
169
+ else
170
+ raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
171
+ end
172
+ rescue
173
+ raise Exception, "Failed to execute PATCH request to Kapacitor REST API (#{$!})"
174
+ end
175
+
176
+ data
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,13 @@
1
+ #
2
+ # version.rb
3
+ #
4
+ # Author: Matteo Cerutti <matteo.cerutti@hotmail.co.uk>
5
+ #
6
+
7
+ module Kapacitor
8
+ VERSION = "0.0.2"
9
+
10
+ def self.version
11
+ VERSION
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kapacitor-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Matteo Cerutti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.0
27
+ description: Ruby client library for Kapacitor JSON REST API
28
+ email: matteo.cerutti@hotmail.co.uk
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - lib/kapacitor/client.rb
36
+ - lib/kapacitor/version.rb
37
+ homepage: https://github.com/m4ce/kapacitor-ruby
38
+ licenses:
39
+ - Apache 2.0
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.4.5.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Ruby client library that allows to interact with the Kapacitor JSON REST
61
+ API
62
+ test_files: []