wavefront-sdk 1.6.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/HISTORY.md +39 -13
  4. data/README.md +75 -28
  5. data/Rakefile +1 -1
  6. data/lib/wavefront-sdk/alert.rb +113 -17
  7. data/lib/wavefront-sdk/cloudintegration.rb +8 -8
  8. data/lib/wavefront-sdk/core/api.rb +99 -0
  9. data/lib/wavefront-sdk/core/api_caller.rb +211 -0
  10. data/lib/wavefront-sdk/{exception.rb → core/exception.rb} +11 -6
  11. data/lib/wavefront-sdk/{logger.rb → core/logger.rb} +2 -3
  12. data/lib/wavefront-sdk/{response.rb → core/response.rb} +69 -52
  13. data/lib/wavefront-sdk/credentials.rb +6 -3
  14. data/lib/wavefront-sdk/dashboard.rb +14 -14
  15. data/lib/wavefront-sdk/{constants.rb → defs/constants.rb} +1 -0
  16. data/lib/wavefront-sdk/defs/version.rb +1 -0
  17. data/lib/wavefront-sdk/derivedmetric.rb +14 -14
  18. data/lib/wavefront-sdk/distribution.rb +75 -0
  19. data/lib/wavefront-sdk/event.rb +13 -13
  20. data/lib/wavefront-sdk/externallink.rb +8 -8
  21. data/lib/wavefront-sdk/integration.rb +9 -9
  22. data/lib/wavefront-sdk/maintenancewindow.rb +54 -8
  23. data/lib/wavefront-sdk/message.rb +4 -4
  24. data/lib/wavefront-sdk/metric.rb +3 -3
  25. data/lib/wavefront-sdk/notificant.rb +9 -9
  26. data/lib/wavefront-sdk/paginator/base.rb +148 -0
  27. data/lib/wavefront-sdk/paginator/delete.rb +11 -0
  28. data/lib/wavefront-sdk/paginator/get.rb +11 -0
  29. data/lib/wavefront-sdk/paginator/post.rb +64 -0
  30. data/lib/wavefront-sdk/paginator/put.rb +11 -0
  31. data/lib/wavefront-sdk/proxy.rb +7 -7
  32. data/lib/wavefront-sdk/query.rb +4 -4
  33. data/lib/wavefront-sdk/report.rb +9 -25
  34. data/lib/wavefront-sdk/savedsearch.rb +8 -8
  35. data/lib/wavefront-sdk/search.rb +16 -13
  36. data/lib/wavefront-sdk/source.rb +14 -14
  37. data/lib/wavefront-sdk/{mixins.rb → support/mixins.rb} +8 -2
  38. data/lib/wavefront-sdk/{parse_time.rb → support/parse_time.rb} +2 -0
  39. data/lib/wavefront-sdk/types/status.rb +52 -0
  40. data/lib/wavefront-sdk/user.rb +8 -8
  41. data/lib/wavefront-sdk/validators.rb +52 -3
  42. data/lib/wavefront-sdk/webhook.rb +8 -8
  43. data/lib/wavefront-sdk/write.rb +153 -52
  44. data/lib/wavefront-sdk/writers/api.rb +38 -0
  45. data/lib/wavefront-sdk/writers/core.rb +146 -0
  46. data/lib/wavefront-sdk/writers/http.rb +42 -0
  47. data/lib/wavefront-sdk/writers/socket.rb +66 -0
  48. data/lib/wavefront-sdk/writers/summary.rb +39 -0
  49. data/lib/wavefront_sdk.rb +9 -0
  50. data/spec/spec_helper.rb +3 -0
  51. data/spec/wavefront-sdk/alert_spec.rb +6 -0
  52. data/spec/wavefront-sdk/{base_spec.rb → core/api_caller_spec.rb} +28 -41
  53. data/spec/wavefront-sdk/core/api_spec.rb +31 -0
  54. data/spec/wavefront-sdk/{logger_spec.rb → core/logger_spec.rb} +3 -3
  55. data/spec/wavefront-sdk/core/response_spec.rb +77 -0
  56. data/spec/wavefront-sdk/credentials_spec.rb +15 -10
  57. data/spec/wavefront-sdk/distribution_spec.rb +78 -0
  58. data/spec/wavefront-sdk/paginator/base_spec.rb +67 -0
  59. data/spec/wavefront-sdk/paginator/post_spec.rb +53 -0
  60. data/spec/wavefront-sdk/report_spec.rb +3 -1
  61. data/spec/wavefront-sdk/search_spec.rb +25 -0
  62. data/spec/wavefront-sdk/stdlib/array_spec.rb +2 -1
  63. data/spec/wavefront-sdk/stdlib/hash_spec.rb +6 -1
  64. data/spec/wavefront-sdk/stdlib/string_spec.rb +2 -0
  65. data/spec/wavefront-sdk/{mixins_spec.rb → support/mixins_spec.rb} +2 -2
  66. data/spec/wavefront-sdk/{parse_time_spec.rb → support/parse_time_spec.rb} +2 -2
  67. data/spec/wavefront-sdk/validators_spec.rb +64 -1
  68. data/spec/wavefront-sdk/write_spec.rb +55 -77
  69. data/spec/wavefront-sdk/writers/api_spec.rb +45 -0
  70. data/spec/wavefront-sdk/writers/core_spec.rb +49 -0
  71. data/spec/wavefront-sdk/writers/http_spec.rb +69 -0
  72. data/spec/wavefront-sdk/writers/socket_spec.rb +104 -0
  73. data/spec/wavefront-sdk/writers/summary_spec.rb +38 -0
  74. data/wavefront-sdk.gemspec +1 -1
  75. metadata +52 -24
  76. data/lib/wavefront-sdk/base.rb +0 -264
  77. data/lib/wavefront-sdk/base_write.rb +0 -185
  78. data/lib/wavefront-sdk/stdlib.rb +0 -5
  79. data/lib/wavefront-sdk/version.rb +0 -1
  80. data/spec/wavefront-sdk/base_write_spec.rb +0 -82
  81. data/spec/wavefront-sdk/response_spec.rb +0 -39
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'webmock/minitest'
4
+ require 'spy'
5
+ require 'spy/integration'
6
+ require 'socket'
7
+ require 'logger'
8
+ require_relative '../../spec_helper'
9
+ require_relative '../../../lib/wavefront-sdk/write'
10
+ require_relative '../resources/dummy_points'
11
+
12
+ # rubocop:disable Style/MutableConstant
13
+ WS_CREDS = { proxy: 'wavefront-proxy' }
14
+ # rubocop:enable Style/MutableConstant
15
+
16
+ # It's not straightforward to test the Writer::Socket class on its
17
+ # own. It makes far more sense to test the Write interface which
18
+ # calls it.
19
+ #
20
+ class WavefrontWriterSocketTest < MiniTest::Test
21
+ attr_reader :wf, :wf_noop
22
+
23
+ def setup
24
+ @wf = Wavefront::Write.new(WS_CREDS, writer: :socket)
25
+ @wf_noop = Wavefront::Write.new(WS_CREDS, writer: :socket, noop: true)
26
+ end
27
+
28
+ def test_writer_class
29
+ assert_instance_of(Wavefront::Writer::Socket, wf.writer)
30
+ end
31
+
32
+ def test_write_openclose
33
+ mocket = Mocket.new
34
+ Spy.on(TCPSocket, :new).and_return(mocket)
35
+ mocket_spy = Spy.on(mocket, :puts)
36
+ wf.write(POINT)
37
+ assert mocket_spy.has_been_called?
38
+ end
39
+
40
+ def test_write_noop
41
+ mocket = Mocket.new
42
+ Spy.on(TCPSocket, :new).and_return(mocket)
43
+ mocket_spy = Spy.on(mocket, :puts)
44
+ wf_noop.open
45
+ wf_noop.write(POINT, false)
46
+ refute mocket_spy.has_been_called?
47
+ end
48
+
49
+ def test_write_noop_openclose
50
+ mocket = Mocket.new
51
+ Spy.on(TCPSocket, :new).and_return(mocket)
52
+ mocket_spy = Spy.on(mocket, :puts)
53
+ wf_noop.write(POINT)
54
+ refute mocket_spy.has_been_called?
55
+ end
56
+
57
+ def test_write
58
+ mocket = Mocket.new
59
+ Spy.on(TCPSocket, :new).and_return(mocket)
60
+ mocket_spy = Spy.on(mocket, :puts)
61
+ wf.open
62
+ wf.write(POINT, false)
63
+ assert mocket_spy.has_been_called?
64
+ end
65
+
66
+ def test_write_array
67
+ mocket = Mocket.new
68
+ Spy.on(TCPSocket, :new).and_return(mocket)
69
+ mocket_spy = Spy.on(mocket, :puts)
70
+ wf.open
71
+ wf.write(POINT_A, false)
72
+ assert mocket_spy.has_been_called?
73
+ end
74
+
75
+ def test_noop_send_point
76
+ mocket = Mocket.new
77
+ Spy.on(TCPSocket, :new).and_return(mocket)
78
+ mocket_spy = Spy.on(mocket, :puts)
79
+ wf_noop.open
80
+ wf_noop.send_point(POINT_L)
81
+ refute mocket_spy.has_been_called?
82
+ end
83
+
84
+ def test_open
85
+ tcp_spy = Spy.on(TCPSocket, :new)
86
+ wf.open
87
+ assert tcp_spy.has_been_called?
88
+ assert_equal(tcp_spy.calls.first.args, ['wavefront-proxy', 2878])
89
+ end
90
+
91
+ def test_noop_open
92
+ tcp_spy = Spy.on(TCPSocket, :new)
93
+ wf_noop.open
94
+ refute tcp_spy.has_been_called?
95
+ end
96
+ end
97
+
98
+ # A mock socket
99
+ #
100
+ class Mocket
101
+ def puts(socket); end
102
+
103
+ def close; end
104
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../../../lib/wavefront-sdk/writers/summary'
4
+ require_relative '../../spec_helper'
5
+
6
+ class WavefronWriterSummaryTest < MiniTest::Test
7
+ attr_reader :wf
8
+
9
+ def setup
10
+ @wf = Wavefront::Writer::Summary.new
11
+ end
12
+
13
+ def test_initial
14
+ assert_equal(0, wf.sent)
15
+ assert_equal(0, wf.unsent)
16
+ assert_equal(0, wf.rejected)
17
+ assert_raises(NoMethodError) { wf.missing }
18
+ end
19
+
20
+ def test_increments_result_and_ok?
21
+ wf_ok = wf.dup
22
+ wf_ok.sent += 1
23
+ assert_equal('OK', wf_ok.result)
24
+ assert wf_ok.ok?
25
+ wf_ok.rejected += 1
26
+ refute wf_ok.ok?
27
+ assert_equal('ERROR', wf_ok.result)
28
+
29
+ wf_not_ok = wf.dup
30
+ wf_not_ok.unsent += 1
31
+ refute wf_not_ok.ok?
32
+ assert_equal('ERROR', wf_not_ok.result)
33
+ end
34
+
35
+ def test_to_h
36
+ assert_equal({ sent: 0, unsent: 0, rejected: 0 }, wf.to_h)
37
+ end
38
+ end
@@ -1,6 +1,6 @@
1
1
  require 'pathname'
