flapjack-diner 2.0.0.pre.alpha.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -10
  3. data/README.md +165 -272
  4. data/flapjack-diner.gemspec +1 -1
  5. data/lib/flapjack-diner.rb +54 -25
  6. data/lib/flapjack-diner/argument_validator.rb +0 -17
  7. data/lib/flapjack-diner/configuration.rb +417 -0
  8. data/lib/flapjack-diner/log_formatter.rb +22 -0
  9. data/lib/flapjack-diner/query.rb +114 -0
  10. data/lib/flapjack-diner/relationships.rb +180 -0
  11. data/lib/flapjack-diner/request.rb +280 -0
  12. data/lib/flapjack-diner/resources.rb +64 -0
  13. data/lib/flapjack-diner/response.rb +91 -0
  14. data/lib/flapjack-diner/tools.rb +46 -456
  15. data/lib/flapjack-diner/utility.rb +16 -0
  16. data/lib/flapjack-diner/version.rb +1 -1
  17. data/spec/flapjack-diner_spec.rb +9 -18
  18. data/spec/{resources/relationships_spec.rb → relationships_spec.rb} +75 -29
  19. data/spec/resources/checks_spec.rb +7 -7
  20. data/spec/resources/contacts_spec.rb +21 -19
  21. data/spec/resources/events_spec.rb +13 -13
  22. data/spec/resources/maintenance_periods_spec.rb +3 -3
  23. data/spec/resources/media_spec.rb +3 -3
  24. data/spec/resources/metrics_spec.rb +1 -1
  25. data/spec/resources/rules_spec.rb +278 -0
  26. data/spec/resources/states_spec.rb +1 -1
  27. data/spec/resources/statistics_spec.rb +1 -1
  28. data/spec/resources/tags_spec.rb +75 -19
  29. data/spec/support/fixture_data.rb +57 -98
  30. metadata +21 -29
  31. data/.rubocop.yml +0 -21
  32. data/.rubocop_todo.yml +0 -135
  33. data/lib/flapjack-diner/resources/acceptors.rb +0 -77
  34. data/lib/flapjack-diner/resources/checks.rb +0 -52
  35. data/lib/flapjack-diner/resources/contacts.rb +0 -54
  36. data/lib/flapjack-diner/resources/events.rb +0 -54
  37. data/lib/flapjack-diner/resources/maintenance_periods.rb +0 -76
  38. data/lib/flapjack-diner/resources/media.rb +0 -75
  39. data/lib/flapjack-diner/resources/metrics.rb +0 -23
  40. data/lib/flapjack-diner/resources/rejectors.rb +0 -77
  41. data/lib/flapjack-diner/resources/relationships.rb +0 -314
  42. data/lib/flapjack-diner/resources/states.rb +0 -24
  43. data/lib/flapjack-diner/resources/statistics.rb +0 -24
  44. data/lib/flapjack-diner/resources/tags.rb +0 -47
  45. data/spec/resources/acceptors_spec.rb +0 -278
  46. data/spec/resources/rejectors_spec.rb +0 -278
@@ -17,6 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.require_paths = ['lib']
18
18
  gem.version = Flapjack::Diner::VERSION
19
19
 
20
- gem.add_dependency('httparty', '>= 0.10')
20
+ gem.add_dependency('httparty', '= 0.13.5')
21
21
  gem.add_dependency('json', '>= 1.7.7')
22
22
  end
@@ -1,46 +1,75 @@
1
1
  require 'httparty'
2
2
  require 'json'
3
3
 
4
+ require 'flapjack-diner/log_formatter'
4
5
  require 'flapjack-diner/version'
5
6
  require 'flapjack-diner/argument_validator'
7
+ require 'flapjack-diner/index_range'
6
8
 
7
- %w(acceptors checks contacts events maintenance_periods media metrics rejectors
8
- states statistics tags relationships).each do |resource|
9
- require "flapjack-diner/resources/#{resource}"
10
- end
11
-
9
+ require 'flapjack-diner/resources'
10
+ require 'flapjack-diner/relationships'
12
11
  require 'flapjack-diner/tools'
13
12
 
