Skip to content

Commit e602f79

Browse files
SIMPLE-6706 fixed the 'owner' attribute of a joined lab (#108)
* SIMPLE-6706 fixed the 'owner' attribute of a joined lab to actually be the name of the user that owns it * SIMPLE-6706 moved setting owner to _import_lab
1 parent 626ac77 commit e602f79

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

virl2_client/models/lab.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class Lab:
8888
"connector_mappings": "labs/{lab_id}/connector_mappings",
8989
"resource_pools": "labs/{lab_id}/resource_pools",
9090
"annotations": "labs/{lab_id}/annotations",
91+
"user_list": "users",
9192
}
9293

9394
def __init__(
@@ -1322,10 +1323,12 @@ def _import_lab(self, topology: dict, created: bool = False) -> None:
13221323
:param topology: The topology to import.
13231324
:param created: The node create API endpoint returns data in the old format,
13241325
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.
13261328
:raises KeyError: If any property is missing in the topology.
13271329
"""
13281330
lab_dict = topology.get("lab")
1331+
default_owner = self.username if created else None
13291332

13301333
if lab_dict is None:
13311334
# 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:
13391342
self._title = topology["lab_title"]
13401343
self._description = topology["lab_description"]
13411344
self._notes = topology["lab_notes"]
1342-
self._owner = topology.get("lab_owner", self.username)
1345+
self._set_owner(topology.get("lab_owner"), default_owner)
13431346
else:
13441347
self._title = lab_dict["title"]
13451348
self._description = lab_dict["description"]
13461349
self._notes = lab_dict["notes"]
1347-
self._owner = lab_dict.get("owner", self.username)
1350+
self._set_owner(lab_dict.get("owner"), default_owner)
13481351

13491352
@locked
13501353
def _handle_import_nodes(self, topology: dict) -> None:
@@ -1977,3 +1980,28 @@ def sync_operational(self) -> None:
19771980
node.sync_operational(node_data)
19781981

19791982
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

virl2_client/virl2_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ def join_existing_lab(self, lab_id: str, sync_lab: bool = True) -> Lab:
812812
if sync_lab:
813813
lab.import_lab(topology)
814814
lab._initialized = True
815-
815+
else:
816+
lab._owner = None
816817
self._labs[lab_id] = lab
817818
return lab
818819

0 commit comments

Comments
 (0)