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::Media, :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,265 +11,251 @@ describe Flapjack::Diner::Resources::Media, :pact => true do
11
11
  context 'create' do
12
12
 
13
13
  it "submits a POST request for a medium" do
14
- data = [{
15
- :type => 'sms',
16
- :address => '0123456789',
17
- :interval => 300,
18
- :rollup_threshold => 5
19
- }]
20
-
21
- flapjack.given("a contact with id 'abc' exists").
14
+ req_data = medium_json(sms_data).merge(
15
+ :relationships => {
16
+ :contact => {
17
+ :data => {
18
+ :type => 'contact',
19
+ :id => contact_data[:id]
20
+ }
21
+ }
22
+ }
23
+ )
24
+ resp_data = medium_json(sms_data).merge(:relationships => medium_rel(sms_data))
25
+
26
+ flapjack.given("a contact exists").
22
27
  upon_receiving("a POST request with one medium").
23
- with(:method => :post, :path => '/contacts/abc/media',
28
+ with(:method => :post,
29
+ :path => '/media',
24
30
  :headers => {'Content-Type' => 'application/vnd.api+json'},
25
- :body => {:media => data}).
31
+ :body => {:data => req_data}).
26
32
  will_respond_with(
27
33
  :status => 201,
28
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
29
- :body => ['abc_sms'] )
34
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
35
+ :body => { :data => resp_data }
36
+ )
30
37
 
31
- result = Flapjack::Diner.create_contact_media('abc', data)
38
+ result = Flapjack::Diner.create_media(sms_data.merge(:contact => contact_data[:id]))
32
39
  expect(result).not_to be_nil
33
- expect(result).to eq(['abc_sms'])
40
+ expect(result).to eq(resultify(resp_data))
34
41
  end
35
42
 
36
43
  it "submits a POST request for several media" do
37
- data = [{
38
- :type => 'sms',
39
- :address => '0123456789',
40
- :interval => 300,
41
- :rollup_threshold => 5
42
- }, {
43
- :type => 'email',
44
- :address => 'ablated@example.org',
45
- :interval => 180,
46
- :rollup_threshold => 3
47
- }]
48
-
49
- flapjack.given("a contact with id 'abc' exists").
44
+ req_data = [medium_json(email_data).merge(
45
+ :relationships => {
46
+ :contact => {
47
+ :data => {
48
+ :type => 'contact',
49
+ :id => contact_data[:id]
50
+ }
51
+ }
52
+ }
53
+ ), medium_json(sms_data).merge(
54
+ :relationships => {
55
+ :contact => {
56
+ :data => {
57
+ :type => 'contact',
58
+ :id => contact_data[:id]
59
+ }
60
+ }
61
+ }
62
+ )]
63
+ resp_data = [
64
+ medium_json(email_data).merge(:relationships => medium_rel(email_data)),
65
+ medium_json(sms_data).merge(:relationships => medium_rel(sms_data))
66
+ ]
67
+
68
+ flapjack.given("a contact exists").
50
69
  upon_receiving("a POST request with two media").
51
- with(:method => :post, :path => '/contacts/abc/media',
52
- :headers => {'Content-Type' => 'application/vnd.api+json'},
53
- :body => {:media => data}).
70
+ with(:method => :post,
71
+ :path => '/media',
72
+ :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'},
73
+ :body => {:data => req_data}).
54
74
  will_respond_with(
55
75
  :status => 201,
56
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
57
- :body => ['abc_sms', 'abc_email'] )
76
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
77
+ :body => {:data => resp_data})
58
78
 
59
- result = Flapjack::Diner.create_contact_media('abc', data)
79
+ result = Flapjack::Diner.create_media(email_data.merge(:contact => contact_data[:id]),
80
+ sms_data.merge(:contact => contact_data[:id]))
60
81
  expect(result).not_to be_nil
