dogapi 1.3.6 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dogapi/common.rb CHANGED
@@ -97,6 +97,15 @@ module Dogapi
97
97
  end
98
98
  end
99
99
 
100
+ def suppress_error_if_silent(e)
101
+ if @silent
102
+ warn e
103
+ return -1, {}
104
+ else
105
+ raise e
106
+ end
107
+ end
108
+
100
109
  # Prepares the request and handles the response
101
110
  #
102
111
  # +method+ is an implementation of Net::HTTP::Request (e.g. Net::HTTP::Post)
data/lib/dogapi/facade.rb CHANGED
@@ -29,6 +29,9 @@ module Dogapi
29
29
  @metric_svc = Dogapi::V1::MetricService.new(@api_key, @application_key, silent)
30
30
  @event_svc = Dogapi::V1::EventService.new(@api_key, @application_key, silent)
31
31
  @tag_svc = Dogapi::V1::TagService.new(@api_key, @application_key, silent)
32
+ @comment_svc = Dogapi::V1::CommentService.new(@api_key, @application_key, silent)
33
+ @search_svc = Dogapi::V1::SearchService.new(@api_key, @application_key, silent)
34
+ @dash_service = Dogapi::V1::DashService.new(@api_key, @application_key, silent)
32
35
 
33
36
  @legacy_event_svc = Dogapi::EventService.new(@datadog_host)
34
37
  end
@@ -133,21 +136,50 @@ module Dogapi
133
136
  end
134
137
  end
135
138
 
139
+ #
140
+ # COMMENTS
141
+ #
142
+
143
+ # Post a comment
144
+ def comment(message, options={})
145
+ @comment_svc.comment(message, options)
146
+ end
147
+
148
+ # Post a comment
149
+ def update_comment(comment_id, options={})
150
+ @comment_svc.update_comment(comment_id, options)
151
+ end
152
+
153
+ def delete_comment(comment_id)
154
+ @comment_svc.delete_comment(comment_id)
155
+ end
156
+
157
+ #
158
+ # SEARCH
159
+ #
160
+
161
+ # Run the given search query.
162
+ def search(query)
163
+ @search_svc.search query
164
+ end
165
+
166
+
136
167
  #
137
168
  # TAGS
138
-
169
+ #
170
+
139
171
  # Get all tags and their associated hosts at your org
140
172
  def all_tags()
141
173
  @tag_svc.get_all()
142
174
  end
143
-
175
+
144
176
  # Get all tags for the given host
145
177
  #
146
178
  # +host_id+ can be the host's numeric id or string name
147
179
  def host_tags(host_id)
148
180
  @tag_svc.get(host_id)
149
181
  end
150
-
182
+
151
183
  # Add the tags to the given host
152
184
  #
153
185
  # +host_id+ can be the host's numeric id or string name
@@ -156,7 +188,7 @@ module Dogapi
156
188
  def add_tags(host_id, tags)
157
189
  @tag_svc.add(host_id, tags)
158
190
  end
159
-
191
+
160
192
  # Replace the tags on the given host
161
193
  #
162
194
  # +host_id+ can be the host's numeric id or string name
@@ -179,6 +211,30 @@ module Dogapi
179
211
  @tag_svc.detach(host_id)
180
212
  end
181
213
 
214
+ #
215
+ # DASHES
216
+ #
217
+
218
+ # Create a dashboard.
219
+ def create_dashboard(title, description, graphs)
220
+ @dash_service.create_dashboard(title, description, graphs)
221
+ end
222
+
223
+ # Update a dashboard.
224
+ def update_dashboard(dash_id, title, description, graphs)
225
+ @dash_service.update_dashboard(dash_id, title, description, graphs)
226
+ end
227
+
228
+ # Fetch the given dashboard.
229
+ def get_dashboard(dash_id)
230
+ @dash_service.get_dashboard(dash_id)
231
+ end
232
+
233
+ # Delete the given dashboard.
234
+ def delete_dashboard(dash_id)
235
+ @dash_service.delete_dashboard(dash_id)
236
+ end
237
+
182
238
  private
183
239
 
184
240
  def override_scope(host, device)
data/lib/dogapi/v1.rb CHANGED
@@ -1,3 +1,6 @@
1
1
  require 'dogapi/v1/event'
2
2
  require 'dogapi/v1/metric'
3
3
  require 'dogapi/v1/tag'
