Skip to content

Commit 2539ef2

Browse files
committed
fix(IAM Policy Management): minor refactor to unify across all languages
1 parent a5c3f7c commit 2539ef2

File tree

1 file changed

+71
-72
lines changed

1 file changed

+71
-72
lines changed

examples/test_iam_policy_management_v1_examples.py

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import os
2121
import pytest
22-
import random
2322
from ibm_cloud_sdk_core import ApiException, read_external_sources
2423
from ibm_platform_services.iam_policy_management_v1 import *
2524

@@ -45,13 +44,13 @@
4544

4645
config = None
4746

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"
47+
example_account_id = None
48+
example_policy_id = None
49+
example_policy_etag = None
50+
example_custom_role_id = None
51+
example_custom_role_etag = None
52+
example_user_id = "IBMid-user1"
53+
example_service_name = "iam-groups"
5554

5655
##############################################################################
5756
# Start of Examples for Service: IamPolicyManagementV1
@@ -77,9 +76,9 @@ def setup_class(cls):
7776
assert iam_policy_management_service is not None
7877

7978
# Load the configuration
80-
global config, test_account_id
79+
global config, example_account_id
8180
config = read_external_sources(IamPolicyManagementV1.DEFAULT_SERVICE_NAME)
82-
test_account_id = config['TEST_ACCOUNT_ID']
81+
example_account_id = config['TEST_ACCOUNT_ID']
8382

8483
print('Setup complete.')
8584

@@ -93,34 +92,34 @@ def test_create_policy_example(self):
9392
create_policy request example
9493
"""
9594
try:
96-
global test_policy_id
95+
global example_policy_id
9796
# begin-create_policy
9897

99-
policy_subject = PolicySubject(
100-
attributes=[SubjectAttribute(name='iam_id', value=test_user_id)])
101-
policy_role = PolicyRole(
98+
policy_subjects = PolicySubject(
99+
attributes=[SubjectAttribute(name='iam_id', value=example_user_id)])
100+
policy_roles = PolicyRole(
102101
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])
102+
account_id_resource_attribute = ResourceAttribute(
103+
name='accountId', value=example_account_id)
104+
service_name_resource_attribute = ResourceAttribute(
105+
name='serviceName', value=example_service_name)
106+
policy_resource_tag = ResourceTag(name='project', value='prototype')
107+
policy_resources = PolicyResource(
108+
attributes=[account_id_resource_attribute,
109+
service_name_resource_attribute],
110+
tags=[policy_resource_tag])
112111

113112
policy = iam_policy_management_service.create_policy(
114113
type='access',
115-
subjects=[policy_subject],
116-
roles=[policy_role],
117-
resources=[policy_resource]
114+
subjects=[policy_subjects],
115+
roles=[policy_roles],
116+
resources=[policy_resources]
118117
).get_result()
119118

120-
print(policy)
119+
print(json.dumps(policy, indent=2))
121120

122121
# end-create_policy
123-
test_policy_id = policy['id']
122+
example_policy_id = policy['id']
124123

125124
except ApiException as e:
126125
pytest.fail(str(e))
@@ -131,18 +130,18 @@ def test_get_policy_example(self):
131130
get_policy request example
132131
"""
133132
try:
134-
global test_policy_etag
133+
global example_policy_etag
135134
# begin-get_policy
136135

137136
response = iam_policy_management_service.get_policy(
138-
policy_id=test_policy_id
137+
policy_id=example_policy_id
139138
)
140139
policy = response.get_result()
141140

142-
print(policy)
141+
print(json.dumps(policy, indent=2))
143142

144143
# end-get_policy
145-
test_policy_etag = response.get_headers().get("Etag")
144+
example_policy_etag = response.get_headers().get("Etag")
146145

147146
except ApiException as e:
148147
pytest.fail(str(e))
@@ -155,30 +154,30 @@ def test_update_policy_example(self):
155154
try:
156155
# begin-update_policy
157156

158-
policy_subject = PolicySubject(
159-
attributes=[SubjectAttribute(name='iam_id', value=test_user_id)])
160-
updated_policy_role = PolicyRole(
157+
policy_subjects = PolicySubject(
158+
attributes=[SubjectAttribute(name='iam_id', value=example_user_id)])
159+
account_id_resource_attribute = ResourceAttribute(
160+
name='accountId', value=example_account_id)
161+
service_name_resource_attribute = ResourceAttribute(
162+
name='serviceName', value=example_service_name)
163+
policy_resource_tag = ResourceTag(name='project', value='prototype')
164+
policy_resources = PolicyResource(
165+
attributes=[account_id_resource_attribute,
166+
service_name_resource_attribute],
167+
tags=[policy_resource_tag])
168+
updated_policy_roles = PolicyRole(
161169
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])
171170