2
2
  require 'date'
3
- require_relative 'lib/wavefront-sdk/version'
3
+ require_relative 'lib/wavefront-sdk/defs/version'
4
4
 
5
5
  # rubocop:disable Metrics/BlockLength:
6
6
  Gem::Specification.new do |gem|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wavefront-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Fisher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-22 00:00:00.000000000 Z
11
+ date: 2018-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -194,77 +194,97 @@ files:
194
194
  - README.md
195
195
  - Rakefile
196
196
  - lib/wavefront-sdk/alert.rb
197
- - lib/wavefront-sdk/base.rb
198
- - lib/wavefront-sdk/base_write.rb
199
197
  - lib/wavefront-sdk/cloudintegration.rb
200
- - lib/wavefront-sdk/constants.rb
198
+ - lib/wavefront-sdk/core/api.rb
199
+ - lib/wavefront-sdk/core/api_caller.rb
200
+ - lib/wavefront-sdk/core/exception.rb
201
+ - lib/wavefront-sdk/core/logger.rb
202
+ - lib/wavefront-sdk/core/response.rb
201
203
  - lib/wavefront-sdk/credentials.rb
202
204
  - lib/wavefront-sdk/dashboard.rb
205
+ - lib/wavefront-sdk/defs/constants.rb
206
+ - lib/wavefront-sdk/defs/version.rb
203
207
  - lib/wavefront-sdk/derivedmetric.rb
