From 6b62105720aef69f65cda250d8388faf1972a2e3 Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Fri, 20 Oct 2023 09:21:16 +0000 Subject: [PATCH] node-proxy: drop dispatch() in NodeProxy() The current logic prevents from using any cherrypy decorators on actual endpoints as we use a set of 'proxy functions' (index and dispatch) instead. Signed-off-by: Guillaume Abrioux (cherry picked from commit 1ec59e6625bae7cd381d83817196bf8669f641ad) --- src/pybind/mgr/cephadm/agent.py | 55 ++++++++++++++++------------- src/pybind/mgr/cephadm/inventory.py | 9 +++-- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/pybind/mgr/cephadm/agent.py b/src/pybind/mgr/cephadm/agent.py index 28e1516f84f..82f289e5077 100644 --- a/src/pybind/mgr/cephadm/agent.py +++ b/src/pybind/mgr/cephadm/agent.py @@ -100,9 +100,6 @@ class NodeProxy: if len(vpath) == 2: hostname = vpath.pop(0) cherrypy.request.params['hostname'] = hostname - cmd = vpath.pop(0) - cherrypy.request.params['cmd'] = cmd - return self @cherrypy.expose @@ -224,37 +221,47 @@ class NodeProxy: def summary(self, **kw: Any) -> Dict[str, Any]: return self.mgr.node_proxy.summary(**kw) + @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @cherrypy.tools.json_out() - def common(self, **kw) -> Dict[str, Any]: - return self.mgr.node_proxy.common(**kw) + def memory(self, **kw: Any) -> Dict[str, Any]: + return self.mgr.node_proxy.common('memory', **kw) @cherrypy.expose @cherrypy.tools.allow(methods=['GET']) @cherrypy.tools.json_out() - def firmwares(self, **kw) -> Dict[str, Any]: - return self.mgr.node_proxy.firmwares(**kw) + def network(self, **kw: Any) -> Dict[str, Any]: + return self.mgr.node_proxy.common('network', **kw) - def dispatch(self, hostname='', cmd=''): - kw = dict(hostname=hostname, cmd=cmd) - try: - func = getattr(self, cmd) - result = func(**kw) - except AttributeError: - try: - result = self.common(**kw) - except RuntimeError as e: - cherrypy.response.status = 404 - result = {"error": f"{e}"} - return {"error": "Not a valid endpoint."} - finally: - return result + @cherrypy.expose + @cherrypy.tools.allow(methods=['GET']) + @cherrypy.tools.json_out() + def processors(self, **kw: Any) -> Dict[str, Any]: + return self.mgr.node_proxy.common('processors', **kw) + + @cherrypy.expose + @cherrypy.tools.allow(methods=['GET']) + @cherrypy.tools.json_out() + def storage(self, **kw: Any) -> Dict[str, Any]: + return self.mgr.node_proxy.common('storage', **kw) + + @cherrypy.expose + @cherrypy.tools.allow(methods=['GET']) + @cherrypy.tools.json_out() + def power(self, **kw: Any) -> Dict[str, Any]: + return self.mgr.node_proxy.common('power', **kw) @cherrypy.expose + @cherrypy.tools.allow(methods=['GET']) @cherrypy.tools.json_out() - def index(self, hostname=None, cmd=''): - result = self.dispatch(hostname, cmd) - return result + def fans(self, **kw: Any) -> Dict[str, Any]: + return self.mgr.node_proxy.common('fans', **kw) + + @cherrypy.expose + @cherrypy.tools.allow(methods=['GET']) + @cherrypy.tools.json_out() + def firmwares(self, **kw: Any) -> Dict[str, Any]: + return self.mgr.node_proxy.firmwares(**kw) class HostData(Server): diff --git a/src/pybind/mgr/cephadm/inventory.py b/src/pybind/mgr/cephadm/inventory.py index b434c68183e..401d675885c 100644 --- a/src/pybind/mgr/cephadm/inventory.py +++ b/src/pybind/mgr/cephadm/inventory.py @@ -1465,16 +1465,15 @@ class NodeProxyCache: else: return _result - def common(self, **kw): - hostname = kw.get('hostname',) - cmd = kw.get('cmd',) + def common(self, endpoint: str, **kw: Any) -> Dict[str, Any]: + hostname = kw.get('hostname') _result = {} for host, data in self.data.items(): try: - _result[host] = data['status'][cmd] + _result[host] = data['status'][endpoint] except KeyError: - raise RuntimeError(f'Invalid node-proxy category {cmd}') + raise RuntimeError(f'Invalid node-proxy endpoint {endpoint}') if hostname and hostname in self.data.keys(): return _result[hostname] -- 2.39.5