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,11 @@
1
+ require_relative 'base'
2
+
3
+ module Wavefront
4
+ module Paginator
5
+ #
6
+ # As far as I know, there are no DELETE methods with paginated
7
+ # output.
8
+ #
9
+ class Delete < Base; end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base'
2
+
3
+ module Wavefront
4
+ module Paginator
5
+ #
6
+ # GET pagination is handled in the Base class without further
7
+ # modification.
8
+ #
9
+ class Get < Base; end
10
+ end
11
+ end
@@ -0,0 +1,64 @@
1
+ require 'json'
2
+ require_relative 'base'
3
+
4
+ module Wavefront
5
+ module Paginator
6
+ #
7
+ # We need to monkey-patch the Base class to pre-process data for
8
+ # a couple of methods.
9
+ #
10
+ class Post < Base
11
+ #
12
+ # super#setpagination requires that the args are an array, and
13
+ # that one of the args is a hash containing limit and offset
14
+ # keys.
15
+ #
16
+ def set_pagination(offset, limit, args)
17
+ super(offset, limit, body_as(Hash, args))
18
+ body_as(String, args)
19
+ end
20
+
21
+ # super#limit_and_offset requires a hash containing the limit
22
+ # and offset values. In a POST that's in the body of the
23
+ # request, which at this point has been turned into a JSON
24
+ # string. We have to temporarily turn it back into an object
25
+ # and pass it up to the superclass.
26
+ #
27
+ # The body is the second argument. We'll allow for it already
28
+ # being an object, just in case.
29
+ #
30
+ def limit_and_offset(args)
31
+ super(body_as(Hash, args))
32
+ end
33
+
34
+ # Faraday needs the body of the POST to be described as a JSON
35
+ # string, but our methods which modify the body for recursive
36
+ # calls need it as a hash. This method takes an array of args
37
+ # and ensures the body element is either a string or an
38
+ # object. If the body cannot be turned into JSON, which some
39
+ # bodies can't, return an empty array.
40
+ #
41
+ # @param desired [Class] String or Hash, what you want the
42
+ # class of the body element to be
43
+ # @param args [Array] the arguments to the Faraday call method
44
+ # @params index [Integer] the index of the body element.
45
+ # Always 1, AFAIK.
46
+ # @return [Array] of args
47
+ #
48
+ def body_as(desired, args, index = 1)
49
+ body = args[index]
50
+
51
+ return args if body.class == desired
52
+
53
+ args[index] = if body.is_a?(String)
54
+ JSON.parse(body, symbolize_names: true)
55
+ else
56
+ body.to_json
57
+ end
58
+ args
59
+ rescue JSON::ParserError
60
+ []
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'base'
2
+
3
+ module Wavefront
4
+ module Paginator
5
+ #
6
+ # As far as I know, there are no PUT methods with paginated
7
+ # output.
8
+ #
9
+ class Put < Base; end
10
+ end
11
+ end
@@ -1,10 +1,10 @@
1
- require_relative 'base'
1
+ require_relative 'core/api'
2
2
 
3
3
  module Wavefront
4
4
  #
5
5
  # Manage and query Wavefront proxies.
6
6
  #
7
- class Proxy < Base
7
+ class Proxy < CoreApi
8
8
  # GET /api/v2/proxy
9
9
  # Get all proxies for a customer
10
10
  #
@@ -12,7 +12,7 @@ module Wavefront
12
12
  # @param limit [Int] the number of proxies to return
13
13
  #
14
14
  def list(offset = 0, limit = 100)
15
- api_get('', offset: offset, limit: limit)
15
+ api.get('', offset: offset, limit: limit)
16
16
  end
17
17
 
18
18
  # DELETE /api/v2/proxy/id
@@ -27,7 +27,7 @@ module Wavefront
27
27
  #
28
28
  def delete(id)
29
29
  wf_proxy_id?(id)
30
- api_delete(id)
30
+ api.delete(id)
31
31
  end
32
32
 
33
33
  # GET /api/v2/proxy/id
@@ -38,7 +38,7 @@ module Wavefront
38
38
  #
39
39
  def describe(id)
40
40
  wf_proxy_id?(id)
41
- api_get(id)
41
+ api.get(id)
42
42
  end