61
- expect(result).to eq(['abc_sms', 'abc_email'])
62
- end
63
-
64
- it "can't find the contact to create a medium for" do
65
- data = [{
66
- :type => 'sms',
67
- :address => '0123456789',
68
- :interval => 300,
69
- :rollup_threshold => 5
70
- }]
71
-
72
- flapjack.given("no contact exists").
73
- upon_receiving("a POST request with one medium").
74
- with(:method => :post, :path => '/contacts/abc/media',
75
- :headers => {'Content-Type' => 'application/vnd.api+json'},
76
- :body => {:media => data}).
77
- will_respond_with(
78
- :status => 422,
79
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
80
- :body => {:errors => ["Contact id: 'abc' could not be loaded"]} )
81
-
82
- result = Flapjack::Diner.create_contact_media('abc', data)
83
- expect(result).to be_nil
84
- expect(Flapjack::Diner.last_error).to eq(:status_code => 422,
85
- :errors => ["Contact id: 'abc' could not be loaded"])
82
+ expect(result).to eq(resultify(resp_data))
86
83
  end
87
84
 
88
85
  end
89
86
 
90
87
  context 'read' do
91
88
 
92
- let(:sms_data) {
93
- {
94
- :type => 'sms',
95
- :address => '0123456789',
96
- :interval => 300,
97
- :rollup_threshold => 5
98
- }
99
- }
100
-
101
- let(:email_data) {
102
- {
103
- :type => 'email',
104
- :address => 'ablated@example.org',
105
- :interval => 180,
106
- :rollup_threshold => 3
107
- }
108
- }
109
-
110
- let(:links) { {:links => {:contacts => ['abc']}} }
111
-
112
89
  it "submits a GET request for all media" do
113
- media_data = [email_data.merge(links), sms_data.merge(links)]
114
-
115
- flapjack.given("a contact with id 'abc' has email and sms media").
90
+ result_data = [
91
+ medium_json(email_data),
92
+ medium_json(sms_data)
93
+ ]
94
+ resp_data = [
95
+ result_data[0].merge(:relationships => medium_rel(email_data)),
96
+ result_data[1].merge(:relationships => medium_rel(sms_data))
97
+ ]
98
+
99
+ flapjack.given("two media exist").
116
100
  upon_receiving("a GET request for all media").
117
- with(:method => :get, :path => '/media').
101
+ with(:method => :get,
102
+ :path => '/media').
118
103
  will_respond_with(
119
104
  :status => 200,
120
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
121
- :body => {:media => media_data} )
105
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
106
+ :body => {:data => resp_data})
122
107
 
123
108
  result = Flapjack::Diner.media
124
- expect(result).to eq(media_data)
109
+ expect(result).to contain_exactly(resultify(resp_data[0]), resultify(resp_data[1]))
125
110
  end
126
111
 
127
112
  it "submits a GET request for one medium" do
128
- media_data = [sms_data.merge(links)]
113
+ resp_data = medium_json(sms_data).merge(:relationships => medium_rel(sms_data))
129
114
 
130
- flapjack.given("a contact with id 'abc' has email and sms media").
131
- upon_receiving("a GET request for sms media").
132
- with(:method => :get, :path => '/media/abc_sms').
115
+ flapjack.given("a medium exists").
116
+ upon_receiving("a GET request for one medium").
117
+ with(:method => :get, :path => "/media/#{sms_data[:id]}").
133
118
  will_respond_with(
134
119
  :status => 200,
135
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
136
- :body => {:media => media_data} )
120
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
121
+ :body => {:data => resp_data} )
137
122
 
138
- result = Flapjack::Diner.media('abc_sms')
139
- expect(result).to eq(media_data)
123
+ result = Flapjack::Diner.media(sms_data[:id])
124
+ expect(result).to eq(resultify(resp_data))
140
125
  end
141
126
 
142
127
  it "submits a GET request for several media" do
