Skip to content

Commit 710d213

Browse files
authored
fix(User Management): add examples (#83)
1 parent a36f76e commit 710d213

File tree

2 files changed

+332
-10
lines changed

2 files changed

+332
-10
lines changed
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
# -*- coding: utf-8 -*-
2+
# (C) Copyright IBM Corp. 2021.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""
17+
Examples for UserManagementV1
18+
"""
19+
20+
import os
21+
import pytest
22+
from ibm_cloud_sdk_core import ApiException, read_external_sources
23+
from ibm_platform_services.user_management_v1 import *
24+
25+
#
26+
# This file provides an example of how to use the User Management service.
27+
#
28+
# The following configuration properties are assumed to be defined:
29+
#
30+
# USER_MANAGEMENT_URL=<service url>
31+
# USER_MANAGEMENT_AUTHTYPE=iam
32+
# USER_MANAGEMENT_AUTH_URL=<IAM token service URL - omit this if using the production environment>
33+
# USER_MANAGEMENT_APIKEY=<IAM apikey>
34+
# USER_MANAGEMENT_ACCOUNT_ID=<account ID>
35+
# USER_MANAGEMENT_USER_ID=<user ID>
36+
# USER_MANAGEMENT_MEMBER_EMAIL=<member email to invite>
37+
# USER_MANAGEMENT_VIEWER_ROLE_ID=<viewer role ID>
38+
# USER_MANAGEMENT_ACCESS_GROUP_ID=<access group ID>
39+
# # alternateService
40+
# USER_MANAGEMENT_ADMIN_URL=<service url>
41+
# USER_MANAGEMENT_ADMIN_AUTHTYPE=iam
42+
# USER_MANAGEMENT_ADMIN_AUTH_URL=<IAM token service URL - omit this if using the production environment>
43+
# USER_MANAGEMENT_ADMIN_APIKEY=<IAM apikey>
44+
#
45+
# These configuration properties can be exported as environment variables, or stored
46+
# in a configuration file and then:
47+
# export IBM_CREDENTIALS_FILE=<name of configuration file>
48+
#
49+
config_file = 'user_management.env'
50+
51+
user_management_service = None
52+
user_management_admin_service = None
53+
54+
55+
config = None
56+
57+
account_id = None
58+
user_id = None
59+
member_email = None
60+
viewer_role_id = None
61+
access_group_id = None
62+
63+
delete_user_id = None
64+
65+
##############################################################################
66+
# Start of Examples for Service: UserManagementV1
67+
##############################################################################
68+
# region
69+
class TestUserManagementV1Examples():
70+
"""
71+
Example Test Class for UserManagementV1
72+
"""
73+
74+
@classmethod
75+
def setup_class(cls):
76+
global user_management_service
77+
if os.path.exists(config_file):
78+
os.environ['IBM_CREDENTIALS_FILE'] = config_file
79+
80+
global user_management_admin_service
81+
82+
# begin-common
83+
84+
user_management_service = UserManagementV1.new_instance(
85+
service_name='USER_MANAGEMENT',
86+
)
87+
88+
user_management_admin_service = UserManagementV1.new_instance(
89+
service_name='USER_MANAGEMENT_ADMIN',
90+
)
91+
92+
# end-common
93+
assert user_management_service is not None
94+
95+
# Load the configuration
96+
global config
97+
config = read_external_sources(
98+
UserManagementV1.DEFAULT_SERVICE_NAME
99+
)
100+
101+
global account_id
102+
account_id = config['ACCOUNT_ID']
103+
104+
global user_id
105+
user_id = config['USER_ID']
106+
107+
global member_email
108+
member_email = config['MEMBER_EMAIL']
109+
110+
global viewer_role_id
111+
viewer_role_id = config['VIEWER_ROLE_ID']
112+
113+
global access_group_id
114+
access_group_id = config['ACCESS_GROUP_ID']
115+
116+
print('Setup complete.')
117+
118+
needscredentials = pytest.mark.skipif(
119+
not os.path.exists(config_file), reason="External configuration not available, skipping..."
120+
)
121+
122+
@needscredentials
123+
def test_invite_users_example(self):
124+
"""
125+
invite_users request example
126+
"""
127+
assert member_email is not None
128+
assert viewer_role_id is not None
129+
assert account_id is not None
130+
assert access_group_id is not None
131+
132+
try:
133+
# begin-invite_users
134+
135+
invite_user_model = {
136+
'email': member_email,
137+
'account_role': 'Member'
138+
}
139+
140+
role_model = {'role_id': viewer_role_id}
141+
142+
attribute_model = {'name': 'accountId', 'value': account_id}
143+
144+
attribute_model2 = {'name': 'resourceGroupId', 'value': '*'}
145+
146+
resource_model = {'attributes': [attribute_model, attribute_model2]}
147+
148+
invite_user_iam_policy_model = {
149+
'type': 'access',
150+
'roles': [role_model],
151+
'resources': [resource_model]
152+
}
153+
154+
invite_user_response = user_management_admin_service.invite_users(
155+
account_id=account_id,
156+
users=[invite_user_model],
157+
iam_policy=[invite_user_iam_policy_model],
158+
access_groups=[access_group_id]
159+
).get_result()
160+
161+
print(json.dumps(invite_user_response, indent=2))
162+
163+
# end-invite_users
164+
165+
global delete_user_id
166+
delete_user_id = invite_user_response.get('resources')[0].get('id')
167+
168+
except ApiException as e:
169+
pytest.fail(str(e))
170+
171+
@needscredentials
172+
def test_list_users_example(self):
173+
"""
174+
list_users request example
175+
"""
176+
assert account_id is not None
177+
178+
try:
179+
# begin-list_users
180+
181+
user_list = user_management_service.list_users(
182+
account_id=account_id,
183+
state='ACTIVE',
184+
limit=100,
185+
).get_result()
186+
187+
print(json.dumps(user_list, indent=2))
188+
189+
# end-list_users
190+
191+
except ApiException as e:
192+
pytest.fail(str(e))
193+
194+
@needscredentials
195+
def test_remove_user_example(self):
196+
"""
197+
remove_user request example
198+
"""
199+
assert account_id is not None
200+
assert delete_user_id is not None
201+
202+
try:
203+
# begin-remove_user
204+
205+
response = user_management_admin_service.remove_user(
206+
account_id=account_id,
207+
iam_id=delete_user_id,
208+
).get_result()
209+
210+
print(json.dumps(response, indent=2))
211+
212+
# end-remove_user
213+
214+
except ApiException as e:
215+
pytest.fail(str(e))
216+
217+
@needscredentials
218+
def test_get_user_profile_example(self):
219+
"""
220+
get_user_profile request example
221+
"""
222+
assert account_id is not None
223+
assert user_id is not None
224+
225+
try:
226+
# begin-get_user_profile
227+
228+
user_profile = user_management_service.get_user_profile(
229+
account_id=account_id,
230+
iam_id=user_id,
231+
).get_result()
232+
233+
print(json.dumps(user_profile, indent=2))
234+
235+
# end-get_user_profile
236+
237+
except ApiException as e:
238+
pytest.fail(str(e))
239+
240+
@needscredentials
241+
def test_update_user_profile_example(self):
242+
"""
243+
update_user_profile request example
244+
"""
245+
assert account_id is not None
246+
assert user_id is not None
247+
248+
try:
249+
# begin-update_user_profile
250+
251+
response = user_management_service.update_user_profile(
252+
account_id=account_id,
253+
iam_id=user_id,
254+
phonenumber='123456789',
255+
).get_result()
256+
257+
print(json.dumps(response, indent=2))
258+
259+
# end-update_user_profile
260+
261+
except ApiException as e:
262+
pytest.fail(str(e))
263+
264+
@needscredentials
265+
def test_get_user_settings_example(self):
266+
"""
267+
get_user_settings request example
268+
"""
269+
assert account_id is not None
270+
assert user_id is not None
271+
272+
try:
273+
# begin-get_user_settings
274+
275+
user_settings = user_management_service.get_user_settings(
276+
account_id=account_id,
277+
iam_id=user_id,
278+
).get_result()
279+
280+
print(json.dumps(user_settings, indent=2))
281+
282+
# end-get_user_settings
283+
284+
except ApiException as e:
285+
pytest.fail(str(e))
286+
287+
@needscredentials
288+
def test_update_user_settings_example(self):
289+
"""
290+
update_user_settings request example
291+
"""
292+
assert account_id is not None
293+
assert user_id is not None
294+
295+
try:
296+
# begin-update_user_settings
297+
298+
response = user_management_service.update_user_settings(
299+
account_id=account_id,
300+
iam_id=user_id,
301+
self_manage=True,
302+
allowed_ip_addresses='192.168.0.2,192.168.0.3',
303+
).get_result()
304+
305+
print(json.dumps(response, indent=2))
306+
307+
# end-update_user_settings
308+
309+
except ApiException as e:
310+
pytest.fail(str(e))
311+
312+
# endregion
313+
##############################################################################
314+
# End of Examples for Service: UserManagementV1
315+
##############################################################################

test/integration/test_user_management_v1.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import unittest
2222
import urllib.parse as urlparse
2323
from urllib.parse import parse_qs
24+
25+
from ibm_cloud_sdk_core.utils import read_external_sources
2426
from ibm_platform_services.user_management_v1 import *
2527

2628
# Read config file
@@ -41,19 +43,24 @@ def setup_class(cls):
4143
raise unittest.SkipTest(
4244
'External configuration not available, skipping...')
4345

46+
cls.config = read_external_sources(
47+
UserManagementV1.DEFAULT_SERVICE_NAME
48+
)
49+
assert cls.config is not None
50+
4451
cls.user_management_service = UserManagementV1.new_instance(
45-
service_name='USERMGMT1')
52+
service_name=UserManagementV1.DEFAULT_SERVICE_NAME)
4653
assert cls.user_management_service is not None
4754

48-
cls.alternate_user_management_service = UserManagementV1.new_instance(
49-
service_name='USERMGMT2')
50-
assert cls.alternate_user_management_service is not None
55+
cls.user_management_admin_service = UserManagementV1.new_instance(
56+
service_name='USER_MANAGEMENT_ADMIN')
57+
assert cls.user_management_admin_service is not None
5158

52-
cls.ACCOUNT_ID = '1aa434630b594b8a88b961a44c9eb2a9'
53-
cls.IAM_USERID = 'IBMid-550008BJPR'
54-
cls.INVITED_USER_EMAIL = '[email protected]'
55-
cls.VIEWER_ROLEID = 'crn:v1:bluemix:public:iam::::role:Viewer'
56-
cls.ACCESS_GROUP_ID = 'AccessGroupId-51675919-2bd7-4ce3-86e4-5faff8065574'
59+
cls.ACCOUNT_ID = cls.config['ACCOUNT_ID']
60+
cls.IAM_USERID = cls.config['USER_ID']
61+
cls.INVITED_USER_EMAIL = cls.config['MEMBER_EMAIL']
62+
cls.VIEWER_ROLEID = cls.config['VIEWER_ROLE_ID']
63+
cls.ACCESS_GROUP_ID = cls.config['ACCESS_GROUP_ID']
5764

5865
print('\nService URL: ', cls.user_management_service.service_url)
5966
print('Setup complete.')
@@ -133,7 +140,7 @@ def test_04_invite_users(self):
133140
'resources': [resource_model]
134141
}
135142

136-
response = self.alternate_user_management_service.invite_users(
143+
response = self.user_management_admin_service.invite_users(
137144
account_id=self.ACCOUNT_ID,
138145
users=[invite_user_model],
139146
iam_policy=[invite_user_iam_policy_model],

0 commit comments

Comments
 (0)