4
+ require 'dogapi/v1/comment'
5
+ require 'dogapi/v1/search'
6
+ require 'dogapi/v1/dash'
@@ -0,0 +1,62 @@
1
+ require 'dogapi'
2
+
3
+ module Dogapi
4
+ class V1 # for namespacing
5
+
6
+ class CommentService < Dogapi::APIService
7
+
8
+ API_VERSION = "v1"
9
+
10
+ # Submit a comment.
11
+ def comment(message, options={})
12
+ begin
13
+ params = {
14
+ :api_key => @api_key,
15
+ :application_key => @application_key
16
+ }
17
+
18
+ body = {
19
+ 'message' => message,
20
+ }.merge options
21
+
22
+ request(Net::HTTP::Post, "/api/#{API_VERSION}/comments", params, body, true)
23
+ rescue Exception => e
24
+ suppress_error_if_silent e
25
+ end
26
+ end
27
+
28
+ # Update a comment.
29
+ def update_comment(comment_id, options={})
30
+ begin
31
+ params = {
32
+ :api_key => @api_key,
33
+ :application_key => @application_key
34
+ }
35
+
36
+ if options.empty?
37
+ raise "Must update something."
38
+ end
39
+
40
+ request(Net::HTTP::Put, "/api/#{API_VERSION}/comments/#{comment_id}", params, options, true)
41
+ rescue Exception => e
42
+ suppress_error_if_silent e
43
+ end
44
+ end
45
+
46
+ def delete_comment(comment_id)
47
+ begin
48
+ params = {
49
+ :api_key => @api_key,
50
+ :application_key => @application_key
51
+ }
52
+
53
+ request(Net::HTTP::Delete, "/api/#{API_VERSION}/comments/#{comment_id}", params, nil, false)
54
+ rescue Exception => e
55
+ suppress_error_if_silent e
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,79 @@
1
+ require 'dogapi'
2
+
3
+ module Dogapi
4
+ class V1 # for namespacing
5
+
6
+ class DashService < Dogapi::APIService
7
+
8
+ API_VERSION = "v1"
9
+
10
+ def create_dashboard(title, description, graphs)
11
+
12
+ begin
13
+ params = {
14
+ :api_key => @api_key,
15
+ :application_key => @application_key
16
+ }
17
+
18
+ body = {
19
+ :title => title,
20
+ :description => description,
21
+ :graphs => graphs
22
+ }
23
+
24
+ request(Net::HTTP::Post, "/api/#{API_VERSION}/dash", params, body, true)
25
+ rescue Exception => e
26
+ suppress_error_if_silent e
27
+ end
28
+ end
29
+
30
+ def update_dashboard(dash_id, title, description, graphs)
31
+
32
+ begin
33
+ params = {
34
+ :api_key => @api_key,
35
+ :application_key => @application_key
36
+ }
37
+
38
+ body = {
39
+ :title => title,
40
+ :description => description,
41
+ :graphs => graphs
42
+ }
43
+
44
+ request(Net::HTTP::Put, "/api/#{API_VERSION}/dash/#{dash_id}", params, body, true)
45
+ rescue Exception => e
46
+ suppress_error_if_silent e
47
+ end
48
+ end
49
+
50
+ def get_dashboard(dash_id)
51
+ begin
52
+ params = {
53
+ :api_key => @api_key,
54
+ :application_key => @application_key
55
+ }
56
+
57
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/dash/#{dash_id}", params, nil, false)
58
+ rescue Exception => e
59
+ suppress_error_if_silent e
60
+ end
61
+ end
62
+
63
+ def delete_dashboard(dash_id)
64
+ begin
65
+ params = {
66
+ :api_key => @api_key,
67
+ :application_key => @application_key
68
+ }
69
+
70
+ request(Net::HTTP::Delete, "/api/#{API_VERSION}/dash/#{dash_id}", params, nil, false)
71
+ rescue Exception => e
72
+ suppress_error_if_silent e
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,27 @@
1
+ require 'dogapi'
2
+
3
+ module Dogapi
4
+ class V1 # for namespacing
5
+
6
+ class SearchService < Dogapi::APIService
7
+
8
+ API_VERSION = "v1"
9
+
10
+ def search(query)
11
+ begin
12
+ params = {
13
+ :api_key => @api_key,
14
+ :application_key => @application_key,
15
+ :q => query
16
+ }
17
+
18
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/search", params, nil, false)
19
+ rescue Exception => e
20
+ suppress_error_if_silent e
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'test/unit'
2
+ require 'dogapi'
3
+ require 'time'
4
+
5
+ unless Kernel.respond_to?(:require_relative)
6
+ module Kernel
7
+ def require_relative(path)
8
+ require File.join(File.dirname(caller[0]), path.to_str)
9
+ end
10
+ end
11
+ end
12
+
13
+
14
+ class TestBase < Test::Unit::TestCase
15
+
16
+ def config_client_test_env
17
+ @api_key = ENV['DATADOG_API_KEY']
18
+ @app_key = ENV['DATADOG_APP_KEY']
19
+ if not @api_key or not @app_key
20
+ puts "\n"
21
+ abort "To run tests in your environment, set 'DATADOG_API_KEY' and 'DATADOG_APP_KEY' to appropriate values for your account. Be aware that the tests will submit data, some of which won't be removed at the end.\n\n"
22
+ end
23
+ end
24
+
25
+ def setup
26
+ config_client_test_env()
27
+ end
28
+ end
29
+
data/tests/test_client.rb CHANGED
@@ -1,21 +1,8 @@
1
- require 'test/unit'
2
1
  require 'dogapi'