172171
policy = iam_policy_management_service.update_policy(
173172
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]
173+
policy_id=example_policy_id,
174+
if_match=example_policy_etag,
175+
subjects=[policy_subjects],
176+
roles=[updated_policy_roles],
177+
resources=[policy_resources]
179178
).get_result()
180179

181-
print(policy)
180+
print(json.dumps(policy, indent=2))
182181

183182
# end-update_policy
184183

@@ -194,10 +193,10 @@ def test_list_policies_example(self):
194193
# begin-list_policies
195194

196195
policy_list = iam_policy_management_service.list_policies(
197-
account_id=test_account_id, iam_id=test_user_id, format='include_last_permit'
196+
account_id=example_account_id, iam_id=example_user_id, format='include_last_permit'
198197
).get_result()
199198

200-
print(policy_list)
199+
print(json.dumps(policy_list, indent=2))
201200

202201
# end-list_policies
203202

@@ -213,10 +212,10 @@ def test_delete_policy_example(self):
213212
# begin-delete_policy
214213

215214
response = iam_policy_management_service.delete_policy(
216-
policy_id=test_policy_id
217-
)
215+
policy_id=example_policy_id
216+
).get_result()
218217

219-
print(response)
218+
print(json.dumps(response, indent=2))
220219

221220
# end-delete_policy
222221

@@ -229,21 +228,21 @@ def test_create_role_example(self):
229228
create_role request example
230229
"""
231230
try:
232-
global test_custom_role_id
231+
global example_custom_role_id
233232
# begin-create_role
234233

235234
custom_role = iam_policy_management_service.create_role(
236235
display_name='IAM Groups read access',
237236
actions=['iam-groups.groups.read'],
238237
name='ExampleRoleIAMGroups',
239-
account_id=test_account_id,
240-
service_name=test_service_name
238+
account_id=example_account_id,
239+
service_name=example_service_name
241240
).get_result()
242241

243-
print(custom_role)
242+
print(json.dumps(custom_role, indent=2))
244243

245244
# end-create_role
246-
test_custom_role_id = custom_role["id"]
245+
example_custom_role_id = custom_role["id"]
247246

248247
except ApiException as e:
249248
pytest.fail(str(e))
@@ -254,18 +253,18 @@ def test_get_role_example(self):
254253
get_role request example
255254
"""
256255
try:
257-
global test_custom_role_etag
256+
global example_custom_role_etag
258257
# begin-get_role
259258

260259
response = iam_policy_management_service.get_role(
261-
role_id=test_custom_role_id
260+
role_id=example_custom_role_id
262261
)
263262
custom_role = response.get_result()
264263

265-
print(custom_role)
264+
print(json.dumps(custom_role, indent=2))
266265

267266
# end-get_role
268-
test_custom_role_etag = response.get_headers().get("Etag")
267+
example_custom_role_etag = response.get_headers().get("Etag")
269268

270269
except ApiException as e:
271270
pytest.fail(str(e))
@@ -280,12 +279,12 @@ def test_update_role_example(self):
280279

281280
updated_role_actions = ['iam-groups.groups.read', 'iam-groups.groups.list']
282281
custom_role = iam_policy_management_service.update_role(
283-
role_id=test_custom_role_id,
284-
if_match=test_custom_role_etag,
282+
role_id=example_custom_role_id,
283+
if_match=example_custom_role_etag,
285284
actions=updated_role_actions
286285
).get_result()
287286

288-
print(custom_role)
287+
print(json.dumps(custom_role, indent=2))
289288

290289
# end-update_role
291290

@@ -301,10 +300,10 @@ def test_list_roles_example(self):
301300
# begin-list_roles
302301

303302
role_list = iam_policy_management_service.list_roles(
304-
account_id=test_account_id
303+
account_id=example_account_id
305304
).get_result()
306305

307-
print(role_list)
306+
print(json.dumps(role_list, indent=2))
308307

309308
# end-list_roles
310309

@@ -320,10 +319,10 @@ def test_delete_role_example(self):
320319
# begin-delete_role
321320

322321
response = iam_policy_management_service.delete_role(
323-
role_id=test_custom_role_id
324-
)
322+
role_id=example_custom_role_id
323+
).get_result()
325324

326-
print(response)
325+
print(json.dumps(response, indent=2))
327326

328327
# end-delete_role
329328

0 commit comments

Comments
 (0)