From a4c131885f1aab78836a30b612e92b6ce0b17fea Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Fri, 6 May 2022 09:55:03 -0400 Subject: [PATCH] mgr/nfs: convert _cmd_nfs_cluster_config_set to EmptyResponder decorator Signed-off-by: John Mulligan --- src/pybind/mgr/nfs/cluster.py | 13 +++++++------ src/pybind/mgr/nfs/module.py | 3 ++- src/pybind/mgr/nfs/tests/test_nfs.py | 6 ++---- src/pybind/mgr/nfs/utils.py | 9 +++++++++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/pybind/mgr/nfs/cluster.py b/src/pybind/mgr/nfs/cluster.py index 9a65158c0f1..c9ddffabb45 100644 --- a/src/pybind/mgr/nfs/cluster.py +++ b/src/pybind/mgr/nfs/cluster.py @@ -12,6 +12,7 @@ import orchestrator from .exception import NFSInvalidOperation, ClusterNotFound from .utils import ( + ManualRestartRequired, NonFatalError, available_clusters, conf_obj_name, @@ -236,23 +237,23 @@ class NFSCluster: except Exception as e: return exception_handler(e, f"Fetching NFS-Ganesha Config failed for {cluster_id}") - def set_nfs_cluster_config(self, cluster_id: str, nfs_config: str) -> Tuple[int, str, str]: + def set_nfs_cluster_config(self, cluster_id: str, nfs_config: str) -> None: try: if cluster_id in available_clusters(self.mgr): rados_obj = self._rados(cluster_id) if rados_obj.check_user_config(): - return 0, "", "NFS-Ganesha User Config already exists" + raise NonFatalError("NFS-Ganesha User Config already exists") rados_obj.write_obj(nfs_config, user_conf_obj_name(cluster_id), conf_obj_name(cluster_id)) log.debug("Successfully saved %s's user config: \n %s", cluster_id, nfs_config) restart_nfs_service(self.mgr, cluster_id) - return 0, "NFS-Ganesha Config Set Successfully", "" + return raise ClusterNotFound() except NotImplementedError: - return 0, "NFS-Ganesha Config Added Successfully "\ - "(Manual Restart of NFS PODS required)", "" + raise ManualRestartRequired("NFS-Ganesha Config Added Successfully") except Exception as e: - return exception_handler(e, f"Setting NFS-Ganesha Config failed for {cluster_id}") + log.exception(f"Setting NFS-Ganesha Config failed for {cluster_id}") + raise ErrorResponse.wrap(e) def reset_nfs_cluster_config(self, cluster_id: str) -> Tuple[int, str, str]: try: diff --git a/src/pybind/mgr/nfs/module.py b/src/pybind/mgr/nfs/module.py index c5d54773eed..7e129591cf1 100644 --- a/src/pybind/mgr/nfs/module.py +++ b/src/pybind/mgr/nfs/module.py @@ -158,7 +158,8 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule): @CLICommand('nfs cluster config set', perm='rw') @CLICheckNonemptyFileInput(desc='NFS-Ganesha Configuration') - def _cmd_nfs_cluster_config_set(self, cluster_id: str, inbuf: str) -> Tuple[int, str, str]: + @object_format.EmptyResponder() + def _cmd_nfs_cluster_config_set(self, cluster_id: str, inbuf: str) -> None: """Set NFS-Ganesha config by `-i `""" return self.nfs.set_nfs_cluster_config(cluster_id=cluster_id, nfs_config=inbuf) diff --git a/src/pybind/mgr/nfs/tests/test_nfs.py b/src/pybind/mgr/nfs/tests/test_nfs.py index 64072591cfb..b04b60a6fbd 100644 --- a/src/pybind/mgr/nfs/tests/test_nfs.py +++ b/src/pybind/mgr/nfs/tests/test_nfs.py @@ -1105,15 +1105,13 @@ NFS_CORE_PARAM { assert rc == 0 assert out == "" - rc, out, err = cluster.set_nfs_cluster_config(self.cluster_id, '# foo\n') - assert rc == 0 + cluster.set_nfs_cluster_config(self.cluster_id, '# foo\n') rc, out, err = cluster.get_nfs_cluster_config(self.cluster_id) assert rc == 0 assert out == "# foo\n" - rc, out, err = cluster.reset_nfs_cluster_config(self.cluster_id) - assert rc == 0 + cluster.reset_nfs_cluster_config(self.cluster_id) rc, out, err = cluster.get_nfs_cluster_config(self.cluster_id) assert rc == 0 diff --git a/src/pybind/mgr/nfs/utils.py b/src/pybind/mgr/nfs/utils.py index 485c5b180f8..1e901c8c529 100644 --- a/src/pybind/mgr/nfs/utils.py +++ b/src/pybind/mgr/nfs/utils.py @@ -26,6 +26,15 @@ class NonFatalError(ErrorResponseBase): return 0, "", self.msg +class ManualRestartRequired(NonFatalError): + """Raise this exception type if all other changes were successful but + user needs to manually restart nfs services. + """ + + def __init__(self, msg: str) -> None: + super().__init__(" ".join((msg, "(Manual Restart of NFS Pods required)"))) + + def export_obj_name(export_id: int) -> str: """Return a rados object name for the export.""" return f"{EXPORT_PREFIX}{export_id}" -- 2.39.5