flapjack-diner 2.0.0b1 → 2.0.0.pre.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +21 -0
  3. data/.rubocop_todo.yml +135 -0
  4. data/.travis.yml +10 -5
  5. data/README.md +125 -143
  6. data/flapjack-diner.gemspec +1 -1
  7. data/lib/flapjack-diner.rb +24 -54
  8. data/lib/flapjack-diner/argument_validator.rb +17 -0
  9. data/lib/flapjack-diner/resources/checks.rb +52 -0
  10. data/lib/flapjack-diner/resources/contacts.rb +54 -0
  11. data/lib/flapjack-diner/resources/events.rb +54 -0
  12. data/lib/flapjack-diner/resources/maintenance_periods.rb +76 -0
  13. data/lib/flapjack-diner/resources/media.rb +75 -0
  14. data/lib/flapjack-diner/resources/metrics.rb +23 -0
  15. data/lib/flapjack-diner/resources/relationships.rb +275 -0
  16. data/lib/flapjack-diner/resources/rules.rb +75 -0
  17. data/lib/flapjack-diner/resources/states.rb +24 -0
  18. data/lib/flapjack-diner/resources/statistics.rb +24 -0
  19. data/lib/flapjack-diner/resources/tags.rb +47 -0
  20. data/lib/flapjack-diner/tools.rb +456 -46
  21. data/lib/flapjack-diner/version.rb +1 -1
  22. data/spec/flapjack-diner_spec.rb +18 -9
  23. data/spec/resources/checks_spec.rb +7 -7
  24. data/spec/resources/contacts_spec.rb +12 -14
  25. data/spec/resources/events_spec.rb +13 -13
  26. data/spec/resources/maintenance_periods_spec.rb +3 -3
  27. data/spec/resources/media_spec.rb +3 -3
  28. data/spec/resources/metrics_spec.rb +1 -1
  29. data/spec/{relationships_spec.rb → resources/relationships_spec.rb} +25 -71
  30. data/spec/resources/rules_spec.rb +62 -62
  31. data/spec/resources/states_spec.rb +1 -1
  32. data/spec/resources/statistics_spec.rb +1 -1
  33. data/spec/resources/tags_spec.rb +19 -75
  34. data/spec/support/fixture_data.rb +43 -80
  35. metadata +22 -17
  36. data/lib/flapjack-diner/configuration.rb +0 -417
  37. data/lib/flapjack-diner/log_formatter.rb +0 -22
  38. data/lib/flapjack-diner/query.rb +0 -114
  39. data/lib/flapjack-diner/relationships.rb +0 -180
  40. data/lib/flapjack-diner/request.rb +0 -280
  41. data/lib/flapjack-diner/resources.rb +0 -64
  42. data/lib/flapjack-diner/response.rb +0 -91
  43. data/lib/flapjack-diner/utility.rb +0 -16
