dogwatch 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+ require_relative '../test_helper'
3
+ require_relative '../../lib/dogwatch/model/config'
4
+
5
+ class TestConfig < Minitest::Test
6
+ def setup
7
+ creds = "api_key: foo\napp_key: bar"
8
+
9
+ IO.stub :read, creds do
10
+ @fromfile = DogWatch::Model::Config.new
11
+ end
12
+ end
13
+
14
+ def test_valid_creds
15
+ assert_equal 'foo', @fromfile.api_key
16
+ assert_equal 'bar', @fromfile.app_key
17
+ end
18
+
19
+ def test_creds_provided_in_initialize
20
+ config = DogWatch::Model::Config.new('foo', 'bar')
21
+ assert_equal 'foo', config.api_key
22
+ assert_equal 'bar', config.app_key
23
+ end
24
+
25
+ def test_no_creds_provided
26
+ Dir.stub :home, '/dev/null' do
27
+ err = assert_raises(RuntimeError) { DogWatch::Model::Config.new }
28
+ assert_equal 'No credentials supplied', err.message
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../test_helper'
2
+ require_relative '../../lib/dogwatch'
3
+
4
+ class TestDogWatch < Minitest::Test
5
+ def test_dogwatch_returns_monitor
6
+ IO.stub :read, "api_key: foo\napp_key: bar" do
7
+ monitor = DogWatch.monitor do
8
+ monitor 'foo' do
9
+ #
10
+ end
11
+ end
12
+ assert_instance_of(DogWatch::Monitor, monitor)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,53 @@
1
+ require_relative '../test_helper'
2
+ require_relative '../../lib/dogwatch/model/monitor'
3
+
4
+ class TestMonitorModel < Minitest::Test
5
+ def setup
6
+ @monitor = DogWatch::Model::Monitor.new('foobar')
7
+ @monitor.type(:service_check)
8
+ @monitor.query('quiz baz')
9
+ end
10
+
11
+ def test_type
12
+ assert_equal 'service check', @monitor.attributes.type
13
+ assert_kind_of String, @monitor.attributes.type
14
+ end
15
+
16
+ def test_query
17
+ assert_equal 'quiz baz', @monitor.attributes.query
18
+ assert_kind_of String, @monitor.attributes.query
19
+ end
20
+
21
+ def test_message
22
+ @monitor.message('The quick brown fox')
23
+ assert_equal 'The quick brown fox', @monitor.attributes.message
24
+ assert_kind_of String, @monitor.attributes.message
25
+ end
26
+
27
+ def test_tags
28
+ @monitor.tags([1, 2, 3])
29
+ assert_equal [1, 2, 3], @monitor.attributes.tags
30
+ assert_kind_of Array, @monitor.attributes.tags
31
+ end
32
+
33
+ def test_options
34
+ @monitor.options do
35
+ notify_no_data false
36
+ no_data_timeframe 3
37
+ end
38
+ expected = {
39
+ notify_no_data: false,
40
+ no_data_timeframe: 3
41
+ }
42
+
43
+ assert_equal expected, @monitor.attributes.options
44
+ assert_kind_of Hash, @monitor.attributes.options
45
+ end
46
+
47
+ def test_validate
48
+ assert_equal true, @monitor.validate
49
+
50
+ @monitor.attributes.query = nil
51
+ assert_equal false, @monitor.validate
52
+ end
53
+ end
@@ -0,0 +1,81 @@
1
+ require_relative '../test_helper'
2
+ require_relative '../../lib/dogwatch/model/options'
3
+
4
+ class TestOptions < Minitest::Test
5
+ OPTS = {
6
+ silenced: { '*': :foobar },
7
+ notify_no_data: true,
8
+ no_data_timeframe: 5,
9
+ timeout_h: 3,
10
+ renotify_interval: 5,
11
+ escalation_message: 'foobar',
12
+ notify_audit: true,
13
+ include_tags: false
14
+ }
15
+
16
+ # rubocop:disable Metrics/AbcSize
17
+ def setup
18
+ @options = DogWatch::Model::Options.new
19
+
20
+ @options.silenced(OPTS[:silenced])
21
+ @options.notify_no_data(OPTS[:notify_no_data])
22
+ @options.no_data_timeframe(OPTS[:no_data_timeframe])
23
+ @options.timeout_h(OPTS[:timeout_h])
24
+ @options.renotify_interval(OPTS[:renotify_interval])
25
+ @options.escalation_message(OPTS[:escalation_message])
26
+ @options.notify_audit(OPTS[:notify_audit])
27
+ @options.include_tags(OPTS[:include_tags])
28
+ end
29
+ # rubocop:enable Metrics/AbcSize
30
+
31
+ def test_silenced
32
+ assert_equal @options.attributes.silenced, '*': :foobar
33
+ assert_instance_of Hash, @options.attributes.silenced
34
+ end
35
+
36
+ def test_notify_no_data
37
+ assert_equal @options.attributes.notify_no_data, true
38
+
39
+ @options.notify_no_data
40
+ assert_equal @options.attributes.notify_no_data, false
41
+ end
42
+
43
+ def test_no_data_timeframe
44
+ assert_equal @options.attributes.no_data_timeframe, 5
45
+ assert_kind_of Integer, @options.attributes.no_data_timeframe
46
+ end
47
+
48
+ def test_timeout_h
49
+ assert_equal @options.attributes.timeout_h, 3
50
+ assert_kind_of Integer, @options.attributes.timeout_h
51
+ end
52
+
53
+ def test_renotify_interval
54
+ assert_equal @options.attributes.renotify_interval, 5
55
+ assert_kind_of Integer, @options.attributes.renotify_interval
56
+ end
57
+
58
+ def test_escalation_message
59
+ assert_equal @options.attributes.escalation_message, 'foobar'
60
+ assert_instance_of String, @options.attributes.escalation_message
61
+ end
62
+
63
+ def test_notify_audit
64
+ assert_equal @options.attributes.notify_audit, true
65
+
66
+ @options.notify_audit
67
+ assert_equal @options.attributes.notify_audit, false
68
+ end
69
+
70
+ def test_include_tags
71
+ assert_equal @options.attributes.include_tags, false
72
+
73
+ @options.include_tags
74
+ assert_equal @options.attributes.include_tags, true
75
+ end
76
+
77
+ def test_render
78
+ assert_equal OPTS, @options.render
79
+ assert_kind_of Hash, @options.render
80
+ end
81
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../test_helper'
2
+ require_relative '../../lib/dogwatch/model/response'
3
+
4
+ class TestResponse < MiniTest::Test
5
+ def setup
6
+ @error_res = DogWatch::Model::Response.new(ERROR_RES)
7
+ @valid_res = DogWatch::Model::Response.new(VALID_RES)
8
+ @accepted_res = DogWatch::Model::Response.new(ACCEPTED_RES)
9
+ end
10
+
11
+ def test_status_is_error
12
+ assert_equal :error, @error_res.status
13
+ end
14
+
15
+ def test_status_is_accepted
16
+ assert_equal :accepted, @accepted_res.status
17
+ end
18
+
19
+ def test_status_message
20
+ assert_equal ["The value provided for parameter 'query' is invalid"], \
21
+ @error_res.message
22
+ end
23
+
24
+ def test_message_output
25
+ assert_equal 'Created monitor foobar with message foobar', @valid_res.message
26
+ end
27
+
28
+ def test_thor_output
29
+ assert_equal [:created, 'Created monitor foobar with message foobar', :green],
30
+ @valid_res.to_thor
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter '/.bundle/'
4
+ end
5
+
6
+ require 'minitest/autorun'
7
+ require 'minitest/reporters'
8
+ require_relative '../lib/dogwatch'
9
+ require_relative 'data/responses'
10
+
11
+ MiniTest::Reporters.use!
12
+
13
+ module Minitest
14
+ module Assertions
15
+ def assert_nothing_raised(*)
16
+ yield
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dogwatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Greene
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dogapi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.21'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.21'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.19'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.19'
41
+ description: |-
42
+ # DogWatch
43
+
44
+ _A DSL to create DataDog monitors_
45
+
46
+ This gem is designed to provide a simple method for creating DataDog monitors in Ruby.
47
+
48
+ ## Installation
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'dogwatch'
53
+ ```
54
+
55
+ And then execute:
56
+ ```shell
57
+ $ bundle exec dogwatch create [--dogfile=DOGFILE] [--api_key=API KEY] [--app_key=APP KEY]
58
+ ```
59
+ Or install it yourself as:
60
+ ```shell
61
+ $ gem install dogwatch
62
+ ```
63
+
64
+ ## Credentials
65
+ [DataDog API credentials](https://app.datadoghq.com/account/settings#api) are required.
66
+
67
+ Generate both an api key and an app key and either place them in a file named `~/.dogwatch/credentials` or pass them via the command line.
68
+
69
+ A sample credentials file is provided in the `example` directory.
70
+ ## Usage
71
+
72
+ DogWatch is a thin DSL over the [DataDog ruby client](https://github.com/DataDog/dogapi-rb) that handles generating DataDog monitors.
73
+
74
+ The following is an example of a `Dogfile`:
75
+ ```ruby
76
+ require 'dogwatch'
77
+
78
+ DogWatch.monitor do
79
+ ## Create a new monitor - monitor name is REQUIRED
80
+ monitor 'MONITOR NAME' do
81
+ type :metric_alert # REQUIRED: One of [:metric_alert | :service_check | :event_alert]
82
+ query 'QUERY' # REQUIRED
83
+ message 'MESSAGE'
84
+ tags %w(A list of tags to associate with your monitor)
85
+
86
+ options do
87
+ silenced '*': nil
88
+ notify_no_data false
89
+ no_data_timeframe 3
90
+ timeout_h 99
91
+ renotify_interval 60
92
+ escalation_message 'oh snap'
93
+ include_tags true
94
+ end
95
+ end
96
+ end
97
+ ```
98
+
99
+ Monitors that already exist are matched by name and updated accordingly. If the name isn't matched exactly, DogWatch assumes you want a new monitor.
100
+
101
+ A sample `Dogfile` is provided in the `example` directory.
102
+
103
+ For a full list of options and a description of each parameter, see [DataDog's API documentation](http://docs.datadoghq.com/api/#monitors).
104
+
105
+ ## TO DO
106
+ * More descriptive errors
107
+ * Better error handling if a monitor fails local validation
108
+ email:
109
+ - David_Greene@rapid7.com
110
+ executables:
111
+ - dogwatch
112
+ extensions: []
113
+ extra_rdoc_files: []
114
+ files:
115
+ - ".gitignore"
116
+ - ".rubocop.yml"
117
+ - ".rubocop_todo.yml"
118
+ - ".ruby-version"
119
+ - Gemfile
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - Thorfile
124
+ - bin/dogwatch
125
+ - dogwatch.gemspec
126
+ - example/Dogfile
127
+ - example/credentials.example
128
+ - lib/dogwatch.rb
129
+ - lib/dogwatch/dogfile.rb
130
+ - lib/dogwatch/model/client.rb
131
+ - lib/dogwatch/model/config.rb
132
+ - lib/dogwatch/model/mixin/colorize.rb
133
+ - lib/dogwatch/model/monitor.rb
134
+ - lib/dogwatch/model/options.rb
135
+ - lib/dogwatch/model/response.rb
136
+ - lib/dogwatch/monitor.rb
137
+ - lib/dogwatch/version.rb
138
+ - test/data/monitors.json
139
+ - test/data/responses.rb
140
+ - test/dogwatch/test_client.rb
141
+ - test/dogwatch/test_colorize.rb
142
+ - test/dogwatch/test_config.rb
143
+ - test/dogwatch/test_dogwatch.rb
144
+ - test/dogwatch/test_monitor_model.rb
145
+ - test/dogwatch/test_options.rb
146
+ - test/dogwatch/test_response.rb
147
+ - test/test_helper.rb
148
+ homepage: https://github.com/rapid7/dogwatch
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.5
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: A DSL to create DataDog Monitors
172
+ test_files:
173
+ - test/data/monitors.json
174
+ - test/data/responses.rb
175
+ - test/dogwatch/test_client.rb
176
+ - test/dogwatch/test_colorize.rb
177
+ - test/dogwatch/test_config.rb
178
+ - test/dogwatch/test_dogwatch.rb
179
+ - test/dogwatch/test_monitor_model.rb
180
+ - test/dogwatch/test_options.rb
181
+ - test/dogwatch/test_response.rb
182
+ - test/test_helper.rb