208
+ - lib/wavefront-sdk/distribution.rb
204
209
  - lib/wavefront-sdk/event.rb
205
- - lib/wavefront-sdk/exception.rb
206
210
  - lib/wavefront-sdk/externallink.rb
207
211
  - lib/wavefront-sdk/integration.rb
208
- - lib/wavefront-sdk/logger.rb
209
212
  - lib/wavefront-sdk/maintenancewindow.rb
210
213
  - lib/wavefront-sdk/message.rb
211
214
  - lib/wavefront-sdk/metric.rb
212
- - lib/wavefront-sdk/mixins.rb
213
215
  - lib/wavefront-sdk/notificant.rb
214
- - lib/wavefront-sdk/parse_time.rb
216
+ - lib/wavefront-sdk/paginator/base.rb
217
+ - lib/wavefront-sdk/paginator/delete.rb
218
+ - lib/wavefront-sdk/paginator/get.rb
219
+ - lib/wavefront-sdk/paginator/post.rb
220
+ - lib/wavefront-sdk/paginator/put.rb
215
221
  - lib/wavefront-sdk/proxy.rb
216
222
  - lib/wavefront-sdk/query.rb
217
223
  - lib/wavefront-sdk/report.rb
218
- - lib/wavefront-sdk/response.rb
219
224
  - lib/wavefront-sdk/savedsearch.rb