3
2
  require 'time'
3
+ require_relative 'test_base.rb'
4
4
 
5
- class TestClient < Test::Unit::TestCase
6
-
7
- def config_client_test_env
8
- @api_key = ENV['DATADOG_API_KEY']
9
- @app_key = ENV['DATADOG_APP_KEY']
10
- if not @api_key or not @app_key
11
- puts "\n"
12
- abort "To run tests in your environment, set 'DATADOG_API_KEY' and 'DATADOG_APP_KEY' to appropriate values for your account. Be aware that the tests will submit data, some of which won't be removed at the end.\n\n"
13
- end
14
- end
15
-
16
- def setup
17
- config_client_test_env()
18
- end
5
+ class TestClient < TestBase
19
6
 
20
7
  def test_tags
21
8
  hostname = 'test.tag.host'
@@ -0,0 +1,35 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require_relative 'test_base.rb'
4
+
5
+ class TestComments < TestBase
6
+
7
+ def test_comments
8
+ dog = Dogapi::Client.new(@api_key, @app_key)
9
+
10
+ # Create a comment
11
+ dog.comment('test comment')
12
+
13
+ # Create a comment as a user.
14
+ handle = "carlo+14.1@datadoghq.com"
15
+ status, comment_response = dog.comment('test comment with handle', :handle => handle)
16
+ comment = comment_response["comment"]
17
+ assert_equal "200", status, "Comment did not succeed"
18
+
19
+ # Reply to a comment.
20
+ status, reply_response = dog.comment('replying!', :related_event_id => comment["id"])
21
+ reply = reply_response["comment"]
22
+ assert_equal "200", status, "Reply did not work."
23
+ # HACK: issue #900 on dogweb. id types should be the same.
24
+ assert_equal comment["id"].to_s, reply["related_event_id"]
25
+
26
+ # Update a comment.
27
+ dog.update_comment(reply["related_event_id"], :message => "Updating the comment")
28
+
29
+ # Delete a comment.
30
+ status, to_delete_response = dog.comment("im dead")
31
+ to_delete = to_delete_response["comment"]
32
+ dog.delete_comment(to_delete["id"])
33
+
34
+ end
35
+ end
@@ -0,0 +1,71 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require_relative 'test_base.rb'
4
+
5
+ class TestDashes < TestBase
6
+
7
+ def test_dashes
8
+ dog = Dogapi::Client.new(@api_key, @app_key)
9
+
10
+ # Create a dashboard.
11
+ title = 'foobar'
12
+ description = 'desc'
13
+ graphs = [{
14
+ "definition" => {
15
+ "events" => [],
16
+ "requests "=> [
17
+ {"q" => "avg:system.mem.free{*}"}
18
+ ],
19
+ "viz" => "timeseries"
20
+ },
21
+ "title" => "Average Memory Free"
22
+ }]
23
+
24
+ status, dash_response = dog.create_dashboard(title, description, graphs)
25
+ assert_equal "200", status, "Creation failed"
26
+
27
+ dash_id = dash_response["dash"]["id"]
28
+
29
+ # Fetch the dashboard and assert all is well.
30
+ status, dash_response = dog.get_dashboard(dash_id)
31
+ assert_equal "200", status, "Fetch failed"
32
+ dash = dash_response["dash"]
33
+ assert_equal title, dash["title"]
34
+ assert_equal description, dash["description"]
35
+ assert_equal graphs, dash["graphs"]
36
+
37
+ # Update the dashboard.
38
+ title = 'blahfoobar'
39
+ description = 'asdfdesc'
40
+ graphs = [{
41
+ "definition" => {
42
+ "events" => [],
43
+ "requests "=> [
44
+ {"q" => "sum:system.mem.free{*}"}
45
+ ],
46
+ "viz" => "timeseries"
47
+ },
48
+ "title" => "Sum Memory Free"
49
+ }]
50
+
51
+ status, dash_response = dog.update_dashboard(dash_id, title, description, graphs)
52
+ assert_equal "200", status, "Updated failed"
53
+
54
+ # Fetch the dashboard and assert all is well.
55
+ status, dash_response = dog.get_dashboard(dash_id)
56
+ assert_equal "200", status, "Fetch failed"
57
+ dash = dash_response["dash"]
58
+ assert_equal title, dash["title"]
59
+ assert_equal description, dash["description"]
60
+ assert_equal graphs, dash["graphs"]
61
+
62
+ # Delete the dashboard.
63
+ status, dash_response = dog.delete_dashboard(dash_id)
64
+ assert_equal "204", status, "Deleted failed"
65
+
66
+ # Fetch the dashboard and assert all it's gone.
67
+ status, dash_response = dog.get_dashboard(dash_id)
68
+ assert_equal "404", status, "Still there failed"
69
+
70
+ end
71
+ end
@@ -0,0 +1,19 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require_relative 'test_base.rb'
4
+
5
+ class TestSearch < TestBase
6
+
7
+ def test_search
8
+ dog = Dogapi::Client.new(@api_key, @app_key)
9
+
10
+ facets = ["hosts", "metrics"]
11
+ facets.each do |facet|
12
+ status, results_body = dog.search("#{facet}:foo")
13
+ results = results_body["results"]
14
+ assert_equal status, "200", "failed search #{facet} facet"
15
+ assert results[facet].kind_of?(Array), "#{facet} facet doesnt exist"
16
+ end
17
+
18
+ end
19
+ end
metadata CHANGED
@@ -1,97 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: dogapi
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 3
9
- - 6
10
- version: 1.3.6
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Datadog, Inc.
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-07-25 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: json
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 1
29
- segments:
30
- - 1
31
- - 5
32
- - 1
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 1.5.1
34
22
  type: :runtime