43
43
 
44
44
  # POST /api/v2/proxy/id/undelete
@@ -51,7 +51,7 @@ module Wavefront
51
51
  #
52
52
  def undelete(id)
53
53
  wf_proxy_id?(id)
54
- api_post([id, 'undelete'].uri_concat)
54
+ api.post([id, 'undelete'].uri_concat)
55
55
  end
56
56
 
57
57
  # PUT /api/v2/proxy/id
@@ -82,7 +82,7 @@ module Wavefront
82
82
  #
83
83
  def update(id, payload)
84
84
  wf_proxy_id?(id)
85
- api_put(id, payload)
85
+ api.put(id, payload)
86
86
  end
87
87
  end
88
88
  end
@@ -1,10 +1,10 @@
1
- require_relative 'base'
1
+ require_relative 'core/api'
2
2
 
3
3
  module Wavefront
4
4
  #
5
5
  # Query Wavefront metrics.
6
6
  #
7
- class Query < Base
7
+ class Query < CoreApi
8
8
  def api_base
9
9
  'chart'
10
10
  end
@@ -41,7 +41,7 @@ module Wavefront
41
41
  options[:e] = parse_time(t_end, true) if t_end
42
42
 
43
43
  options.delete_if { |k, v| v == false && k != :i }
44
- api_get('api', options)
44
+ api.get('api', options)
45
45
  end
46
46
 
47
47
  # GET /api/v2/chart/raw
@@ -70,7 +70,7 @@ module Wavefront
70
70
  options[:startTime] = parse_time(t_start, true) if t_start
71
71
  options[:endTime] = parse_time(t_end, true) if t_end
72
72
 
73
- api_get('raw', options)
73
+ api.get('raw', options)
74
74
  end
75
75
 
76
76
  # Fake a response which looks like we get from all the other
@@ -1,32 +1,16 @@
1
- require_relative 'base_write'
1
+ require_relative 'write'
2
2
 
3
3
  module Wavefront
4
4
  #
5
- # This class helps you send points direct to the Wavefront API.
5
+ # This class is now a shim around Wavefront::Write, which forces
6
+ # the use of the Wavefront::Writer::Api writer. It is probably
7
+ # better to use Wavefront::Write directly. This class has been
8
+ # left in for backward-compatability.
6
9
  #
7
- # The points are prepped in the BaseWrite class, which this
8
- # extends. This class provides the transport mechanism.
9
- #
10
- class Report < BaseWrite
11
- def api_path
12
- '/report'
13
- end
14
-
15
- def write(points = [], _openclose = true, prefix = nil)
16
- _write_loop(prepped_points(points, prefix))
17
- end
18
-
19
- def really_send_point(point)
20
- api_post('/?f=graphite_v2', point, 'application/octet-stream')
21
- end
22
-
23
- private
24
-
25
- # Because API calls are expensive (compared to hitting a local
26
- # proxy) we will bundle up points into a single call.
27
- #
28
- def _write_loop(points)
29
- send_point(points.map { |p| hash_to_wf(p) }.join("\n"))
10
+ class Report < Write
11
+ def initialize(creds = {}, opts = {})
12
+ opts[:writer] = :api
13
+ super(creds, opts)
30
14
  end
31
15
  end
32
16
  end
@@ -1,11 +1,11 @@
1
- require_relative 'base'
1
+ require_relative 'core/api'
2
2
 
3
3
  module Wavefront
4
4
  #
5
5
  # View and manage Cloud Integrations. These are identified by
6
6
  # a UUID.
7
7
  #
8
- class SavedSearch < Base
8
+ class SavedSearch < CoreApi
9
9
  # GET /api/v2/savedsearch
10
10
  # Get all saved searches for a user.
11
11
  #
@@ -14,7 +14,7 @@ module Wavefront
14
14
  # @return [Wavefront::Response]
15
15
  #
16
16
  def list(offset = 0, limit = 100)
17
- api_get('', offset: offset, limit: limit)
17
+ api.get('', offset: offset, limit: limit)
18
18
  end
19
19
 
20
20
  # POST /api/v2/savedsearch
@@ -26,7 +26,7 @@ module Wavefront
26
26
  #
27
27
  def create(body)
