influxdb-api 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +14 -0
  3. data/README.md +2 -0
  4. data/influxdb/Dockerfile.0.7.3 +8 -0
  5. data/influxdb/Dockerfile.0.8.3 +8 -0
  6. data/influxdb/Dockerfile.0.8.8 +8 -0
  7. data/lib/influxdb/api.rb +3 -0
  8. data/lib/influxdb/api/client.rb +5 -1
  9. data/lib/influxdb/api/database.rb +14 -2
  10. data/lib/influxdb/api/namespaces/base.rb +7 -0
  11. data/lib/influxdb/api/namespaces/cluster_admins.rb +4 -0
  12. data/lib/influxdb/api/namespaces/continuous_queries.rb +28 -4
  13. data/lib/influxdb/api/namespaces/databases.rb +4 -0
  14. data/lib/influxdb/api/namespaces/series.rb +3 -0
  15. data/lib/influxdb/api/namespaces/shards.rb +15 -0
  16. data/lib/influxdb/api/namespaces/users.rb +7 -0
  17. data/lib/influxdb/api/server_version.rb +6 -2
  18. data/lib/influxdb/api/version.rb +1 -1
  19. data/spec/intergrations/cluster_admins_spec.rb +75 -0
  20. data/spec/intergrations/continuous_queries_spec.rb +106 -0
  21. data/spec/intergrations/databases_spec.rb +68 -0
  22. data/spec/intergrations/series_spec.rb +104 -0
  23. data/spec/intergrations/servers_spec.rb +35 -0
  24. data/spec/intergrations/shard_spaces_spec.rb +126 -0
  25. data/spec/intergrations/shards_spec.rb +85 -0
  26. data/spec/intergrations/users_0_7_3_spec.rb +104 -0
  27. data/spec/intergrations/users_spec.rb +124 -0
  28. data/spec/lib/influxdb/api/database_spec.rb +32 -4
  29. data/spec/lib/influxdb/api/namespaces/{continuous_queries_spec.rb → continuous_queries/api_spec.rb} +2 -2
  30. data/spec/lib/influxdb/api/namespaces/continuous_queries/sql_spec.rb +41 -0
  31. data/spec/lib/influxdb/api/server_version_spec.rb +1 -0
  32. data/spec/spec_helper.rb +18 -0
  33. data/spec/support/version_resolver.rb +56 -0
  34. metadata +30 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b75a2d9451bb4769cdeb997320a0f13ab86e2720
4
- data.tar.gz: 64a82894fa549ac9e121efa329fd2cf46e102660
3
+ metadata.gz: 57e3e37757669373a7d4fe649bfb8eb505fc3141
4
+ data.tar.gz: 9bdf91d6fab7e146290bc6b8bbd71924158de63c
5
5
  SHA512:
6
- metadata.gz: b941d094565812ef1300f2220fcf3bf4984ab223b3c5f09dc06623d35711afa4d25cf93b3cdd4bc321d43d05b7749180430760dbbd2b8c2baed06bd66042d4eb
7
- data.tar.gz: 15e64056ba5e997146f3f31d0a3689ab9cc5b039c9d70298a4e904f1cf3bd32532c82b5d36f00e225ffe5915cc17f6093573e028ec9767bece7013dfc61cc515
6
+ metadata.gz: db81efe8613a8ed544c3857ce29ae448e94efca422dfbbe90565ecbb6856cd6185c32191e2d872691380324d1ade40b2a2ab59355104e763adceb040fde9a56c
7
+ data.tar.gz: 77d6bad87f7374fdf7d5f8894972cf0a9aa491f72e4d4941739719d08bc29f10ac2c5154a147c4a3fa5880129008c8ec74744aca466258a2f30bfd62a191a9d2
@@ -4,3 +4,17 @@ rvm:
4
4
  - 2.0.0
5
5
  - 2.1.1
6
6
  - rbx-2