13
+ # HTTParty master contains a non-hacky way of doing this, but 0.13.5 doesn't
14
+ module HTTParty
15
+ module Logger
16
+ def self.build(logger, level, formatter)
17
+ level ||= :info
18
+ formatter ||= :apache
19
+
20
+ case formatter
21
+ when :'flapjack-diner'
22
+ Flapjack::Diner::LogFormatter.new(logger, level)
23
+ when :curl
24
+ Logger::CurlLogger.new(logger, level)
25
+ else
26
+ Logger::ApacheLogger.new(logger, level)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
14
32
  # NB: clients will need to handle any exceptions caused by,
15
33
  # e.g., network failures or non-parseable JSON data.
16
-
17
34
  module Flapjack
18
35
 
19
36
  # Top level module for Flapjack::Diner API consumer.
20
37
  module Diner
38
+ UUID_RE = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
39
+
21
40
  include HTTParty
22
41
  format :json
23
42
 
24
- UUID_RE = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
25
-
26
43
  class << self
27
- attr_accessor :logger, :return_keys_as_strings
28
-
29
- include Flapjack::Diner::Resources::Acceptors
30
- include Flapjack::Diner::Resources::Checks
31
- include Flapjack::Diner::Resources::Contacts
32
- include Flapjack::Diner::Resources::Events
33
- include Flapjack::Diner::Resources::MaintenancePeriods
34
- include Flapjack::Diner::Resources::Media
35
- include Flapjack::Diner::Resources::Metrics
36
- include Flapjack::Diner::Resources::Rejectors
37
- include Flapjack::Diner::Resources::States
38
- include Flapjack::Diner::Resources::Statistics
39
- include Flapjack::Diner::Resources::Tags
40
-
41
- include Flapjack::Diner::Resources::Relationships
42
-
43
- include Flapjack::Diner::Tools
44
+ attr_accessor :return_keys_as_strings
45
+
46
+ # redefine HTTParty logger methods for getter/setter style
47
+ alias :original_logger :logger
48
+ def logger=(lgr)
49
+ original_logger(lgr, :info, :'flapjack-diner')
50
+ end
51
+ def logger
52
+ default_options[:logger]
53
+ end
54
+
55
+ def output
56
+ return if !instance_variable_defined?('@response') || @response.nil?
57
+ @response.output
58
+ end
59
+
60
+ def context
61
+ return if !instance_variable_defined?('@response') || @response.nil?
62
+ @response.context
63
+ end
64
+
65
+ def error
66
+ return if !instance_variable_defined?('@response') || @response.nil?
67
+ @response.error
68
+ end
44
69
  end
70
+
71
+ include Flapjack::Diner::Resources
72
+ include Flapjack::Diner::Relationships
73
+ include Flapjack::Diner::Tools
45
74
  end
46
75
  end
@@ -111,14 +111,6 @@ module Flapjack
111
111
  end
112
112
  end
113
113
 
114
- def singular_link(*elements)
115
- elements.each do |element|
116
- target = @query[element]
117
- next if target.nil? || (target.is_a?(String) && !target.empty?)
118
- @errors << "'#{target}' association must be a String ID."
119
- end
120
- end
121
-
122
114
  def singular_link_uuid(*elements)
123
115
  elements.each do |element|
124
116
  target = @query[element]
@@ -128,15 +120,6 @@ module Flapjack
128
120
  end
129
121
  end
130
122
 
131
- def multiple_link(*elements)
132
- elements.each do |element|
133
- target = @query[element]
134
- next if target.nil? || (target.is_a?(Array) &&
135
- target.all? {|t| t.is_a?(String) && !t.empty? })
136
- @errors << "'#{target}' association must be an Array of String IDs."
137
- end
138
- end
139
-
140
123
  def multiple_link_uuid(*elements)
141
124
  elements.each do |element|
142
125
  target = @query[element]