28
28
  raise ArgumentError unless body.is_a?(Hash)
29
- api_post('', body, 'application/json')
29
+ api.post('', body, 'application/json')
30
30
  end
31
31
 
32
32
  # DELETE /api/v2/savedsearch/id
@@ -37,7 +37,7 @@ module Wavefront
37
37
  #
38
38
  def delete(id)
39
39
  wf_savedsearch_id?(id)
40
- api_delete(id)
40
+ api.delete(id)
41
41
  end
42
42
 
43
43
  # GET /api/v2/savedsearch/id
@@ -48,7 +48,7 @@ module Wavefront
48
48
  #
49
49
  def describe(id)
50
50
  wf_savedsearch_id?(id)
51
- api_get(id)
51
+ api.get(id)
52
52
  end
53
53
 
54
54
  # PUT /api/v2/savedsearch/id
@@ -60,7 +60,7 @@ module Wavefront
60
60
  def update(id, body)
61
61
  wf_savedsearch_id?(id)
62
62
  raise ArgumentError unless body.is_a?(Hash)
63
- api_put(id, body)
63
+ api.put(id, body)
64
64
  end
65
65
 
66
66
  # GET /api/v2/savedsearch/type/entitytype
@@ -73,7 +73,7 @@ module Wavefront
73
73
  #
74
74
  def entity(entitytype, offset = 0, limit = 100)
75
75
  wf_savedsearch_entity?(entitytype)
