dogapi 1.22.0 → 1.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -1
  4. data/.rubocop.yml +13 -0
  5. data/.travis.yml +3 -1
  6. data/CHANGELOG.md +16 -1
  7. data/Gemfile +6 -3
  8. data/README.rdoc +1 -1
  9. data/Rakefile +8 -15
  10. data/lib/capistrano/datadog.rb +12 -12
  11. data/lib/capistrano/datadog/v2.rb +5 -5
  12. data/lib/capistrano/datadog/v3.rb +9 -4
  13. data/lib/dogapi/common.rb +43 -39
  14. data/lib/dogapi/event.rb +4 -4
  15. data/lib/dogapi/facade.rb +57 -54
  16. data/lib/dogapi/metric.rb +2 -2
  17. data/lib/dogapi/v1/alert.rb +17 -80
  18. data/lib/dogapi/v1/comment.rb +8 -39
  19. data/lib/dogapi/v1/dash.rb +20 -67
  20. data/lib/dogapi/v1/embed.rb +11 -57
  21. data/lib/dogapi/v1/event.rb +37 -72
  22. data/lib/dogapi/v1/metric.rb +17 -46
  23. data/lib/dogapi/v1/monitor.rb +54 -194
  24. data/lib/dogapi/v1/screenboard.rb +8 -77
  25. data/lib/dogapi/v1/search.rb +5 -11
  26. data/lib/dogapi/v1/service_check.rb +6 -15
  27. data/lib/dogapi/v1/snapshot.rb +10 -15
  28. data/lib/dogapi/v1/tag.rb +39 -99
  29. data/lib/dogapi/v1/user.rb +15 -69
  30. data/lib/dogapi/version.rb +1 -1
  31. data/spec/integration/alert_spec.rb +48 -0
  32. data/spec/integration/comment_spec.rb +32 -0
  33. data/spec/integration/common_spec.rb +32 -0
  34. data/spec/integration/dash_spec.rb +60 -0
  35. data/spec/integration/embed_spec.rb +43 -0
  36. data/spec/integration/event_spec.rb +73 -0
  37. data/spec/integration/metric_spec.rb +96 -0
  38. data/spec/integration/monitor_spec.rb +106 -0
  39. data/spec/integration/screenboard_spec.rb +61 -0
  40. data/spec/integration/search_spec.rb +11 -0
  41. data/spec/integration/service_check_spec.rb +12 -0
  42. data/spec/integration/snapshot_spec.rb +24 -0
  43. data/spec/integration/tag_spec.rb +66 -0
  44. data/spec/integration/user_spec.rb +46 -0
  45. data/spec/spec_helper.rb +85 -16
  46. data/spec/unit/common_spec.rb +101 -0
  47. data/spec/unit/facade_spec.rb +79 -0
  48. metadata +55 -52
  49. data/spec/alerts_spec.rb +0 -33
  50. data/spec/common_spec.rb +0 -37
  51. data/spec/facade_spec.rb +0 -166
  52. data/spec/support/cassettes/Alerts/create/returns_HTTP_code_200.yml +0 -114
  53. data/spec/support/cassettes/Alerts/create/returns_a_valid_event_ID.yml +0 -114
  54. data/spec/support/cassettes/Alerts/create/returns_the_same_query_as_sent.yml +0 -114
  55. data/spec/support/cassettes/Facade/Events/emits_aggregate_events.yml +0 -193
  56. data/spec/support/cassettes/Facade/Events/emits_events_and_retrieves_them.yml +0 -100
  57. data/spec/support/cassettes/Facade/Events/emits_events_with_specified_priority.yml +0 -98
  58. data/spec/support/cassettes/Facade/Tags/adds_updates_and_detaches_tags.yml +0 -442
  59. data/tests/test_alerts.rb +0 -38
  60. data/tests/test_base.rb +0 -30
  61. data/tests/test_client.rb +0 -23
  62. data/tests/test_comments.rb +0 -39
  63. data/tests/test_dashes.rb +0 -85
  64. data/tests/test_embed.rb +0 -194
  65. data/tests/test_monitors.rb +0 -192
  66. data/tests/test_screenboard.rb +0 -90
  67. data/tests/test_search.rb +0 -20
  68. data/tests/test_snapshot.rb +0 -28
  69. data/tests/test_users.rb +0 -65
@@ -5,23 +5,18 @@ module Dogapi
5
5
 
6
6
  class SnapshotService < Dogapi::APIService
7
7
 
8
- API_VERSION = "v1"
8
+ API_VERSION = 'v1'
9
9
 
10
10
  def snapshot(metric_query, start_ts, end_ts, event_query=nil)