143
- media_data = [email_data.merge(links), sms_data.merge(links)]
144
-
145
- flapjack.given("a contact with id 'abc' has email and sms media").
146
- upon_receiving("a GET request for email and sms media").
147
- with(:method => :get, :path => '/media/abc_email,abc_sms').
128
+ resp_data = [
129
+ medium_json(email_data).merge(:relationships => medium_rel(email_data)),
130
+ medium_json(sms_data).merge(:relationships => medium_rel(sms_data))
131
+ ]
132
+
133
+ flapjack.given("two media exist").
134
+ upon_receiving("a GET request for two media").
135
+ with(:method => :get, :path => '/media',
136
+ :query => "filter%5B%5D=id%3A#{email_data[:id]}%7C#{sms_data[:id]}").
148
137
  will_respond_with(
149
138
  :status => 200,
150
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
151
- :body => {:media => media_data} )
152
-
153
- result = Flapjack::Diner.media('abc_email', 'abc_sms')
154
- expect(result).to eq(media_data)
155
- end
156
-
157
- it "can't find the contact with media to read" do
158
- flapjack.given("no contact exists").
159
- upon_receiving("a GET request for sms media").
160
- with(:method => :get, :path => '/media/abc_sms').
161
- will_respond_with(
162
- :status => 404,
163
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
164
- :body => {:errors => ["could not find contact 'abc'"]} )
139
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
140
+ :body => {:data => resp_data} )
165
141
 
166
- result = Flapjack::Diner.media('abc_sms')
167
- expect(result).to be_nil
168
- expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
169
- :errors => ["could not find contact 'abc'"])
142
+ result = Flapjack::Diner.media(email_data[:id], sms_data[:id])
143
+ expect(result).to eq(resultify(resp_data))
170
144
  end
171
145
 
172
146
  end
173
147
 
174
148
  context 'update' do
175
149
 
176
- it "submits a PATCH request for one medium" do
177
- flapjack.given("a contact with id 'abc' has email and sms media").
178
- upon_receiving("a PATCH request for email media").
150
+ it 'submits a PATCH request for a medium' do
151
+ flapjack.given("a medium exists").
152
+ upon_receiving("a PATCH request for a single medium").
179
153
  with(:method => :patch,
180
- :path => '/media/abc_email',
181
- :headers => {'Content-Type'=>'application/json-patch+json'},
182
- :body => [{:op => 'replace', :path => '/media/0/interval', :value => 50},
183
- {:op => 'replace', :path => '/media/0/rollup_threshold', :value => 3}]).
154
+ :path => "/media/#{sms_data[:id]}",
155
+ :body => {:data => {:id => sms_data[:id], :type => 'medium', :attributes => {:interval => 50}}},
156
+ :headers => {'Content-Type' => 'application/vnd.api+json'}).
184
157
  will_respond_with(
185
158
  :status => 204,
186
159
  :body => '' )
187
160
 
188
- result = Flapjack::Diner.update_media('abc_email', :interval => 50, :rollup_threshold => 3)
189
- expect(result).not_to be_nil
190
- expect(result).to be_truthy
161
+ result = Flapjack::Diner.update_media(:id => sms_data[:id], :interval => 50)
162
+ expect(result).to be_a(TrueClass)
191
163
  end
192
164
 
193
- it "submits a PATCH request for several media" do
194
- flapjack.given("a contact with id 'abc' has email and sms media").
195
- upon_receiving("a PATCH request for email and sms media").
165
+ it 'submits a PATCH request for several media' do
166
+ flapjack.given("two media exist").
167
+ upon_receiving("a PATCH request for two media").
196
168
  with(:method => :patch,
197
- :path => '/media/abc_email,abc_sms',
198
- :headers => {'Content-Type'=>'application/json-patch+json'},
199
- :body => [{:op => 'replace', :path => '/media/0/interval', :value => 50},
200
- {:op => 'replace', :path => '/media/0/rollup_threshold', :value => 3}]).
169
+ :path => "/media",
170
+ :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'},
171
+ :body => {:data => [{:id => email_data[:id], :type => 'medium', :attributes => {:interval => 50}},
172
+ {:id => sms_data[:id], :type => 'medium', :attributes => {:rollup_threshold => 5}}]}).
201
173
  will_respond_with(
202
174
  :status => 204,
203
175
  :body => '' )
