Skip to content

feat: allow resource.get to accept Namespace instance instead of just str #2391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ocp_resources/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ def to_dict(self) -> None:
_spec["finalizers"] = self.finalizers

# End of generated code

def __str__(self) -> str:
"""Return human-readable string "name" rather than default __repr__.

Calling str, format or print on a Namespace object will result in it being resolved
to its name attribute if found else the repr will be returned.

Returns:
str: Human-readable string "name" or the output of __repr__.
"""
return name if (name := getattr(self, "name", None)) else self.__repr__()
4 changes: 2 additions & 2 deletions ocp_resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ def _get() -> Generator["NamespacedResource|ResourceInstance", None, None]:
yield cls(
client=dyn_client,
name=resource_field.metadata.name,
namespace=resource_field.metadata.namespace,
namespace=str(resource_field.metadata.namespace),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue seems like an user error situation. namespace here is namespace name. This is true for all wrapper objects, if I recall correctly. Just like an oc command does not work with kinds as params, wrapper expects namespace strings when you are providing a namespace.

)
except TypeError:
if raw:
Expand All @@ -1377,7 +1377,7 @@ def _get() -> Generator["NamespacedResource|ResourceInstance", None, None]:
yield cls(
client=dyn_client,
name=_resources.metadata.name,
namespace=_resources.metadata.namespace,
namespace=str(_resources.metadata.namespace),
)

return Resource.retry_cluster_exceptions(func=_get, exceptions_dict=exceptions_dict)
Expand Down