35
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.1
36
30
  description:
37
31
  email: packages@datadoghq.com
38
32
  executables: []
39
-
40
33
  extensions: []
41
-
42
- extra_rdoc_files:
34
+ extra_rdoc_files:
43
35
  - README.rdoc
44
- files:
36
+ files:
45
37
  - lib/capistrano/datadog.rb
46
38
  - lib/capistrano/README.md
47
39
  - lib/dogapi/common.rb
48
40
  - lib/dogapi/event.rb
49
41
  - lib/dogapi/facade.rb
50
42
  - lib/dogapi/metric.rb
43
+ - lib/dogapi/v1/comment.rb
44
+ - lib/dogapi/v1/dash.rb
51
45
  - lib/dogapi/v1/event.rb
52
46
  - lib/dogapi/v1/metric.rb
47
+ - lib/dogapi/v1/search.rb
53
48
  - lib/dogapi/v1/tag.rb
54
49
  - lib/dogapi/v1.rb
55
50
  - lib/dogapi.rb
51
+ - tests/test_base.rb
56
52
  - tests/test_client.rb
53
+ - tests/test_comments.rb
54
+ - tests/test_dashes.rb
55
+ - tests/test_search.rb
57
56
  - README.rdoc
58
57
  homepage: http://datadoghq.com/
59
- licenses:
58
+ licenses:
60
59
  - BSD
61
60
  post_install_message:
62
- rdoc_options:
61
+ rdoc_options:
63
62
  - --title
64
63
  - DogAPI -- DataDog Client
65
64
  - --main
66
65
  - README.rdoc
67
66
  - --line-numbers
68
67
  - --inline-source
69
- require_paths:
68
+ require_paths:
70
69
  - lib
71
- required_ruby_version: !ruby/object:Gem::Requirement
70
+ required_ruby_version: !ruby/object:Gem::Requirement
72
71
  none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- hash: 3
77
- segments:
78
- - 0
79
- version: "0"
80
- required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
77
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- hash: 3
86
- segments:
87
- - 0
88
- version: "0"
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
89
82
  requirements: []
90
-
91
83
  rubyforge_project:
92
84
  rubygems_version: 1.8.24
93
85
  signing_key:
94
86
  specification_version: 3
95
87
  summary: Ruby bindings for Datadog's API
96
- test_files:
88
+ test_files:
97
89
  - tests/test_client.rb