wavefront-sdk 3.6.1 → 5.0.1

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +43 -2
  3. data/.travis.yml +0 -1
  4. data/HISTORY.md +33 -0
  5. data/README.md +4 -3
  6. data/lib/wavefront-sdk/account.rb +303 -0
  7. data/lib/wavefront-sdk/api_mixins/user.rb +20 -0
  8. data/lib/wavefront-sdk/core/api_caller.rb +50 -7
  9. data/lib/wavefront-sdk/core/exception.rb +6 -0
  10. data/lib/wavefront-sdk/core/response.rb +2 -1
  11. data/lib/wavefront-sdk/defs/version.rb +1 -3
  12. data/lib/wavefront-sdk/ingestionpolicy.rb +85 -0
  13. data/lib/wavefront-sdk/paginator/base.rb +21 -15
  14. data/lib/wavefront-sdk/query.rb +0 -1
  15. data/lib/wavefront-sdk/role.rb +128 -0
  16. data/lib/wavefront-sdk/spy.rb +126 -0
  17. data/lib/wavefront-sdk/stdlib/array.rb +1 -1
  18. data/lib/wavefront-sdk/stdlib/time.rb +13 -0
  19. data/lib/wavefront-sdk/unstable/README.md +4 -0
  20. data/lib/wavefront-sdk/unstable/chart.rb +90 -0
  21. data/lib/wavefront-sdk/unstable/unstable.rb +9 -0
  22. data/lib/wavefront-sdk/usage.rb +31 -0
  23. data/lib/wavefront-sdk/user.rb +41 -0
  24. data/lib/wavefront-sdk/usergroup.rb +17 -16
  25. data/lib/wavefront-sdk/validators.rb +65 -7
  26. data/lib/wavefront-sdk/write.rb +13 -3
  27. data/spec/.rubocop.yml +42 -1
  28. data/spec/spec_helper.rb +4 -0
  29. data/spec/support/minitest_assertions.rb +4 -4
  30. data/spec/wavefront-sdk/account_spec.rb +238 -0
  31. data/spec/wavefront-sdk/core/api_caller_spec.rb +43 -0
  32. data/spec/wavefront-sdk/ingestionpolicy_spec.rb +43 -0
  33. data/spec/wavefront-sdk/metric_helper_spec.rb +1 -1
  34. data/spec/wavefront-sdk/role_spec.rb +96 -0
  35. data/spec/wavefront-sdk/spy_spec.rb +113 -0
  36. data/spec/wavefront-sdk/unstable/chart_spec.rb +39 -0
  37. data/spec/wavefront-sdk/usage_spec.rb +33 -0
  38. data/spec/wavefront-sdk/user_spec.rb +20 -0
  39. data/spec/wavefront-sdk/usergroup_spec.rb +21 -11
  40. data/spec/wavefront-sdk/validators_spec.rb +52 -6
  41. data/wavefront-sdk.gemspec +4 -4
  42. metadata +30 -9
@@ -107,12 +107,14 @@ module Wavefront
107
107
  end
108
108
 
109
109
  # Compound the responses of all chunked writes into one. It will
110
- # be 'ok' only if *everything* passed.
110
+ # be 'ok' only if *everything* passed. Returns 400 as the HTTP code on
111
+ # error, regardless of what actual errors occurred.
111
112
  # @param responses [Array[Wavefront::Response]]
112
113
  # @return Wavefront::Response
113
114
  #
114
115
  def composite_response(responses)
115
- result = responses.all?(&:ok?) ? 'OK' : 'ERROR'
116
+ result, code = response_results(responses)
117
+
116
118
  summary = { sent: 0, rejected: 0, unsent: 0 }
117
119
 
118
120
  %i[sent rejected unsent].each do |k|
@@ -120,11 +122,19 @@ module Wavefront
120
122
  end
121
123
 
