Skip to content

Commit

Permalink
SIMPLE-6105 node.remove no longer always raises NodeNotFound (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-valent authored Dec 15, 2023
1 parent 791efb9 commit 6962edb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
1 change: 0 additions & 1 deletion virl2_client/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,6 @@ def vnc_key(self) -> str:
url = self._url_for("vnc_key")
return self._session.get(url).json()

@check_stale
def remove(self) -> None:
"""Remove the node from the system."""
self.lab.remove_node(self)
Expand Down
30 changes: 13 additions & 17 deletions virl2_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def __repr__(self):
UNCHANGED = _Sentinel()


def _make_not_found(instance: Element, owner: Type[Element]) -> ElementNotFound:
def _make_not_found(instance: Element) -> ElementNotFound:
"""Composes and raises an ElementNotFound error for the given instance."""
class_name = owner.__name__
class_name = type(instance).__name__
instance_id = instance._id
instance_label = instance._title if class_name == "Lab" else instance._label

Expand All @@ -72,43 +72,39 @@ def _make_not_found(instance: Element, owner: Type[Element]) -> ElementNotFound:
def _check_and_mark_stale(
func: Callable,
instance: Element,
owner: Type[Element] | None = None,
*args,
**kwargs,
):
"""
Check staleness before and after calling `func`
and updates staleness if a 404 is raised.
:param func: the function to be called if not stale
:param instance: the instance of the parent class of `func`
which has a `_stale` attribute
:param owner: the class of `instance`
:param args: positional arguments to be passed to `func`
:param kwargs: keyword arguments to be passed to `func`
:param func: The function to be called if the instance is not stale.
:param instance: The instance of the parent class of `func`
which has a `_stale` attribute.
:param args: Positional arguments to be passed to `func`.
:param kwargs: Keyword arguments to be passed to `func`.
"""
if owner is None:
owner = type(instance)

if instance._stale:
raise _make_not_found(instance, owner)
raise _make_not_found(instance)

try:
ret = func(*args, **kwargs)
if instance._stale:
raise _make_not_found(instance, owner)
raise _make_not_found(instance)
return ret

except httpx.HTTPStatusError as exc:
resp = exc.response
class_name = owner.__name__
class_name = type(instance).__name__
instance_id = instance._id
if (
resp.status_code == 404
and f"{class_name} not found: {instance_id}" in resp.text
):
instance._stale = True
raise _make_not_found(instance, owner) from exc
raise _make_not_found(instance) from exc
raise


Expand All @@ -117,7 +113,7 @@ def check_stale(func: TCallable) -> TCallable:

@wraps(func)
def wrapper_stale(*args, **kwargs):
return _check_and_mark_stale(func, args[0], None, *args, **kwargs)
return _check_and_mark_stale(func, args[0], *args, **kwargs)

return cast(TCallable, wrapper_stale)

Expand All @@ -126,7 +122,7 @@ class property_s(property):
"""A modified `property` that will check staleness."""

def __get__(self, instance, owner):
return _check_and_mark_stale(super().__get__, instance, owner, instance, owner)
return _check_and_mark_stale(super().__get__, instance, instance, owner)


def locked(func: TCallable) -> TCallable:
Expand Down

0 comments on commit 6962edb

Please sign in to comment.