wavefront-sdk 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae4a49d72cb51beb4506cf0ea7049cc992e59d7699c37e7e59be1312c7b8d3b0
4
- data.tar.gz: 8f0c3227f635bf86bdb1eca4e0091d9fb31abf0a99ada3cfcacd57e20a90655c
3
+ metadata.gz: b70702ec3f4199746297f9cae5d0f3d2c5158761d6cef25b662964731200c194
4
+ data.tar.gz: a4a43f7af561ee87f9e1479e98441d5db7a9a319ed5e26fcaf61bfb461c888e5
5
5
  SHA512:
6
- metadata.gz: dd4638fdafec9d0bc5afc327451f81a5d1dd8ef36409f4249e868e948ceb836528bed3bfa369f568dc0b959817a44c83aa5340eee20145d4347ac0a61a1d7904
7
- data.tar.gz: c1b87ee6385563a046e6d76c9e914ff5f4b635c4cdcf9179f84f44c1db6f87967028df4fdfb40ba25a0dca745f83a1bd43c229e91ae6893e170eba7c66b34dbc
6
+ metadata.gz: 202a64ad28993dbc87e1e5aa3533d2f28634febf7986ef4b0e46c884d9ccd662e31870a563d4950ef930d937ac25ca2611005ff5ff2e9c045b4c850ab519eb18
7
+ data.tar.gz: 4d5f91ce8d84fca57067278619723777f219ec2129b1c04e8d93bcf8db96d8175ecc386b7f078450c76fa701fbf069fdd2a7a95d313acb5e03b119cb4dfbae2e
data/HISTORY.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.5.0 (21/02/2019)
4
+ * New `Wavefront::UserGroup` class, for new API `UserGroup` feature.
5
+ * Extended `Wavefront::User` to cover new API methods.
6
+
3
7
  ## 2.4.0 (28/01/2019)
4
8
  * New `Wavefront::MetricHelper` class creates and in-memory buffer
5
9
  to which you can instantaneously add metrics, flushing it to
@@ -17,7 +17,7 @@ module Wavefront
17
17
  #
18
18
  # @param offset [Int] alert at which the list begins
19
19
  # @param limit [Int] the number of alerts to return
20
- # @return [Hash]
20
+ # @return [Wavefront::Response]
21
21
  #
22
22
  def list(offset = 0, limit = 100)
23
23
  api.get('', offset: offset, limit: limit)
@@ -40,6 +40,7 @@ module Wavefront
40
40
  class InvalidTimeUnit < RuntimeError; end
41
41
  class InvalidTimestamp < RuntimeError; end
42
42
  class InvalidUserId < RuntimeError; end
43
+ class InvalidUserGroupId < RuntimeError; end
43
44
  class InvalidVersion < RuntimeError; end
44
45
  class InvalidWebhookId < RuntimeError; end
45
46
  class NotImplemented < RuntimeError; end
@@ -1 +1 @@
1
- WF_SDK_VERSION = '2.4.0'.freeze
1
+ WF_SDK_VERSION = '2.5.0'.freeze
@@ -0,0 +1,24 @@
1
+ module Wavefront
2
+ #
3
+ # Things needed by User and UserGroup classes
4
+ #
5
+ module UserMixins
6
+ # Validate a list of users.
7
+ # @param list [Array[String]] list of user IDs
8
+ # @raise Wavefront::Exception::InvalidUser
9
+ #
10
+ def validate_user_list(list)
11
+ raise ArgumentError unless list.is_a?(Array)
12
+ list.each { |id| wf_user_id?(id) }
13
+ end
14
+
15
+ # Validate a list of user groups
16
+ # @param list [Array[String]] list of user group IDs
17
+ # @raise Wavefront::Exception::InvalidUserGroup
18
+ #
19
+ def validate_usergroup_list(list)
20
+ raise ArgumentError unless list.is_a?(Array)
21
+ list.each { |id| wf_usergroup_id?(id) }
22
+ end
23
+ end
24
+ end
@@ -1,10 +1,13 @@
1
1
  require_relative 'core/api'
2
+ require_relative 'support/user_mixins'
2
3
 
3
4
  module Wavefront
4
5
  #
5
6
  # Manage and query Wavefront users
6
7
  #
7
8
  class User < CoreApi
9
+ include Wavefront::UserMixins
10
+
8
11
  # GET /api/v2/user
9
12
  # Get all users.
10
13
  #