122
124
  Wavefront::Response.new(
123
- { status: { result: result, message: nil, code: nil },
125
+ { status: { result: result, message: nil, code: code },
124
126
  response: summary.to_h }.to_json, nil
125
127
  )
126
128
  end
127
129
 
130
+ def response_results(responses)
131
+ if responses.all?(&:ok?)
132
+ ['OK', 200]
133
+ else
134
+ ['ERROR', 400]
135
+ end
136
+ end
137
+
128
138
  def manage_conn
129
139
  opts[:noauto] ? false : true
130
140
  end
@@ -5,9 +5,50 @@ Metrics/AbcSize:
5
5
  # Offense count: 5
6
6
  # Configuration parameters: CountComments.
7
7
  Metrics/ClassLength:
8
- Max: 300
8
+ Max: 400
9
9
 
10
10
  # Offense count: 46
11
11
  # Configuration parameters: CountComments, ExcludedMethods.
12
12
  Metrics/MethodLength:
13
13
  Max: 39
14
+
15
+ # New cops
16
+ #
17
+ Lint/RaiseException:
18
+ Enabled: true
19
+ Lint/StructNewOverride:
20
+ Enabled: true
21
+ Style/ExponentialNotation:
22
+ Enabled: true
23
+ Style/HashEachMethods:
24
+ Enabled: true
25
+ Style/HashTransformKeys:
26
+ Enabled: true
27
+ Style/HashTransformValues:
28
+ Enabled: true
29
+ Layout/EmptyLinesAroundAttributeAccessor:
30
+ Enabled: true
31
+ Layout/SpaceAroundMethodCallOperator:
32
+ Enabled: true
33
+ Style/SlicingWithRange:
34
+ Enabled: true
35
+ Lint/DeprecatedOpenSSLConstant:
36
+ Enabled: true
37
+ Lint/MixedRegexpCaptureTypes:
38
+ Enabled: true
39
+ Style/RedundantRegexpCharacterClass:
40
+ Enabled: true
41
+ Style/RedundantRegexpEscape:
42
+ Enabled: true
43
+ Style/AccessorGrouping:
44
+ Enabled: true
45
+ Style/BisectedAttrAccessor:
46
+ Enabled: true
47
+ Style/RedundantAssignment:
48
+ Enabled: true
49
+ Style/RedundantFetchBlock:
50
+ Enabled: true
51
+
52
+ # Is nothing sacred?
53
+ Layout/LineLength:
54
+ Max: 80
@@ -23,6 +23,10 @@ class WavefrontTestBase < MiniTest::Test
23
23
  "../lib/wavefront-sdk/#{class_basename.downcase}"
24
24
  end
25
25
 
26
+ def dummy_response
27
+ DUMMY_RESPONSE
28
+ end
29
+
26
30
  private
27
31
 
28
32
  def setup
@@ -20,7 +20,7 @@ module Minitest
20
20
  headers = DEFAULT_HEADERS
21
21
  stub_request(:get, uri(api_path))
22
22
  .with(headers: headers)
23
- .to_return(body: DUMMY_RESPONSE, status: 200)
23
+ .to_return(body: dummy_response, status: 200)
24
24
  yield block
25
25
  assert_requested(:get, uri(api_path), headers: headers)
26
26
  WebMock.reset!
@@ -39,7 +39,7 @@ module Minitest
39
39
  payload = 'null' if payload.nil?
40
40
  stub_request(:post, uri(api_path))
41
41
  .with(body: payload, headers: headers)
42
- .to_return(body: DUMMY_RESPONSE, status: 200)
42
+ .to_return(body: dummy_response, status: 200)
43
43
  yield block
44
44
  assert_requested(:post, uri(api_path), headers: headers)
45
45
  WebMock.reset!
@@ -58,7 +58,7 @@ module Minitest
58
58
  payload = 'null' if payload.nil?
59
59
  stub_request(:put, uri(api_path))
60
60
  .with(body: payload, headers: headers)
61
- .to_return(body: DUMMY_RESPONSE, status: 200)
61
+ .to_return(body: dummy_response, status: 200)
62
62
  yield block
63
63
  assert_requested(:put, uri(api_path), headers: headers)
64
64
  WebMock.reset!
@@ -72,7 +72,7 @@ module Minitest
72
72
  headers = DEFAULT_HEADERS
73
73
  stub_request(:delete, uri(api_path))
74
74
  .with(headers: headers)
75
- .to_return(body: DUMMY_RESPONSE, status: 200)
75
+ .to_return(body: dummy_response, status: 200)
76
76
  yield block
77
77
  assert_requested(:delete, uri(api_path), headers: headers)
78
78
  WebMock.reset!
@@ -0,0 +1,238 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../spec_helper'
5
+ require_relative '../test_mixins/general'
6
+
7
+ # Unit tests for Account class
8
+ #
9
+ class WavefrontAccountTest < WavefrontTestBase
10
+ include WavefrontTest::List
11
+ include WavefrontTest::Delete
12
+ include WavefrontTest::Describe
13
+
14
+ def test_add_roles
15
+ assert_posts("/api/v2/account/#{id}/addRoles", roles.to_json) do
16
+ wf.add_roles(id, roles)
17
+ end
18
+
19
+ assert_invalid_id { wf.add_roles(invalid_id, roles) }
20
+
21
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
22
+ wf.add_roles(id, invalid_role)
23
+ end
24
+ end
25
+
26
+ def test_add_user_groups
27
+ assert_posts("/api/v2/account/#{id}/addUserGroups", groups.to_json) do
28
+ wf.add_user_groups(id, groups)
29
+ end
30
+
31
+ assert_invalid_id { wf.add_user_groups(invalid_id, groups) }
32
+ end
33
+
34
+ def test_business_functions
35
+ assert_gets("/api/v2/account/#{id}/businessFunctions") do
36
+ wf.business_functions(id)
37
+ end
38
+
39
+ assert_raises(ArgumentError) { wf.business_functions }
40
+ end
41
+
42
+ def test_grant_to_single_user
43
+ assert_posts("/api/v2/account/#{id}/grant/#{permission}") do
44
+ wf.grant(id, permission)
45
+ end
46
+ end
47
+
48
+ def test_grant_to_multiple_users
49
+ assert_posts("/api/v2/account/grant/#{permission}", id_list.to_json) do
50
+ wf.grant(id_list, permission)
51
+ end
52
+
53
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
54
+ wf.remove_roles(id, invalid_role)
55
+ end
56
+ end
57
+
58
+ def test_remove_roles
59
+ assert_posts("/api/v2/account/#{id}/removeRoles", roles.to_json) do
60
+ wf.remove_roles(id, roles)
61
+ end
62
+
63
+ assert_invalid_id { wf.remove_roles(invalid_id, roles) }
64
+
65
+ assert_raises(Wavefront::Exception::InvalidRoleId) do
66
+ wf.remove_roles(id, invalid_role)
67
+ end
68
+ end
69
+
70
+ def test_remove_user_groups
71
+ assert_posts("/api/v2/account/#{id}/removeUserGroups", groups.to_json) do
72
+ wf.remove_user_groups(id, groups)
73
+ end
74
+
75
+ assert_invalid_id { wf.remove_user_groups(invalid_id, groups) }
76
+
77
+ assert_raises(Wavefront::Exception::InvalidUserGroupId) do
78
+ wf.remove_user_groups(id, invalid_group)
79
+ end
80
+ end
81
+
82
+ def test_revoke_from_single_user
83
+ assert_posts("/api/v2/account/#{id}/revoke/#{permission}") do
84
+ wf.revoke(id, permission)
85
+ end
86
+
87
+ assert_invalid_id { wf.revoke(invalid_id, permission) }
88
+
89
+ assert_raises(Wavefront::Exception::InvalidPermission) do
90
+ wf.revoke(id, invalid_permission)
91
+ end
92
+ end
93
+
94
+ def test_revoke_from_multiple_users
95
+ assert_posts("/api/v2/account/revoke/#{permission}", id_list.to_json) do
96
+ wf.revoke(id_list, permission)
97
+ end
98
+ end
99
+
100
+ def test_delete_accounts
101
+ assert_posts('/api/v2/account/deleteAccounts', id_list.to_json) do
102
+ wf.delete_accounts(id_list)
103
+ end
104
+
105
+ assert_invalid_id { wf.delete_accounts([invalid_id]) }
106
+ end
107
+
108
+ def test_add_ingestion_policy
109
+ assert_posts('/api/v2/account/addingestionpolicy',
110
+ { ingestionPolicyId: policy_id,
111
+ accounts: id_list }.to_json) do
112
+ wf.add_ingestion_policy(policy_id, id_list)
113
+ end
114
+
115
+ assert_raises Wavefront::Exception::InvalidIngestionPolicyId do
116
+ wf.add_ingestion_policy(invalid_policy_id, id_list)
117
+ end
118
+
119
+ assert_invalid_id { wf.add_ingestion_policy(policy_id, [invalid_id]) }
120
+ end
121
+
122
+ def test_remove_ingestion_policy
123
+ assert_posts('/api/v2/account/removeingestionpolicies',
124
+ { ingestionPolicyId: policy_id,
125
+ accounts: id_list }.to_json) do
126
+ wf.remove_ingestion_policy(policy_id, id_list)
127
+ end
128
+
129
+ assert_raises Wavefront::Exception::InvalidIngestionPolicyId do
130
+ wf.add_ingestion_policy(invalid_policy_id, id_list)
131
+ end
132
+
133
+ assert_invalid_id { wf.add_ingestion_policy(policy_id, [invalid_id]) }
134
+ end
135
+
136
+ def test_user_list
137
+ assert_gets('/api/v2/account/user?offset=0&limit=100') do
138
+ wf.user_list
139
+ end
140
+
141
+ assert_gets('/api/v2/account/user?offset=10&limit=50') do
142
+ wf.user_list(10, 50)
143
+ end
144
+ end
145
+
146
+ def test_user_describe
147
+ assert_gets("/api/v2/account/user/#{id}") { wf.user_describe(id) }
148
+
149
+ assert_raises(Wavefront::Exception::InvalidUserId) do
150
+ wf.user_describe(invalid_id)
151
+ end
152
+
153
+ assert_raises(ArgumentError) { wf.user_describe }
154
+ end
155
+
156
+ def test_user_create
157
+ assert_posts('/api/v2/account/user', payload.to_json) do
158
+ wf.user_create(payload)
159
+ end
160
+
161
+ assert_raises(ArgumentError) { wf.user_create }
162
+ assert_raises(ArgumentError) { wf.user_create('test') }
163
+ end
164
+
165
+ def test_user_invite
166
+ assert_posts('/api/v2/account/user/invite', [payload].to_json) do
167
+ wf.user_invite([payload])
168
+ end
169
+
170
+ assert_raises(ArgumentError) { wf.user_invite }
171
+ assert_raises(ArgumentError) { wf.user_invite('test') }
172
+ end
173
+
174
+ def test_validate_accounts
175
+ assert_posts('/api/v2/account/validateAccounts', id_list.to_json) do
176
+ wf.validate_accounts(id_list)
177
+ end
178
+
179
+ assert_raises(ArgumentError) { wf.validate_accounts }
180
+ end
181
+
182
+ private
183
+
184
+ def api_class
185
+ 'account'
186
+ end
187
+
188
+ def id
189
+ 'sa::tester'
190
+ end
191
+
192
+ def invalid_id
193
+ 'bad_id' * 1000
194
+ end
195
+
196
+ def groups
197
+ %w[f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672
198
+ 2659191e-aad4-4302-a94e-9667e1517127]
199
+ end
200
+
201
+ def roles
202
+ %w[f8dc0c14-91a0-4ca9-8a2a-7d47f4db1234
203
+ 2659191e-aad4-4302-a94e-9667e1515678]
204
+ end
205
+
206
+ def invalid_role
207
+ %w[bad_role]
208
+ end
209
+
210
+ def invalid_group
211
+ %w[bad_group]
212
+ end
213
+
214
+ def id_list
215
+ %w[sa:test user@example.com]
216
+ end
217
+
218
+ def invalid_permission
219
+ 'some_nonsense_permission_i_made_up'
220
+ end
221
+
222
+ def permission
223
+ 'agent_management'
224
+ end
225
+
226
+ def policy_id
227
+ 'testpolicy-1579537565010'
228
+ end
229
+
230
+ def invalid_policy_id
231
+ 'badpolicy'
232
+ end
233
+
234
+ def payload
235
+ { emailAddress: id,
236
+ groups: %w[browse] }
237
+ end
238
+ end
@@ -37,6 +37,49 @@ class WavefrontApiCallerTest < MiniTest::Test
37
37
  assert_requested(:get, uri, headers: headers)
38
38
  end
39
39
 
40
+ def test_get_flat_params
41
+ query = { rkey: %w[val1 val2 val3], ukey: 36 }
42
+ uri = "#{uri_base}/path?rkey=val1&rkey=val2&rkey=val3&ukey=36"
43
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
44
+ wf.get_flat_params('/path', query)
45
+ assert_requested(:get, uri, headers: headers)
46
+ end
47
+
48
+ def test_get_stream
49
+ uri = "#{uri_base}/path?key1=val1"
50
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
51
+ out, err = capture_io { wf.get_stream('/path', key1: 'val1') }
52
+ assert_requested(:get, uri, headers: headers)
53
+ assert_equal(out.strip, DUMMY_RESPONSE)
54
+ assert_empty(err)
55
+ end
56
+
57
+ def test_get_stream_array_params
58
+ uri = "#{uri_base}/path?key=val1&key=val2"
59
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
60
+ out, err = capture_io { wf.get_stream('/path', key: %w[val1 val2]) }
61
+ assert_requested(:get, uri, headers: headers)
62
+ assert_equal(out.strip, DUMMY_RESPONSE)
63
+ assert_empty(err)
64
+ end
65
+
66
+ def test_get_stream_timestamp
67
+ uri = "#{uri_base}/path?key1=val1"
68
+ stub_request(:get, uri).to_return(body: DUMMY_RESPONSE, status: 200)
69
+
70
+ out, err = capture_io do
71
+ wf.get_stream('/path',
72
+ { key1: 'val1' },
73
+ timestamp_chunks: true)
74
+ end
75
+
76
+ assert_requested(:get, uri, headers: headers)
77
+ out_lines = out.split("\n")
78
+ assert_match(/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d \+\d{4}$/, out_lines[0])
79
+ assert_equal(out_lines[1], DUMMY_RESPONSE)
80
+ assert_empty(err)
81
+ end
82
+
40
83
  def test_post
41
84
  uri = "#{uri_base}/path"
42
85
  obj = { key: 'value' }
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../spec_helper'
5
+ require_relative '../test_mixins/general'
6
+
7
+ # Unit tests for IngestionPolicy class
8
+ #
9
+ class WavefrontIngestionPolicyTest < WavefrontTestBase
10
+ include WavefrontTest::List
11
+ include WavefrontTest::Describe
12
+ include WavefrontTest::Update
13
+ include WavefrontTest::Delete
14
+
15
+ private
16
+
17
+ def payload
18
+ { sampledUserAccounts: ['string'],
19
+ userAccountCount: 0,
20
+ sampledServiceAccounts: [
21
+ 'string'
22
+ ],
23
+ serviceAccountCount: 0,
24
+ name: 'string',
25
+ id: 'string',
26
+ description: 'string',
27
+ customer: 'string',
28
+ lastUpdatedMs: 0,
29
+ lastUpdatedAccountId: 'string' }
30
+ end
31
+
32
+ def api_class
33
+ 'usage/ingestionpolicy'
34
+ end
35
+
36
+ def id
37
+ 'test-ingestion-policy-1579538401492'
38
+ end
39
+
40
+ def invalid_id
41
+ 'bad_id'
42
+ end
43
+ end