@@ -0,0 +1,417 @@
1
+ require 'flapjack-diner/request'
2
+ require 'flapjack-diner/response'
3
+
4
+ require 'flapjack-diner/argument_validator'
5
+
6
+ module Flapjack
7
+ module Diner
8
+ module Configuration
9
+
10
+ RESOURCES_GET = {
11
+ [:fields, :sort, :include] => :string_or_array_of_strings,
12
+ :filter => :hash,
13
+ [:page, :per_page] => :positive_integer
14
+ }
15
+
16
+ # relationships are extracted from flapjack data models'
17
+ # "jsonapi_associations" class methods
18
+
19
+ RESOURCES = {
20
+ :acknowledgements => {
21
+ :resource => 'acknowledgement',
22
+ :requests => {
23
+ :post => {
24
+ :duration => :positive_integer,
25
+ :summary => :non_empty_string,
26
+ [:check, :tag] => :singular_link_uuid
27
+ },
28
+ },
29
+ :request_validations => {
30
+ :post => proc {
31
+ # _events_validate_association(data, 'acknowledgement')
32
+ }
33
+ },
34
+ :relationships => {
35
+ :check => {
36
+ :post => true,
37
+ :number => :singular, :link => false, :includable => false,
38
+ :resource => 'check'
39
+ },
40
+ :tag => {
41
+ :post => true,
42
+ :number => :singular, :link => false, :includable => false,
43
+ :resource => 'tag'
44
+ }
45
+ }
46
+ },
47
+
48
+ :checks => {
49
+ :resource => 'check',
50
+ :requests => {
51
+ :post => {
52
+ :id => :uuid,
53
+ :name => [:required, :non_empty_string],
54
+ :enabled => :boolean,
55
+ :initial_failure_delay => :positive_integer,
56
+ :repeat_failure_delay => :positive_integer,
57
+ :initial_recovery_delay => :positive_integer,
58
+ :tags => :multiple_link_uuid
59
+ },
60
+ :get => RESOURCES_GET,
61
+ :patch => {
62
+ :id => [:required, :uuid],
63
+ :name => :non_empty_string,
64
+ :enabled => :boolean,
65
+ :initial_failure_delay => :positive_integer,
66
+ :repeat_failure_delay => :positive_integer,
67
+ :initial_recovery_delay => :positive_integer,
68
+ :tags => :multiple_link_uuid
69
+ },
70
+ :delete => {}
71
+ },
72
+ :relationships => {
73
+ :alerting_media => {
74
+ :get => true,
75
+ :number => :multiple, :link => true, :includable => true,
76
+ :resource => 'medium'
77
+ },
78
+ :contacts => {
79
+ :get => true,
80
+ :number => :multiple, :link => true, :includable => true,
81
+ :resource => 'contact'
82
+ },
83
+ :current_scheduled_maintenances => {
84
+ :get => true,
85
+ :number => :multiple, :link => true, :includable => true,
86
+ :resource => 'scheduled_maintenance'
87
+ },
88
+ :current_state => {
89
+ :get => true,
90
+ :number => :singular, :link => true, :includable => true,
91
+ :resource => 'state'
92
+ },
93
+ :current_unscheduled_maintenance => {
94
+ :get => true,
95
+ :number => :singular, :link => true, :includable => true,
96
+ :resource => 'unscheduled_maintenance'
97
+ },
98
+ :latest_notifications => {
99
+ :get => true,
100
+ :number => :multiple, :link => true, :includable => true,
101
+ :resource => 'state'
102
+ },
103
+ :scheduled_maintenances => {
104
+ :get => true,
105
+ :number => :multiple, :link => true, :includable => false,
106
+ :resource => 'scheduled_maintenance'
107
+ },
108
+ :states => {
109
+ :get => true,
110
+ :number => :multiple, :link => true, :includable => false,
111
+ :resource => 'state'
112
+ },
113
+ :tags => {
114
+ :post => true, :get => true, :patch => true, :delete => true,
115
+ :number => :multiple, :link => true, :includable => true,
116
+ :resource => 'tag'
117
+ },
118
+ :unscheduled_maintenances => {
119
+ :get => true,
120
+ :number => :multiple, :link => true, :includable => false,
121
+ :resource => 'unscheduled_maintenance'
122
+ }
123
+ }
124
+ },
125
+
126
+ :contacts => {
127
+ :resource => 'contact',
128
+ :requests => {
129
+ :post => {
130
+ :id => :uuid,
131
+ :name => [:required, :non_empty_string],
132
+ :timezone => :non_empty_string,
133
+ :tags => :multiple_link_uuid
134
+ },
135
+ :get => RESOURCES_GET,
136
+ :patch => {
137
+ :id => [:required, :uuid],
138
+ [:name, :timezone] => :non_empty_string,
139
+ :tags => :multiple_link_uuid
140
+ },
141
+ :delete => {}
142
+ },
143
+ :relationships => {
144
+ :checks => {
145
+ :get => true,
146
+ :number => :multiple, :link => true, :includable => true,
147
+ :resource => 'check'
148
+ },
149
+ :media => {
150
+ :get => true,
151
+ :number => :multiple, :link => true, :includable => true,
152
+ :resource => 'medium'
153
+ },
154
+ :rules => {
155
+ :get => :true,
156
+ :number => :multiple, :link => true, :includable => true,
157
+ :resource => 'rule'
158
+ },
159
+ :tags => {
160
+ :post => true, :get => true, :patch => true, :delete => true,
161
+ :number => :multiple, :link => true, :includable => true,
162
+ :resource => 'tag'
163
+ }
164
+ }
165
+ },
166
+
167
+ :media => {
168
+ :resource => 'medium',
169
+ :requests => {
170
+ :post => {
171
+ :id => :uuid,
172
+ :transport => [:non_empty_string, :required],
173
+ [:address, :pagerduty_subdomain,
174
+ :pagerduty_token] => :non_empty_string,
175
+ [:interval, :rollup_threshold,
176
+ :pagerduty_ack_duration] => :positive_integer,
177
+ :contact => [:singular_link_uuid, :required],
178
+ :rules => :multiple_link_uuid
179
+ },
180
+ :get => RESOURCES_GET,
181
+ :patch => {
182
+ :id => [:uuid, :required],
183
+ [:address, :pagerduty_subdomain,
184
+ :pagerduty_token] => :non_empty_string,
185
+ [:interval, :rollup_threshold,
186
+ :pagerduty_ack_duration] => :positive_integer,
187
+ :rules => :multiple_link_uuid
188
+ },
189
+ :delete => {}
190
+ },
191
+ :relationships => {
192
+ :alerting_checks => {
193
+ :get => true,
194
+ :number => :multiple, :link => true, :includable => true,
195
+ :resource => 'check'
196
+ },
197
+ :contact => {
198
+ :post => true, :get => true,
199
+ :number => :singular, :link => true, :includable => true,
200
+ :resource => 'contact'
201
+ },
202
+ :rules => {
203
+ :post => true, :get => true, :patch => true, :delete => true,
204
+ :number => :multiple, :link => true, :includable => true,
205
+ :resource => 'rule'
206
+ }
207
+ }
208
+ },
209
+
210
+ :metrics => {
211
+ :resource => 'metrics',
212
+ :requests => {
213
+ :get => {
214
+ :fields => :string_or_array_of_strings
215
+ }
216
+ }
217
+ },
218
+
219
+ :rules => {
220
+ :resource => 'rule',
221
+ :requests => {
222
+ :post => {
223
+ :id => :uuid,
224
+ :name => :string,
225
+ :enabled => :boolean,
226
+ :blackhole => :boolean,
227
+ :strategy => :string,
228
+ :conditions_list => :string,
229
+ :time_restriction_ical => :string,
230
+ :contact => [:singular_link_uuid, :required],
231
+ [:media, :tags] => :multiple_link_uuid
232
+ },
233
+ :get => RESOURCES_GET,
234
+ :patch => {
235
+ :id => [:uuid, :required],
236
+ :name => :string,
237
+ :enabled => :boolean,
238
+ :blackhole => :boolean,
239
+ :strategy => :string,
240
+ :conditions_list => :string,
241
+ :time_restriction_ical => :string,
242
+ [:media, :tags] => :multiple_link_uuid
243
+ },
244
+ :delete => {}
245
+ },
246
+ :relationships => {
247
+ :contact => {
248
+ :post => true, :get => true,
249
+ :number => :singular, :link => true, :includable => true,
250
+ :resource => 'contact'
251
+ },
252
+ :media => {
253
+ :post => true, :get => true, :patch => true, :delete => true,
254
+ :number => :multiple, :link => true, :includable => true,
255
+ :resource => 'medium'
256
+ },
257
+ :tags => {
258
+ :post => true, :get => true, :patch => true, :delete => true,
259
+ :number => :multiple, :link => true, :includable => true,
260
+ :resource => 'tag'
261
+ }
262
+ }
263
+ },
264
+
265
+ :scheduled_maintenances => {
266
+ :resource => 'scheduled_maintenance',
267
+ :requests => {
268
+ :post => {
269
+ :id => :uuid,
270
+ [:start_time, :end_time] => [:required, :time],
271
+ :summary => :non_empty_string,
272
+ [:check, :tag] => :singular_link_uuid
273
+ },
274
+ :request_validations => {
275
+ :post => proc {
276
+ # _maintenance_periods_validate_association(data, 'scheduled maintenance period')
277
+ }
278
+ },
279
+ :patch => {
280
+ :id => [:required, :uuid],
281
+ [:start_time, :end_time] => :time
282
+ },
283
+ :delete => {}
284
+ },
285
+ :relationships => {
286
+ :check => {
287
+ :post => true, :get => true,
288
+ :number => :singular, :link => true, :includable => true,
289
+ :resource => 'check'
290
+ },
291
+ :tag => {
292
+ :post => true,
293
+ :number => :singular, :link => false, :includable => false,
294
+ :resource => 'tag'
295
+ }
296
+ }
297
+ },
298
+
299
+ :states => {
300
+ :resource => 'state',
301
+ :requests => {
302
+ :get => RESOURCES_GET
303
+ },
304
+ :relationships => {
305
+ :check => {
306
+ :get => true,
307
+ :number => :singular, :link => true, :includable => true,
308
+ :resource => 'check'
309
+ }
310
+ }
311
+ },
312
+
313
+ :statistics => {
314
+ :resource => 'statistic',
315
+ :requests => {
316
+ :get => RESOURCES_GET
317
+ }
318
+ },
319
+
320
+ :tags => {
321
+ :resource => 'tag',
322
+ :requests => {
323
+ :post => {
324
+ :id => :uuid,
325
+ :name => [:required, :non_empty_string],
326
+ [:checks, :contacts, :rules] => :multiple_link_uuid
327
+ },
328
+ :get => RESOURCES_GET,
329
+ :patch => {
330
+ :id => [:required, :uuid],
331
+ :name => :non_empty_string,
332
+ [:checks, :contacts, :rules] => :multiple_link_uuid
333
+ },
334
+ :delete => {}
335
+ },
336
+ :relationships => {
337
+ :checks => {
338
+ :post => true, :get => true, :patch => true, :delete => true,
339
+ :number => :multiple, :link => true, :includable => true,
340
+ :resource => 'check'
341
+ },
342
+ :contacts => {
343
+ :post => true, :get => true, :patch => true, :delete => true,
344
+ :number => :multiple, :link => true, :includable => true,
345
+ :resource => 'contact'
346
+ },
347
+ :rules => {
348
+ :post => true, :get => true, :patch => true, :delete => true,
349
+ :number => :multiple, :link => true, :includable => true,
350
+ :resource => 'rule'
351
+ },
352
+ :scheduled_maintenances => {
353
+ :get => true,
354
+ :number => :multiple, :link => true, :includable => false,
355
+ :resource => 'scheduled_maintenance'
356
+ },
357
+ :states => {
358
+ :get => true,
359
+ :number => :multiple, :link => true, :includable => false,
360
+ :resource => 'state'
361
+ },
362
+ :unscheduled_maintenances => {
363
+ :get => true,
364
+ :number => :multiple, :link => true, :includable => false,
365
+ :resource => 'unscheduled_maintenance'
366
+ }
367
+ }
368
+ },
369
+
370
+ :test_notifications => {
371
+ :resource => 'test_notification',
372
+ :requests => {
373
+ :post => {
374
+ :summary => :non_empty_string,
375
+ :condition => :non_empty_string,
376
+ [:check, :tag] => :singular_link_uuid
377
+ }
378
+ },
379
+ :request_validations => {
380
+ :post => proc {
381
+ # _events_validate_association(data, 'test notification')
382
+ }
383
+ },
384
+ :relationships => {
385
+ :check => {
386
+ :post => true,
387
+ :number => :singular, :link => false, :includable => false,
388
+ :resource => 'check'
389
+ },
390
+ :tag => {
391
+ :post => true,
392
+ :number => :singular, :link => false, :includable => false,
393
+ :resource => 'tag'
394
+ }
395
+ }
396
+ },
397
+
398
+ :unscheduled_maintenances => {
399
+ :resource => 'unscheduled_maintenance',
400
+ :requests => {
401
+ :patch => {
402
+ :id => [:required, :uuid],
403
+ :end_time => [:required, :time]
404
+ }
405
+ },
406
+ :relationships => {
407
+ :check => {
408
+ :get => true,
409
+ :number => :singular, :link => false, :includable => true,
410
+ :resource => 'check'
411
+ }
412
+ }
413
+ }
414
+ }
415
+ end
416
+ end
417
+ end