76
- api_get(['type', entitytype].uri_concat, offset: offset,
76
+ api.get(['type', entitytype].uri_concat, offset: offset,
77
77
  limit: limit)
78
78
  end
79
79
  end
@@ -1,4 +1,4 @@
1
- require_relative 'base'
1
+ require_relative 'core/api'
2
2
 
3
3
  module Wavefront
4
4
  #
@@ -7,7 +7,7 @@ module Wavefront
7
7
  # this class covers the whole API with two methods, but leaves a
8
8
  # lot up to the user. It may grow, with convenience methods.
9
9
  #
10
- class Search < Base
10
+ class Search < CoreApi
11
11
  # POST /api/v2/search/entity
12
12
  # POST /api/v2/search/entity/deleted
13
13
  # Run a search query. This single method maps to many API paths.
@@ -45,15 +45,18 @@ module Wavefront
45
45
  # Build a query body
46
46
  #
47
47
  def body(query, options)
48
- ret = {
49
- limit: options[:limit] || 10,
50
- offset: options[:offset] || 0,
51
- query: [query].flatten,
52
- sort: { field: [query].flatten.first[:key],
53
- ascending: !options[:desc] || true }
54
- }
48
+ ret = { limit: options[:limit] || 10,
49
+ offset: options[:offset] || 0 }
50
+
51
+ if query && !query.empty?
52
+ ret[:query] = [query].flatten.map do |q|
53
+ q.tap { |iq| iq[:matchingMethod] ||= 'CONTAINS' }
54
+ end
55
+
56
+ ret[:sort] = { field: [query].flatten.first[:key],
57
+ ascending: !options[:desc] || true }
58
+ end
55
59
 
56
- ret[:query].map { |q| q[:matchingMethod] ||= 'CONTAINS' }
57
60
  ret
58
61
  end
59
62
 
@@ -78,7 +81,7 @@ module Wavefront
78
81
 
79
82
  path = [entity]
80
83
  path.<< 'deleted' if deleted
81
- api_post(path, body.to_json, 'application/json')
84
+ api.post(path, body, 'application/json')
82
85
  end
83
86
 
84
87
  # @param entity [String] the type of Wavefront object you wish
@@ -98,8 +101,8 @@ module Wavefront
98
101
 
99
102
  path = [entity]
100
103
  path.<< 'deleted' if deleted
101
- path.<< facet ? facet : 'facets'
102
- api_post(path, body, 'application/json')
104
+ path.<< facet || 'facets'
105
+ api.post(path, body, 'application/json')
103
106
  end
104
107
  end
105
108
  end
@@ -1,10 +1,10 @@
1
- require_relative 'base'
1
+ require_relative 'core/api'
2
2
 
3
3
  module Wavefront
4
4
  #
5
5
  # View and manage source metadata.
6
6
  #
7
- class Source < Base
7
+ class Source < CoreApi
8
8
  def update_keys
9
9
  %i[sourceName tags description]
10
10
  end
@@ -21,7 +21,7 @@ module Wavefront
21
21
  qs[:limit] = limit if limit
22
22
  qs[:cursor] = cursor if cursor
23
23
 
24
- api_get('', qs)
24
+ api.get('', qs)
25
25
  end
26
26
 
27
27
  # POST /api/v2/source
@@ -34,7 +34,7 @@ module Wavefront
34
34
  #
35
35
  def create(body)
36
36
  raise ArgumentError unless body.is_a?(Hash)
37
- api_post('', body, 'application/json')
37
+ api.post('', body, 'application/json')
38
38
  end
39
39
 
40
40
  # DELETE /api/v2/source/id
@@ -45,7 +45,7 @@ module Wavefront
45
45
  #
46
46
  def delete(id)
47
47
  wf_source_id?(id)
48
- api_delete(id)
48
+ api.delete(id)
49
49
  end
50
50
 
51
51
  # POST /api/v2/source/id/description
@@ -53,7 +53,7 @@ module Wavefront
53
53
 
54
54
  def description_set(id, description)
55
55
  wf_source_id?(id)
56
- api_post([id, 'description'].uri_concat, description,
56
+ api.post([id, 'description'].uri_concat, description,
57
57
  'application/json')
58
58
  end
59
59
 
@@ -62,7 +62,7 @@ module Wavefront
62
62
 
63
63
  def description_delete(id)
64
64
  wf_source_id?(id)
65
- api_delete([id, 'description'].uri_concat)
65
+ api.delete([id, 'description'].uri_concat)
66
66
  end
67
67
 
68
68
  # GET /api/v2/source/id
@@ -76,7 +76,7 @@ module Wavefront
76
76
  wf_version?(version) if version
77
77
  fragments = [id]
78
78
  fragments += ['history', version] if version
79
- api_get(fragments.uri_concat)
79
+ api.get(fragments.uri_concat)
80
80
  end
81
81
 
82
82
  # PUT /api/v2/source/id
@@ -94,9 +94,9 @@ module Wavefront
94
94
  wf_source_id?(id)
95
95
  raise ArgumentError unless body.is_a?(Hash)
96
96
 
97
- return api_put(id, body, 'application/json') unless modify
97
+ return api.put(id, body, 'application/json') unless modify
98
98
 
99
- api_put(id, hash_for_update(describe(id).response, body),
99
+ api.put(id, hash_for_update(describe(id).response, body),
100
100
  'application/json')
101
101
  end
102
102
 
@@ -108,7 +108,7 @@ module Wavefront
108
108
  #
109
109
  def tags(id)
110
110
  wf_source_id?(id)
111
- api_get([id, 'tag'].uri_concat)
111
+ api.get([id, 'tag'].uri_concat)
112
112
  end
113
113
 
114
114
  # POST /api/v2/source/id/tag
@@ -122,7 +122,7 @@ module Wavefront
122
122
  wf_source_id?(id)
123
123
  tags = Array(tags)
124
124
  tags.each { |t| wf_string?(t) }
125
- api_post([id, 'tag'].uri_concat, tags.to_json, 'application/json')
125
+ api.post([id, 'tag'].uri_concat, tags.to_json, 'application/json')
126
126
  end
127
127
 
128
128
  # DELETE /api/v2/source/id/tag/tagValue
@@ -135,7 +135,7 @@ module Wavefront
135
135
  def tag_delete(id, tag)
136
136
  wf_source_id?(id)
137
137
  wf_string?(tag)
138
- api_delete([id, 'tag', tag].uri_concat)
138
+ api.delete([id, 'tag', tag].uri_concat)
139
139
  end
140
140
 
141
141
  # PUT /api/v2/source/id/tag/tagValue
@@ -148,7 +148,7 @@ module Wavefront
148
148
  def tag_add(id, tag)
149
149
  wf_source_id?(id)
150
150
  wf_string?(tag)
151
- api_put([id, 'tag', tag].uri_concat)
151
+ api.put([id, 'tag', tag].uri_concat)
152
152
  end
153
153
  end
154
154
  end