Skip to content

Commit

Permalink
SIMPLE-6706 fixed the 'owner' attribute of a joined lab (#108)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
daniel-valent authored Aug 1, 2024
1 parent 626ac77 commit e602f79
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
34 changes: 31 additions & 3 deletions virl2_client/models/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Lab:
"connector_mappings": "labs/{lab_id}/connector_mappings",
"resource_pools": "labs/{lab_id}/resource_pools",
"annotations": "labs/{lab_id}/annotations",
"user_list": "users",
}

def __init__(
Expand Down Expand Up @@ -1322,10 +1323,12 @@ def _import_lab(self, topology: dict, created: bool = False) -> None:
:param topology: The topology to import.
:param created: The node create API endpoint returns data in the old format,
which would print an unnecessary old schema warning;
setting this flag to True skips that warning.
setting this flag to True skips that warning. Also decides whether default
username is to be the current user or None.
:raises KeyError: If any property is missing in the topology.
"""
lab_dict = topology.get("lab")
default_owner = self.username if created else None

if lab_dict is None:
# If we just created the lab, we skip the warning, since the
Expand All @@ -1339,12 +1342,12 @@ def _import_lab(self, topology: dict, created: bool = False) -> None:
self._title = topology["lab_title"]
self._description = topology["lab_description"]
self._notes = topology["lab_notes"]
self._owner = topology.get("lab_owner", self.username)
self._set_owner(topology.get("lab_owner"), default_owner)
else:
self._title = lab_dict["title"]
self._description = lab_dict["description"]
self._notes = lab_dict["notes"]
self._owner = lab_dict.get("owner", self.username)
self._set_owner(lab_dict.get("owner"), default_owner)

@locked
def _handle_import_nodes(self, topology: dict) -> None:
Expand Down Expand Up @@ -1977,3 +1980,28 @@ def sync_operational(self) -> None:
node.sync_operational(node_data)

self._last_sync_operational_time = time.time()

def _user_name(self, user_id: str) -> str | None:
"""
Get the username of the user with the given ID.
:param user_id: User unique identifier.
:returns: Username.
"""
# Need an endpoint here in Lab that would normally be handled by UserManagement,
# but a Lab has no access to UserManagement, this seems like a better idea than
# dragging the entire UserManagement to the Lab for two lines
url = self._url_for("user_list")
users = self._session.get(url).json()
for user in users:
if user["id"] == user_id:
return user["username"]
return None

def _set_owner(self, user_id: str | None = None, user_name: str | None = None):
"""Sets the owner to the name of the user specified by the provided user_id.
If given ID is None/doesn't exist, we fall back to the given user_name,
which will usually be the name of the current user or None."""
if user_id:
user_name = self._user_name(user_id) or user_name
self._owner = user_name
3 changes: 2 additions & 1 deletion virl2_client/virl2_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,8 @@ def join_existing_lab(self, lab_id: str, sync_lab: bool = True) -> Lab:
if sync_lab:
lab.import_lab(topology)
lab._initialized = True

else:
lab._owner = None
self._labs[lab_id] = lab
return lab

Expand Down

0 comments on commit e602f79

Please sign in to comment.