11
- begin
12
- params = {
13
- :api_key => @api_key,
14
- :application_key => @application_key,
15
- :metric_query => metric_query,
16
- :start => start_ts,
17
- :end => end_ts,
18
- :event_query => event_query
19
- }
20
-
21
- request(Net::HTTP::Get, "/api/#{API_VERSION}/graph/snapshot", params, nil, false)
22
- rescue Exception => e
23
- suppress_error_if_silent e
24
- end
11
+ extra_params = {
12
+ :metric_query => metric_query,
13
+ :start => start_ts,
14
+ :end => end_ts,
15
+ }
16
+
17
+ extra_params[:event_query] = event_query if event_query
18
+
19
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/graph/snapshot", extra_params, nil, false)
25
20
  end
26
21
 
27
22
  end
@@ -3,136 +3,76 @@ require 'dogapi'
3
3
  module Dogapi
4
4
  class V1 # for namespacing
5
5
 
6
- #
6
+ #
7
7
  class TagService < Dogapi::APIService
8
8
 
9
- API_VERSION = "v1"
9
+ API_VERSION = 'v1'
10
10
 
11
11
  # Gets all tags in your org and the hosts tagged with them
12
12
  def get_all(source=nil)
13
- begin
14
- params = {
15
- :api_key => @api_key,
16
- :application_key => @application_key
17
- }
18
- if source
19
- params['source'] = source
20
- end
21
-
22
- request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts', params, nil, false)
23
- rescue Exception => e
24
- if @silent
25
- warn e
26
- return -1, {}
27
- else
28
- raise e
29
- end
13
+ extra_params = {}
14
+ if source
15
+ extra_params['source'] = source
30
16
  end
17
+
18
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts', extra_params, nil, false)
31
19
  end
32
20
 
33
21
  # Gets all tags for a given host
34
22
  def get(host_id, source=nil, by_source=false)
35
- begin
36
- params = {
37
- :api_key => @api_key,
38
- :application_key => @application_key
39
- }
40
- if source
41
- params['source'] = source
42
- end
43
- if by_source
44
- params['by_source'] = 'true'
45
- end
46
-
47
- request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
48
- rescue Exception => e
49
- if @silent
50
- warn e
51
- return -1, {}
52
- else
53
- raise e
54
- end
23
+ extra_params = {}
24
+ if source
25
+ extra_params['source'] = source
26
+ end
27
+ if by_source
28
+ extra_params['by_source'] = 'true'
55
29
  end
30
+
31
+ request(Net::HTTP::Get, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, extra_params, nil, false)
56
32
  end
57
33
 
58
34
  # Adds a list of tags to a host
59
35
  def add(host_id, tags, source=nil)
