flapjack-diner 1.4.0 → 2.0.0.a4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -1
  4. data/README.md +620 -413
  5. data/flapjack-diner.gemspec +1 -1
  6. data/lib/flapjack-diner/argument_validator.rb +77 -7
  7. data/lib/flapjack-diner/configuration.rb +409 -0
  8. data/lib/flapjack-diner/index_range.rb +42 -0
  9. data/lib/flapjack-diner/log_formatter.rb +22 -0
  10. data/lib/flapjack-diner/query.rb +114 -0
  11. data/lib/flapjack-diner/relationships.rb +180 -0
  12. data/lib/flapjack-diner/request.rb +280 -0
  13. data/lib/flapjack-diner/resources.rb +64 -0
  14. data/lib/flapjack-diner/response.rb +91 -0
  15. data/lib/flapjack-diner/tools.rb +47 -251
  16. data/lib/flapjack-diner/utility.rb +16 -0
  17. data/lib/flapjack-diner/version.rb +1 -1
  18. data/lib/flapjack-diner.rb +54 -20
  19. data/spec/argument_validator_spec.rb +87 -28
  20. data/spec/flapjack-diner_spec.rb +42 -64
  21. data/spec/relationships_spec.rb +211 -0
  22. data/spec/resources/checks_spec.rb +219 -79
  23. data/spec/resources/contacts_spec.rb +179 -151
  24. data/spec/resources/events_spec.rb +208 -0
  25. data/spec/resources/maintenance_periods_spec.rb +177 -565
  26. data/spec/resources/media_spec.rb +157 -171
  27. data/spec/resources/metrics_spec.rb +45 -0
  28. data/spec/resources/rules_spec.rb +278 -0
  29. data/spec/resources/states_spec.rb +93 -0
  30. data/spec/resources/statistics_spec.rb +53 -0
  31. data/spec/resources/tags_spec.rb +243 -0
  32. data/spec/spec_helper.rb +16 -0
  33. data/spec/support/fixture_data.rb +541 -0
  34. metadata +33 -31
  35. data/.rubocop.yml +0 -21
  36. data/.rubocop_todo.yml +0 -135
  37. data/lib/flapjack-diner/resources/checks.rb +0 -64
  38. data/lib/flapjack-diner/resources/contacts.rb +0 -70
  39. data/lib/flapjack-diner/resources/entities.rb +0 -68
  40. data/lib/flapjack-diner/resources/maintenance_periods.rb +0 -82
  41. data/lib/flapjack-diner/resources/media.rb +0 -61
  42. data/lib/flapjack-diner/resources/notification_rules.rb +0 -66
  43. data/lib/flapjack-diner/resources/notifications.rb +0 -28
  44. data/lib/flapjack-diner/resources/pagerduty_credentials.rb +0 -59
  45. data/lib/flapjack-diner/resources/reports.rb +0 -33
  46. data/spec/pacts/flapjack-diner-flapjack.json +0 -4515
  47. data/spec/resources/entities_spec.rb +0 -181
  48. data/spec/resources/notification_rules_spec.rb +0 -341
  49. data/spec/resources/notifications_spec.rb +0 -208
  50. data/spec/resources/pagerduty_credentials_spec.rb +0 -237
  51. data/spec/resources/reports_spec.rb +0 -255
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'flapjack-diner'
3
3
 
4
- describe Flapjack::Diner::Resources::Checks, :pact => true do
4
+ describe Flapjack::Diner::Resources, :pact => true do
5
5
 
6
6
  before(:each) do
7
7
  Flapjack::Diner.base_uri('localhost:19081')
@@ -11,87 +11,177 @@ describe Flapjack::Diner::Resources::Checks, :pact => true do
11
11
  context 'create' do
12
12
 
13
13
  it "submits a POST request for a check" do
14
- check_data = [{
15
- :name => 'SSH',
16
- :entity_id => '1234'
17
- }]
14
+ req_data = check_json(check_data)
15
+ resp_data = req_data.merge(:relationships => check_rel(check_data))
18
16
 
19
- flapjack.given("an entity 'www.example.com' with id '1234' exists").
17
+ flapjack.given("no data exists").
20
18
  upon_receiving("a POST request with one check").
21
19
  with(:method => :post, :path => '/checks',
22
20
  :headers => {'Content-Type' => 'application/vnd.api+json'},
23
- :body => {:checks => check_data}).
21
+ :body => {:data => req_data}).
24
22
  will_respond_with(
25
23
  :status => 201,
26
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
27
- :body => ['www.example.com:SSH'] )
24
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
25
+ :body => {:data => resp_data})
28
26
 
29
27
  result = Flapjack::Diner.create_checks(check_data)