@@ -26,7 +29,7 @@ module Wavefront
26
29
  end
27
30
 
28
31
  # DELETE /api/v2/user/id
29
- # Delete a specific user.
32
+ # Delete a specific user. See also #delete_users.
30
33
  #
31
34
  # @param id [String] ID of the user
32
35
  # @return [Wavefront::Response]
@@ -47,23 +50,72 @@ module Wavefront
47
50
  api.get(id)
48
51
  end
49
52
 
53
+ # PUT /api/v2/user/id
54
+ # Update a specific user definition.
55
+ #
56
+ # @param id [String] a Wavefront user ID
57
+ # @param body [Hash] key-value hash of the parameters you wish
58
+ # to change
59
+ # @param modify [true, false] if true, use {#describe()} to get
60
+ # a hash describing the existing object, and modify that with
61
+ # the new body. If false, pass the new body straight through.
62
+ # @return [Wavefront::Response]
63
+
64
+ def update(id, body, modify = true)
65
+ wf_user_id?(id)
66
+ raise ArgumentError unless body.is_a?(Hash)
67
+
68
+ return api.put(id, body, 'application/json') unless modify
69
+
70
+ api.put(id, hash_for_update(describe(id).response, body),
71
+ 'application/json')
72
+ end
73
+
74
+ # POST /api/v2/user/{id}/addUserGroups
75
+ # Adds specific user groups to the user
76
+ #
77
+ # @param id [String] ID of the user
78
+ # @param group_list [Array[String]] list of groups to add
79
+ # @return [Wavefront::Response]
80
+ #
81
+ def add_groups_to_user(id, group_list = [])
82
+ wf_user_id?(id)
83
+ validate_usergroup_list(group_list)
84
+ api.post([id, 'addUserGroups'].uri_concat, group_list,
85
+ 'application/json')
86
+ end
87
+
88
+ # POST /api/v2/user/{id}/removeUserGroups
89
+ # Removes specific user groups from the user
90
+ # @param id [String] ID of the user
91
+ # @param group_list [Array[String]] list of groups to remove
92
+ # @return [Wavefront::Response]
93
+ #
94
+ def remove_groups_from_user(id, group_list = [])
95
+ wf_user_id?(id)
96
+ validate_usergroup_list(group_list)
97
+ api.post([id, 'removeUserGroups'].uri_concat, group_list,
98
+ 'application/json')
99
+ end
100
+
50
101
  # PUT /api/v2/user/id/grant
51
102
  # Grants a specific user permission.
52
103
  #
53
104
  # @param id [String] ID of the user
54
- # @param group [String] group to add user to. We do not validate
55
- # this so that changes to the API do not mandate changes to
56
- # the SDK. At the time of writing, valid values are browse,
105
+ # @param pgroup [String] permission group to grant to user. We
106
+ # do not validate this so that changes to the API do not mandate
107
+ # changes to the SDK. At the time of writing, valid values are
108
+ # browse,
57
109
  # agent_management, alerts_management, dashboard_management,
58
110
  # embedded_charts, events_management,
59
111
  # external_links_management, host_tag_management,
60
112
  # metrics_management, user_management,
61
113
  # @return [Wavefront::Response]
62
114
  #
63
- def grant(id, group)
115
+ def grant(id, pgroup)
64
116
  wf_user_id?(id)
