]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: inventory.NodeProxyCache() refactor
authorGuillaume Abrioux <gabrioux@ibm.com>
Mon, 20 Nov 2023 14:55:26 +0000 (14:55 +0000)
committerGuillaume Abrioux <gabrioux@ibm.com>
Thu, 25 Jan 2024 15:07:20 +0000 (15:07 +0000)
This modifies fullreport(), summary() and common() methods
so they use the same logic as firmwares() and criticals()

Signed-off-by: Guillaume Abrioux <gabrioux@ibm.com>
src/pybind/mgr/cephadm/inventory.py

index 9d3eb4904e559532df29bf290ae3b342fb818105..a320fb1f2395d9dbf0bd8c2a5b907d26bd23e5e7 100644 (file)
@@ -1437,16 +1437,41 @@ class NodeProxyCache:
         self.mgr.set_store(f"{NODE_PROXY_CACHE_PREFIX}/{host}", json.dumps(data))
 
     def fullreport(self, **kw: Any) -> Dict[str, Any]:
+        """
+        Retrieves the full report for the specified hostname.
+
+        If a hostname is provided in the keyword arguments, it retrieves the full report
+        data for that specific host. If no hostname is provided, it fetches the full
+        report data for all hosts available.
+
+        :param kw: Keyword arguments including 'hostname'.
+        :type kw: dict
+
+        :return: The full report data for the specified hostname(s).
+        :rtype: dict
+        """
         hostname = kw.get('hostname')
-        if hostname not in self.data.keys():
-            return [self.data[h] for h in self.data.keys()]
-        else:
-            return self.data[hostname]
+        hosts = [hostname] if hostname else self.data.keys()
+        return {host: self.data[host] for host in hosts}
 
     def summary(self, **kw: Any) -> Dict[str, Any]:
-        hostname = kw.get('hostname')
-        results = self.data
+        """
+        Summarizes the health status of components for specified hosts or all hosts.
 
+        Generates a summary of the health status of components for given hosts. If
+        no hostname is provided, it generates the health status summary for all hosts.
+        It inspects the status of each component and categorizes it as 'ok' or 'error'
+        based on the health status of its members.
+
+        :param kw: Keyword arguments including 'hostname'.
+        :type kw: dict
+
+        :return: A dictionary containing the health status summary for each specified
+                host or all hosts and their components.
+        :rtype: Dict[str, Dict[str, str]]
+        """
+        hostname = kw.get('hostname')
+        hosts = [hostname] if hostname else self.data.keys()
         mapper: Dict[bool, str] = {
             True: 'error',
             False: 'ok'
@@ -1454,31 +1479,40 @@ class NodeProxyCache:
 
         _result: Dict[str, Any] = {}
 
-        for host, data in results.items():
+        for host in hosts:
             _result[host] = {}
+            data = self.data[host]
             for component, details in data['status'].items():
                 res = any([member['status']['health'].lower() != 'ok' for member in data['status'][component].values()])
                 _result[host][component] = mapper[res]
 
-        if hostname and hostname in results.keys():
-            return _result[hostname]
-        else:
-            return _result
+        return _result
 
     def common(self, endpoint: str, **kw: Any) -> Dict[str, Any]:
+        """
+        Retrieves specific endpoint information for a specific hostname or all hosts.
+
+        Retrieves information from the specified 'endpoint' for all available hosts.
+        If 'hostname' is provided, retrieves the specified 'endpoint' information for that host.
+
+        :param endpoint: The endpoint for which information is retrieved.
+        :type endpoint: str
+        :param kw: Keyword arguments, including 'hostname' if specified.
+        :type kw: dict
+
+        :return: Endpoint information for the specified host(s).
+        :rtype: Union[Dict[str, Any], Any]
+        """
         hostname = kw.get('hostname')
         _result = {}
+        hosts = [hostname] if hostname else self.data.keys()
 
-        for host, data in self.data.items():
+        for host in hosts:
             try:
-                _result[host] = data['status'][endpoint]
+                _result[host] = self.data[host]['status'][endpoint]
             except KeyError:
-                raise RuntimeError(f'Invalid node-proxy endpoint {endpoint}')
-
-        if hostname and hostname in self.data.keys():
-            return _result[hostname]
-        else:
-            return _result
+                raise KeyError(f'Invalid host {host} or component {endpoint}.')
+        return _result
 
     def firmwares(self, **kw: Any) -> Dict[str, Any]:
         """