@@ -88,6 +88,7 @@ class Lab:
88
88
"connector_mappings" : "labs/{lab_id}/connector_mappings" ,
89
89
"resource_pools" : "labs/{lab_id}/resource_pools" ,
90
90
"annotations" : "labs/{lab_id}/annotations" ,
91
+ "user_list" : "users" ,
91
92
}
92
93
93
94
def __init__ (
@@ -1322,10 +1323,12 @@ def _import_lab(self, topology: dict, created: bool = False) -> None:
1322
1323
:param topology: The topology to import.
1323
1324
:param created: The node create API endpoint returns data in the old format,
1324
1325
which would print an unnecessary old schema warning;
1325
- setting this flag to True skips that warning.
1326
+ setting this flag to True skips that warning. Also decides whether default
1327
+ username is to be the current user or None.
1326
1328
:raises KeyError: If any property is missing in the topology.
1327
1329
"""
1328
1330
lab_dict = topology .get ("lab" )
1331
+ default_owner = self .username if created else None
1329
1332
1330
1333
if lab_dict is None :
1331
1334
# If we just created the lab, we skip the warning, since the
@@ -1339,12 +1342,12 @@ def _import_lab(self, topology: dict, created: bool = False) -> None:
1339
1342
self ._title = topology ["lab_title" ]
1340
1343
self ._description = topology ["lab_description" ]
1341
1344
self ._notes = topology ["lab_notes" ]
1342
- self ._owner = topology .get ("lab_owner" , self . username )
1345
+ self ._set_owner ( topology .get ("lab_owner" ), default_owner )
1343
1346
else :
1344
1347
self ._title = lab_dict ["title" ]
1345
1348
self ._description = lab_dict ["description" ]
1346
1349
self ._notes = lab_dict ["notes" ]
1347
- self ._owner = lab_dict .get ("owner" , self . username )
1350
+ self ._set_owner ( lab_dict .get ("owner" ), default_owner )
1348
1351
1349
1352
@locked
1350
1353
def _handle_import_nodes (self , topology : dict ) -> None :
@@ -1977,3 +1980,28 @@ def sync_operational(self) -> None:
1977
1980
node .sync_operational (node_data )
1978
1981
1979
1982
self ._last_sync_operational_time = time .time ()
1983
+
1984
+ def _user_name (self , user_id : str ) -> str | None :
1985
+ """
1986
+ Get the username of the user with the given ID.
1987
+
1988
+ :param user_id: User unique identifier.
1989
+ :returns: Username.
1990
+ """
1991
+ # Need an endpoint here in Lab that would normally be handled by UserManagement,
1992
+ # but a Lab has no access to UserManagement, this seems like a better idea than
1993
+ # dragging the entire UserManagement to the Lab for two lines
1994
+ url = self ._url_for ("user_list" )
1995
+ users = self ._session .get (url ).json ()
1996
+ for user in users :
1997
+ if user ["id" ] == user_id :
1998
+ return user ["username" ]
1999
+ return None
2000
+
2001
+ def _set_owner (self , user_id : str | None = None , user_name : str | None = None ):
2002
+ """Sets the owner to the name of the user specified by the provided user_id.
2003
+ If given ID is None/doesn't exist, we fall back to the given user_name,
2004
+ which will usually be the name of the current user or None."""
2005
+ if user_id :
2006
+ user_name = self ._user_name (user_id ) or user_name
2007
+ self ._owner = user_name
0 commit comments