]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/nfs: convert _cmd_nfs_export_create_cephfs to use Responder decorator
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 5 May 2022 18:31:48 +0000 (14:31 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 12 Jan 2023 18:44:11 +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
src/pybind/mgr/nfs/utils.py

index f336d718491d8976e8cc89c11395fbf4141dd32e..4646696ec151926b79c8e4237d92c1134ed98475 100644 (file)
@@ -31,6 +31,7 @@ from .exception import NFSException, NFSInvalidOperation, FSNotFound
 from .utils import (
     CONF_PREFIX,
     EXPORT_PREFIX,
+    NonFatalError,
     USER_CONF_PREFIX,
     export_obj_name,
     conf_obj_name,
@@ -378,8 +379,8 @@ class ExportMgr:
             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]:
+    def create_export(self, addr: Optional[List[str]] = None, **kwargs: Any) -> Dict[str, Any]:
+        self._validate_cluster_id(kwargs['cluster_id'])
         # if addr(s) are provided, construct client list and adjust outer block
         clients = []
         if addr:
@@ -409,7 +410,8 @@ class ExportMgr:
                 return self.create_rgw_export(**kwargs)
             raise NotImplementedError()
         except Exception as e:
-            return exception_handler(e, f"Failed to create {kwargs['pseudo_path']} export for {kwargs['cluster_id']}")
+            log.exception(f"Failed to create {kwargs['pseudo_path']} export for {kwargs['cluster_id']}")
+            raise ErrorResponse.wrap(e)
 
     @export_cluster_checker
     def delete_export(self,
@@ -646,7 +648,7 @@ class ExportMgr:
                              squash: str,
                              access_type: str,
                              clients: list = [],
-                             sectype: Optional[List[str]] = None) -> Tuple[int, str, str]:
+                             sectype: Optional[List[str]] = None) -> Dict[str, Any]:
         pseudo_path = normalize_path(pseudo_path)
 
         if not self._fetch_export(cluster_id, pseudo_path):
@@ -676,8 +678,8 @@ class ExportMgr:
                 "cluster": cluster_id,
                 "mode": export.access_type,
             }
-            return (0, json.dumps(result, indent=4), '')
-        return 0, "", "Export already exists"
+            return result
+        raise NonFatalError("Export already exists")
 
     def create_rgw_export(self,
                           cluster_id: str,
@@ -688,11 +690,11 @@ class ExportMgr:
                           bucket: Optional[str] = None,
                           user_id: Optional[str] = None,
                           clients: list = [],
-                          sectype: Optional[List[str]] = None) -> Tuple[int, str, str]:
+                          sectype: Optional[List[str]] = None) -> Dict[str, Any]:
         pseudo_path = normalize_path(pseudo_path)
 
         if not bucket and not user_id:
-            return -errno.EINVAL, "", "Must specify either bucket or user_id"
+            raise ErrorResponse("Must specify either bucket or user_id")
 
         if not self._fetch_export(cluster_id, pseudo_path):
             export = self.create_export_from_dict(
@@ -721,8 +723,8 @@ class ExportMgr:
                 "mode": export.access_type,
                 "squash": export.squash,
             }
-            return (0, json.dumps(result, indent=4), '')
-        return 0, "", "Export already exists"
+            return result
+        raise NonFatalError("Export already exists")
 
     def _apply_export(
             self,
index 537c6be39eb0a505e0dc2359654d7e2013f2ef94..0e28e1b7934624b3b5b5a50d48554e26c72aae11 100644 (file)
@@ -26,6 +26,7 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule):
             self.inited = True
 
     @CLICommand('nfs export create cephfs', perm='rw')
+    @object_format.Responder()
     def _cmd_nfs_export_create_cephfs(
             self,
             cluster_id: str,
@@ -36,7 +37,7 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule):
             client_addr: Optional[List[str]] = None,
             squash: str = 'none',
             sectype: Optional[List[str]] = None,
-    ) -> Tuple[int, str, str]:
+    ) -> Dict[str, Any]:
         """Create a CephFS export"""
         return self.export_mgr.create_export(
             fsal_type='cephfs',
@@ -51,6 +52,7 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule):
         )
 
     @CLICommand('nfs export create rgw', perm='rw')
+    @object_format.Responder()
     def _cmd_nfs_export_create_rgw(
             self,
             cluster_id: str,
@@ -61,7 +63,7 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule):
             client_addr: Optional[List[str]] = None,
             squash: str = 'none',
             sectype: Optional[List[str]] = None,
-    ) -> Tuple[int, str, str]:
+    ) -> Dict[str, Any]:
         """Create an RGW export"""
         return self.export_mgr.create_export(
             fsal_type='rgw',
index 5607396bbe007cf5656bd798367fb0724d611afd..97512f1a356d2fa88eef03b644366075d285d9fe 100644 (file)
@@ -924,7 +924,7 @@ NFS_CORE_PARAM {
             squash='root',
             addr=["192.168.0.0/16"]
         )
-        assert r[0] == 0
+        assert r["bind"] == "/mybucket"
 
         ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
@@ -967,7 +967,7 @@ NFS_CORE_PARAM {
             squash='root',
             addr=["192.168.0.0/16"]
         )
-        assert r[0] == 0
+        assert r["bind"] == "/mybucket"
 
         ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
@@ -1009,7 +1009,7 @@ NFS_CORE_PARAM {
             squash='root',
             addr=["192.168.0.0/16"]
         )
-        assert r[0] == 0
+        assert r["bind"] == "/mybucket"
 
         ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
@@ -1052,7 +1052,7 @@ NFS_CORE_PARAM {
             squash='root',
             addr=["192.168.1.0/8"],
         )
-        assert r[0] == 0
+        assert r["bind"] == "/cephfs2"
 
         ls = conf.list_exports(cluster_id=self.cluster_id)
         assert len(ls) == 3
index ac857d6d920e7af5805ded89028f8ce4bd367643..485c5b180f8597c0fd17a9b7acbb5f60971ee720 100644 (file)
@@ -1,5 +1,6 @@
-from typing import List, TYPE_CHECKING
+from typing import List, Tuple, TYPE_CHECKING
 
+from object_format import ErrorResponseBase
 import orchestrator
 
 if TYPE_CHECKING:
@@ -10,6 +11,21 @@ CONF_PREFIX: str = "conf-nfs."
 USER_CONF_PREFIX: str = "userconf-nfs."
 
 
+class NonFatalError(ErrorResponseBase):
+    """Raise this exception when you want to interrupt the flow of a function
+    and return an informative message to the user. In certain situations the
+    NFS MGR module wants to indicate an action was or was not taken but still
+    return a success code so that non-interactive scripts continue as if the
+    overall action was completed.
+    """
+    def __init__(self, msg: str) -> None:
+        super().__init__(msg)
+        self.msg = msg
+
+    def format_response(self) -> Tuple[int, str, str]:
+        return 0, "", self.msg
+
+
 def export_obj_name(export_id: int) -> str:
     """Return a rados object name for the export."""
     return f"{EXPORT_PREFIX}{export_id}"