220
225
  - lib/wavefront-sdk/search.rb
221
226
  - lib/wavefront-sdk/source.rb
222
- - lib/wavefront-sdk/stdlib.rb
223
227
  - lib/wavefront-sdk/stdlib/array.rb
224
228
  - lib/wavefront-sdk/stdlib/hash.rb
225
229
  - lib/wavefront-sdk/stdlib/string.rb
230
+ - lib/wavefront-sdk/support/mixins.rb
231
+ - lib/wavefront-sdk/support/parse_time.rb
232
+ - lib/wavefront-sdk/types/status.rb
226
233
  - lib/wavefront-sdk/user.rb
227
234
  - lib/wavefront-sdk/validators.rb
228
- - lib/wavefront-sdk/version.rb
229
235
  - lib/wavefront-sdk/webhook.rb
230
236
  - lib/wavefront-sdk/write.rb
237
+ - lib/wavefront-sdk/writers/api.rb
238
+ - lib/wavefront-sdk/writers/core.rb
239
+ - lib/wavefront-sdk/writers/http.rb
240
+ - lib/wavefront-sdk/writers/socket.rb
241
+ - lib/wavefront-sdk/writers/summary.rb
242
+ - lib/wavefront_sdk.rb
231
243
  - pkg/wavefront-client-3.5.0.gem
232
244
  - spec/.rubocop.yml
233
245
  - spec/spec_helper.rb
234
246
  - spec/wavefront-sdk/alert_spec.rb
235
- - spec/wavefront-sdk/base_spec.rb
236
- - spec/wavefront-sdk/base_write_spec.rb
237
247
  - spec/wavefront-sdk/cloudintegration_spec.rb
