From b7c778a17e1253d83b1b40a13b1d835e148d6fcf Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Thu, 23 Nov 2023 16:08:18 +0000 Subject: [PATCH] node-proxy: raise HTTPError 404 error when no host is found Raise a 404 HTTPError when these differents endpoints are passed an inexisting hostname. Otherwise the code will fail with a `KeyError` exception. Signed-off-by: Guillaume Abrioux (cherry picked from commit bda2568d8c73bbdc77897d22a99f74c1f4a23511) --- src/pybind/mgr/cephadm/agent.py | 80 ++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/src/pybind/mgr/cephadm/agent.py b/src/pybind/mgr/cephadm/agent.py index b8453f719af..316cc7c0da3 100644 --- a/src/pybind/mgr/cephadm/agent.py +++ b/src/pybind/mgr/cephadm/agent.py @@ -358,8 +358,14 @@ class NodeProxy: :return: The full report data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.fullreport(**kw) + try: + results = self.mgr.node_proxy.fullreport(**kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -376,8 +382,14 @@ class NodeProxy: :return: Critical information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.criticals(**kw) + try: + results = self.mgr.node_proxy.criticals(**kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -394,8 +406,14 @@ class NodeProxy: :return: Summary information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.summary(**kw) + try: + results = self.mgr.node_proxy.summary(**kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -413,8 +431,14 @@ class NodeProxy: :return: Specific information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.common('memory', **kw) + try: + results = self.mgr.node_proxy.common('memory', **kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -432,8 +456,14 @@ class NodeProxy: :return: Specific information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.common('network', **kw) + try: + results = self.mgr.node_proxy.common('network', **kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -451,8 +481,14 @@ class NodeProxy: :return: Specific information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.common('processors', **kw) + try: + results = self.mgr.node_proxy.common('processors', **kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -470,8 +506,14 @@ class NodeProxy: :return: Specific information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.common('storage', **kw) + try: + results = self.mgr.node_proxy.common('storage', **kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -489,8 +531,14 @@ class NodeProxy: :return: Specific information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.common('power', **kw) + try: + results = self.mgr.node_proxy.common('power', **kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -508,8 +556,14 @@ class NodeProxy: :return: Specific information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.common('fans', **kw) + try: + results = self.mgr.node_proxy.common('fans', **kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @@ -526,8 +580,14 @@ class NodeProxy: :return: Firmware information data. :rtype: dict[str, Any] + + :raises cherrypy.HTTPError 404: If the passed hostname is not found. """ - return self.mgr.node_proxy.firmwares(**kw) + try: + results = self.mgr.node_proxy.firmwares(**kw) + except KeyError: + raise cherrypy.HTTPError(404, f"{kw.get('hostname')} not found.") + return results class HostData(Server): -- 2.39.5