Skip to content

Commit a5c3f7c

Browse files
committed
fix(IAM Policy Management): fixed examples and integration tests
1 parent a889e68 commit a5c3f7c

File tree

2 files changed

+145
-97
lines changed

2 files changed

+145
-97
lines changed

examples/test_iam_policy_management_v1_examples.py

Lines changed: 142 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,39 @@
1919

2020
import os
2121
import pytest
22+
import random
2223
from ibm_cloud_sdk_core import ApiException, read_external_sources
2324
from ibm_platform_services.iam_policy_management_v1 import *
2425

26+
#
27+
# Below are examples on how to use IAM Policy Management service
28+
#
29+
# The following environment variables are assumed to be defined when running examples below:
30+
#
31+
# IAM_POLICY_MANAGEMENT_URL=https://iam.cloud.ibm.com
32+
# IAM_POLICY_MANAGEMENT_AUTH_TYPE=iam
33+
# IAM_POLICY_MANAGEMENT_AUTH_URL=https://iam.cloud.ibm.com/identity/token
34+
# IAM_POLICY_MANAGEMENT_APIKEY= <YOUR_APIKEY>
35+
# IAM_POLICY_MANAGEMENT_TEST_ACCOUNT_ID= <YOUR_ACCOUNT_ID>
36+
#
37+
# Alternatively, above environment variables can be placed in a "credentials" file and then:
38+
# export IBM_CREDENTIALS_FILE=<name of credentials file>
39+
#
40+
2541
# Config file name
26-
config_file = 'iam_policy_management_v1.env'
42+
config_file = 'iam_policy_management.env'
2743

2844
iam_policy_management_service = None
2945

3046
config = None
3147

48+
test_account_id = None
49+
test_policy_id = None
50+
test_policy_etag = None
51+
test_custom_role_id = None
52+
test_custom_role_etag = None
53+
test_user_id = "IBMid-SDKPython" + str(random.randint(0, 99999))
54+
test_service_name = "iam-groups"
3255

3356
##############################################################################
3457
# Start of Examples for Service: IamPolicyManagementV1
@@ -54,8 +77,9 @@ def setup_class(cls):
5477
assert iam_policy_management_service is not None
5578

5679
# Load the configuration
57-
global config
80+
global config, test_account_id
5881
config = read_external_sources(IamPolicyManagementV1.DEFAULT_SERVICE_NAME)
82+
test_account_id = config['TEST_ACCOUNT_ID']
5983

6084
print('Setup complete.')
6185

@@ -64,52 +88,61 @@ def setup_class(cls):
6488
)
6589

6690
@needscredentials
67-
def test_list_policies_example(self):
91+
def test_create_policy_example(self):
6892
"""
69-
list_policies request example
93+
create_policy request example
7094
"""
7195
try:
72-
# begin-list_policies
96+
global test_policy_id
97+
# begin-create_policy
7398