30
- expect(result).to be_truthy
28
+ expect(result).to eq(resultify(resp_data))
31
29
  end
32
30
 
33
31
  it "submits a POST request for several checks" do
34
- check_data = [{
35
- :name => 'SSH',
36
- :entity_id => '1234'
37
- }, {
38
- :name => 'PING',
39
- :entity_id => '5678'
40
- }]
41
-
42
- flapjack.given("entities 'www.example.com', id '1234' and 'www2.example.com', id '5678' exist").
32
+ req_data = [check_json(check_data), check_json(check_2_data)]
33
+ resp_data = [
34
+ req_data[0].merge(:relationships => check_rel(check_data)),
35
+ req_data[1].merge(:relationships => check_rel(check_2_data))
36
+ ]
37
+
38
+ flapjack.given("no data exists").
43
39
  upon_receiving("a POST request with two checks").
40
+ with(:method => :post, :path => '/checks',
41
+ :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'},
42
+ :body => {:data => req_data}).
43
+ will_respond_with(
44
+ :status => 201,
45
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
46
+ :body => {'data' => resp_data})
47
+
48
+ result = Flapjack::Diner.create_checks(check_data, check_2_data)
49
+ expect(result).to eq(resultify(resp_data))
50
+ end
51
+
52
+ # TODO fails to create with invalid data
53
+
54
+ it "creates a check and links it to a tag" do
55
+ req_data = check_json(check_data).merge(
56
+ :relationships => {
57
+ :tags => {
58
+ :data => [{:type => 'tag', :id => tag_data[:id]}]
59
+ }
60
+ }
61
+ )
62
+ resp_data = req_data.merge(:relationships => check_rel(check_data))
63
+
64
+ flapjack.given("a tag exists").
65
+ upon_receiving("a POST request with a check linking to a tag").
44
66
  with(:method => :post, :path => '/checks',
45
67
  :headers => {'Content-Type' => 'application/vnd.api+json'},
46
- :body => {:checks => check_data}).
68
+ :body => {:data => req_data}).
47
69
  will_respond_with(
48
70
  :status => 201,
49
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
50
- :body => ['www.example.com:SSH', 'www2.example.com:PING'] )
71
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
72
+ :body => {:data => resp_data})
51
73
 
52
- result = Flapjack::Diner.create_checks(check_data)
53
- expect(result).to be_truthy
74
+ result = Flapjack::Diner.create_checks(check_data.merge(:tags => [tag_data[:id]]))
75
+ expect(result).to eq(resultify(resp_data))
54
76
  end
55
77
 
56
78
  end
57
79
 
58
80
  context 'read' do
59
81
 
60
- let(:check_data) { {:id => 'www.example.com:SSH',
61
- :name => 'SSH',
62
- :entity_name => 'www.example.com',
63
- :links => {
64
- :entities => ['1234']
65
- }
66
- }
67
- }
68
-
69
82
  context 'GET all checks' do
70
83
 
71
84
  it "has no data" do
72
- flapjack.given("no entity exists").
85
+ flapjack.given("no data exists").
73
86
  upon_receiving("a GET request for all checks").
74
87
  with(:method => :get, :path => '/checks').
75
88
  will_respond_with(
76
89
  :status => 200,
77
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
78
- :body => {:checks => []} )
90
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
91
+ :body => {:data => []} )
79
92
 
80
93
  result = Flapjack::Diner.checks
81
94
  expect(result).to eq([])
82
95
  end
83
96
 
84
97
  it "has some data" do
85
- flapjack.given("a check 'www.example.com:SSH' exists").
98
+ resp_data = [check_json(check_data).merge(:relationships => check_rel(check_data))]
99
+
100
+ flapjack.given("a check exists").
86
101
  upon_receiving("a GET request for all checks").
87
102
  with(:method => :get, :path => '/checks').
88
103
  will_respond_with(
89
104
  :status => 200,
90
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
91
- :body => {:checks => [check_data]} )
105
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
106
+ :body => {:data => resp_data} )
92
107
 
93
108
  result = Flapjack::Diner.checks