@@ -0,0 +1,23 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ require 'flapjack-diner/version'
6
+ require 'flapjack-diner/argument_validator'
7
+
8
+ module Flapjack
9
+ module Diner
10
+ module Resources
11
+ module Metrics
12
+ # singular resource, no id
13
+ def metrics(*args)
14
+ data = unwrap_data(*args)
15
+ validate_params(data) do
16
+ validate :query => :fields, :as => :string_or_array_of_strings
17
+ end
18
+ perform_get('/metrics', [], data)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,275 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ require 'flapjack-diner/version'
6
+ require 'flapjack-diner/argument_validator'
7
+
8
+ module Flapjack
9
+ module Diner
10
+ module Resources
11
+ module Relationships
12
+
13
+ TYPES = {
14
+ :acknowledgements => 'acknowledgement',
15
+ :checks => 'check',
16
+ :contacts => 'contact',
17
+ :media => 'medium',
18
+ :rules => 'rule',
19
+ :scheduled_maintenances => 'scheduled_maintenance',
20
+ :states => 'state',
21
+ :tags => 'tag',
22
+ :test_notifications => 'test_notification',
23
+ :unscheduled_maintenances => 'unscheduled_maintenance'
24
+ }
25
+
26
+ # FIXME include option for tools.rb/GET should check these config settings for includable
27
+
28
+ # extracted from flapjack data models' "jsonapi_associations" class method
29
+ ASSOCIATIONS = {
30
+ :acknowledgements => {
31
+ :check => {
32
+ :post => true,
33
+ :number => :singular, :link => false, :includable => false
34
+ },
35
+ :tag => {
36
+ :post => true,
37
+ :number => :singular, :link => false, :includable => false
38
+ }
39
+ },
40
+ :checks => {
41
+ :alerting_media => {
42
+ :get => true,
43
+ :number => :multiple, :link => true, :includable => true
44
+ },
45
+ :contacts => {
46
+ :get => true,
47
+ :number => :multiple, :link => true, :includable => true
48
+ },
49
+ :current_scheduled_maintenances => {
50
+ :get => true,
51
+ :number => :multiple, :link => true, :includable => true
52
+ },
53
+ :current_state => {
54
+ :get => true,
55
+ :number => :singular, :link => true, :includable => true
56
+ },
57
+ :current_unscheduled_maintenance => {
58
+ :get => true,
59
+ :number => :singular, :link => true, :includable => true
60
+ },
61
+ :latest_notifications => {
62
+ :get => true,
63
+ :number => :multiple, :link => true, :includable => true
64
+ },
65
+ :scheduled_maintenances => {
66
+ :get => true,
67
+ :number => :multiple, :link => true, :includable => false
68
+ },
69
+ :states => {
70
+ :get => true,
71
+ :number => :multiple, :link => true, :includable => false
72
+ },
73
+ :tags => {
74
+ :post => true, :get => true, :patch => true, :delete => true,
75
+ :number => :multiple, :link => true, :includable => true
76
+ },
77
+ :unscheduled_maintenances => {
78
+ :get => true,
79
+ :number => :multiple, :link => true, :includable => false
80
+ }
81
+ },
82
+ :contacts => {
83
+ :checks => {
84
+ :get => true,
85
+ :number => :multiple, :link => true, :includable => true
86
+ },
87
+ :media => {
88
+ :get => true,
89
+ :number => :multiple, :link => true, :includable => true
90
+ },
91
+ :rules => {
92
+ :get => :true,
93
+ :number => :multiple, :link => true, :includable => true
94
+ }
95
+ },
96
+ :media => {
97
+ :alerting_checks => {
98
+ :get => true,
99
+ :number => :multiple, :link => true, :includable => true
100
+ },
101
+ :contact => {
102
+ :post => true, :get => true,
103
+ :number => :singular, :link => true, :includable => true
104
+ },
105
+ :rules => {
106
+ :post => true, :get => true, :patch => true, :delete => true,
107
+ :number => :multiple, :link => true, :includable => true
108
+ }
109
+ },
110
+ :rules => {
111
+ :contact => {
112
+ :post => true, :get => true,
113
+ :number => :singular, :link => true, :includable => true
114
+ },
115
+ :media => {
116
+ :post => true, :get => true, :patch => true, :delete => true,
117
+ :number => :multiple, :link => true, :includable => true
118
+ },
119
+ :tags => {
120
+ :post => true, :get => true, :patch => true, :delete => true,
121
+ :number => :multiple, :link => true, :includable => true
122
+ }
123
+ },
124
+ :scheduled_maintenances => {
125
+ :check => {
126
+ :post => true, :get => true,
127
+ :number => :singular, :link => true, :includable => true
128
+ },
129
+ :tag => {
130
+ :post => true,
131
+ :number => :singular, :link => false, :includable => false
132
+ }
133
+ },
134
+ :states => {
135
+ :check => {
136
+ :get => true,
137
+ :number => :singular, :link => true, :includable => true
138
+ }
139
+ },
140
+ :tags => {
141
+ :checks => {
142
+ :post => true, :get => true, :patch => true, :delete => true,
143
+ :number => :multiple, :link => true, :includable => true
144
+ },
145
+ :rules => {
146
+ :post => true, :get => true, :patch => true, :delete => true,
147
+ :number => :multiple, :link => true, :includable => true
148
+ }
149
+ },
150
+ :test_notifications => {
151
+ :check => {
152
+ :post => true,
153
+ :number => :singular, :link => false, :includable => false
154
+ },
155
+ :tag => {
156
+ :post => true,
157
+ :number => :singular, :link => false, :includable => false
158
+ }
159
+ },
160
+ :unscheduled_maintenances => {
161
+ :check => {
162
+ :get => true,
163
+ :number => :singular, :link => false, :includable => true
164
+ }
165
+ }
166
+ }
167
+
168
+ ASSOCIATIONS.each_pair do |resource, mappings|
169
+ res = TYPES[resource]
170
+ resource_id_validator = :tags.eql?(resource) ? :string : :uuid
171
+
172
+ # NB: no remaining non-GET singular link methods, so these are commented out for now
173
+
174
+ # mappings.select {|n, a| a[:post] && a[:link] && :singular.eql?(a[:number]) }.each do |linked, assoc|
175
+
176
+ # linked_id_validator = :tag.eql?(linked) ? :singular_link : :singular_link_uuid
177
+
178
+ # define_method("create_#{res}_link_#{linked}") do |resource_id, linked_id|
179
+ # validate_params(:resource_id => resource_id, :linked_id => linked_id) do
180
+ # validate :query => :resource_id, :as => resource_id_validator
181
+ # validate :query => :linked_id, :as => linked_id_validator
182
+ # end
183
+ # perform_post_links(linked,
184
+ # "/#{resource}/#{resource_id}/relationships/#{linked}", linked_id)
185
+ # end
186
+ # end
187
+
188
+ mappings.select {|n, a| a[:post] && a[:link] && :multiple.eql?(a[:number]) }.each do |linked, assoc|
189
+ type = TYPES[linked] || linked
190
+ linked_ids_validator = :tags.eql?(linked) ? :multiple_link : :multiple_link_uuid
191
+
192
+ define_method("create_#{res}_link_#{linked}") do |resource_id, *linked_ids|
193
+ validate_params(:resource_id => resource_id, :linked_ids => linked_ids) do
194
+ validate :query => :resource_id, :as => resource_id_validator
195
+ validate :query => :linked_ids, :as => linked_ids_validator
196
+ end
197
+ perform_post_links(type,
198
+ "/#{resource}/#{resource_id}/relationships/#{linked}", *linked_ids)
199
+ end
200
+ end
201
+
202
+ mappings.select {|n, a| a[:get] && a[:link] }.each do |linked, assoc|
203
+ define_method("#{res}_link_#{linked}") do |resource_id, opts = {}|
204
+ validate_params(:resource_id => resource_id) do
205
+ validate :query => :resource_id, :as => resource_id_validator
206
+ end
207
+ validate_params(opts) do
208
+ validate :query => [:fields, :sort, :include], :as => :string_or_array_of_strings
209
+ validate :query => :filter, :as => :hash
210
+ validate :query => [:page, :per_page], :as => :positive_integer
211
+ end
212
+ perform_get("/#{resource}/#{resource_id}/#{linked}", [], opts, {:assoc => linked})
213
+ end
214
+ end
215
+
216
+ # mappings.select {|n, a| a[:patch] && a[:link] && :singular.eql?(a[:number]) }.each do |linked, assoc|
217
+ # linked_id_validator = :tag.eql?(linked) ? :singular_link : :singular_link_uuid
218
+
219
+ # define_method("update_#{res}_link_#{linked}") do |resource_id, linked_id|
220
+ # validate_params(:resource_id => resource_id, :linked_id => linked_id) do
221
+ # validate :query => :resource_id, :as => resource_id_validator
222
+ # validate :query => :linked_id, :as => linked_id_validator
223
+ # end
224
+ # perform_patch_links(linked,
225
+ # "/#{resource}/#{resource_id}/relationships/#{linked}", true, linked_id)
226
+ # end
227
+ # end
228
+
229
+ mappings.select {|n, a| a[:patch] && a[:link] && :multiple.eql?(a[:number]) }.each do |linked, assoc|
230
+ type = TYPES[linked] || linked
231
+ linked_ids_validator = :tags.eql?(linked) ? :multiple_link : :multiple_link_uuid
232
+
233
+ define_method("update_#{res}_link_#{linked}") do |resource_id, *linked_ids|
234
+ validate_params(:resource_id => resource_id, :linked_ids => linked_ids) do
235
+ validate :query => :resource_id, :as => resource_id_validator
236
+ validate :query => :linked_id, :as => linked_ids_validator
237
+ end
238
+ perform_patch_links(type,
239
+ "/#{resource}/#{resource_id}/relationships/#{linked}", false, *linked_ids)
240
+ end
241
+ end
242
+
243
+ # mappings.select {|n, a| a[:delete] && a[:link] && :singular.eql?(a[:number]) }.each do |linked, assoc|
244
+ # linked_id_validator = :tag.eql?(linked) ? :singular_link : :singular_link_uuid
245
+
246
+ # define_method("delete_#{res}_link_#{linked}") do |resource_id, linked_id|
247
+ # validate_params(:resource_id => resource_id, :linked_id => linked_id) do
248
+ # validate :query => :resource_id, :as => resource_id_validator
249
+ # validate :query => :linked_id, :as => linked_id_validator
250
+ # end
251
+ # perform_delete_links(linked,
252
+ # "/#{resource}/#{resource_id}/relationships/#{linked}", linked_id)
253
+ # end
254
+ # end
255
+
256
+ mappings.select {|n, a| a[:delete] && a[:link] && :multiple.eql?(a[:number]) }.each do |linked, assoc|
257
+ type = TYPES[linked] || linked
258
+ linked_ids_validator = :tags.eql?(linked) ? :string_or_array_of_strings : :uuid_or_array_of_uuids
259
+
260
+ define_method("delete_#{res}_link_#{linked}") do |resource_id, *linked_ids|
261
+ validate_params(:resource_id => resource_id, :linked_ids => linked_ids) do
262
+ validate :query => :resource_id, :as => resource_id_validator
263
+ validate :query => :linked_ids, :as => linked_ids_validator
264
+ end
265
+ perform_delete_links(type,
266
+ "/#{resource}/#{resource_id}/relationships/#{linked}", *linked_ids)
267
+ end
268
+ end
269
+ end
270
+
271
+ end
272
+
273
+ end
274
+ end
275
+ end
@@ -0,0 +1,75 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ require 'flapjack-diner/version'
6
+ require 'flapjack-diner/argument_validator'
7
+
8
+ module Flapjack
9
+ module Diner
10
+ module Resources
11
+ module Rules
12
+
13
+ def create_rules(*args)
14
+ data = unwrap_data(*args)
15
+ validate_params(data) do
16
+ validate :query => :id, :as => :uuid
17
+ # TODO proper validation of time_restrictions field
18
+ validate :query => :conditions_list, :as => :string
19
+ validate :query => :is_blackhole, :as => :boolean
20
+ validate :query => :contact, :as => [:singular_link_uuid, :required]
21
+ validate :query => :media, :as => :multiple_link_uuid
22
+ validate :query => :tags, :as => :multiple_link
23
+ end
24
+ _rules_validate_association(data)
25
+ perform_post(:rules, '/rules', data)
26
+ end
27
+
28
+ def rules(*args)
29
+ ids, data = unwrap_uuids(*args), unwrap_data(*args)
30
+ validate_params(data) do
31
+ validate :query => [:fields, :sort, :include], :as => :string_or_array_of_strings
32
+ validate :query => :filter, :as => :hash
33
+ validate :query => [:page, :per_page], :as => :positive_integer
34
+ end
35
+ perform_get('/rules', ids, data)
36
+ end
37
+
38
+ def update_rules(*args)
39
+ data = unwrap_data(*args)
40
+ validate_params(data) do
41
+ validate :query => :id, :as => [:uuid, :required]
42
+ # TODO proper validation of time_restrictions field
43
+ validate :query => :conditions_list, :as => :string
44
+ validate :query => :is_blackhole, :as => :boolean
45
+ validate :query => :media, :as => :multiple_link_uuid
46
+ validate :query => :tags, :as => :multiple_link
47
+ end
48
+ perform_patch(:rules, "/rules", data)
49
+ end
50
+
51
+ def delete_rules(*ids)
52
+ raise "'delete_rules' requires at least one rule id parameter" if ids.nil? || ids.empty?
53
+ perform_delete(:rules, '/rules', *ids)
54
+ end
55
+
56
+ private
57
+
58
+ def _rules_validate_association(data)
59
+ case data
60
+ when Array
61
+ data.each do |d|
62
+ unless d.has_key?(:contact)
63
+ raise ArgumentError.new("Contact association must be provided for all rules")
64
+ end
65
+ end
66
+ when Hash
67
+ unless data.has_key?(:contact)
68
+ raise ArgumentError.new("Contact association must be provided for rule")
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,24 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ require 'flapjack-diner/version'
6
+ require 'flapjack-diner/argument_validator'
7
+
8
+ module Flapjack
9
+ module Diner
10
+ module Resources
11
+ module States
12
+ def states(*args)
13
+ ids, data = unwrap_uuids(*args), unwrap_data(*args)
14
+ validate_params(data) do
15
+ validate :query => [:fields, :sort, :include], :as => :string_or_array_of_strings
16
+ validate :query => :filter, :as => :hash
17
+ validate :query => [:page, :per_page], :as => :positive_integer
18
+ end
19
+ perform_get('/states', ids, data)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ require 'flapjack-diner/version'
6
+ require 'flapjack-diner/argument_validator'
7
+
8
+ module Flapjack
9
+ module Diner
10
+ module Resources
11
+ module Statistics
12
+ def statistics(*args)
13
+ ids, data = unwrap_ids(*args), unwrap_data(*args)
14
+ validate_params(data) do
15
+ validate :query => :fields, :as => :string_or_array_of_strings
16
+ validate :query => :filter, :as => :hash
17
+ validate :query => [:page, :per_page], :as => :positive_integer
18
+ end
19
+ perform_get('/statistics', ids, data)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,47 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ require 'flapjack-diner/version'
6
+ require 'flapjack-diner/argument_validator'
7
+
8
+ module Flapjack
9
+ module Diner
10
+ module Resources
11
+ module Tags
12
+ def create_tags(*args)
13
+ data = unwrap_data(*args)
14
+ validate_params(data) do
15
+ validate :query => :name, :as => [:required, :non_empty_string]
16
+ validate :query => [:checks, :rules], :as => :multiple_link_uuid
17
+ end
18
+ perform_post(:tags, '/tags', data)
19
+ end
20
+
21
+ def tags(*args)
22
+ ids, data = unwrap_ids(*args), unwrap_data(*args)
23
+ validate_params(data) do
24
+ validate :query => [:fields, :sort, :include], :as => :string_or_array_of_strings
25
+ validate :query => :filter, :as => :hash
26
+ validate :query => [:page, :per_page], :as => :positive_integer
27
+ end
28
+ perform_get('/tags', ids, data)
29
+ end
30
+
31
+ def update_tags(*args)
32
+ data = unwrap_data(*args)
33
+ validate_params(data) do
34
+ validate :query => [:checks, :rules], :as => :multiple_link_uuid
35
+ end
36
+ perform_patch(:tags, "/tags", data)
37
+ end
38
+
39
+ def delete_tags(*ids)
40
+ raise "'delete_tags' requires at least one tag ID " \
41
+ 'parameter' if ids.nil? || ids.empty?
42
+ perform_delete(:tags, '/tags', *ids)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end