]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/nfs: convert _cmd_nfs_export_ls to use Responder decorator
authorJohn Mulligan <jmulligan@redhat.com>
Sat, 7 May 2022 14:04:35 +0000 (10:04 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 12 Jan 2023 18:44:10 +0000 (13:44 -0500)
Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/pybind/mgr/nfs/export.py
src/pybind/mgr/nfs/module.py
src/pybind/mgr/nfs/tests/test_nfs.py

index 8fabb78f247a0edc4341a56579c5a93d24925c98..8622df70c35dd9090b1994e2fc8291a3d71cff02 100644 (file)
@@ -16,6 +16,7 @@ from os.path import normpath
 
 from rados import TimedOut, ObjectNotFound, Rados, LIBRADOS_ALL_NSPACES
 
+from object_format import ErrorResponse
 from orchestrator import NoOrchestrator
 from mgr_module import NFS_POOL_NAME as POOL_NAME, NFS_GANESHA_SUPPORTED_FSALS
 
@@ -368,6 +369,15 @@ class ExportMgr:
         if need_nfs_service_restart:
             restart_nfs_service(self.mgr, export.cluster_id)
 
+    def _validate_cluster_id(self, cluster_id: str) -> None:
+        """Raise an exception if cluster_id is not valid."""
+        clusters = known_cluster_ids(self.mgr)
+        log.debug("checking for %r in known nfs clusters: %r",
+                  cluster_id, clusters)
+        if cluster_id not in clusters:
+            raise ErrorResponse(f"Cluster {cluster_id!r} does not exist",
+                                return_value=-errno.ENOENT)
+
     @export_cluster_checker
     def create_export(self, addr: Optional[List[str]] = None, **kwargs: Any) -> Tuple[int, str, str]:
         # if addr(s) are provided, construct client list and adjust outer block
@@ -426,23 +436,24 @@ class ExportMgr:
             r.extend([e.to_dict() for e in ls])
         return r
 
-    @export_cluster_checker
     def list_exports(self,
                      cluster_id: str,
-                     detailed: bool = False) -> Tuple[int, str, str]:
+                     detailed: bool = False) -> List[Any]:
+        self._validate_cluster_id(cluster_id)
         try:
             if detailed:
                 result_d = [export.to_dict() for export in self.exports[cluster_id]]
-                return 0, json.dumps(result_d, indent=2), ''
+                return result_d
             else:
                 result_ps = [export.pseudo for export in self.exports[cluster_id]]
-                return 0, json.dumps(result_ps, indent=2), ''
+                return result_ps
 
         except KeyError:
             log.warning("No exports to list for %s", cluster_id)
-            return 0, '', ''
+            return []
         except Exception as e:
-            return exception_handler(e, f"Failed to list exports for {cluster_id}")
+            log.exception(f"Failed to list exports for {cluster_id}")
+            raise ErrorResponse.wrap(e)
 
     def _get_export_dict(self, cluster_id: str, pseudo_path: str) -> Optional[Dict[str, Any]]:
         export = self._fetch_export(cluster_id, pseudo_path)
index 340792e81b33b228a0dbecd600e9fc279b065c5e..5bf3b9d7469e7f6c0224fa715d89a631e04b6815 100644 (file)
@@ -3,6 +3,7 @@ import threading
 from typing import Tuple, Optional, List, Dict, Any
 
 from mgr_module import MgrModule, CLICommand, Option, CLICheckNonemptyFileInput
+import object_format
 import orchestrator
 
 from .export import ExportMgr
@@ -85,7 +86,8 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule):
         return self.export_mgr.delete_export(cluster_id=cluster_id, pseudo_path=pseudo_path)
 
     @CLICommand('nfs export ls', perm='r')
-    def _cmd_nfs_export_ls(self, cluster_id: str, detailed: bool = False) -> Tuple[int, str, str]:
+    @object_format.Responder()
+    def _cmd_nfs_export_ls(self, cluster_id: str, detailed: bool = False) -> List[Any]:
         """List exports of a NFS cluster"""
         return self.export_mgr.list_exports(cluster_id=cluster_id, detailed=detailed)
 
index b984426b6d87ccc398f5ecdc45c741e02a624151..6fdf6d1583b6e3bd533ed76723afca3adee2c023 100644 (file)
@@ -912,8 +912,7 @@ NFS_CORE_PARAM {
         nfs_mod = Module('nfs', '', '')
         conf = ExportMgr(nfs_mod)
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 2
 
         r = conf.create_export(
@@ -927,8 +926,7 @@ NFS_CORE_PARAM {
         )
         assert r[0] == 0
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
 
         export = conf._fetch_export('foo', '/mybucket')
@@ -956,8 +954,7 @@ NFS_CORE_PARAM {
         nfs_mod = Module('nfs', '', '')
         conf = ExportMgr(nfs_mod)
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 2
 
         r = conf.create_export(
@@ -972,8 +969,7 @@ NFS_CORE_PARAM {
         )
         assert r[0] == 0
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
 
         export = conf._fetch_export('foo', '/mybucket')
@@ -1001,8 +997,7 @@ NFS_CORE_PARAM {
         nfs_mod = Module('nfs', '', '')
         conf = ExportMgr(nfs_mod)
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 2
 
         r = conf.create_export(
@@ -1016,8 +1011,7 @@ NFS_CORE_PARAM {
         )
         assert r[0] == 0
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
 
         export = conf._fetch_export('foo', '/mybucket')
@@ -1045,8 +1039,7 @@ NFS_CORE_PARAM {
         nfs_mod = Module('nfs', '', '')
         conf = ExportMgr(nfs_mod)
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 2
 
         r = conf.create_export(
@@ -1061,8 +1054,7 @@ NFS_CORE_PARAM {
         )
         assert r[0] == 0
 
-        exports = conf.list_exports(cluster_id=self.cluster_id)
-        ls = json.loads(exports[1])
+        ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
 
         export = conf._fetch_export('foo', '/cephfs2')