94
- expect(result).to eq([check_data])
109
+ expect(result).to eq(resultify(resp_data))
110
+ end
111
+
112
+ end
113
+
114
+ context 'GET several checks' do
115
+
116
+ it 'has some data' do
117
+ resp_data = [
118
+ check_json(check_data).merge(:relationships => check_rel(check_data)),
119
+ check_json(check_2_data).merge(:relationships => check_rel(check_2_data))
120
+ ]
121
+
122
+ flapjack.given("two checks exist").
123
+ upon_receiving("a GET request for two checks").
124
+ with(:method => :get, :path => "/checks",
125
+ :query => "filter%5B%5D=id%3A#{check_data[:id]}%7C#{check_2_data[:id]}").
126
+ will_respond_with(
127
+ :status => 200,
128
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
129
+ :body => {:data => resp_data})
130
+
131
+ result = Flapjack::Diner.checks(check_data[:id], check_2_data[:id])
132
+ expect(result).to eq(resultify(resp_data))
133
+ end
134
+
135
+ it 'has no data' do
136
+ flapjack.given("no data exists").
137
+ upon_receiving("a GET request for two checks").
138
+ with(:method => :get, :path => "/checks",
139
+ :query => "filter%5B%5D=id%3A#{check_data[:id]}%7C#{check_2_data[:id]}").
140
+ will_respond_with(
141
+ :status => 200,
142
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
143
+ :body => {:data => []}
144
+ )
145
+
146
+ result = Flapjack::Diner.checks(check_data[:id], check_2_data[:id])
147
+ expect(result).to eq([])
148
+ end
149
+
150
+ end
151
+
152
+ context 'GET checks by name' do
153
+
154
+ let(:name) { CGI.escape(check_data[:name]) }
155
+
156
+ it 'has some data' do
157
+ resp_data = [check_json(check_data).merge(:relationships => check_rel(check_data))]
158
+
159
+ flapjack.given("a check exists").
160
+ upon_receiving("a GET request for checks by name").
161
+ with(:method => :get, :path => "/checks",
162
+ :query => "filter%5B%5D=name%3A#{name}").
163
+ will_respond_with(
164
+ :status => 200,
165
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
166
+ :body => {:data => resp_data} )
167
+
168
+ result = Flapjack::Diner.checks(:filter => {:name => check_data[:name]})
169
+ expect(result).to eq(resultify(resp_data))
170
+ end
171
+
172
+ it "can't find check" do
173
+ flapjack.given("no data exists").
174
+ upon_receiving("a GET request for checks by name").
175
+ with(:method => :get, :path => "/checks",
176
+ :query => "filter%5B%5D=name%3A#{name}").
177
+ will_respond_with(
178
+ :status => 200,
179
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
180
+ :body => {:data => []}
181
+ )
182
+
183
+ result = Flapjack::Diner.checks(:filter => {:name => check_data[:name]})
184
+ expect(result).to eq([])
95
185
  end
96
186
 
97
187
  end
@@ -99,71 +189,121 @@ describe Flapjack::Diner::Resources::Checks, :pact => true do
99
189
  context 'GET a single check' do
100
190
 
101
191
  it "has some data" do
102
- flapjack.given("a check 'www.example.com:SSH' exists").
103
- upon_receiving("a GET request for check 'www.example.com:SSH'").
104
- with(:method => :get, :path => '/checks/www.example.com:SSH').
192
+ resp_data = check_json(check_data).merge(:relationships => check_rel(check_data))
193
+
194
+ flapjack.given("a check exists").
195
+ upon_receiving("a GET request for a check").
196
+ with(:method => :get, :path => "/checks/#{check_data[:id]}").
105
197
  will_respond_with(
106
198
  :status => 200,
107
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
108
- :body => {:checks => [check_data]} )
199
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
200
+ :body => {:data => resp_data} )
109
201
 
110
- result = Flapjack::Diner.checks('www.example.com:SSH')
111
- expect(result).to eq([check_data])
202
+ result = Flapjack::Diner.checks(check_data[:id])
203
+ expect(result).to eq(resultify(resp_data))
112
204
  end
113
205
 
114
- it "can't find entity for a check" do
115
- flapjack.given("no entity exists").
116
- upon_receiving("a GET request for check 'www.example.com:SSH'").
117
- with(:method => :get, :path => '/checks/www.example.com:SSH').
206
+ it "can't find check" do
207
+ flapjack.given("no data exists").
208
+ upon_receiving("a GET request for a check").
209
+ with(:method => :get, :path => "/checks/#{check_data[:id]}").
118
210
  will_respond_with(
119
211
  :status => 404,
120
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
121
- :body => {:errors => ["could not find entity checks: 'www.example.com:SSH'"]} )
122
-
123
- result = Flapjack::Diner.checks('www.example.com:SSH')
212
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
213
+ :body => {:errors => [{
214
+ :status => '404',
215
+ :detail => "could not find Check record, id: '#{check_data[:id]}'"
216
+ }]}
217
+ )
218
+
219
+ result = Flapjack::Diner.checks(check_data[:id])
124
220
  expect(result).to be_nil
125
- expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
126
- :errors => ["could not find entity checks: 'www.example.com:SSH'"])
221
+ expect(Flapjack::Diner.error).to eq([{:status => '404',
222
+ :detail => "could not find Check record, id: '#{check_data[:id]}'"}])
127
223
  end
128
-
129
224
  end
130
-
131
225
  end
