kapacitor-ruby 1.0.1 → 1.0.2
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 +4 -4
- data/lib/kapacitor/client.rb +26 -38
- data/lib/kapacitor/version.rb +1 -1
- metadata +15 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 97fd28ae0c38ca2649dc1aa6caf64d93486148ab
         | 
| 4 | 
            +
              data.tar.gz: 87c38785fbf478b9c4bb5bcb1aed5f983173da63
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 976de61620d4e124b53d5acfc26fcf711ff2fdcb0c9e936fbc85d2cd652262a94f9bf67078205ae6afb88d53fa34733373490617c2a0e7de166821710628e0c4
         | 
| 7 | 
            +
              data.tar.gz: 6c2fe44b7f27e0bf7b40b1ce636c057bf3b0fe876c17b51b7e140a3d70ce6ce03c007603401b1332fa60d4238abd0ba1279670d7aea5a12545d6049c6e2a8ca8
         | 
    
        data/lib/kapacitor/client.rb
    CHANGED
    
    | @@ -1,10 +1,10 @@ | |
| 1 | 
            -
            require ' | 
| 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 : | 
| 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(url: 'http://localhost:9092', version: 'v1preview')
         | 
| 17 | 
            -
                  @ | 
| 18 | 
            -
                  @http =  | 
| 16 | 
            +
                def initialize(url: 'http://localhost:9092/kapacitor', version: 'v1preview')
         | 
| 17 | 
            +
                  @url = URI.join(url, version)
         | 
| 18 | 
            +
                  @http = HTTPClient.new
         | 
| 19 19 | 
             
                end
         | 
| 20 20 |  | 
| 21 21 | 
             
                # Define a Kapacitor template
         | 
| @@ -71,7 +71,7 @@ module Kapacitor | |
| 71 71 | 
             
                  ret = []
         | 
| 72 72 |  | 
| 73 73 | 
             
                  loop do
         | 
