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
@@ -0,0 +1,60 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::Client do
4
+ DASH_ID = 424_242
5
+ TITLE = 'My awesome dashboard'.freeze
6
+ DESCRIPTION = 'Lorem ipsum'.freeze
7
+ GRAPHS = [{
8
+ 'definition' => {
9
+ 'events' => [],
10
+ 'requests ' => [
11
+ { 'q' => 'avg:system.mem.free{*}' }
12
+ ],
13
+ 'viz' => 'timeseries'
14
+ },
15
+ 'title' => 'Average Memory Free'
16
+ }].freeze
17
+ TEMPLATE_VARIABLES = [{
18
+ 'name' => 'host1',
19
+ 'prefix' => 'host',
20
+ 'default' => 'host:my-host'
21
+ }].freeze
22
+
23
+ DASH_BODY = {
24
+ title: TITLE,
25
+ description: DESCRIPTION,
26
+ graphs: GRAPHS,
27
+ template_variables: TEMPLATE_VARIABLES
28
+ }.freeze
29
+ DASH_ARGS = DASH_BODY.values
30
+
31
+ describe '#create_dashboard' do
32
+ it_behaves_like 'an api method',
33
+ :create_dashboard, DASH_ARGS,
34
+ :post, '/dash', DASH_BODY
35
+ end
36
+
37
+ describe '#update_dashboard' do
38
+ it_behaves_like 'an api method',
39
+ :update_dashboard, [DASH_ID] + DASH_ARGS,
40
+ :put, "/dash/#{DASH_ID}", DASH_BODY
41
+ end
42
+
43
+ describe '#get_dashboard' do
44
+ it_behaves_like 'an api method',
45
+ :get_dashboard, [DASH_ID],
46
+ :get, "/dash/#{DASH_ID}"
47
+ end
48
+
49
+ describe '#get_dashboards' do
50
+ it_behaves_like 'an api method',
51
+ :get_dashboards, [],
52
+ :get, '/dash'
53
+ end
54
+
55
+ describe '#delete_dashboard' do
56
+ it_behaves_like 'an api method',
57
+ :delete_dashboard, [DASH_ID],
58
+ :delete, "/dash/#{DASH_ID}"
59
+ end
60
+ end
@@ -0,0 +1,43 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::Client do
4
+ EMBED_ID = 42_424_242
5
+ GRAPH_JSON = '{
6
+ "requests": [{
7
+ "q": "avg:system.load.1{*}"
8
+ }],
9
+ "viz": "timeseries",
10
+ "events": []
11
+ }'.freeze
12
+
13
+ describe '#get_all_embeds' do
14
+ it_behaves_like 'an api method',
15
+ :get_all_embeds, [],
16
+ :get, '/graph/embed'
17
+ end
18
+
19
+ describe '#get_embed' do
20
+ it_behaves_like 'an api method with optional params',
21
+ :get_embed, [EMBED_ID],
22
+ :get, "/graph/embed/#{EMBED_ID}",
23
+ embed_size: 'medium', timeframe: '1_hour', legend: 'no', title: 'fsafasfdas'
24
+ end
25
+
26
+ describe '#create_embed' do
27
+ it_behaves_like 'an api method with options',
28
+ :create_embed, [GRAPH_JSON],
29
+ :post, '/graph/embed', graph_json: GRAPH_JSON
30
+ end
31
+
32
+ describe '#enable_embed' do
33
+ it_behaves_like 'an api method',
34
+ :enable_embed, [EMBED_ID],
35
+ :get, "/graph/embed/#{EMBED_ID}/enable"
36
+ end
37
+
38
+ describe '#revoke_embed' do
39
+ it_behaves_like 'an api method',
40
+ :revoke_embed, [EMBED_ID],
41
+ :get, "/graph/embed/#{EMBED_ID}/revoke"
42
+ end
43
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+
4
+ describe Dogapi::Client do
5
+ EVENT_ID = 123_456
6
+ EVENT_START = Time.now.to_i - 10
7
+ EVENT_END = EVENT_START + 5
8
+ EVENT_OPTIONS = {
9
+ msg_text: 'My event text',
10
+ date_happened: EVENT_START,
11
+ msg_title: 'My event title',
12
+ priority: 'normal',
13
+ parent: nil,
14
+ tags: ['test:test'],
15
+ aggregation_key: 'fasdfasffergeqgqe',
16
+ alert_type: nil,
17
+ event_type: nil,
18
+ source_type_name: nil,
19
+ title: 'My event title',
20
+ text: 'My event text',
21
+ host: 'data.dog',
22
+ device: nil
23
+ }.freeze
24
+ EVENT = Dogapi::Event.new 'My event text', EVENT_OPTIONS
25
+ STREAM_PARAMS = {
26
+ priority: 'normal',
27
+ sources: 'chef',
28
+ tags: %w(fds fdsfsa)
29
+ }.freeze
30
+
31
+ describe '#emit_event' do
32
+ it 'queries the api' do
33
+ url = api_url + '/events'
34
+ stub_request(:post, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
35
+ expect(dog.send(:emit_event, EVENT)).to eq ['200', {}]
36
+
37
+ body = MultiJson.dump(EVENT_OPTIONS)
38
+
39
+ expect(WebMock).to have_requested(:post, url).with(
40
+ query: { 'api_key' => api_key },
41
+ body: body
42
+ )
43
+ end
44
+ end
45
+
46
+ describe '#get_event' do
47
+ it_behaves_like 'an api method',
48
+ :get_event, [EVENT_ID],
49
+ :get, "/events/#{EVENT_ID}"
50
+ end
51
+
52
+ describe '#delete_event' do
53
+ it_behaves_like 'an api method',
54
+ :delete_event, [EVENT_ID],
55
+ :delete, "/events/#{EVENT_ID}"
56
+ end
57
+
58
+ describe '#stream' do
59
+ it 'queries the api with params' do
60
+ url = api_url + '/events'
61
+ stub_request(:get, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
62
+ expect(dog.send(:stream, EVENT_START, EVENT_END, STREAM_PARAMS)).to eq ['200', {}]
63
+
64
+ params = STREAM_PARAMS.merge(start: EVENT_START, end: EVENT_END)
65
+ params.each { |k, v| params[k] = v.join(',') if v.is_a? Array }
66
+ params.merge! default_query
67
+
68
+ expect(WebMock).to have_requested(:get, url).with(
69
+ query: params
70
+ )
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,96 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::Client do
4
+ METRIC_QUERY = 'avg(last_10m):avg:test.metric.metric{host:test.metric.host} > 5'.freeze
5
+ FROM = Time.now
6
+ TO = FROM + 10
7
+ METRIC = 'test.metric'.freeze
8
+ POINTS = [[FROM, 10], [TO, 20.0]].freeze
9
+
10
+ EMIT_BODY = {
11
+ 'series' => [{
12
+ 'metric' => 'test.metric',
13
+ 'points' => [[FROM.to_i, 10.0], [TO.to_i, 20.0]],
14
+ 'type' => 'gauge',
15
+ 'host' => 'data.dog',
16
+ 'device' => nil
17
+ }]
18
+ }.freeze
19
+
20
+ BATCH_BODY = {
21
+ 'series' => [{
22
+ 'metric' => 'test.metric',
23
+ 'points' => [[FROM.to_i, 10.0]],
24
+ 'type' => 'gauge',
25
+ 'host' => 'data.dog',
26
+ 'device' => nil
27
+ }, {
28
+ 'metric' => 'test.metric',
29
+ 'points' => [[TO.to_i, 20.0]],
30
+ 'type' => 'gauge',
31
+ 'host' => 'data.dog',
32
+ 'device' => nil
33
+ }]
34
+ }.freeze
35
+
36
+ METRIC_PARAMS = { query: METRIC_QUERY, from: FROM.to_i, to: TO.to_i }.freeze
37
+
38
+ describe '#emit_point' do
39
+ it 'queries the api' do
40
+ url = api_url + '/series'
41
+ stub_request(:post, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
42
+ points = Marshal.load(Marshal.dump(POINTS[0]))
43
+ expect(dog.send(:emit_points, METRIC, [points])).to eq ['200', {}]
44
+
45
+ body = Marshal.load(Marshal.dump(EMIT_BODY))
46
+ body['series'][0]['points'] = [body['series'][0]['points'][0]]
47
+ body = MultiJson.dump(body)
48
+
49
+ expect(WebMock).to have_requested(:post, url).with(
50
+ query: { 'api_key' => api_key },
51
+ body: body
52
+ )
53
+ end
54
+ end
55
+
56
+ describe '#emit_points' do
57
+ it 'queries the api' do
58
+ url = api_url + '/series'
59
+ stub_request(:post, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
60
+ points = Marshal.load(Marshal.dump(POINTS))
61
+ expect(dog.send(:emit_points, METRIC, points)).to eq ['200', {}]
62
+
63
+ body = MultiJson.dump(EMIT_BODY)
64
+
65
+ expect(WebMock).to have_requested(:post, url).with(
66
+ query: { 'api_key' => api_key },
67
+ body: body
68
+ )
69
+ end
70
+ end
71
+
72
+ describe '#get_points' do
73
+ it_behaves_like 'an api method with params',
74
+ :get_points, [],
75
+ :get, '/query', METRIC_PARAMS
76
+ end
77
+
78
+ describe '#batch_metrics' do
79
+ it 'queries the api' do
80
+ url = api_url + '/series'
81
+ stub_request(:post, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
82
+ points = Marshal.load(Marshal.dump(POINTS))
83
+ dog.batch_metrics do
84
+ dog.emit_points(METRIC, points[0, 1].clone)
85
+ dog.emit_points(METRIC, points[1, 1].clone)
86
+ end
87
+
88
+ body = MultiJson.dump(BATCH_BODY)
89
+
90
+ expect(WebMock).to have_requested(:post, url).with(
91
+ query: { 'api_key' => api_key },
92
+ body: body
93
+ )
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,106 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::Client do
4
+ MONITOR_ID = 42
5
+ MONITOR_TYPE = 'custom type'.freeze
6
+ MONITOR_QUERY = 'avg(last_10m):avg:test.metric.metric{host:test.metric.host} > 5'.freeze
7
+ DOWNTIME_SCOPE = 'host:vagrant-ubuntu-trusty-64'.freeze
8
+ DOWNTIME_ID = 424_242_424_242
9
+ MUTE_HOSTNAME = 'vagrant-ubuntu-trusty-32'.freeze
10
+
11
+ describe '#monitor' do
12
+ it_behaves_like 'an api method with options',
13
+ :monitor, [MONITOR_TYPE, MONITOR_QUERY],
14
+ :post, '/monitor', 'type' => MONITOR_TYPE, 'query' => MONITOR_QUERY
15
+ end
16
+
17
+ describe '#update_monitor' do
18
+ it_behaves_like 'an api method with options',
19
+ :update_monitor, [MONITOR_ID, MONITOR_QUERY],
20
+ :put, "/monitor/#{MONITOR_ID}", 'query' => MONITOR_QUERY
21
+ end
22
+
23
+ describe '#get_monitor' do
24
+ it_behaves_like 'an api method with optional params',
25
+ :get_monitor, [MONITOR_ID],
26
+ :get, "/monitor/#{MONITOR_ID}", group_states: %w(custom all)
27
+ end
28
+
29
+ describe '#delete_monitor' do
30
+ it_behaves_like 'an api method',
31
+ :delete_monitor, [MONITOR_ID],
32
+ :delete, "/monitor/#{MONITOR_ID}"
33
+ end
34
+
35
+ describe '#get_all_monitors' do
36
+ it_behaves_like 'an api method with optional params',
37
+ :get_all_monitors, [],
38
+ :get, '/monitor', group_states: %w(custom all), tags: ['test', 'key:value']
39
+ end
40
+
41
+ describe '#mute_monitors' do
42
+ it_behaves_like 'an api method',
43
+ :mute_monitors, [],
44
+ :post, '/monitor/mute_all'
45
+ end
46
+
47
+ describe '#unmute_monitors' do
48
+ it_behaves_like 'an api method',
49
+ :unmute_monitors, [],
50
+ :post, '/monitor/unmute_all'
51
+ end
52
+
53
+ describe '#mute_monitor' do
54
+ it_behaves_like 'an api method',
55
+ :mute_monitor, [MONITOR_ID],
56
+ :post, "/monitor/#{MONITOR_ID}/mute", {}
57
+ end
58
+
59
+ describe '#unmute_monitor' do
60
+ it_behaves_like 'an api method',
61
+ :unmute_monitor, [MONITOR_ID],
62
+ :post, "/monitor/#{MONITOR_ID}/unmute", {}
63
+ end
64
+
65
+ describe '#schedule_downtime' do
66
+ it_behaves_like 'an api method with options',
67
+ :schedule_downtime, [DOWNTIME_SCOPE],
68
+ :post, '/downtime', 'scope' => DOWNTIME_SCOPE
69
+ end
70
+
71
+ describe '#update_downtime' do
72
+ it_behaves_like 'an api method with options',
73
+ :update_downtime, [DOWNTIME_ID],
74
+ :put, "/downtime/#{DOWNTIME_ID}", {}
75
+ end
76
+
77
+ describe '#get_downtime' do
78
+ it_behaves_like 'an api method',
79
+ :get_downtime, [DOWNTIME_ID],
80
+ :get, "/downtime/#{DOWNTIME_ID}"
81
+ end
82
+
83
+ describe '#cancel_downtime' do
84
+ it_behaves_like 'an api method',
85
+ :cancel_downtime, [DOWNTIME_ID],
86
+ :delete, "/downtime/#{DOWNTIME_ID}"
87
+ end
88
+
89
+ describe '#get_all_downtimes' do
90
+ it_behaves_like 'an api method',
91
+ :get_all_downtimes, [],
92
+ :get, '/downtime'
93
+ end
94
+
95
+ describe '#mute_host' do
96
+ it_behaves_like 'an api method with options',
97
+ :mute_host, [MUTE_HOSTNAME],
98
+ :post, "/host/#{MUTE_HOSTNAME}/mute", {}
99
+ end
100
+
101
+ describe '#unmute_host' do
102
+ it_behaves_like 'an api method',
103
+ :unmute_host, [MUTE_HOSTNAME],
104
+ :post, "/host/#{MUTE_HOSTNAME}/unmute", {}
105
+ end
106
+ end
@@ -0,0 +1,61 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::Client do
4
+ BOARD_ID = 4_242_424_242
5
+ SCREEN_DESCRIPTION = {
6
+ 'width' => 1024,
7
+ 'height' => 768,
8
+ 'board_title' => 'dogapi test',
9
+ 'widgets' => [
10
+ {
11
+ 'type' => 'image',
12
+ 'height' => 20,
13
+ 'width' => 32,
14
+ 'y' => 7,
15
+ 'x' => 32,
16
+ 'url' => 'https://path/to/image.jpg'
17
+ }
18
+ ]
19
+ }.freeze
20
+ describe '#create_screenboard' do
21
+ it_behaves_like 'an api method',
22
+ :create_screenboard, [SCREEN_DESCRIPTION],
23
+ :post, '/screen', SCREEN_DESCRIPTION
24
+ end
25
+
26
+ describe '#update_screenboard' do
27
+ it_behaves_like 'an api method',
28
+ :update_screenboard, [BOARD_ID] + [SCREEN_DESCRIPTION],
29
+ :put, "/screen/#{BOARD_ID}", SCREEN_DESCRIPTION
30
+ end
31
+
32
+ describe '#get_screenboard' do
33
+ it_behaves_like 'an api method',
34
+ :get_screenboard, [BOARD_ID],
35
+ :get, "/screen/#{BOARD_ID}"
36
+ end
37
+
38
+ describe '#get_all_screenboards' do
39
+ it_behaves_like 'an api method',
40
+ :get_all_screenboards, [],
41
+ :get, '/screen'
42
+ end
43
+
44
+ describe '#delete_screenboard' do
45
+ it_behaves_like 'an api method',
46
+ :delete_screenboard, [BOARD_ID],
47
+ :delete, "/screen/#{BOARD_ID}"
48
+ end
49
+
50
+ describe '#share_screenboard' do
51
+ it_behaves_like 'an api method',
52
+ :share_screenboard, [BOARD_ID],
53
+ :get, "/screen/share/#{BOARD_ID}"
54
+ end
55
+
56
+ describe '#revoke_screenboard' do
57
+ it_behaves_like 'an api method',
58
+ :revoke_screenboard, [BOARD_ID],
59
+ :delete, "/screen/share/#{BOARD_ID}"
60
+ end
61
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Dogapi::Client do
4
+ SEARCH_QUERY = 'hosts:database'.freeze
5
+
6
+ describe '#search' do
7
+ it_behaves_like 'an api method with params',
8
+ :search, [],
9
+ :get, '/search', q: SEARCH_QUERY
10
+ end
11
+ end