13
13
14
14
15
15
class SageMakerDomainImporter :
16
-
17
16
def __init__ (self , region , stage , federation_role , account_id ) -> None :
18
17
self .region = region
19
18
self .stage = stage
@@ -171,7 +170,7 @@ def _map_users(self):
171
170
172
171
# get role name from arn "arn:aws:iam::047923724610:role/service-role/AmazonSageMaker-ExecutionRole-20241008T155288"
173
172
role_name = self .sm_user_info ["exec_role_arn" ][
174
- self .sm_user_info ["exec_role_arn" ].rfind ("/" ) + 1 :
173
+ self .sm_user_info ["exec_role_arn" ].rfind ("/" ) + 1 :
175
174
] # rfind searches from back of string
176
175
self .iam_client .tag_role (RoleName = role_name , Tags = sm_exec_role_tags )
177
176
print (
@@ -183,21 +182,38 @@ def _map_users(self):
183
182
print (t )
184
183
185
184
print ("--------------------------------------------------------------------" )
186
- print ("Getting DataZone UserProfiles in account..." )
187
- dz_users = self .dz_client .search_user_profiles (
188
- domainIdentifier = self .dz_domain_id , userType = "DATAZONE_IAM_USER"
189
- )["items" ]
185
+ print ("Getting IAM DataZone UserProfiles in account..." )
190
186
191
- dz_users_map_id_to_arn = {}
192
- for dz_user in dz_users :
193
- user_role_arn = dz_user ["details" ]["iam" ]["arn" ]
194
- dz_user_id = dz_user ["id" ]
195
- print (f"UserId: { dz_user_id } UserRole: { user_role_arn } " )
196
- dz_users_map_id_to_arn [dz_user_id ] = user_role_arn
187
+ user_types = [
188
+ "SSO_USER" ,
189
+ # remove DATAZONE_USER as these are redundant with others.
190
+ "DATAZONE_SSO_USER" ,
191
+ "DATAZONE_IAM_USER" ,
192
+ ]
193
+ all_dz_users = [] # [( payload, type ), ... ]
194
+ for user_type in user_types :
195
+ search_response = self .dz_client .search_user_profiles (
196
+ domainIdentifier = self .dz_domain_id , userType = user_type
197
+ )["items" ]
198
+ dz_user_map = {"Items" : search_response , "Type" : user_type }
199
+ all_dz_users .append (dz_user_map )
200
+
201
+ # For all user types, iterate through all users.
202
+ for dz_user_map in all_dz_users :
203
+ dz_users , user_type = dz_user_map ["Items" ], dz_user_map ["Type" ]
204
+ for dz_user in dz_users :
205
+ user_name = "None"
206
+ if user_type == "DATAZONE_IAM_USER" :
207
+ user_name = dz_user ["details" ]["iam" ]["arn" ]
208
+ if user_type == "SSO_USER" or user_type == "DATAZONE_SSO_USER" :
209
+ user_name = dz_user ["details" ]["sso" ]["username" ]
210
+
211
+ dz_user_id = dz_user ["id" ]
212
+ print (f"UserId: { dz_user_id } \t UserType: { user_type } \t User: { user_name } " )
197
213
198
214
self .dz_users_id_list = []
199
215
while True :
200
- dz_uzer = input ("Enter a DataZone UserId to map to (or 'done' to finish): " )
216
+ dz_uzer = input ("Enter a DataZone UserId to map to ('done' to finish): " )
201
217
if dz_uzer == "done" :
202
218
break
203
219
self .dz_users_id_list .append (dz_uzer )
@@ -295,6 +311,24 @@ def _configure_environment(self):
295
311
296
312
return self .env_id
297
313
314
+ def _add_environment_action (self ):
315
+ items = self .dz_client .list_environment_actions (
316
+ domainIdentifier = self .dz_domain_id , environmentIdentifier = self .env_id
317
+ )["items" ]
318
+ sm_env_action = None
319
+ for item in items :
320
+ if "sageMaker" in item ["parameters" ]:
321
+ sm_env_action = item
322
+
323
+ if sm_env_action is None :
324
+ self .dz_client .create_environment_action (
325
+ domainIdentifier = self .dz_domain_id ,
326
+ environmentIdentifier = self .env_id ,
327
+ name = "SageMaker Environment Action Link" ,
328
+ description = "Link from DataZone Data Portal to SageMaker Studio" ,
329
+ parameters = {"sageMaker" : {}},
330
+ )
331
+
298
332
def _associate_fed_role (self ):
299
333
# Associate fed role
300
334
print ("--------------------------------------------------------------------" )
@@ -397,7 +431,7 @@ def _debug_print_results(self):
397
431
f"Listing linked items for domain { self .dz_domain_id } , project { self .dz_project_id } , and environment { self .env_id } ."
398
432
)
399
433
list_result = self .byod_client .list_linked_types (
400
- self .dz_domain_id ,
434
+ domainIdentifier = self .dz_domain_id ,
401
435
projectIdentifier = self .dz_project_id ,
402
436
environmentIdentifier = self .env_id ,
403
437
)
@@ -418,10 +452,10 @@ def _get_env_link(self):
418
452
link = link_response ["actionLink" ]
419
453
print (link )
420
454
except botocore .exceptions .ClientError as error :
421
- print (error )
422
455
print (
423
456
"Environment action link could not be generated - this is most likely due to the current principal is not a user of the DataZone project."
424
457
)
458
+ print (error )
425
459
426
460
def import_interactive (self ):
427
461
print (
@@ -434,6 +468,7 @@ def import_interactive(self):
434
468
self ._map_users ()
435
469
self ._configure_blueprint ()
436
470
self ._configure_environment ()
471
+ self ._add_environment_action ()
437
472
self ._associate_fed_role ()
438
473
self ._link_domain ()
439
474
self ._link_users ()
0 commit comments