| 74 | 
            -
                    res = api_get(endpoint: " | 
| 74 | 
            +
                    res = api_get(endpoint: "templates?offset=#{offset}&limit=#{limit}")['templates']
         | 
| 75 75 | 
             
                    break unless res.size > 0
         | 
| 76 76 | 
             
                    ret += res
         | 
| 77 77 | 
             
                    offset += limit
         | 
| @@ -162,11 +162,11 @@ module Kapacitor | |
| 162 162 | 
             
                  tasks = []
         | 
| 163 163 |  | 
| 164 164 | 
             
                  loop do
         | 
| 165 | 
            -
                    res = api_get(endpoint: " | 
| 165 | 
            +
                    res = api_get(endpoint: "tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
         | 
| 166 166 | 
             
                    break unless res.size > 0
         | 
| 167 167 |  | 
| 168 168 | 
             
                    res.each do |task|
         | 
| 169 | 
            -
                      tasks << api_get(endpoint: " | 
| 169 | 
            +
                      tasks << api_get(endpoint: "tasks/#{task['id']}")
         | 
| 170 170 | 
             
                    end
         | 
| 171 171 |  | 
| 172 172 | 
             
                    offset += limit
         | 
| @@ -227,28 +227,27 @@ module Kapacitor | |
| 227 227 | 
             
                # @return [Array[Hash]] List of handlers
         | 
| 228 228 | 
             
                #
         | 
| 229 229 | 
             
                def topic_handlers(topic:)
         | 
| 230 | 
            -
                   | 
| 230 | 
            +
                  api_get(endpoint: "alerts/topics/#{topic}/handlers")['handlers']
         | 
| 231 231 | 
             
                end
         | 
| 232 232 |  | 
| 233 233 | 
             
            private
         | 
| 234 234 | 
             
                # Perform a HTTP GET request
         | 
| 235 235 | 
             
                #
         | 
| 236 236 | 
             
                # @param endpoint [String] HTTP API endpoint
         | 
| 237 | 
            +
                # @param query [String] HTTP query
         | 
| 237 238 | 
             
                # @return [Array[Hash], Hash] API response
         | 
| 238 239 | 
             
                #
         | 
| 239 | 
            -
                def api_get(endpoint:)
         | 
| 240 | 
            +
                def api_get(endpoint:, query: nil)
         | 
| 240 241 | 
             
                  begin
         | 
| 241 | 
            -
                     | 
| 242 | 
            -
                    resp  | 
| 243 | 
            -
             | 
| 244 | 
            -
                    if resp.code == '200'
         | 
| 242 | 
            +
                    resp = self.http.get([self.url, endpoint].join('/'), query, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
         | 
| 243 | 
            +
                    if resp.status == 200
         | 
| 245 244 | 
             
                      begin
         | 
| 246 245 | 
             
                        data = JSON.parse(resp.body)
         | 
| 247 246 | 
             
                      rescue JSON::ParserError
         | 
| 248 247 | 
             
                        raise Exception, "Failed to decode response message"
         | 
| 249 248 | 
             
                      end
         | 
| 250 249 | 
             
                    else
         | 
| 251 | 
            -
                      raise Exception, "Query returned a non successful HTTP code ( | 
| 250 | 
            +
                      raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
         | 
| 252 251 | 
             
                    end
         | 
| 253 252 | 
             
                  rescue
         | 
| 254 253 | 
             
                    raise Exception, "Failed to execute GET request to Kapacitor REST API (#{$!})"
         | 
| @@ -264,18 +263,15 @@ private | |
| 264 263 | 
             
                #
         | 
| 265 264 | 
             
                def api_post(endpoint:, data:)
         | 
| 266 265 | 
             
                  begin
         | 
| 267 | 
            -
                     | 
| 268 | 
            -
                     | 
| 269 | 
            -
                    resp = self.http.request(req)
         | 
| 270 | 
            -
             | 
| 271 | 
            -
                    if resp.code == '200'
         | 
| 266 | 
            +
                    resp = self.http.post([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
         | 
| 267 | 
            +
                    if resp.status == 200
         | 
| 272 268 | 
             
                      begin
         | 
| 273 269 | 
             
                        data = JSON.parse(resp.body)
         | 
| 274 270 | 
             
                      rescue JSON::ParserError
         | 
| 275 271 | 
             
                        raise Exception, "Failed to decode response message"
         | 
| 276 272 | 
             
                      end
         | 
| 277 273 | 
             
                    else
         | 
| 278 | 
            -
                      raise Exception, "Query returned a non successful HTTP code ( | 
| 274 | 
            +
                      raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
         | 
| 279 275 | 
             
                    end
         | 
| 280 276 | 
             
                  rescue
         | 
| 281 277 | 
             
                    raise Exception, "Failed to execute POST request to Kapacitor REST API (#{$!})"
         | 
| @@ -290,10 +286,8 @@ private | |
| 290 286 | 
             
                #
         | 
| 291 287 | 
             
                def api_delete(endpoint:)
         | 
| 292 288 | 
             
                  begin
         | 
| 293 | 
            -
                     | 
| 294 | 
            -
                    resp  | 
| 295 | 
            -
             | 
| 296 | 
            -
                    if resp.code == '204'
         | 
| 289 | 
            +
                    resp = self.http.delete([self.url, endpoint].join('/'), {'Content-type' => 'application/json', 'Accept' => 'application/json'})
         | 
| 290 | 
            +
                    if resp.status == 204
         | 
| 297 291 | 
             
                      if resp.body
         | 
| 298 292 | 
             
                        begin
         | 
| 299 293 | 
             
                          data = JSON.parse(resp.body)
         | 
| @@ -302,7 +296,7 @@ private | |
| 302 296 | 
             
                        end
         | 
| 303 297 | 
             
                      end
         | 
| 304 298 | 
             
                    else
         | 
| 305 | 
            -
                      raise Exception, "Query returned a non successful HTTP code ( | 
| 299 | 
            +
                      raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
         | 
| 306 300 | 
             
                    end
         | 
| 307 301 | 
             
                  rescue
         | 
| 308 302 | 
             
                    raise Exception, "Failed to execute DELETE request to Kapacitor REST API (#{$!})"
         | 
| @@ -318,18 +312,15 @@ private | |
| 318 312 | 
             
                #
         | 
| 319 313 | 
             
                def api_patch(endpoint:, data:)
         | 
| 320 314 | 
             
                  begin
         | 
| 321 | 
            -
                     | 
| 322 | 
            -
                     | 
| 323 | 
            -
                    resp = self.http.request(req)
         | 
| 324 | 
            -
             | 
| 325 | 
            -
                    if resp.code == '200'
         | 
| 315 | 
            +
                    resp = self.http.patch([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
         | 
| 316 | 
            +
                    if resp.status == 200
         | 
| 326 317 | 
             
                      begin
         | 
| 327 318 | 
             
                        data = JSON.parse(resp.body)
         | 
| 328 319 | 
             
                      rescue JSON::ParserError
         | 
| 329 320 | 
             
                        raise Exception, "Failed to decode response message"
         | 
| 330 321 | 
             
                      end
         | 
| 331 322 | 
             
                    else
         | 
| 332 | 
            -
                      raise Exception, "Query returned a non successful HTTP code (Code: #{resp. | 
| 323 | 
            +
                      raise Exception, "Query returned a non successful HTTP code (Code: #{resp.status}, Reason: #{resp.reason})"
         | 
| 333 324 | 
             
                    end
         | 
| 334 325 | 
             
                  rescue
         | 
| 335 326 | 
             
                    raise Exception, "Failed to execute PATCH request to Kapacitor REST API (#{$!})"
         | 
| @@ -345,18 +336,15 @@ private | |
| 345 336 | 
             
                #
         | 
| 346 337 | 
             
                def api_put(endpoint:, data:)
         | 
| 347 338 | 
             
                  begin
         | 
| 348 | 
            -
                     | 
| 349 | 
            -
                     | 
| 350 | 
            -
                    resp = self.http.request(req)
         | 
| 351 | 
            -
             | 
| 352 | 
            -
                    if resp.code == '200'
         | 
| 339 | 
            +
                    resp = self.http.put([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
         | 
| 340 | 
            +
                    if resp.status == 200
         | 
| 353 341 | 
             
                      begin
         | 
| 354 342 | 
             
                        data = JSON.parse(resp.body)
         | 
| 355 343 | 
             
                      rescue JSON::ParserError
         | 
| 356 344 | 
             
                        raise Exception, "Failed to decode response message"
         | 
| 357 345 | 
             
                      end
         | 
| 358 346 | 
             
                    else
         | 
| 359 | 
            -
                      raise Exception, "Query returned a non successful HTTP code ( | 
| 347 | 
            +
                      raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
         | 
| 360 348 | 
             
                    end
         | 
| 361 349 | 
             
                  rescue
         | 
| 362 350 | 
             
                    raise Exception, "Failed to execute PUT request to Kapacitor REST API (#{$!})"
         | 
    
        data/lib/kapacitor/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 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.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Matteo Cerutti
         | 
| @@ -10,6 +10,20 @@ bindir: bin | |
| 10 10 | 
             
            cert_chain: []
         | 
| 11 11 | 
             
            date: 2017-09-04 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
         |