204
176
 
205
- result = Flapjack::Diner.update_media('abc_email', 'abc_sms', :interval => 50, :rollup_threshold => 3)
206
- expect(result).not_to be_nil
207
- expect(result).to be_truthy
177
+ result = Flapjack::Diner.update_media(
178
+ {:id => email_data[:id], :interval => 50},
179
+ {:id => sms_data[:id], :rollup_threshold => 5})
180
+ expect(result).to be_a(TrueClass)
208
181
  end
209
182
 
210
- it "can't find the contact with media to update" do
211
- flapjack.given("no contact exists").
212
- upon_receiving("a PATCH request for email media").
183
+ it "can't find the medium to update" do
184
+ flapjack.given("no data exists").
185
+ upon_receiving("a PATCH request for a single medium").
213
186
  with(:method => :patch,
214
- :path => '/media/abc_email',
215
- :headers => {'Content-Type'=>'application/json-patch+json'},
216
- :body => [{:op => 'replace', :path => '/media/0/interval', :value => 50}]).
217
- will_respond_with(:status => 404,
218
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
219
- :body => {:errors => ["could not find contact 'abc'"]} )
220
-
221
- result = Flapjack::Diner.update_media('abc_email', :interval => 50)
187
+ :path => "/media/#{email_data[:id]}",
188
+ :body => {:data => {:id => email_data[:id], :type => 'medium', :attributes => {:interval => 50}}},
189
+ :headers => {'Content-Type' => 'application/vnd.api+json'}).
190
+ will_respond_with(
191
+ :status => 404,
192
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
193
+ :body => {:errors => [{
194
+ :status => '404',
195
+ :detail => "could not find Medium record, id: '#{email_data[:id]}'"
196
+ }]}
197
+ )
198
+
199
+ result = Flapjack::Diner.update_media(:id => email_data[:id], :interval => 50)
222
200
  expect(result).to be_nil
223
- expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
224
- :errors => ["could not find contact 'abc'"])
201
+ expect(Flapjack::Diner.error).to eq([{:status => '404',
202
+ :detail => "could not find Medium record, id: '#{email_data[:id]}'"}])
225
203
  end
226
204
 
227
205
  end
228
206
 
229
207
  context 'delete' do
208
+
230
209
  it "submits a DELETE request for one medium" do
231
210
 
232
- flapjack.given("a contact with id 'abc' has email and sms media").
211
+ flapjack.given("a medium exists").
233
212
  upon_receiving("a DELETE request for one medium").
234
213
  with(:method => :delete,
235
- :path => '/media/abc_email',
214
+ :path => "/media/#{sms_data[:id]}",
236
215
  :body => nil).
237
216
  will_respond_with(:status => 204,
238
217
  :body => '')
239
218
 
240
- result = Flapjack::Diner.delete_media('abc_email')
241
- expect(result).not_to be_nil
242
- expect(result).to be_truthy
219
+ result = Flapjack::Diner.delete_media(sms_data[:id])
220
+ expect(result).to be_a(TrueClass)
243
221
  end
244
222
 
245
223
  it "submits a DELETE request for several media" do
246
- flapjack.given("a contact with id 'abc' has email and sms media").
224
+ media_data = [{:type => 'medium', :id => sms_data[:id]},
225
+ {:type => 'medium', :id => email_data[:id]}]
226
+
227
+ flapjack.given("two media exist").
247
228
  upon_receiving("a DELETE request for two media").