7
+ env:
8
+ - INFLUXDB_VERSION=0.7.3
9
+ - INFLUXDB_VERSION=0.8.3
10
+ - INFLUXDB_VERSION=0.8.8
11
+ - INFLUXDB_VERSION=latest
12
+ before_install:
13
+ - wget http://s3.amazonaws.com/influxdb/influxdb_${INFLUXDB_VERSION}_amd64.deb
14
+ - sudo dpkg -i influxdb_${INFLUXDB_VERSION}_amd64.deb
15
+ - sudo /etc/init.d/influxdb start
16
+ - sleep 8
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: rbx-2
20
+ - env: "INFLUXDB_VERSION=latest"
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/undr/influxdb-api.svg?branch=master)](https://travis-ci.org/undr/influxdb-api) [![Code Climate](https://codeclimate.com/github/undr/influxdb-api/badges/gpa.svg)](https://codeclimate.com/github/undr/influxdb-api) [![Gem Version](https://badge.fury.io/rb/influxdb-api.svg)](http://badge.fury.io/rb/influxdb-api)
4
4
 
5
+ It tested on influxdb till 0.8.3.
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -0,0 +1,8 @@
1
+ FROM debian:jessie
2
+
3
+ ADD http://s3.amazonaws.com/influxdb/influxdb_0.7.3_amd64.deb /influxdb_0.7.3_amd64.deb
4
+ RUN dpkg -i /influxdb_0.7.3_amd64.deb
5
+
6
+ EXPOSE 8083 8086
7
+
8
+ CMD ["/opt/influxdb/current/influxdb", "-config=/opt/influxdb/current/config.toml"]
@@ -0,0 +1,8 @@
1
+ FROM debian:jessie
2
+
3
+ ADD http://s3.amazonaws.com/influxdb/influxdb_0.8.3_amd64.deb /influxdb_0.8.3_amd64.deb
4
+ RUN dpkg -i /influxdb_0.8.3_amd64.deb
5
+
6
+ EXPOSE 8083 8086
7
+
8
+ CMD ["/opt/influxdb/current/influxdb", "-config=/opt/influxdb/current/config.toml"]
@@ -0,0 +1,8 @@
1
+ FROM debian:jessie
2
+
3
+ ADD http://s3.amazonaws.com/influxdb/influxdb_0.8.8_amd64.deb /influxdb_0.8.8_amd64.deb
4
+ RUN dpkg -i /influxdb_0.8.8_amd64.deb
5
+
6
+ EXPOSE 8083 8086
7
+
8
+ CMD ["/opt/influxdb/current/influxdb", "-config=/opt/influxdb/current/config.toml"]
@@ -13,6 +13,9 @@ require_relative 'api/client'
13
13
 
14
14
  module Influxdb
15
15
  module Api
16
+ class Error < StandardError;end
17
+ class UnsupportedFeature < Error;end
18
+
16
19
  extend self
17
20
 
18
21
  attr_writer :client, :config
@@ -18,10 +18,14 @@ module Influxdb
18
18
  response = with_retry(path, params) do |connection, url|
19
19
  connection.basic_auth(config.user, config.password)
20
20
  headers = { 'Content-Type' => 'application/json' }
21
+ body = body ? convert_to_json(body) : nil
21
22
 
22
- connection.run_request(method, url, (body ? convert_to_json(body) : nil), headers, &block)
23
+ logger.debug "=> #{method.upcase} #{url} #{body}" if logger
24
+ connection.run_request(method, url, body, headers, &block)
23
25
  end
24
26
 
27
+ logger.debug "<= [#{response.status}] #{response.body}" if logger
28
+
25
29
  raise_transport_error(response) if response.status.to_i >= 300
26
30
 
27
31
  json = config.serializer.load(response.body) if response.headers && response.headers["content-type"] =~ /json/
@@ -17,11 +17,23 @@ module Influxdb
17
17
  end
18
18
 
19
19
  def continuous_queries
20
- @continuous_queries ||= Namespaces::ContinuousQueries.new(client, name)
20
+ @continuous_queries ||= if client.version > '0.8.3'
21
+ Namespaces::ContinuousQueries::Sql.new(client, name)
22
+ else
23
+ Namespaces::ContinuousQueries::Api.new(client, name)
24
+ end
21
25
  end
22
26
 
23
27
  def shard_spaces
24
- @shard_spaces ||= Namespaces::ShardSpaces.new(client, name)
28
+ @shard_spaces ||= begin
29
+ version = client.version
30
+ raise(
31
+ UnsupportedFeature,
32
+ "Shard space's API is supported only after 0.7.3 version. Current is #{version.to_s(:mini)}"
33
+ ) if version < '0.8.3'
34
+
35
+ Namespaces::ShardSpaces.new(client, name)
36
+ end
25
37
  end
26
38
  end
27
39
  end
@@ -34,6 +34,13 @@ module Influxdb
34
34
 
35
35
  protected
36
36
 
37
+ def return_false_if_doesnt_exist(type)
38
+ yield
39
+ rescue Influxdb::Api::Client::Errors::BadRequest => e
40
+ raise e unless e.message =~ /#{type} (.*) doesn\'t exist/
41
+ false
42
+ end
43
+
37
44
  def resource_path(*args)
38
45
  [path_prefix, self.class._resource_path, path_postfix, args].compact.flatten.join(?/).squeeze(?/)
39
46
  end
@@ -8,6 +8,10 @@ module Influxdb
8
8
  perform_post(resource_path(name), {}, attributes)
9
9
  true
10
10
  end
11
+
12
+ def delete(*_)
13
+ return_false_if_doesnt_exist('User'){ super }
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -1,11 +1,35 @@
1
1
  module Influxdb
2
2
  module Api
3
3
  module Namespaces
4
- class ContinuousQueries < WithDatabase
5
- resource_path '/continuous_queries'
4
+ module ContinuousQueries
5
+ class Api < WithDatabase
6
+ resource_path '/continuous_queries'
6
7
 
7
- def create(query)
8
- super(query: query)
8
+ def create(query)
9
+ super(query: query)
10
+ end
11
+ end
12
+
13
+ class Sql < WithDatabase
14
+ def create(query)
15
+ series.execute(query)
16
+ true
17
+ end
18
+
19
+ def all
20
+ series.execute('LIST CONTINUOUS QUERIES')['continuous queries']
21
+ end
22
+
23
+ def delete(id)
24
+ series.execute("DROP CONTINUOUS QUERY #{id}")
25
+ true
26
+ end
27
+
28
+ private
29
+
30
+ def series
31
+ client.databases(database_name).series
32
+ end
9
33
  end
10
34
  end
11
35
  end
@@ -7,6 +7,10 @@ module Influxdb
7
7
  def create(name)
8
8
  super(name: name)
9
9
  end
10
+
11
+ def delete(*_)
12
+ return_false_if_doesnt_exist('Database'){ super }
13
+ end
10
14
  end
11
15
  end
12
16
  end
@@ -35,6 +35,9 @@ module Influxdb
35
35
  params = { q: query_string }
36
36
  params[:time_precision] = time_precision if time_precision
37
37
  perform_get(resource_path, params)
38
+ rescue Influxdb::Api::Client::Errors::BadRequest => e
39
+ raise e unless e.message =~ /Couldn\'t look up columns/
40
+ []
38
41
  end
39
42
 
40
43
  private
@@ -4,6 +4,21 @@ module Influxdb
4
4
  class Shards < Base
5
5
  resource_path '/cluster/shards'
6
6
 
7
+ def create(attributes)
8
+ attributes['startTime'] = cast_datetime(attributes['startTime']) if attributes['startTime']
9
+ attributes[:startTime] = cast_datetime(attributes[:startTime]) if attributes[:startTime]
10
+ attributes['endTime'] = cast_datetime(attributes['endTime']) if attributes['endTime']
11
+ attributes[:endTime] = cast_datetime(attributes[:endTime]) if attributes[:endTime]
12
+
13
+ super(attributes)
14
+ end
15
+
16
+ private
17
+
18
+ def cast_datetime(value)
19
+ value = value.to_i if value && !value.is_a?(Integer)
20
+ value
21
+ end
7
22
  end
8
23
  end
9
24
  end
@@ -6,12 +6,19 @@ module Influxdb
6
6
 
7
7
  def find(name)
8
8
  perform_get(resource_path(name))
9
+ rescue Influxdb::Api::Client::Errors::BadRequest => e
10
+ raise e unless e.message =~ /Invalid username/
11
+ nil
9
12
  end
10
13
 
11
14
  def update(name, attributes)
12
15
  perform_post(resource_path(name), {}, attributes)
13
16
  true
14
17
  end
18
+
19
+ def delete(*_)
20
+ return_false_if_doesnt_exist('User'){ super }
21
+ end
15
22
  end
16
23
  end
17
24
  end
@@ -34,8 +34,12 @@ module Influxdb
34
34
  [major <=> other_major, minor <=> other_minor, patch <=> other_patch].detect{|c| c != 0 } || 0
35
35
  end
36
36
 
37
- def to_s
38
- source
37
+ def to_s(format = :full)
38
+ if format == :mini
39
+ "#{major}.#{minor}.#{patch}"
40
+ else
41
+ source
42
+ end
39
43
  end
40
44
 
41
45
  def inspect
@@ -1,5 +1,5 @@
1
1
  module Influxdb
2
2
  module Api
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'cluster_admins', integration: true do
4
+ let(:config){ Influxdb::Api::Configuration.new }
5
+ let(:client){ Influxdb::Api::Client.new(config) }
6
+
7
+ subject{ client.cluster_admins }
8
+
9
+ before{ subject.all.each{|u| subject.delete(u['name']) if u['name'] != 'root' } }
10
+ after{ subject.all.each{|u| subject.delete(u['name']) if u['name'] != 'root' } }
11
+
12
+ describe '.all' do
13
+ it 'returns list of users' do
14
+ expect(subject.all).to eq([{ 'name' => 'root' }])
15
+ end
16
+ end
17
+
18
+ describe '.create' do
19
+ it 'creates cluster admin' do
20
+ subject.create(name: 'username', password: 'pass')
21
+ expect(subject.all).to match_array([{ 'name' => 'root' }, { 'name' => 'username' }])
22
+ end
23
+
24
+ context 'when there is user with same name' do
25
+ it 'raises error' do
26
+ expect{ subject.create(name: 'root', password: 'pass') }.to raise_error(
27
+ Influxdb::Api::Client::Errors::BadRequest, '[400] User root already exists'
28
+ )
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '.update' do
34
+ context 'when user exists' do
35
+ let(:new_client){ Influxdb::Api::Client.new(new_config) }
36
+ let(:new_config) do
37
+ Influxdb::Api::Configuration.new.tap do |c|
38
+ c.user = 'username'
39
+ c.password = 'pass1'
40
+ end
41
+ end
42
+
43
+ before{ subject.create(name: 'username', password: 'pass') }
44
+
45
+ it 'updates user attributes' do
46
+ expect{ subject.update('username', password: 'pass1') }.not_to raise_error
47
+ expect{ new_client.version }.not_to raise_error
48
+ end
49
+ end
50
+
51
+ context 'when user does not exist' do
52
+ it 'raises error' do
53
+ expect{ subject.update('username', password: 'pass') }.to raise_error(
54
+ Influxdb::Api::Client::Errors::BadRequest, '[400] Invalid user name username'
55
+ )
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '.delete' do
61
+ context 'when user does not exist' do
62
+ it 'returns false' do
63
+ expect(subject.delete('username')).to be_falsy
64
+ end
65
+ end
66
+
67
+ context 'when user exists' do
68
+ before{ subject.create(name: 'username', password: 'pass') }
69
+
70
+ it 'removes user and returns true' do
71
+ expect(subject.delete('username')).to be_truthy
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'continuous_queries', integration: true do
4
+ let(:config){ Influxdb::Api::Configuration.new }
5
+ let(:client){ Influxdb::Api::Client.new(config) }
6
+ let(:continuous_query1){ 'select MEAN(user) as user_mean from cpu group by time(1m) into cpu.1m.user_mean' }
7
+ let(:continuous_query2){ 'select MAX(user) as user_max from cpu group by time(1m) into cpu.1m.user_max' }
8
+ let(:continuous_query1_088){ 'select MEAN(user) as user_mean from "cpu" group by time(1m) into cpu.1m.user_mean' }
9
+ let(:continuous_query2_088){ 'select MAX(user) as user_max from "cpu" group by time(1m) into cpu.1m.user_max' }
10
+
11
+ subject{ client.databases('db_name').continuous_queries }
12
+
13
+ before do
14
+ client.databases.create('db_name')
15
+ subject.all.each{|q| subject.delete(q['id']) }
16
+ end
17
+
18
+ after{ client.databases.delete('db_name') }
19
+
20
+ describe '.all' do
21
+ context 'when there are no continuous queries' do
22
+ it 'returns empty array' do
23
+ expect(subject.all).to eq([])
24
+ end
25
+ end
26
+
27
+ context 'when there are some continuous queries', version: '<=0.8.3' do
28
+ before do
29
+ subject.create(continuous_query1)
30
+ subject.create(continuous_query2)
31
+ end
32
+
33
+ it 'returns the list of users' do
34
+ expect(subject.all).to match_array([
35
+ { 'id' => 1, 'query' => continuous_query1 }, { 'id' => 2, 'query' => continuous_query2 }
36
+ ])
37
+ end
38
+ end
39
+
40
+ context 'when there are some continuous queries', version: '>0.8.3' do
41
+ before do
42
+ subject.create(continuous_query1)
43
+ subject.create(continuous_query2)
44
+ end
45
+
46
+ it 'returns the list of users' do
47
+ expect(subject.all).to match_array([
48
+ { 'time' => 0, 'id' => 1, 'query' => continuous_query1_088 },
49
+ { 'time' => 0, 'id' => 2, 'query' => continuous_query2_088 }
50
+ ])
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '.create' do
56
+ it 'creates a new query', version: '<=0.8.3' do
57
+ subject.create(continuous_query1)
58
+ expect(subject.all).to eq([{ 'id' => 1, 'query' => continuous_query1 }])
59
+ end
60
+
61
+ it 'creates a new query', version: '>0.8.3' do
62
+ subject.create(continuous_query1)
63
+ expect(subject.all).to eq([{ 'time' => 0, 'id' => 1, 'query' => continuous_query1_088 }])
64
+ end
65
+
66
+ context 'when there is the same query', version: '<=0.8.3' do
67
+ before{ subject.create(continuous_query1) }
68
+
69
+ it 'creates one more' do
70
+ subject.create(continuous_query1)
71
+ expect(subject.all).to match_array([
72
+ { 'id' => 1, 'query' => continuous_query1 }, { 'id' => 2, 'query' => continuous_query1 }
73
+ ])
74
+ end
75
+ end
76
+
77
+ context 'when there is the same query', version: '>0.8.3' do
78
+ before{ subject.create(continuous_query1) }
79
+
80
+ it 'creates one more' do
81
+ subject.create(continuous_query1)
82
+ expect(subject.all).to match_array([
83
+ { 'time' => 0, 'id' => 1, 'query' => continuous_query1_088 },
84
+ { 'time' => 0, 'id' => 2, 'query' => continuous_query1_088 }
85
+ ])
86
+ end
87
+ end
88
+ end
89
+
90
+ describe '.delete' do
91
+ context 'when query does not exist' do
92
+ it 'returns true' do
93
+ expect(subject.delete(1)).to be_truthy
94
+ end
95
+ end
96
+
97
+ context 'when query exists' do
98
+ before{ subject.create(continuous_query1) }
99
+
100
+ it 'removes db and returns true' do
101
+ expect(subject.delete(1)).to be_truthy
102
+ expect(subject.all).to eq([])
103
+ end
104
+ end
105
+ end
106
+ end