74-
policy_list = iam_policy_management_service.list_policies(
75-
account_id='testString'
99+
policy_subject = PolicySubject(
100+
attributes=[SubjectAttribute(name='iam_id', value=test_user_id)])
101+
policy_role = PolicyRole(
102+
role_id='crn:v1:bluemix:public:iam::::role:Viewer')
103+
resource_account_attribute = ResourceAttribute(
104+
name='accountId', value=test_account_id)
105+
resource_service_attribute = ResourceAttribute(
106+
name='serviceName', value=test_service_name)
107+
resource_tag = ResourceTag(name='project', value='prototype')
108+
policy_resource = PolicyResource(
109+
attributes=[resource_account_attribute,
110+
resource_service_attribute],
111+
tags=[resource_tag])
112+
113+
policy = iam_policy_management_service.create_policy(
114+
type='access',
115+
subjects=[policy_subject],
116+
roles=[policy_role],
117+
resources=[policy_resource]
76118
).get_result()
77119

78-
print(json.dumps(policy_list, indent=2))
120+
print(policy)
79121

80-
# end-list_policies
122+
# end-create_policy
123+
test_policy_id = policy['id']
81124

82125
except ApiException as e:
83126
pytest.fail(str(e))
84127

85128
@needscredentials
86-
def test_create_policy_example(self):
129+
def test_get_policy_example(self):
87130
"""
88-
create_policy request example
131+
get_policy request example
89132
"""
90133
try:
91-
# begin-create_policy
92-
93-
policy_subject_model = {
94-
}
95-
96-
policy_role_model = {
97-
'role_id': 'testString'
98-
}
99-
100-
policy_resource_model = {
101-
}
134+
global test_policy_etag
135+
# begin-get_policy
102136

103-
policy = iam_policy_management_service.create_policy(
104-
type='testString',
105-
subjects=[policy_subject_model],
106-
roles=[policy_role_model],
107-
resources=[policy_resource_model]
108-
).get_result()
137+
response = iam_policy_management_service.get_policy(
138+
policy_id=test_policy_id
139+
)
140+
policy = response.get_result()
109141

110-
print(json.dumps(policy, indent=2))
142+
print(policy)
111143

112-
# end-create_policy
144+
# end-get_policy
145+
test_policy_etag = response.get_headers().get("Etag")
113146

114147
except ApiException as e:
115148
pytest.fail(str(e))
@@ -122,64 +155,70 @@ def test_update_policy_example(self):
122155
try:
123156
# begin-update_policy
124157

125-
policy_subject_model = {
126-
}
127-
128-
policy_role_model = {
129-
'role_id': 'testString'
130-
}
131-
132-
policy_resource_model = {
133-
}
158+
policy_subject = PolicySubject(
159+
attributes=[SubjectAttribute(name='iam_id', value=test_user_id)])
160+
updated_policy_role = PolicyRole(
161+
role_id='crn:v1:bluemix:public:iam::::role:Editor')
162+
resource_account_attribute = ResourceAttribute(
163+
name='accountId', value=test_account_id)
164+
resource_service_attribute = ResourceAttribute(
165+
name='serviceName', value=test_service_name)
166+
resource_tag = ResourceTag(name='project', value='prototype')
167+
policy_resource = PolicyResource(
168+
attributes=[resource_account_attribute,
169+
resource_service_attribute],
170+
tags=[resource_tag])
134171

135172
policy = iam_policy_management_service.update_policy(
136-
policy_id='testString',
137-
if_match='testString',
138-
type='testString',
139-
subjects=[policy_subject_model],
140-
roles=[policy_role_model],
141-
resources=[policy_resource_model]
173+
type='access',
174+
policy_id=test_policy_id,
175+
if_match=test_policy_etag,
176+
subjects=[policy_subject],
177+
roles=[updated_policy_role],
178+
resources=[policy_resource]
142179
).get_result()
143180

144-
print(json.dumps(policy, indent=2))
181+
print(policy)
145182

146183
# end-update_policy
147184

148185
except ApiException as e:
149186
pytest.fail(str(e))
150187

151188
@needscredentials
152-
def test_get_policy_example(self):
189+
def test_list_policies_example(self):
153190
"""
154-
get_policy request example
191+
list_policies request example
155192
"""
156193
try:
157-
# begin-get_policy
194+
# begin-list_policies
158195

159-
policy = iam_policy_management_service.get_policy(
160-
policy_id='testString'
196+
policy_list = iam_policy_management_service.list_policies(
197+
account_id=test_account_id, iam_id=test_user_id, format='include_last_permit'
161198
).get_result()
162199

163-
print(json.dumps(policy, indent=2))
200+
print(policy_list)
164201

165-
# end-get_policy
202+
# end-list_policies
166203

167204
except ApiException as e:
168205
pytest.fail(str(e))
169206

170207
@needscredentials
171-
def test_list_roles_example(self):
208+
def test_delete_policy_example(self):
172209
"""
173-
list_roles request example
210+
delete_policy request example
174211
"""
175212
try:
176-
# begin-list_roles
213+
# begin-delete_policy
177214

178-
role_list = iam_policy_management_service.list_roles().get_result()
215+
response = iam_policy_management_service.delete_policy(
216+
policy_id=test_policy_id
217+
)
179218

180-
print(json.dumps(role_list, indent=2))
219+
print(response)
181220

182-
# end-list_roles
221+
# end-delete_policy
183222

184223
except ApiException as e:
185224
pytest.fail(str(e))
@@ -190,96 +229,103 @@ def test_create_role_example(self):
190229
create_role request example
191230
"""
192231
try:
232+
global test_custom_role_id
193233
# begin-create_role
194234

195235
custom_role = iam_policy_management_service.create_role(
196-
display_name='testString',
197-
actions=['testString'],
198-
name='testString',
199-
account_id='testString',
200-
service_name='testString'
236+
display_name='IAM Groups read access',
237+
actions=['iam-groups.groups.read'],
238+
name='ExampleRoleIAMGroups',
239+
account_id=test_account_id,
240+
service_name=test_service_name
201241
).get_result()
202242

203-
print(json.dumps(custom_role, indent=2))
243+
print(custom_role)
204244

205245
# end-create_role
246+
test_custom_role_id = custom_role["id"]
206247

207248
except ApiException as e:
208249
pytest.fail(str(e))
209250

210251
@needscredentials
211-
def test_update_role_example(self):
252+
def test_get_role_example(self):
212253
"""
213-
update_role request example
254+
get_role request example
214255
"""
215256
try:
216-
# begin-update_role
257+
global test_custom_role_etag
258+
# begin-get_role
217259

218-
custom_role = iam_policy_management_service.update_role(
219-
role_id='testString',
220-
if_match='testString',
221-
).get_result()
260+
response = iam_policy_management_service.get_role(
261+
role_id=test_custom_role_id
262+
)
263+
custom_role = response.get_result()
222264

223-
print(json.dumps(custom_role, indent=2))
265+
print(custom_role)
224266

225-
# end-update_role
267+
# end-get_role
268+
test_custom_role_etag = response.get_headers().get("Etag")
226269

227270
except ApiException as e:
228271
pytest.fail(str(e))
229272

230273
@needscredentials
231-
def test_get_role_example(self):
274+
def test_update_role_example(self):
232275
"""
233-
get_role request example
276+
update_role request example
234277
"""
235278
try:
236-
# begin-get_role
279+
# begin-update_role
237280

238-
custom_role = iam_policy_management_service.get_role(
239-
role_id='testString'
281+
updated_role_actions = ['iam-groups.groups.read', 'iam-groups.groups.list']
282+
custom_role = iam_policy_management_service.update_role(
283+
role_id=test_custom_role_id,
284+
if_match=test_custom_role_etag,
285+
actions=updated_role_actions
240286
).get_result()
241287

242-
print(json.dumps(custom_role, indent=2))
288+
print(custom_role)
243289

244-
# end-get_role
290+
# end-update_role
245291

246292
except ApiException as e:
247293
pytest.fail(str(e))
248294

249295
@needscredentials
250-
def test_delete_role_example(self):
296+
def test_list_roles_example(self):
251297
"""
252-
delete_role request example
298+
list_roles request example
253299
"""
254300
try:
255-
# begin-delete_role
301+
# begin-list_roles
256302

257-
response = iam_policy_management_service.delete_role(
258-
role_id='testString'
303+
role_list = iam_policy_management_service.list_roles(
304+
account_id=test_account_id
259305
).get_result()
260306

261-
print(json.dumps(response, indent=2))
307+
print(role_list)
262308

263-
# end-delete_role
309+
# end-list_roles
264310

265311
except ApiException as e:
266312
pytest.fail(str(e))
267313

268314
@needscredentials
269-
def test_delete_policy_example(self):
315+
def test_delete_role_example(self):
270316
"""
271-
delete_policy request example
317+
delete_role request example
272318
"""
273319
try:
274-
# begin-delete_policy
320+
# begin-delete_role
275321

276-
response = iam_policy_management_service.delete_policy(
277-
policy_id='testString'
278-
).get_result()
322+
response = iam_policy_management_service.delete_role(
323+
role_id=test_custom_role_id
324+
)
279325

280-
print(json.dumps(response, indent=2))
326+
print(response)
281327

282-
# end-delete_policy
328+
# end-delete_role
283329

284330
except ApiException as e:
285331
pytest.fail(str(e))

test/integration/test_iam_policy_management_v1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ def setUpClass(cls):
6363
cls.testPolicySubject = PolicySubject(attributes=
6464
[SubjectAttribute(name='iam_id', value=cls.testUserId)])
6565
cls.testPolicyRole = PolicyRole(role_id=cls.testViewerRoleCrn)
66+
resource_tag = ResourceTag(name='project', value='prototype',
67+
operator='stringEquals')
6668
cls.testPolicyResources = PolicyResource(attributes=
6769
[ResourceAttribute(name='accountId', value=cls.testAccountId,
6870
operator='stringEquals'),
6971
ResourceAttribute(name='serviceName', value=cls.testServiceName,
70-
operator='stringEquals')])
72+
operator='stringEquals')], tags=[resource_tag])
7173

7274
cls.testCustomRoleId = ""
7375
cls.testCustomRoleETag = ""

0 commit comments

Comments
 (0)