60
- begin
61
- params = {
62
- :api_key => @api_key,
63
- :application_key => @application_key
64
- }
65
- if source
66
- params['source'] = source
67
- end
68
-
69
- body = {
70
- :tags => tags
71
- }
72
-
73
- request(Net::HTTP::Post, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
74
- rescue Exception => e
75
- if @silent
76
- warn e
77
- return -1, {}
78
- else
79
- raise e
80
- end
36
+ extra_params = {}
37
+ if source
38
+ extra_params['source'] = source
81
39
  end
40
+
41
+ body = {
42
+ :tags => tags
43
+ }
44
+
45
+ request(Net::HTTP::Post, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, extra_params, body, true)
82
46
  end
83
47
 
84
48
  # Remove all tags from a host and replace them with a new list
85
49
  def update(host_id, tags, source=nil)
86
- begin
87
- params = {
88
- :api_key => @api_key,
89
- :application_key => @application_key
90
- }
91
- if source
92
- params['source'] = source
93
- end
94
-
95
- body = {
96
- :tags => tags
97
- }
98
-
99
- request(Net::HTTP::Put, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, body, true)
100
- rescue Exception => e
101
- if @silent
102
- warn e
103
- return -1, {}
104
- else
105
- raise e
106
- end
50
+ extra_params = {}
51
+ if source
52
+ extra_params['source'] = source
107
53
  end
54
+
55
+ body = {
56
+ :tags => tags
57
+ }
58
+
59
+ request(Net::HTTP::Put, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, extra_params, body, true)
108
60
  end
109
61
 
110
62
  # <b>DEPRECATED:</b> Spelling mistake temporarily preserved as an alias.
111
63
  def detatch(host_id)
112
- warn "[DEPRECATION] Dogapi::V1::TagService.detatch() is deprecated. Use `detach` instead."
64
+ warn '[DEPRECATION] Dogapi::V1::TagService.detatch() is deprecated. Use `detach` instead.'
113
65
  detach(host_id)
114
66
  end
115
67
 
116
68
  # Remove all tags from a host
117
69
  def detach(host_id, source=nil)
118
- begin
119
- params = {
120
- :api_key => @api_key,
121
- :application_key => @application_key
122
- }
123
- if source
124
- params['source'] = source
125
- end
126
-
127
- request(Net::HTTP::Delete, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, params, nil, false)
128
- rescue Exception => e
129
- if @silent
130
- warn e
131
- return -1, {}
132
- else
133
- raise e
134
- end
70
+ extra_params = {}
71
+ if source
72
+ extra_params['source'] = source
135
73
  end
74
+
75
+ request(Net::HTTP::Delete, '/api/' + API_VERSION + '/tags/hosts/' + host_id.to_s, extra_params, nil, false)
136
76
  end
137
77
 
138
78
  end
@@ -5,72 +5,36 @@ module Dogapi
5
5
 
6
6
  class UserService < Dogapi::APIService
7
7
 
8
- API_VERSION = "v1"
8
+ API_VERSION = 'v1'
9
9
 
10
10
  # <b>DEPRECATED:</b> Going forward, we're using a new and more restful API,
11
11
  # the new methods are get_user, create_user, update_user, disable_user
12
- def invite(emails, options = {})
13
- warn "[DEPRECATION] Dogapi::V1::UserService::invite has been deprecated in favor of Dogapi::V1::UserService::create_user"
14
- begin
15
- params = {
16
- :api_key => @api_key,
17
- :application_key => @application_key
18
- }
12
+ def invite(emails, options= {})
13
+ warn '[DEPRECATION] Dogapi::V1::UserService::invite has been deprecated in favor of Dogapi::V1::UserService::create_user'
14
+ body = {
15
+ 'emails' => emails,
16
+ }.merge options
19
17
 
20
- body = {
21
- 'emails' => emails,
22
- }.merge options
23
-
24
- request(Net::HTTP::Post, "/api/#{API_VERSION}/invite_users", params, body, true)
25
- rescue Exception => e
26
- suppress_error_if_silent e
27
- end
18
+ request(Net::HTTP::Post, "/api/#{API_VERSION}/invite_users", nil, body, true)
28
19
  end
29
20
 
30
21
  # Create a user
31
22
  #
32
23
  # :description => Hash: user description containing 'handle' and 'name' properties
33
- def create_user(description = {})
34
- begin
35
- params = {
36
- :api_key => @api_key,
37
- :application_key => @application_key
38
- }
39
-
40
- request(Net::HTTP::Post, "/api/#{API_VERSION}/user", params, description, true)
41
- rescue Exception => e
42
- suppress_error_if_silent e
43
- end
24
+ def create_user(description= {})
25
+ request(Net::HTTP::Post, "/api/#{API_VERSION}/user", nil, description, true)
44
26
  end
45
27
 
46
28
  # Retrieve user information
47
29
  #
48
30
  # :handle => String: user handle
49
31
  def get_user(handle)
50
- begin
51
- params = {
52
- :api_key => @api_key,
53
- :application_key => @application_key
54
- }
55
-
56
- request(Net::HTTP::Get, "/api/#{API_VERSION}/user/#{handle}", params, nil, false)
57
- rescue Exception => e
58
- suppress_error_if_silent e
59
- end
32
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/user/#{handle}", nil, nil, false)
60
33
  end
61
34
 
62
35
  # Retrieve all users
63
- def get_all_users()
64
- begin
65
- params = {
66
- :api_key => @api_key,
67
- :application_key => @application_key
68
- }
69
-
70
- request(Net::HTTP::Get, "/api/#{API_VERSION}/user", params, nil, false)
71
- rescue Exception => e
72
- suppress_error_if_silent e
73
- end
36
+ def get_all_users
37
+ request(Net::HTTP::Get, "/api/#{API_VERSION}/user", nil, nil, false)
74
38
  end
75
39
 
76
40
  # Update a user
@@ -78,33 +42,15 @@ module Dogapi
78
42
  # :handle => String: user handle
79
43
  # :description => Hash: user description optionally containing 'name', 'email',
80
44
  # 'is_admin', 'disabled' properties
81
- def update_user(handle, description = {})
82
- begin
83
- params = {
84
- :api_key => @api_key,
85
- :application_key => @application_key
86
- }
87
-
88
- request(Net::HTTP::Put, "/api/#{API_VERSION}/user/#{handle}", params, description, true)
89
- rescue Exception => e
90
- suppress_error_if_silent e
91
- end
45
+ def update_user(handle, description= {})
46
+ request(Net::HTTP::Put, "/api/#{API_VERSION}/user/#{handle}", nil, description, true)
92
47
  end
93
48
 
94
49
  # Disable a user
95
50
  #
96
51
  # :handle => String: user handle
97
52
  def disable_user(handle)
98
- begin
99
- params = {
100
- :api_key => @api_key,
101
- :application_key => @application_key
102
- }
103
-
104
- request(Net::HTTP::Delete, "/api/#{API_VERSION}/user/#{handle}", params, nil, false)
105
- rescue Exception => e
106
- suppress_error_if_silent e
107
- end
53
+ request(Net::HTTP::Delete, "/api/#{API_VERSION}/user/#{handle}", nil, nil, false)
108
54
  end
109
55
 
110
56
  end
@@ -1,3 +1,3 @@
1
1
  module Dogapi
2
- VERSION = "1.22.0"
2
+ VERSION = '1.23.0'
3
3
  end
@@ -0,0 +1,48 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::Client do
4
+ ALERT_ID = 42
5
+ QUERY = 'avg(last_10m):avg:test.metric.metric{host:test.metric.host} > 5'.freeze
6
+
7
+ describe '#alert' do
8
+ it_behaves_like 'an api method with options',
9
+ :alert, [QUERY],
10
+ :post, '/alert', 'query' => QUERY
11
+ end
12
+
13
+ describe '#update_alert' do
14
+ it_behaves_like 'an api method with options',
15
+ :update_alert, [ALERT_ID, QUERY],
16
+ :put, "/alert/#{ALERT_ID}", 'query' => QUERY
17
+ end
18
+
19
+ describe '#get_alert' do
20
+ it_behaves_like 'an api method',
21
+ :get_alert, [ALERT_ID],
22
+ :get, "/alert/#{ALERT_ID}"
23
+ end
24
+
25
+ describe '#delete_alert' do
26
+ it_behaves_like 'an api method',
27
+ :delete_alert, [ALERT_ID],
28
+ :delete, "/alert/#{ALERT_ID}"
29
+ end
30
+
31
+ describe '#get_all_alerts' do
32
+ it_behaves_like 'an api method',
33
+ :get_all_alerts, [],
34
+ :get, '/alert'
35
+ end
36
+
37
+ describe '#mute_alerts' do
38
+ it_behaves_like 'an api method',
39
+ :mute_alerts, [], :post,
40
+ '/mute_alerts'
41
+ end
42
+
43
+ describe '#unmute_alerts' do
44
+ it_behaves_like 'an api method',
45
+ :unmute_alerts, [],
46
+ :post, '/unmute_alerts'
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+
4
+ describe Dogapi::Client do
5
+ COMMENT_ID = 4242
6
+ MESSAGE = 'this is an example message: éè'.freeze
7
+
8
+ describe '#comment' do
9
+ it_behaves_like 'an api method with options',
10
+ :comment, [MESSAGE],
11
+ :post, '/comments', 'message' => MESSAGE
12
+ end
13
+
14
+ describe '#update_comment' do
15
+ it 'queries the api with options' do
16
+ url = api_url + "/comments/#{COMMENT_ID}"
17
+ options = { 'zzz' => 'aaa' }
18
+ stub_request(:put, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
19
+ expect(dog.send(:update_comment, COMMENT_ID, options)).to eq ['200', {}]
20
+
21
+ expect(WebMock).to have_requested(:put, url).with(
22
+ query: default_query
23
+ )
24
+ end
25
+ end
26
+
27
+ describe '#delete_comment' do
28
+ it_behaves_like 'an api method',
29
+ :delete_comment, [COMMENT_ID],
30
+ :delete, "/comments/#{COMMENT_ID}"
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::APIService do
4
+ describe '#connect' do
5
+ let(:url) { api_url + '/awesome' }
6
+ let(:second_url) { 'http://app.example.com/api/v1/awesome' }
7
+
8
+ context 'when only the default endpoint is used' do
9
+ let(:service) { Dogapi::APIService.new api_key, app_key }
10
+ context 'and it is up' do
11
+ it 'only queries one endpoint' do
12
+ stub_request(:get, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
13
+ expect(service.request(Net::HTTP::Get, '/api/v1/awesome', nil, nil, true, true)).to eq(['200', {}])
14
+
15
+ expect(WebMock).to have_requested(:get, url).with(
16
+ query: default_query
17
+ )
18
+ end
19
+ end
20
+ context 'and it is down' do
21
+ it 'only queries one endpoint' do
22
+ stub_request(:get, /#{url}/).to_timeout
23
+ expect(service.request(Net::HTTP::Get, '/api/v1/awesome', nil, nil, true, true)).to eq([-1, {}])
24
+
25
+ expect(WebMock).to have_requested(:get, url).with(
26
+ query: default_query
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end