65
- raise ArgumentError unless group.is_a?(String)
66
- api.post([id, 'grant'].uri_concat, "group=#{group}",
117
+ raise ArgumentError unless pgroup.is_a?(String)
118
+ api.post([id, 'grant'].uri_concat, "group=#{pgroup}",
67
119
  'application/x-www-form-urlencoded')
68
120
  end
69
121
 
@@ -71,33 +123,91 @@ module Wavefront
71
123
  # Revokes a specific user permission.
72
124
  #
73
125
  # @param id [String] ID of the user
74
- # @param group [String] group to add user to. We do not validate
75
- # this so that changes to the API do not mandate changes to
76
- # the SDK. See #update for valid values.
126
+ # @param pgroup [String] permission group to revoke from the
127
+ # user. We do not validate this so that changes to the API do
128
+ # not mandate changes to the SDK. See #update for valid values.
77
129
  # @return [Wavefront::Response]
78
130
  #
79
- def revoke(id, group)
131
+ def revoke(id, pgroup)
80
132
  wf_user_id?(id)
81
- raise ArgumentError unless group.is_a?(String)
82
- api.post([id, 'revoke'].uri_concat, "group=#{group}",
133
+ raise ArgumentError unless pgroup.is_a?(String)
134
+ api.post([id, 'revoke'].uri_concat, "group=#{pgroup}",
83
135
  'application/x-www-form-urlencoded')
84
136
  end
85
137
 
138
+ # POST /api/v2/user/deleteUsers
139
+ # Deletes multiple users
140
+ #
141
+ # Yep, a POST that DELETEs. Not to be confused with DELETE. I
142
+ # don't make the API, I just cover it.
143
+ # @param user_list [Array[String]] list of user IDs
144
+ # @return [Wavefront::Response]
145
+ #
146
+ def delete_users(user_list)
147
+ raise ArgumentError unless user_list.is_a?(Array)
148
+ validate_user_list(user_list)
149
+ api.post('deleteUsers', user_list, 'application/json')
150
+ end
151
+
152
+ # POST /api/v2/user/grant/{permission}
153
+ # Grants a specific user permission to multiple users
154
+ # See #grant for possible permissions. This method operates on
155
+ # multiple users.
156
+ # @param permission [String] permission to grant
157
+ # @param user_list [Array[String]] users who should receive the
158
+ # permission
159
+ # @return [Wavefront::Response]
160
+ #
161
+ def grant_permission(permission, user_list)
162
+ raise ArgumentError unless user_list.is_a?(Array)
163
+ validate_user_list(user_list)
164
+ api.post(['grant', permission].uri_concat, user_list,
165
+ 'application/json')
166
+ end
167
+
168
+ # POST /api/v2/user/revoke/{permission}
169
+ # Revokes a specific user permission from multiple users
170
+ # See #grant for possible permissions. This method operates on
171
+ # multiple users.
172
+ # @param permission [String] permission to revoke
173
+ # @param user_list [Array[String]] users who should lose the
174
+ # permission
175
+ # @return [Wavefront::Response]
176
+ #
177
+ def revoke_permission(permission, user_list)
178
+ raise ArgumentError unless user_list.is_a?(Array)
179
+ validate_user_list(user_list)
180
+ api.post(['revoke', permission].uri_concat, user_list,
181
+ 'application/json')
182
+ end
183
+
184
+ # POST /api/v2/user/invite
185
+ # Invite users with given user groups and permissions.
186
+ # @param body [Array[Hash]] array of hashes describing a user.
187
+ # See API docs for more details.
188
+ # @return [Wavefront::Response]
189
+ #
190
+ def invite(body)
191
+ raise ArgumentError unless body.is_a?(Array)
192
+ raise ArgumentError unless body.first.is_a?(Hash)
193
+ api.post('invite', body, 'application/json')
194
+ end
195
+
86
196
  # Fake a response which looks like we get from all the other
87
197
  # paths. I'm expecting the user response model to be made
88
198
  # consistent with others in the future.
89
199
  #
90
200
  def response_shim(body, status)
91
- items = JSON.parse(body)
201
+ items = JSON.parse(body, symbolize_names: true)
92
202
 
93
203
  { response: { items: items,
94
204
  offset: 0,
95
205
  limit: items.size,
96
206
  totalItems: items.size,
97
207
  modeItems: false },
98
- status: { result: status == 200 ? 'OK' : 'ERROR',
99
- message: '',
100
- code: status } }.to_json
208
+ status: { result: status == 200 ? 'OK' : 'ERROR',
209
+ message: extract_api_message(status, items),
210
+ code: status } }.to_json
101
211
  end
102
212
 
103
213
  # the user API class does not support pagination. Be up-front
@@ -106,5 +216,12 @@ module Wavefront
106
216
  def everything
107
217
  raise NoMethodError
108
218
  end
219
+
220
+ private
221
+
222
+ def extract_api_message(status, items)
223
+ return '' if status < 300
224
+ items.fetch(:message, 'no message from API')
225
+ end
109
226
  end
110
227
  end
@@ -0,0 +1,134 @@
1
+ require_relative 'core/api'
2
+ require_relative 'support/user_mixins'
3
+
4
+ module Wavefront
5
+ #
6
+ # Manage and query Wavefront user groups
7
+ #
8
+ class UserGroup < CoreApi
9
+ include Wavefront::UserMixins
10
+
11
+ def update_keys
12
+ %i[id name]
13
+ end
14
+
15
+ # GET /api/v2/usergroup
16
+ # Get all user groups for a customer
17
+ # @param offset [Int] alert at which the list begins
18
+ # @param limit [Int] the number of alerts to return
19
+ # @return [Wavefront::Response]
20
+ #
21
+ def list(offset = 0, limit = 100)
22
+ api.get('', offset: offset, limit: limit)
23
+ end
24
+
25
+ # POST /api/v2/usergroup
26
+ # Create a specific user group
27
+ #
28
+ # @param body [Hash] a hash of parameters describing the group.
29
+ # Please refer to the Wavefront Swagger docs for key:value
30
+ # information
31
+ # @return [Wavefront::Response]
32
+ #
33
+ def create(body)
34
+ raise ArgumentError unless body.is_a?(Hash)
35
+ api.post('', body, 'application/json')
36
+ end
37
+
38
+ # DELETE /api/v2/usergroup/{id}
39
+ # Delete a specific user group
40
+ #
41
+ # @param id [String] ID of the user group
42
+ # @return [Wavefront::Response]
43
+ #
44
+ def delete(id)
45
+ wf_usergroup_id?(id)
46
+ api.delete(id)
47
+ end
48
+
49
+ # GET /api/v2/usergroup/{id}
50
+ # Get a specific user group
51
+ #
52
+ # @param id [String] ID of the user group
53
+ # @return [Wavefront::Response]
54
+ #
55
+ def describe(id)
56
+ wf_usergroup_id?(id)
57
+ api.get(id)
58
+ end
59
+
60
+ # PUT /api/v2/usergroup/{id}
61
+ # Update a specific user group
62
+ #
63
+ # @param id [String] a Wavefront usergroup ID
64
+ # @param body [Hash] key-value hash of the parameters you wish
65
+ # to change
66
+ # @param modify [true, false] if true, use {#describe()} to get
67
+ # a hash describing the existing object, and modify that with
68
+ # the new body. If false, pass the new body straight through.
69
+ # @return [Wavefront::Response]
70
+
71
+ def update(id, body, modify = true)
72
+ wf_usergroup_id?(id)
73
+ raise ArgumentError unless body.is_a?(Hash)
74
+
75
+ return api.put(id, body, 'application/json') unless modify
76
+
77
+ api.put(id, hash_for_update(describe(id).response, body),
78
+ 'application/json')
79
+ end
80
+
81
+ # POST /api/v2/usergroup/{id}/addUsers
82
+ # Add multiple users to a specific user group
83
+ #
84
+ # @param id [String] ID of the user group
85
+ # @param user_list [Array[String]] list of users to add
86
+ # @return [Wavefront::Response]
87
+ #
88
+ def add_users_to_group(id, user_list = [])
89
+ wf_usergroup_id?(id)
90
+ validate_user_list(user_list)
91
+ api.post([id, 'addUsers'].uri_concat, user_list, 'application/json')
92
+ end
93
+
94
+ # POST /api/v2/usergroup/{id}/removeUsers
95
+ # Remove multiple users from a specific user group
96
+ #
97
+ # @param id [String] ID of the user group
98
+ # @param user_list [Array[String]] list of users to remove
99
+ # @return [Wavefront::Response]
100
+ #
101
+ def remove_users_from_group(id, user_list = [])
102
+ wf_usergroup_id?(id)
103
+ validate_user_list(user_list)
104
+ api.post([id, 'removeUsers'].uri_concat, user_list,
105
+ 'application/json')
106
+ end
107
+
108
+ # POST /api/v2/usergroup/grant/{permission}
109
+ # Grants a single permission to user group(s)
110
+ #
111
+ # @param perm [String] permission to grant
112
+ # @param group_list [Array[String]] list of groups who should
113
+ # receive permission
114
+ # @return [Wavefront::Response]
115
+ #
116
+ def grant(perm, group_list = [])
117
+ validate_usergroup_list(group_list)
118
+ api.post(['grant', perm].uri_concat, group_list, 'application/json')
119
+ end
120
+
121
+ # POST /api/v2/usergroup/revoke/{permission}
122
+ # Revokes a single permission from user group(s)
123
+ #
124
+ # @param perm [String] permission to revoke
125
+ # @param group_list [Array[String]] list of groups who should
126
+ # lose permission
127
+ # @return [Wavefront::Response]
128
+ #
129
+ def revoke(perm, group_list = [])
130
+ validate_usergroup_list(group_list)
131
+ api.post(['revoke', perm].uri_concat, group_list, 'application/json')
132
+ end
133
+ end
134
+ end
@@ -12,6 +12,16 @@ module Wavefront
12
12
  #
13
13
  # rubocop:disable Metrics/ModuleLength
14
14
  module Validators
15
+ # Is the given string a UUID? These are used for various item
16
+ # IDs.
17
+ #
18
+ # @param id [String]
19
+ # @return [Bool]
20
+ #
21
+ def uuid?(str)
22
+ str.is_a?(String) && str =~ /([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12})/
23
+ end
24
+
15
25
  # Ensure the given argument is a valid external link template
16
26
  #
17
27
  # @return true if it is valid
@@ -192,12 +202,7 @@ module Wavefront
192
202
  # is not valid
193
203
  #
194
204
  def wf_proxy_id?(id)
195
- if id.is_a?(String) && id.match(
196
- /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
197
- )
198
- return true
199
- end
200
-
205
+ return true if uuid?(id)
201
206
  raise Wavefront::Exception::InvalidProxyId
202
207
  end
203
208
 
@@ -225,12 +230,7 @@ module Wavefront
225
230
  # integration ID is not valid
226
231
  #
227
232
  def wf_cloudintegration_id?(id)
228
- if id.is_a?(String) && id.match(
229
- /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
230
- )
231
- return true
232
- end
233
-
233
+ return true if uuid?(id)
234
234
  raise Wavefront::Exception::InvalidCloudIntegrationId
235
235
  end
236
236
 
@@ -394,6 +394,17 @@ module Wavefront
394
394
  raise Wavefront::Exception::InvalidUserId
395
395
  end
396
396
 
397
+ # Ensure the given argument is a valid user group.
398
+ #
399
+ # @param gid [String] user group identiier
400
+ # @return true if valid
401
+ # @raise Wavefront::Exceptions::InvalidUserGroupId if not valid
402
+ #
403
+ def wf_usergroup_id?(gid)
404
+ return true if uuid?(gid)
405
+ raise Wavefront::Exception::InvalidUserGroupId
406
+ end
407
+
397
408
  # Ensure the given argument is a valid webhook ID.
398
409
  #
399
410
  # @param id [String] webhook ID
@@ -12,7 +12,7 @@ DERIVED_METRIC_BODY = {
12
12
  processRateMinutes: 1
13
13
  }.freeze
14
14
 
15
- # Unit tests for dashboard class
15
+ # Unit tests for derived metric class
16
16
  #
17
17
  class WavefrontDerivedMetricTest < WavefrontTestBase
18
18
  def test_list
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../../spec_helper'
4
+ require_relative '../../../lib/wavefront-sdk/support/user_mixins'
5
+ require_relative '../../../lib/wavefront-sdk/validators'
6
+ require_relative '../../../lib/wavefront-sdk/core/exception'
7
+
8
+ # Test user mixins
9
+ #
10
+ class WavefrontUserMixinsTest < MiniTest::Test
11
+ include Wavefront::UserMixins
12
+ include Wavefront::Validators
13
+
14
+ def test_validate_user_list
15
+ assert validate_user_list(%w[u1@d1.net u2@d2.org u3@d3.com])
16
+
17
+ assert_raises(Wavefront::Exception::InvalidUserId) do
18
+ validate_user_list(%w[u1d1.net u2@d2.org u3@d3.com])
19
+ end
20
+
21
+ assert_raises(Wavefront::Exception::InvalidUserId) do
22
+ validate_user_list(%w[u1@d1.net org u3@d3.com])
23
+ end
24
+
25
+ assert_raises(ArgumentError) { validate_user_list('u1@d1.net') }
26
+ end
27
+
28
+ def validate_usergroup_list
29
+ assert_raises(Wavefront::Exception::InvalidUserGroupId) do
30
+ validate_user_list(%w[f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672
31
+ bad_id
32
+ 2659191e-aad4-4302-a94e-9667e1517127])
33
+ end
34
+
35
+ assert_raises(ArgumentError) do
36
+ validate_usergroup_list('f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672')
37
+ end
38
+ end
39
+ end
@@ -3,12 +3,14 @@
3
3
  require_relative '../spec_helper'
4
4
 
5
5
  USER = 'user@example.com'.freeze
6
- GROUP = 'agent_management'.freeze
6
+ PERMISSION = 'agent_management'.freeze
7
7
 
8
- USER_BODY = {
9
- emailAddress: USER,
10
- groups: %w[browse]
11
- }.freeze
8
+ USER_BODY = { emailAddress: USER, groups: %w[browse] }.freeze
9
+
10
+ USERGROUP_LIST = %w[f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672
11
+ 2659191e-aad4-4302-a94e-9667e1517127].freeze
12
+
13
+ USER_LIST = %w[user@example.com other@elsewhere.com].freeze
12
14
 
13
15
  # Unit tests for User class
14
16
  #
@@ -17,12 +19,6 @@ class WavefrontUserTest < WavefrontTestBase
17
19
  should_work(:list, nil, '')
18
20
  end
19
21
 
20
- def test_describe
21
- should_work(:describe, USER, USER)
22
- should_be_invalid(:describe, 'abcdefg')
23
- assert_raises(ArgumentError) { wf.describe }
24
- end
25
-
26
22
  def test_create
27
23
  should_work(:create, [USER_BODY, true], '?sendEmail=true', :post,
28
24
  JSON_POST_HEADERS, USER_BODY.to_json)
@@ -36,23 +32,91 @@ class WavefrontUserTest < WavefrontTestBase
36
32
  assert_raises(ArgumentError) { wf.delete }
37
33
  end
38
34
 
35
+ def test_describe
36
+ should_work(:describe, USER, USER)
37
+ should_be_invalid(:describe, 'abcdefg')
38
+ assert_raises(ArgumentError) { wf.describe }
39
+ end
40
+
41
+ def test_update
42
+ should_work(:update, [USER, USER_BODY, false], USER, :put,
43
+ JSON_POST_HEADERS, USER_BODY.to_json)
44
+ should_be_invalid(:update, ['!invalid uid!', USER_BODY])
45
+ assert_raises(ArgumentError) { wf.update }
46
+ end
47
+
48
+ def test_add_groups_to_user
49
+ should_work(:add_groups_to_user, [USER, USERGROUP_LIST],
50
+ [USER, :addUserGroups].uri_concat, :post,
51
+ JSON_POST_HEADERS, USERGROUP_LIST.to_json)
52
+
53
+ assert_raises(Wavefront::Exception::InvalidUserId) do
54
+ wf.add_groups_to_user('invalid address', USERGROUP_LIST)
55
+ end
56
+ end
57
+
58
+ def test_remove_groups_from_user
59
+ should_work(:remove_groups_from_user, [USER, USERGROUP_LIST],
60
+ [USER, :removeUserGroups].uri_concat, :post,
61
+ JSON_POST_HEADERS, USERGROUP_LIST.to_json)
62
+
63
+ assert_raises(Wavefront::Exception::InvalidUserId) do
64
+ wf.remove_groups_from_user('invalid address', USERGROUP_LIST)
65
+ end
66
+ end
67
+
39
68
  def test_grant
40
- should_work(:grant, [USER, GROUP], 'user%40example.com/grant',
69
+ should_work(:grant, [USER, PERMISSION], 'user%40example.com/grant',
41
70
  :post, JSON_POST_HEADERS.merge(
42
71
  'Content-Type': 'application/x-www-form-urlencoded'
43
72
  ),
44
- "group=#{GROUP}")
45
- should_be_invalid(:grant, ['abcde', GROUP])
73
+ "group=#{PERMISSION}")
74
+ should_be_invalid(:grant, ['abcde', PERMISSION])
46
75
  assert_raises(ArgumentError) { wf.grant }
47
76
  end
48
77
 
49
78
  def test_revoke
50
- should_work(:revoke, [USER, GROUP], 'user%40example.com/revoke',
79
+ should_work(:revoke, [USER, PERMISSION], 'user%40example.com/revoke',
51
80
  :post, JSON_POST_HEADERS.merge(
52
81
  'Content-Type': 'application/x-www-form-urlencoded'
53
82
  ),
54
- "group=#{GROUP}")
55
- should_be_invalid(:revoke, ['abcde', GROUP])
83
+ "group=#{PERMISSION}")
84
+ should_be_invalid(:revoke, ['abcde', PERMISSION])
56
85
  assert_raises(ArgumentError) { wf.revoke }
57
86
  end
87
+
88
+ def test_delete_users
89
+ should_work(:delete_users, [[USER, 'other@example.com']],
90
+ 'deleteUsers', :post, JSON_POST_HEADERS,
91
+ [USER, 'other@example.com'].to_json)
92
+
93
+ assert_raises(Wavefront::Exception::InvalidUserId) do
94
+ wf.delete_users(['invalid address'])
95
+ end
96
+
97
+ assert_raises(ArgumentError) { wf.delete_users('a@b.com') }
98
+ end
99
+
100
+ def test_grant_permission
101
+ should_work(:grant_permission, [PERMISSION, USER_LIST],
102
+ [:grant, PERMISSION].uri_concat, :post,
103
+ JSON_POST_HEADERS, USER_LIST.to_json)
104
+ should_be_invalid(:grant, ['abcde', PERMISSION])
105
+ assert_raises(ArgumentError) { wf.grant }
106
+ end
107
+
108
+ def test_revoke_permission
109
+ should_work(:revoke_permission, [PERMISSION, USER_LIST],
110
+ [:revoke, PERMISSION].uri_concat, :post,
111
+ JSON_POST_HEADERS, USER_LIST.to_json)
112
+ should_be_invalid(:revoke, ['abcde', PERMISSION])
113
+ assert_raises(ArgumentError) { wf.grant }
114
+ end
115
+
116
+ def test_invite
117
+ should_work(:invite, [[USER_BODY]], 'invite', :post,
118
+ JSON_POST_HEADERS, [USER_BODY].to_json)
119
+ assert_raises(ArgumentError) { wf.invite }
120
+ assert_raises(ArgumentError) { wf.invite('test') }
121
+ end
58
122
  end
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ USERGROUP_ID = 'f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672'.freeze
6
+ BAD_USERGROUP_ID = 'some_rubbish'.freeze
7
+
8
+ USERGROUP_BODY = { name: 'test group',
9
+ permissions: %w[alerts_management
10
+ dashboard_management
11
+ events_management] }.freeze
12
+
13
+ USER_LIST = %w[someone@somewhere.com other@elsewhere.net].freeze
14
+ BAD_USER_LIST = %w[badusername very!bad!username].freeze
15
+
16
+ PERMISSION = 'alerts_management'.freeze
17
+
18
+ GROUP_LIST = %w[f8dc0c14-91a0-4ca9-8a2a-7d47f4db4672
19
+ 2659191e-aad4-4302-a94e-9667e1517127].freeze
20
+
21
+ BAD_GROUP_LIST = %w[some-nonsense more-nonsense].freeze
22
+
23
+ # Unit tests for WavefrontUserGroup
24
+ #
25
+ class WavefrontUserGroupTest < WavefrontTestBase
26
+ def test_list
27
+ should_work(:list, 10, '?offset=10&limit=100')
28
+ end
29
+
30
+ def test_create
31
+ should_work(:create, USERGROUP_BODY, '', :post,
32
+ JSON_POST_HEADERS, USERGROUP_BODY.to_json)
33
+ assert_raises(ArgumentError) { wf.create }
34
+ assert_raises(ArgumentError) { wf.create('test') }
35
+ end
36
+
37
+ def test_delete
38
+ should_work(:delete, USERGROUP_ID, USERGROUP_ID, :delete)
39
+ should_be_invalid(:delete)
40
+ end
41
+
42
+ def test_describe
43
+ should_work(:describe, USERGROUP_ID, USERGROUP_ID)
44
+ assert_raises(ArgumentError) { wf.describe }
45
+ end
46
+
47
+ def test_update
48
+ should_work(:update, [USERGROUP_ID, USERGROUP_BODY, false],
49
+ USERGROUP_ID, :put, JSON_POST_HEADERS,
50
+ USERGROUP_BODY.to_json)
51
+ should_be_invalid(:update, ['!some rubbish!', USERGROUP_BODY])
52
+ assert_raises(ArgumentError) { wf.update }
53
+ end
54
+
55
+ def test_add_users_to_group
56
+ should_work(:add_users_to_group, [USERGROUP_ID, USER_LIST],
57
+ [USERGROUP_ID, :addUsers].uri_concat, :post)
58
+
59
+ assert_raises(Wavefront::Exception::InvalidUserId) do
60
+ wf.add_users_to_group(USERGROUP_ID, BAD_USER_LIST)
61
+ end
62
+
63
+ assert_raises(Wavefront::Exception::InvalidUserGroupId) do
64
+ wf.add_users_to_group(BAD_USERGROUP_ID, USER_LIST)
65
+ end
66
+ end
67
+
68
+ def test_remove_users_from_group
69
+ should_work(:remove_users_from_group, [USERGROUP_ID, USER_LIST],
70
+ [USERGROUP_ID, :removeUsers].uri_concat, :post)
71
+
72
+ assert_raises(Wavefront::Exception::InvalidUserId) do
73
+ wf.remove_users_from_group(USERGROUP_ID, BAD_USER_LIST)
74
+ end
75
+
76
+ assert_raises(Wavefront::Exception::InvalidUserGroupId) do
77
+ wf.remove_users_from_group(BAD_USERGROUP_ID, USER_LIST)
78
+ end
79
+ end
80
+
81
+ def test_grant
82
+ should_work(:grant, [PERMISSION, GROUP_LIST],
83
+ [:grant, PERMISSION].uri_concat, :post)
84
+
85
+ assert_raises(Wavefront::Exception::InvalidUserGroupId) do
86
+ wf.grant(PERMISSION, BAD_GROUP_LIST)
87
+ end
88
+ end
89
+
90
+ def test_revoke
91
+ should_work(:revoke, [PERMISSION, GROUP_LIST],
92
+ [:revoke, PERMISSION].uri_concat, :post)
93
+
94
+ assert_raises(Wavefront::Exception::InvalidUserGroupId) do
95
+ wf.revoke(PERMISSION, BAD_GROUP_LIST)
96
+ end
97
+ end
98
+ end
@@ -207,6 +207,14 @@ class WavefrontValidatorsTest < MiniTest::Test
207
207
  good_and_bad('wf_user_id?', 'InvalidUserId', good, bad)
208
208
  end
209
209
 
210
+ def test_wf_usergroup_id?
211
+ good = %w[2f17beb4-51b1-4362-b19f-098e3e4ab44d
212
+ 42622766-52c2-4a8b-8070-b6f4623028c1]
213
+ bad = %w[word Name 42622766-52c2-4a8b-8070-b6f4623028c
214
+ z2622766-52c2-4a8b-8070-b6f4623028c1]
215
+ good_and_bad('wf_usergroup_id?', 'InvalidUserGroupId', good, bad)
216
+ end
217
+
210
218
  def test_wf_webhook_id?
211
219
  good = %w[4OfsEM8RcvkM7nwG]
212
220
  bad = %w[4OfsEM8RcvkM7n 4OfsEM8Rcvk-7nw]
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: 2.4.0
4
+ version: 2.5.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: 2019-01-28 00:00:00.000000000 Z
11
+ date: 2019-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -230,8 +230,10 @@ files:
230
230
  - lib/wavefront-sdk/stdlib/string.rb
231
231
  - lib/wavefront-sdk/support/mixins.rb
232
232
  - lib/wavefront-sdk/support/parse_time.rb
233
+ - lib/wavefront-sdk/support/user_mixins.rb
233
234
  - lib/wavefront-sdk/types/status.rb
234
235
  - lib/wavefront-sdk/user.rb
236
+ - lib/wavefront-sdk/usergroup.rb
235
237
  - lib/wavefront-sdk/validators.rb
236
238
  - lib/wavefront-sdk/webhook.rb
237
239
  - lib/wavefront-sdk/write.rb
@@ -278,7 +280,9 @@ files:
278
280
  - spec/wavefront-sdk/stdlib/string_spec.rb
279
281
  - spec/wavefront-sdk/support/mixins_spec.rb
280
282
  - spec/wavefront-sdk/support/parse_time_spec.rb
283
+ - spec/wavefront-sdk/support/user_mixins_spec.rb
281
284
  - spec/wavefront-sdk/user_spec.rb
285
+ - spec/wavefront-sdk/usergroup_spec.rb
282
286
  - spec/wavefront-sdk/validators_spec.rb
283
287
  - spec/wavefront-sdk/webhook_spec.rb
284
288
  - spec/wavefront-sdk/write_spec.rb
@@ -348,7 +352,9 @@ test_files:
348
352
  - spec/wavefront-sdk/stdlib/string_spec.rb
349
353
  - spec/wavefront-sdk/support/mixins_spec.rb
350
354
  - spec/wavefront-sdk/support/parse_time_spec.rb
355
+ - spec/wavefront-sdk/support/user_mixins_spec.rb
351
356
  - spec/wavefront-sdk/user_spec.rb
357
+ - spec/wavefront-sdk/usergroup_spec.rb
352
358
  - spec/wavefront-sdk/validators_spec.rb
353
359
  - spec/wavefront-sdk/webhook_spec.rb
354
360
  - spec/wavefront-sdk/write_spec.rb