132
226
 
133
227
  context 'update' do
134
228
 
135
- it "submits a PATCH request for a check" do
136
- flapjack.given("a check 'www.example.com:SSH' exists").
229
+ it 'submits a PATCH request for a check' do
230
+ flapjack.given("a check exists").
137
231
  upon_receiving("a PATCH request for a single check").
138
232
  with(:method => :patch,
139
- :path => '/checks/www.example.com:SSH',
140
- :body => [{:op => 'replace', :path => '/checks/0/enabled', :value => false}],
141
- :headers => {'Content-Type'=>'application/json-patch+json'}).
233
+ :path => "/checks/#{check_data[:id]}",
234
+ :body => {:data => {:id => check_data[:id], :type => 'check', :attributes => {:enabled => false}}},
235
+ :headers => {'Content-Type' => 'application/vnd.api+json'}).
236
+ will_respond_with(
237
+ :status => 204,
238
+ :body => '' )
239
+
240
+ result = Flapjack::Diner.update_checks(:id => check_data[:id], :enabled => false)
241
+ expect(result).to be_a(TrueClass)
242
+ end
243
+
244
+ it 'submits a PATCH request for several checks' do
245
+ flapjack.given("two checks exist").
246
+ upon_receiving("a PATCH request for two checks").
247
+ with(:method => :patch,
248
+ :path => "/checks",
249
+ :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'},
250
+ :body => {:data => [{:id => check_data[:id], :type => 'check', :attributes => {:enabled => false}},
251
+ {:id => check_2_data[:id], :type => 'check', :attributes => {:enabled => true}}]}).
142
252
  will_respond_with(
143
253
  :status => 204,
144
- :body => '')
254
+ :body => '' )
145
255
 
146
- result = Flapjack::Diner.update_checks('www.example.com:SSH', :enabled => false)
147
- expect(result).not_to be_nil
148
- expect(result).to be_truthy
256
+ result = Flapjack::Diner.update_checks(
257
+ {:id => check_data[:id], :enabled => false},
258
+ {:id => check_2_data[:id], :enabled => true})
259
+ expect(result).to be_a(TrueClass)
149
260
  end
150
261
 
151
- it "doesn't find the entity of the check to update" do
152
- flapjack.given("no entity exists").
262
+ it "can't find the check to update" do
263
+ flapjack.given("no data exists").
153
264
  upon_receiving("a PATCH request for a single check").
154
265
  with(:method => :patch,
155
- :path => '/checks/www.example.com:SSH',
156
- :body => [{:op => 'replace', :path => '/checks/0/enabled', :value => false}],
157
- :headers => {'Content-Type'=>'application/json-patch+json'}).
266
+ :path => "/checks/#{check_data[:id]}",
267
+ :body => {:data => {:id => check_data[:id], :type => 'check', :attributes => {:enabled => false}}},
268
+ :headers => {'Content-Type' => 'application/vnd.api+json'}).
158
269
  will_respond_with(
159
270
  :status => 404,
160
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
161
- :body => {:errors => ["could not find entity 'www.example.com'"]} )
162
-
163
- result = Flapjack::Diner.update_checks('www.example.com:SSH', :enabled => false)
271
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
272
+ :body => {:errors => [{
273
+ :status => '404',
274
+ :detail => "could not find Check record, id: '#{check_data[:id]}'"
275
+ }]}
276
+ )
277
+
278
+ result = Flapjack::Diner.update_checks(:id => check_data[:id], :enabled => false)
164
279
  expect(result).to be_nil
165
- expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
166
- :errors => ["could not find entity 'www.example.com'"])
280
+ expect(Flapjack::Diner.error).to eq([{:status => '404',
281
+ :detail => "could not find Check record, id: '#{check_data[:id]}'"}])
282
+ end
283
+
284
+ it "replaces the tags for a check" do
285
+ req_data = {
286
+ :id => check_data[:id],
287
+ :type => 'check',
288
+ :relationships => {
289
+ :tags => {
290
+ :data => [{:type => 'tag', :id => tag_data[:id]}]
291
+ }
292
+ }
293
+ }
294
+
295
+ flapjack.given("a check and a tag exist").
296
+ upon_receiving("a PATCH request for a single check").
297
+ with(:method => :patch,
298
+ :path => "/checks/#{check_data[:id]}",
299
+ :body => {:data => req_data},
300
+ :headers => {'Content-Type' => 'application/vnd.api+json'}).
301
+ will_respond_with(
302
+ :status => 204,
303
+ :body => '' )
304
+
305
+ result = Flapjack::Diner.update_checks(:id => check_data[:id], :tags => [tag_data[:id]])
306
+ expect(result).to be_a(TrueClass)
167
307
  end
168
308
 
169
309
  end