19
19
20
20
import os
21
21
import pytest
22
+ import random
22
23
from ibm_cloud_sdk_core import ApiException , read_external_sources
23
24
from ibm_platform_services .iam_policy_management_v1 import *
24
25
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
+
25
41
# Config file name
26
- config_file = 'iam_policy_management_v1 .env'
42
+ config_file = 'iam_policy_management .env'
27
43
28
44
iam_policy_management_service = None
29
45
30
46
config = None
31
47
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"
32
55
33
56
##############################################################################
34
57
# Start of Examples for Service: IamPolicyManagementV1
@@ -54,8 +77,9 @@ def setup_class(cls):
54
77
assert iam_policy_management_service is not None
55
78
56
79
# Load the configuration
57
- global config
80
+ global config , test_account_id
58
81
config = read_external_sources (IamPolicyManagementV1 .DEFAULT_SERVICE_NAME )
82
+ test_account_id = config ['TEST_ACCOUNT_ID' ]
59
83
60
84
print ('Setup complete.' )
61
85
@@ -64,52 +88,61 @@ def setup_class(cls):
64
88
)
65
89
66
90
@needscredentials
67
- def test_list_policies_example (self ):
91
+ def test_create_policy_example (self ):
68
92
"""
69
- list_policies request example
93
+ create_policy request example
70
94
"""
71
95
try :
72
- # begin-list_policies
96
+ global test_policy_id
97
+ # begin-create_policy
73
98
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 ]
76
118
).get_result ()
77
119
78
- print (json . dumps ( policy_list , indent = 2 ) )
120
+ print (policy )
79
121
80
- # end-list_policies
122
+ # end-create_policy
123
+ test_policy_id = policy ['id' ]
81
124
82
125
except ApiException as e :
83
126
pytest .fail (str (e ))
84
127
85
128
@needscredentials
86
- def test_create_policy_example (self ):
129
+ def test_get_policy_example (self ):
87
130
"""
88
- create_policy request example
131
+ get_policy request example
89
132
"""
90
133
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
102
136
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 ()
109
141
110
- print (json . dumps ( policy , indent = 2 ) )
142
+ print (policy )
111
143
112
- # end-create_policy
144
+ # end-get_policy
145
+ test_policy_etag = response .get_headers ().get ("Etag" )
113
146
114
147
except ApiException as e :
115
148
pytest .fail (str (e ))
@@ -122,64 +155,70 @@ def test_update_policy_example(self):
122
155
try :
123
156
# begin-update_policy
124
157
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 ])
134
171
135
172
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 ]
142
179
).get_result ()
143
180
144
- print (json . dumps ( policy , indent = 2 ) )
181
+ print (policy )
145
182
146
183
# end-update_policy
147
184
148
185
except ApiException as e :
149
186
pytest .fail (str (e ))
150
187
151
188
@needscredentials
152
- def test_get_policy_example (self ):
189
+ def test_list_policies_example (self ):
153
190
"""
154
- get_policy request example
191
+ list_policies request example
155
192
"""
156
193
try :
157
- # begin-get_policy
194
+ # begin-list_policies
158
195
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 '
161
198
).get_result ()
162
199
163
- print (json . dumps ( policy , indent = 2 ) )
200
+ print (policy_list )
164
201
165
- # end-get_policy
202
+ # end-list_policies
166
203
167
204
except ApiException as e :
168
205
pytest .fail (str (e ))
169
206
170
207
@needscredentials
171
- def test_list_roles_example (self ):
208
+ def test_delete_policy_example (self ):
172
209
"""
173
- list_roles request example
210
+ delete_policy request example
174
211
"""
175
212
try :
176
- # begin-list_roles
213
+ # begin-delete_policy
177
214
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
+ )
179
218
180
- print (json . dumps ( role_list , indent = 2 ) )
219
+ print (response )
181
220
182
- # end-list_roles
221
+ # end-delete_policy
183
222
184
223
except ApiException as e :
185
224
pytest .fail (str (e ))
@@ -190,96 +229,103 @@ def test_create_role_example(self):
190
229
create_role request example
191
230
"""
192
231
try :
232
+ global test_custom_role_id
193
233
# begin-create_role
194
234
195
235
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
201
241
).get_result ()
202
242
203
- print (json . dumps ( custom_role , indent = 2 ) )
243
+ print (custom_role )
204
244
205
245
# end-create_role
246
+ test_custom_role_id = custom_role ["id" ]
206
247
207
248
except ApiException as e :
208
249
pytest .fail (str (e ))
209
250
210
251
@needscredentials
211
- def test_update_role_example (self ):
252
+ def test_get_role_example (self ):
212
253
"""
213
- update_role request example
254
+ get_role request example
214
255
"""
215
256
try :
216
- # begin-update_role
257
+ global test_custom_role_etag
258
+ # begin-get_role
217
259
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 ()
222
264
223
- print (json . dumps ( custom_role , indent = 2 ) )
265
+ print (custom_role )
224
266
225
- # end-update_role
267
+ # end-get_role
268
+ test_custom_role_etag = response .get_headers ().get ("Etag" )
226
269
227
270
except ApiException as e :
228
271
pytest .fail (str (e ))
229
272
230
273
@needscredentials
231
- def test_get_role_example (self ):
274
+ def test_update_role_example (self ):
232
275
"""
233
- get_role request example
276
+ update_role request example
234
277
"""
235
278
try :
236
- # begin-get_role
279
+ # begin-update_role
237
280
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
240
286
).get_result ()
241
287
242
- print (json . dumps ( custom_role , indent = 2 ) )
288
+ print (custom_role )
243
289
244
- # end-get_role
290
+ # end-update_role
245
291
246
292
except ApiException as e :
247
293
pytest .fail (str (e ))
248
294
249
295
@needscredentials
250
- def test_delete_role_example (self ):
296
+ def test_list_roles_example (self ):
251
297
"""
252
- delete_role request example
298
+ list_roles request example
253
299
"""
254
300
try :
255
- # begin-delete_role
301
+ # begin-list_roles
256
302
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
259
305
).get_result ()
260
306
261
- print (json . dumps ( response , indent = 2 ) )
307
+ print (role_list )
262
308
263
- # end-delete_role
309
+ # end-list_roles
264
310
265
311
except ApiException as e :
266
312
pytest .fail (str (e ))
267
313
268
314
@needscredentials
269
- def test_delete_policy_example (self ):
315
+ def test_delete_role_example (self ):
270
316
"""
271
- delete_policy request example
317
+ delete_role request example
272
318
"""
273
319
try :
274
- # begin-delete_policy
320
+ # begin-delete_role
275
321
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
+ )
279
325
280
- print (json . dumps ( response , indent = 2 ) )
326
+ print (response )
281
327
282
- # end-delete_policy
328
+ # end-delete_role
283
329
284
330
except ApiException as e :
285
331
pytest .fail (str (e ))
0 commit comments