From: John Mulligan Date: Sat, 7 May 2022 14:04:35 +0000 (-0400) Subject: mgr/nfs: convert _cmd_nfs_export_ls to use Responder decorator X-Git-Tag: v18.1.0~491^2~16 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=656376d90a347a4ed6f0890f248eaa568f83ddd6;p=ceph.git mgr/nfs: convert _cmd_nfs_export_ls to use Responder decorator Signed-off-by: John Mulligan --- diff --git a/src/pybind/mgr/nfs/export.py b/src/pybind/mgr/nfs/export.py index 8fabb78f247a..8622df70c35d 100644 --- a/src/pybind/mgr/nfs/export.py +++ b/src/pybind/mgr/nfs/export.py @@ -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) diff --git a/src/pybind/mgr/nfs/module.py b/src/pybind/mgr/nfs/module.py index 340792e81b33..5bf3b9d7469e 100644 --- a/src/pybind/mgr/nfs/module.py +++ b/src/pybind/mgr/nfs/module.py @@ -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) diff --git a/src/pybind/mgr/nfs/tests/test_nfs.py b/src/pybind/mgr/nfs/tests/test_nfs.py index b984426b6d87..6fdf6d1583b6 100644 --- a/src/pybind/mgr/nfs/tests/test_nfs.py +++ b/src/pybind/mgr/nfs/tests/test_nfs.py @@ -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')