248
229
  with(:method => :delete,
249
- :path => '/media/abc_email,abc_sms',
250
- :body => nil).
230
+ :headers => {'Content-Type' => 'application/vnd.api+json; ext=bulk'},
231
+ :path => "/media",
232
+ :body => {:data => media_data}).
251
233
  will_respond_with(:status => 204,
252
234
  :body => '')
253
235
 
254
- result = Flapjack::Diner.delete_media('abc_email', 'abc_sms')
255
- expect(result).not_to be_nil
256
- expect(result).to be_truthy
236
+ result = Flapjack::Diner.delete_media(sms_data[:id], email_data[:id])
237
+ expect(result).to be_a(TrueClass)
257
238
  end
258
239
 
259
240
  it "can't find the contact with media to delete" do
260
- flapjack.given("no contact exists").
241
+ flapjack.given("no data exists").
261
242
  upon_receiving("a DELETE request for one medium").
262
243
  with(:method => :delete,
263
- :path => '/media/abc_email',
244
+ :path => "/media/#{sms_data[:id]}",
264
245
  :body => nil).
265
- will_respond_with(:status => 404,
266
- :headers => {'Content-Type' => 'application/vnd.api+json; charset=utf-8'},
267
- :body => {:errors => ["could not find contact 'abc'"]} )
268
-
269
- result = Flapjack::Diner.delete_media('abc_email')
246
+ will_respond_with(
247
+ :status => 404,
248
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
249
+ :body => {:errors => [{
250
+ :status => '404',
251
+ :detail => "could not find Medium record, id: '#{sms_data[:id]}'"
252
+ }]}
253
+ )
254
+
255
+ result = Flapjack::Diner.delete_media(sms_data[:id])
270
256
  expect(result).to be_nil
271
- expect(Flapjack::Diner.last_error).to eq(:status_code => 404,
272
- :errors => ["could not find contact 'abc'"])
257
+ expect(Flapjack::Diner.error).to eq([{:status => '404',
258
+ :detail => "could not find Medium record, id: '#{sms_data[:id]}'"}])
273
259
  end
274
260
 
275
261
  end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ require 'flapjack-diner'
3
+
4
+ describe Flapjack::Diner::Resources, :pact => true do
5
+
6
+ before(:each) do
7
+ Flapjack::Diner.base_uri('localhost:19081')
8
+ Flapjack::Diner.logger = nil
9
+ end
10
+
11
+ context 'read' do
12
+ it 'gets all metrics' do
13
+ resp_data = metrics_json(metrics_data)
14
+
15
+ flapjack.given("no data exists").
16
+ upon_receiving("a GET request for all metrics").
17
+ with(:method => :get, :path => '/metrics').
18
+ will_respond_with(
19
+ :status => 200,
20
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
21
+ :body => {:data => resp_data} )
22
+
23
+ result = Flapjack::Diner.metrics
24
+ expect(result).to eq(resultify(resp_data))
25
+ end
26
+
27
+ it 'gets a subset of metrics' do
28
+ resp_data = metrics_json(metrics_data)
29
+ resp_data[:attributes].delete_if {|k,v| ![:processed_events, :total_keys].include?(k)}
30
+
31
+ flapjack.given("no data exists").
32
+ upon_receiving("a GET request for some metrics").
33
+ with(:method => :get, :path => '/metrics',
34
+ :query => 'fields%5B%5D=total_keys&fields%5B%5D=processed_events').
35
+ will_respond_with(
36
+ :status => 200,
37
+ :headers => {'Content-Type' => 'application/vnd.api+json; supported-ext=bulk; charset=utf-8'},
38
+ :body => {:data => resp_data} )
39
+
40
+ result = Flapjack::Diner.metrics(:fields => ['total_keys', 'processed_events'])
41
+ expect(result).to eq(resultify(resp_data))
42
+ end
43
+ end
44
+
45
+ end