248
+ - spec/wavefront-sdk/core/api_caller_spec.rb
249
+ - spec/wavefront-sdk/core/api_spec.rb
250
+ - spec/wavefront-sdk/core/logger_spec.rb
251
+ - spec/wavefront-sdk/core/response_spec.rb
238
252
  - spec/wavefront-sdk/credentials_spec.rb
239
253
  - spec/wavefront-sdk/dashboard_spec.rb
240
254
  - spec/wavefront-sdk/derivedmetric_spec.rb
255
+ - spec/wavefront-sdk/distribution_spec.rb
241
256
  - spec/wavefront-sdk/event_spec.rb
242
257
  - spec/wavefront-sdk/externallink_spec.rb
243
258
  - spec/wavefront-sdk/integration_spec.rb
244
- - spec/wavefront-sdk/logger_spec.rb
245
259
  - spec/wavefront-sdk/maintenancewindow_spec.rb
246
260
  - spec/wavefront-sdk/message_spec.rb
247
261
  - spec/wavefront-sdk/metric_spec.rb
248
- - spec/wavefront-sdk/mixins_spec.rb
249
262
  - spec/wavefront-sdk/notificant_spec.rb
250
- - spec/wavefront-sdk/parse_time_spec.rb
263
+ - spec/wavefront-sdk/paginator/base_spec.rb
264
+ - spec/wavefront-sdk/paginator/post_spec.rb
251
265
  - spec/wavefront-sdk/proxy_spec.rb
252
266
  - spec/wavefront-sdk/query_spec.rb
253
267
  - spec/wavefront-sdk/report_spec.rb
254
268
  - spec/wavefront-sdk/resources/dummy_points.rb
255
269
  - spec/wavefront-sdk/resources/test.conf
256
270
  - spec/wavefront-sdk/resources/test2.conf
257
- - spec/wavefront-sdk/response_spec.rb
258
271
  - spec/wavefront-sdk/savedsearch_spec.rb
259
272
  - spec/wavefront-sdk/search_spec.rb
260
273
  - spec/wavefront-sdk/source_spec.rb
261
274
  - spec/wavefront-sdk/stdlib/array_spec.rb
262
275
  - spec/wavefront-sdk/stdlib/hash_spec.rb
263
276
  - spec/wavefront-sdk/stdlib/string_spec.rb
277
+ - spec/wavefront-sdk/support/mixins_spec.rb
278
+ - spec/wavefront-sdk/support/parse_time_spec.rb
264
279
  - spec/wavefront-sdk/user_spec.rb
265
280
  - spec/wavefront-sdk/validators_spec.rb
266
281
  - spec/wavefront-sdk/webhook_spec.rb
267
282
  - spec/wavefront-sdk/write_spec.rb
283
+ - spec/wavefront-sdk/writers/api_spec.rb
284
+ - spec/wavefront-sdk/writers/core_spec.rb
285
+ - spec/wavefront-sdk/writers/http_spec.rb
286
+ - spec/wavefront-sdk/writers/socket_spec.rb
287
+ - spec/wavefront-sdk/writers/summary_spec.rb
268
288
  - wavefront-sdk.gemspec
269
289
  homepage: https://github.com/snltd/wavefront-sdk
270
290
  licenses:
@@ -294,36 +314,44 @@ test_files:
294
314
  - spec/.rubocop.yml
295
315
  - spec/spec_helper.rb
296
316
  - spec/wavefront-sdk/alert_spec.rb
297
- - spec/wavefront-sdk/base_spec.rb
298
- - spec/wavefront-sdk/base_write_spec.rb
299
317
  - spec/wavefront-sdk/cloudintegration_spec.rb
318
+ - spec/wavefront-sdk/core/api_caller_spec.rb
319
+ - spec/wavefront-sdk/core/api_spec.rb
320
+ - spec/wavefront-sdk/core/logger_spec.rb
321
+ - spec/wavefront-sdk/core/response_spec.rb
300
322
  - spec/wavefront-sdk/credentials_spec.rb
301
323
  - spec/wavefront-sdk/dashboard_spec.rb
302
324
  - spec/wavefront-sdk/derivedmetric_spec.rb
