1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-03 23:51:48 +00:00

refactor: allow raising resource not found from any module (#761)

##### SUMMARY

Allows to raise a not found error from any module.
This commit is contained in:
Jonas L. 2025-12-10 12:25:17 +01:00 committed by GitHub
parent e3e3f8dd2d
commit 095fa8a2e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,8 +33,8 @@ def client_check_required_lib():
raise ClientException(missing_required_lib("python-dateutil"))
def _client_resource_not_found(resource: str, param: str | int):
return ClientException(f"resource ({resource.rstrip('s')}) does not exist: {param}")
def client_resource_not_found(resource: str, param: str | int):
return ClientException(f"resource ({resource}) does not exist: {param}")
def client_get_by_name_or_id(client: Client, resource: str, param: str | int):
@ -55,13 +55,13 @@ def client_get_by_name_or_id(client: Client, resource: str, param: str | int):
try:
int(param)
except ValueError as exception:
raise _client_resource_not_found(resource, param) from exception
raise client_resource_not_found(resource.rstrip("s"), param) from exception
try:
return resource_client.get_by_id(param)
except APIException as exception:
if exception.code == "not_found":
raise _client_resource_not_found(resource, param) from exception
raise client_resource_not_found(resource.rstrip("s"), param) from exception
raise exception