325
+ - spec/wavefront-sdk/distribution_spec.rb
303
326
  - spec/wavefront-sdk/event_spec.rb
304
327
  - spec/wavefront-sdk/externallink_spec.rb
305
328
  - spec/wavefront-sdk/integration_spec.rb
306
- - spec/wavefront-sdk/logger_spec.rb
307
329
  - spec/wavefront-sdk/maintenancewindow_spec.rb
308
330
  - spec/wavefront-sdk/message_spec.rb
309
331
  - spec/wavefront-sdk/metric_spec.rb
310
- - spec/wavefront-sdk/mixins_spec.rb
311
332
  - spec/wavefront-sdk/notificant_spec.rb
312
- - spec/wavefront-sdk/parse_time_spec.rb
333
+ - spec/wavefront-sdk/paginator/base_spec.rb
334
+ - spec/wavefront-sdk/paginator/post_spec.rb
313
335
  - spec/wavefront-sdk/proxy_spec.rb
314
336
  - spec/wavefront-sdk/query_spec.rb
315
337
  - spec/wavefront-sdk/report_spec.rb
316
338
  - spec/wavefront-sdk/resources/dummy_points.rb
317
339
  - spec/wavefront-sdk/resources/test.conf
318
340
  - spec/wavefront-sdk/resources/test2.conf
319
- - spec/wavefront-sdk/response_spec.rb
320
341
  - spec/wavefront-sdk/savedsearch_spec.rb
321
342
  - spec/wavefront-sdk/search_spec.rb
322
343
  - spec/wavefront-sdk/source_spec.rb
323
344
  - spec/wavefront-sdk/stdlib/array_spec.rb
324
345
  - spec/wavefront-sdk/stdlib/hash_spec.rb
325
346
  - spec/wavefront-sdk/stdlib/string_spec.rb
347
+ - spec/wavefront-sdk/support/mixins_spec.rb
348
+ - spec/wavefront-sdk/support/parse_time_spec.rb
326
349
  - spec/wavefront-sdk/user_spec.rb
327
350
  - spec/wavefront-sdk/validators_spec.rb
328
351
  - spec/wavefront-sdk/webhook_spec.rb
329
352
  - spec/wavefront-sdk/write_spec.rb
353
+ - spec/wavefront-sdk/writers/api_spec.rb
354
+ - spec/wavefront-sdk/writers/core_spec.rb
355
+ - spec/wavefront-sdk/writers/http_spec.rb
356
+ - spec/wavefront-sdk/writers/socket_spec.rb
357
+ - spec/wavefront-sdk/writers/summary_spec.rb
@@ -1,264 +0,0 @@
1
- require 'json'
2
- require 'time'
3
- require 'faraday'
4
- require 'pp'
5
- require 'ostruct'
6
- require 'addressable'
7
- require_relative 'logger'
8
- require_relative 'exception'
9
- require_relative 'mixins'
10
- require_relative 'response'
11
- require_relative 'validators'
12
- require_relative 'version'
13
-
14
- module Wavefront
15
- #
16
- # Abstract class from which all API classes inherit. When you make
17
- # any call to the Wavefront API from this SDK, you are returned an
18
- # OpenStruct object.
19
- #
20
- # @return a Wavefront::Response object
21
- #
22
- class Base
23
- include Wavefront::Validators
24
- include Wavefront::Mixins
25
- attr_reader :opts, :debug, :noop, :verbose, :net, :conn,
26
- :update_keys, :logger
27
-
28
- # Create a new API object. This will always be called from a
29
- # class which inherits this one. If the inheriting class defines
30
- # #post_initialize, that method will be called afterwards, with
31
- # the same arguments.
32
- #
33
- # @param creds [Hash] must contain the keys `endpoint` (the
34
- # Wavefront API server) and `token`, the user token with which
35
- # you wish to access the endpoint. Can optionally contain
36
- # `agent`, which will become the `user-agent` string sent with
37
- # all requests.
38
- # @param opts [Hash] options governing class behaviour. Expected
39
- # keys are `debug`, `noop` and `verbose`, all boolean; and
40
- # `logger`, which must be a standard Ruby logger object. You
41
- # can also pass :response_only. If this is true, you will only
42
- # be returned a hash of the 'response' object returned by
43
- # Wavefront.
44
- # @return [Nil]
45
- #
46
- def initialize(creds = {}, opts = {})
47
- @opts = opts
48
- @debug = opts[:debug] || false
49
- @noop = opts[:noop] || false
50
- @verbose = opts[:verbose] || false
51
- @logger = Wavefront::Logger.new(opts)
52
- setup_endpoint(creds)
53
- post_initialize(creds, opts) if respond_to?(:post_initialize)
54
- end
55
-
56
- def log(message, severity = :info)
57
- logger.log(message, severity)
58
- end
59
-
60
- # Convert an epoch timestamp into epoch milliseconds. If the
61
- # timestamp looks like it's already epoch milliseconds, return
62
- # it as-is.
63
- #
64
- # @param t [Integer] epoch timestamp
65
- # @return [Ingeter] epoch millisecond timestamp
66
- #
67
- def time_to_ms(time)
68
- return false unless time.is_a?(Integer)
69
- return time if time.to_s.size == 13
70
- (time.to_f * 1000).round
71
- end
72
-
73
- # Derive the first part of the API path from the class name. You
74
- # can override this in your class if you wish
75
- #
76
- # @return [String] portion of API URI
77
- #
78
- def api_base
79
- self.class.name.split('::').last.downcase
80
- end
81
-
82
- # Create a Faraday connection object. The server comes from the
83
- # endpoint passed to the initializer in the 'creds' hash; the
84
- # root of the URI is dynamically derived by the #setup_endpoint
85
- # method.
86
- #
87
- # @param headers [Hash] additional headers
88
- # @return [URI::HTTPS]
89
- #
90
- def mk_conn(path, headers = {})
91
- url = format('https://%s%s', net[:endpoint], [net[:api_base],
92
- path].uri_concat)
93
- Faraday.new(url: Addressable::URI.encode(url),
94
- headers: net[:headers].merge(headers))
95
- end
96
-
97
- # Make a GET call to the Wavefront API and return the result as
98
- # a Ruby hash. Can optionally perform a verbose noop, if the
99
- # @noop class variable is set. If @verbose is set, then prints
100
- # the information used to build the URI.
101
- #
102
- # @param path [String] path to be appended to the
103
- # #net[:api_base] path.
104
- # @param query [Hash] optional key-value pairs with will be made
105
- # into aquery string
106
- # @return [Hash] API response
107
- #
108
- def api_get(path, query = {})
109
- make_call(mk_conn(path), :get, nil, query)
110
- end
111
-
112
- # Make a POST call to the Wavefront API and return the result as
113
- # a Ruby hash. Can optionally perform a verbose noop, if the
114
- # @noop class variable is set. If @verbose is set, then prints
115
- # the information used to build the URI.
116
- #
117
- # @param path [String] path to be appended to the
118
- # #net[:api_base] path.
119
- # @param body [String] optional body text to post
120
- # @param ctype [String] the content type to use when posting
121
- # @return [Hash] API response
122
- #
123
- def api_post(path, body = nil, ctype = 'text/plain')
124
- body = body.to_json unless body.is_a?(String)
125
- make_call(mk_conn(path, 'Content-Type': ctype,
126
- 'Accept': 'application/json'),
127
- :post, nil, body)
128
- end
129
-
130
- # Make a PUT call to the Wavefront API and return the result as
131
- # a Ruby hash. Can optionally perform a verbose noop, if the
132
- # @noop class variable is set. If @verbose is set, then prints
133
- # the information used to build the URI.
134
- #
135
- # @param path [String] path to be appended to the
136
- # #net[:api_base] path.
137
- # @param body [String] optional body text to post
138
- # @param ctype [String] the content type to use when putting
139
- # @return [Hash] API response
140
- #
141
- def api_put(path, body = nil, ctype = 'application/json')
142
- make_call(mk_conn(path, 'Content-Type': ctype,
143
- 'Accept': 'application/json'),
144
- :put, nil, body.to_json)
145
- end
146
-
147
- # Make a DELETE call to the Wavefront API and return the result
148
- # as a Ruby hash. Can optionally perform a verbose noop, if the
149
- # @noop class variable is set. If @verbose is set, then prints
150
- # the information used to build the URI.
151
- #
152
- # @param path [String] path to be appended to the
153
- # #net[:api_base] path.
154
- # @return [Hash] API response
155
- #
156
- def api_delete(path)
157
- make_call(mk_conn(path), :delete)
158
- end
159
-
160
- # doing a PUT to update an object requires only a certain subset of
161
- # the keys returned by #describe(). This method takes the
162
- # existing description of an object and turns it into a new has
163
- # which can be PUT.
164
- #
165
- # @param old [Hash] a hash of the existing object
166
- # @param new [Hash] the keys you wish to update
167
- # @return [Hash] a hash containing only the keys which need to be
168
- # sent to the API. Keys will be symbolized.
169
- #
170
- def hash_for_update(old, new)
171
- raise ArgumentError unless old.is_a?(Hash) && new.is_a?(Hash)
172
-
173
- Hash[old.merge(new).map { |k, v| [k.to_sym, v] }].select do |k, _v|
174
- update_keys.include?(k)
175
- end
176
- end
177
-
178
- # If we need to massage a raw response to fit what the
179
- # Wavefront::Response class expects (I'm looking at you,
180
- # 'User'), a class can provide a {#response_shim} method.
181
- #
182
- def respond(resp)
183
- body = if respond_to?(:response_shim)
184
- response_shim(resp.body, resp.status)
185
- else
186
- resp.body
187
- end
188
-
189
- Wavefront::Response.new(body, resp.status, opts)
190
- end
191
-
192
- # Return all objects using a lazy enumerator
193
- # @return Enumerable
194
- #
195
- def everything
196
- Enumerator.new do |y|
197
- offset = 0
198
- limit = 100
199
-
200
- loop do
201
- resp = api_get('', offset: offset, limit: limit).response
202
- resp.items.map { |i| y.<< i }
203
- offset += limit
204
- raise StopIteration unless resp.moreItems == true
205
- end
206
- end.lazy
207
- end
208
-
209
- def api_path
210
- ['', 'api', 'v2', api_base].uri_concat
211
- end
212
-
213
- private
214
-
215
- # Try to describe the actual HTTP calls we make. There's a bit
216
- # of clumsy guesswork here
217
- #
218
- def verbosity(conn, method, *args)
219
- log format('uri: %s %s', method.upcase, conn.url_prefix)
220
-
221
- return unless args.last && !args.last.empty?
222
-
223
- log method == :get ? "params: #{args.last}" : "body: #{args.last}"
224
- end
225
-
226
- # Make the API call, or not, if noop is set.
227
- #
228
- def make_call(conn, method, *args)
229
- verbosity(conn, method, *args) if noop || verbose
230
- return if noop
231
-
232
- resp = conn.public_send(method, *args)
233
-
234
- if debug
235
- require 'pp'
236
- pp resp
237
- end
238
-
239
- respond(resp)
240
- end
241
-
242
- def validate_credentials(creds)
243
- %w[endpoint token].each do |k|
244
- unless creds.key?(k.to_sym)
245
- raise(Wavefront::Exception::CredentialError,
246
- format('credentials must contain %s', k))
247
- end
248
- end
249
- end
250
-
251
- def setup_endpoint(creds)
252
- validate_credentials(creds)
253
-
254
- unless creds.key?(:agent) && creds[:agent]
255
- creds[:agent] = "wavefront-sdk #{WF_SDK_VERSION}"
256
- end
257
-
258
- @net = { headers: { 'Authorization': "Bearer #{creds[:token]}",
259
- 'user-agent': creds[:agent] },
260
- endpoint: creds[:endpoint],
261
- api_base: api_path }
262
- end
263
- end
264
- end